Misc. Utilities¶
Convert a dump from gdb’s
info sharedlibrarycommand to a set of options that can be passed to CLE to replicate the address space from the gdb session- Parameters:
fname – The name of a file containing the dump
- Returns:
A dict appropriate to be passed as
**kwargsforangr.Projectorcle.Loader
- cle.gdb.convert_info_proc_maps(fname)[source]¶
Convert a dump from gdb’s
info proc mapscommand to a set of options that can be passed to CLE to replicate the address space from the gdb session- Parameters:
fname – The name of a file containing the dump
- Returns:
A dict appropriate to be passed as
**kwargsforangr.Projectorcle.Loader
- class cle.memory.ClemoryBase[source]¶
Bases:
objectThe base class of all Clemory classes.
- unpack(addr: int, fmt: str) tuple[Any, ...][source]¶
Use the
structmodule to unpack the data at address addr with the format fmt.
- unpack_word(addr: int, size: int | None = None, signed: bool = False, endness: Endness | None = None) int[source]¶
Use the
structmodule to unpack a single integer from the address addr.You may override any of the attributes of the word being extracted:
- Parameters:
size (
int|None) – The size in bytes to pack/unpack. Defaults to wordsize (e.g. 4 bytes on a 32 bit architecture)signed (
bool) – Whether the data should be extracted signed/unsigned. Default unsignedendness (
Endness|None) – The endian to use in packing/unpacking. Defaults to memory endnessaddr (int)
- Return type:
- load_null_terminated_bytes(addr: int, max_size: int = 4096) bytes[source]¶
Load a null-terminated string from memory at address addr with a maximum size of max_size. Useful
- pack(addr: int, fmt: str, *data)[source]¶
Use the
structmodule to pack data into memory at address addr with the format fmt.
- pack_word(addr: int, data: int, size: int | None = None, signed: bool = False, endness: Endness | None = None)[source]¶
Use the
structmodule to pack a single integer data into memory at the address addr.You may override any of the attributes of the word being packed:
- Parameters:
size (
int|None) – The size in bytes to pack/unpack. Defaults to wordsize (e.g. 4 bytes on a 32 bit architecture)signed (
bool) – Whether the data should be extracted signed/unsigned. Default unsignedendness (
Endness|None) – The endian to use in packing/unpacking. Defaults to memory endnessaddr (int)
data (int)
- read(nbytes: int)[source]¶
The stream-like function that reads up to a number of bytes starting from the current position and updates the current position. Use with
seek().Up to nbytes bytes will be read, halting at the beginning of the first unmapped region encountered.
- Parameters:
nbytes (int)
- class cle.memory.Clemory[source]¶
Bases:
ClemoryBaseAn object representing a memory space.
Accesses can be made with [index] notation.
- add_backer(start: int, data: bytes | bytearray | memoryview | list[int] | Clemory | mmap, overwrite: bool = False)[source]¶
Adds a backer to the memory.
- Parameters:
start (
int) – The address where the backer should be loaded.data (
bytes|bytearray|memoryview|list[int] |Clemory|mmap) – The backer itself. Can be either a bytestring or anotherClemory.overwrite (
bool) – If True and the range overlaps any existing backer, the existing backer will be split up and the overlapping part will be replaced with the new backer.
- split_backer(addr: int)[source]¶
Ensures that
addris the start of a backer, if it is backed.- Parameters:
addr (int)
- backers(addr=0) Iterator[tuple[int, bytearray | memoryview | mmap | list[int]]][source]¶
Iterate through each backer for this clemory and all its children, yielding tuples of
(start_addr, backer)where each backer is a bytearray.
- load(addr, n)[source]¶
Read up to n bytes at address addr in memory and return a bytes object.
Reading will stop at the beginning of the first unallocated region found, or when n bytes have been read.
- store(addr, data)[source]¶
Write bytes from data at address addr.
Note: If the store runs off the end of a backer and into unbacked space, this function will update the backer but also raise
KeyError.
- class cle.memory.ClemoryView[source]¶
Bases:
ClemoryBaseA Clemory which presents a subset of another Clemory as an address space.
- class cle.memory.ClemoryTranslator[source]¶
Bases:
ClemoryBaseUses a function to translate between address spaces when accessing a child clemory. Intended to be used only as a stream object.
- __init__(backer: ClemoryBase, func)[source]¶
- Parameters:
backer (ClemoryBase)
- class cle.memory.UninitializedClemory[source]¶
Bases:
ClemoryA special kind of Clemory that acts as a placeholder for uninitialized and invalid memory. This is needed for the PAGEZERO segment for MachO binaries, which is 4GB worth of memory This does _not_ handle data being written to it, this is only for uninitialized memory that is technically occupied but should never be accessed
- add_backer(start, data, overwrite=False)[source]¶
Adds a backer to the memory.
- Parameters:
start – The address where the backer should be loaded.
data – The backer itself. Can be either a bytestring or another
Clemory.overwrite – If True and the range overlaps any existing backer, the existing backer will be split up and the overlapping part will be replaced with the new backer.
- backers(addr=0)[source]¶
Technically this object has no real backer We could create a fake backer on demand, but that would be a waste of memory, and code like the function prolog discovery for MachO binaries would search 4GB worth of nullbytes for a prolog, which is a waste of time Instead we just return an empty byte array, which seems to pass the test cases :type addr: :param addr: :return:
- load(addr, n)[source]¶
Read up to n bytes at address addr in memory and return a bytes object.
Reading will stop at the beginning of the first unallocated region found, or when n bytes have been read.
- store(addr, data)[source]¶
Write bytes from data at address addr.
Note: If the store runs off the end of a backer and into unbacked space, this function will update the backer but also raise
KeyError.
- class cle.patched_stream.PatchedStream[source]¶
Bases:
objectAn object that wraps a readable stream, performing passthroughs on seek and read operations, except to make it seem like the data has actually been patched by the given patches.
- class cle.address_translator.AddressTranslator[source]¶
Bases:
object- __init__(rva, owner)[source]¶
- Parameters:
rva (int) – virtual address relative to owner’s object image base
owner (cle.Backend) – The object owner address relates to
- classmethod from_linked_va(lva, owner)¶
Loads address translator with LVA
- classmethod from_va(mva, owner)¶
Loads address translator with MVA
- classmethod from_mapped_va(mva, owner)¶
Loads address translator with MVA
- classmethod from_relative_va(rva, owner)¶
Loads address translator with RVA
- to_linked_va()¶
VA -> LVA :rtype: int
- to_va()¶
RVA -> MVA :rtype: int
- to_mapped_va()¶
RVA -> MVA :rtype: int
- to_relative_va()¶
RVA -> RVA :rtype: int
- cle.address_translator.AT¶
alias of
AddressTranslator