Skip to main content

A library for manipulating in-memory C data structures

Project description

Moria

GitHub release (latest SemVer) Build Status PyPI Version Python Versions codecov

A library for interacting with in-memory C structures. With Moria, you can:

  • Extract C struct information from compiled binaries (using DWARF debug info)
  • Turn them into high-level python types
  • Manipulate values including nested structs, pointers, and arrays
  • Serialize into binary compatbile with the original program
  • Pack objects into a binary buffer
  • Automatically arrange string buffers, etc. in memory
  • Automatically compute pointer values in packed objects

Why?

Data-only memory corruption exploits can involve reading and writing complex data structures in the target address space. Moria makes development of these types of exploits much easier.

Install

pip install moria-c

Examples

Moria can manipulate complicated in-memory C datastructures using high-level python objects. For example, take the following C declaration for a linked list of user data:

struct user
{
    int id;
    char name[MAX_USERNAME_LEN];
    struct user *prev;
    struct user *next;
};

Moria can automatically extract the types, sizes, and offsets of the structure from binary compiled with debug info:

with open("uesrlist.bin", "rb") as binary:
    structs = DwarfParser(binary).parse()

user1 = structs.user()
user2 = structs.user()

You can set field values, including nested types and pointers that reference other objects, fields, or values:

user1.id = 1
user1.name = "alice"
user1.next = user2.ref()
user1.prev = user2.ref()

user2.id = 2
user2.name = "bob"
user2.next = user1.ref()
user2.prev = user1.ref()

Finally, you can pack your collection of objects into a byte array, automatically computing pointer values using a base address, ready to be injected into the target address space!

start_address = 0x560A61DF4000 # e.g. heap address
packed = namespace.pack_values(start_address, 0x1000, [user1, user2])

The result:

0000560a61df4000  01 00 00 00 61 6c 69 63  65 00 00 00 00 00 00 00  |....alice.......|
0000560a61df4010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000560a61df4020  00 00 00 00 00 00 00 00  38 40 df 61 0a 56 00 00  |........8@.a.V..|
0000560a61df4030  38 40 df 61 0a 56 00 00  02 00 00 00 62 6f 62 00  |8@.a.V......bob.|
0000560a61df4040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000560a61df4050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000560a61df4060  00 40 df 61 0a 56 00 00  00 40 df 61 0a 56 00 00  |.@.a.V...@.a.V..|

See Also

  1. Connor, Richard J. III, Improved Architectures for Secure Intra-process Isolation. PhD diss., University of Tennessee, 2021. https://trace.tennessee.edu/utk_graddiss/6533
  2. proc/mem attack

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moria-c-0.3.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moria_c-0.3.1-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file moria-c-0.3.1.tar.gz.

File metadata

  • Download URL: moria-c-0.3.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for moria-c-0.3.1.tar.gz
Algorithm Hash digest
SHA256 1fa217f3c7b1540de2aee6f6b005aa52966a8cec8f2a7286dc66480caee83624
MD5 0b2d2f77910499d5425d8b94f24f30e5
BLAKE2b-256 956f79f3f8144e557a9cd35dc98916d13a786c7075d10535436bd9c36093e264

See more details on using hashes here.

File details

Details for the file moria_c-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: moria_c-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for moria_c-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e46565ca8c0d94223f5d86240bdbff400d341c9d09d91aefa67265fd1b47a6a
MD5 93b5e21284a442efe5ac1ca556a29372
BLAKE2b-256 9211a99adc4fe5f7c41b294b6dc943da56700d0d71a3fd13ccc3d32ac6093206

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page