ducky.util module

class ducky.util.BinaryFile(logger, stream)[source]

Bases: object

Base class of all classes that represent “binary” files - binaries, core dumps. It provides basic methods for reading and writing structures.

static do_open(logger, path, mode='rb', klass=None)[source]
static open(*args, **kwargs)[source]
read_struct(st_class)[source]

Read structure from current position in file.

Returns:instance of class st_class with content read from file
Return type:st_class
setup()[source]
write_struct(st)[source]

Write structure into file at the current position.

Parameters:st (class) – ctype-based structure
class ducky.util.Flags[source]

Bases: object

classmethod create(**kwargs)[source]
classmethod encoding()[source]
classmethod from_encoding(encoding)[source]
classmethod from_int(u)[source]
classmethod from_string(s)[source]
load_encoding(encoding)[source]
load_int(u)[source]
load_string(s)[source]
save_encoding(encoding)[source]
to_encoding()[source]
to_int()[source]
to_string()[source]
class ducky.util.Formatter[source]

Bases: string.Formatter

format_field(value, format_spec)[source]
format_int(format_spec, value)[source]
class ducky.util.LRUCache(logger, size, *args, **kwargs)[source]

Bases: collections.OrderedDict

Simple LRU cache, based on OrderedDict, with limited size. When limit is reached, the least recently inserted item is removed.

Parameters:
  • logger (logging.Logger) – logger object instance should use for logging.
  • size (int) – maximal number of entries allowed.
get_object(key)[source]

The real workhorse - responsible for getting requested item from outside when it’s not present in cache. Called by __missing__ method. This method itself makes no changes to cache at all.

make_space()[source]

This method is called when there is no free space in cache. It’s responsible for freeing at least one slot, upper limit of removed entries is not enforced.

class ducky.util.StringTable[source]

Bases: object

Simple string table, used by many classes operating with files (core, binaries, ...). String can be inserted into table and read, each has its starting offset and its end is marked with null byte ().

Thsi is a helper class - it makes working with string, e.g. section and symbol names, much easier.

get_string(offset)[source]

Read string from table.

Parameters:offset (int) – offset of the first character from the beginning of the table
Returns:string
Return type:string
put_string(s)[source]

Insert new string into table. String is appended at the end of internal buffer, and terminating zero byte () is appended to mark end of string.

Returns:offset of inserted string
Return type:int
class ducky.util.SymbolTable(binary)[source]

Bases: dict

get_symbol(name)[source]
ducky.util.align(boundary, n)[source]
ducky.util.isfile(o)[source]
ducky.util.sizeof_fmt(n, suffix='B', max_unit='Zi')[source]
ducky.util.str2int(s)[source]