Utilities

leb128

Little Endian Base 128 (LEB128) variable length encoding

https://en.wikipedia.org/wiki/LEB128#Unsigned_LEB128

ppci.utils.leb128.signed_leb128_decode(data) → int

Read variable length encoded 128 bits signed integer.

>>> from ppci.utils.leb128 import signed_leb128_decode
>>> signed_leb128_decode(iter(bytes([0x9b, 0xf1, 0x59])))
-624485
ppci.utils.leb128.signed_leb128_encode(value: int) → bytes

Encode the given number as signed leb128

>>> from ppci.utils.leb128 import signed_leb128_encode
>>> signed_leb128_encode(-1337)
b'Çu'
ppci.utils.leb128.unsigned_leb128_decode(data) → int

Read variable length encoded 128 bits unsigned integer

>>> from ppci.utils.leb128 import unsigned_leb128_decode
>>> signed_leb128_decode(iter(bytes([0xe5, 0x8e, 0x26])))
624485
ppci.utils.leb128.unsigned_leb128_encode(value: int) → bytes

Encode number as into unsigned leb128 encoding

>>> from ppci.utils.leb128 import unsigned_leb128_encode
>>> unsigned_leb128_encode(42)
b'*'

Codepage

Cool idea to load actual object code into memory and execute it from python using ctypes

Credits for idea: Luke Campagnola

class ppci.utils.codepage.MemoryPage(size)

Allocate a memory slab in the current process.

write(data)

Fill page with the given data

class ppci.utils.codepage.Mod(obj, imports=None)

Container for machine code

class ppci.utils.codepage.WinPage(size)

Nice windows hack to emulate mmap.

Copied from: https://github.com/campagnola/pycca/blob/master/pycca/asm/codepage.py

ppci.utils.codepage.load_code_as_module(source_file, reporter=None)

Load c3 code as a module

ppci.utils.codepage.load_obj(obj, imports=None)

Load an object into memory.

Parameters:
  • obj – the code object to load.
  • imports – A dictionary of functions to attach.

Optionally a dictionary of functions that must be imported can be provided.

Reporting

To create a nice report of what happened during compilation, this file implements several reporting types.

Reports can be written to plain text, or html.

class ppci.utils.reporting.DummyReportGenerator

Report generator which reports into the void

dump_exception(einfo)

List the given exception in report

dump_instructions(instructions, arch)

Print instructions

class ppci.utils.reporting.HtmlReportGenerator(dump_file)
dump_dag(dags)

Write selection dag to dumpfile

dump_exception(einfo)

List the given exception in report

dump_frame(frame)

Dump frame to file for debug purposes

dump_instructions(instructions, arch)

Print instructions

dump_raw_text(text)

Spitout text not to be formatted

class ppci.utils.reporting.MyHandler(backlog)
emit(record)

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

class ppci.utils.reporting.ReportGenerator

Implement all these function to create a custom reporting generator

dump_exception(einfo)

List the given exception in report

dump_instructions(instructions, arch)

Print instructions

class ppci.utils.reporting.TextReportGenerator(dump_file)
dump_dag(dags)

Write selection dag to dumpfile

dump_exception(einfo)

List the given exception in report

dump_frame(frame)

Dump frame to file for debug purposes

class ppci.utils.reporting.TextWritingReporter(dump_file)
dump_instructions(instructions, arch)

Print instructions

print(*args, end='\n')

Convenience helper for printing to dumpfile

Hexdump

Utilities to dump binary data in hex

ppci.utils.hexdump.chunks(data, chunk_size=16)

Split data into pieces of maximum chunk_size bytes

ppci.utils.hexdump.hexdump(data, address=0)

Hexdump of the given bytes.