Giter Site home page Giter Site logo

py-elf-structs's Introduction

Py-elf-structs

This repository parse dwarf information from elfs and generate python structs accordingly

Usage

First lets write our elf:

struct command {
    char command[64];
};

struct command_with_args {
    char arg1[128];
    struct command command;
};
/*
    Ignore this part it is only done for disabling optimization
    Optimization will omit the structs if they are not being used 
    -O0 omits this structs from the output for some reason
*/
void main() {
    struct command a = {};
    struct command_with_args b = {};
    printf("a = %p, b=%p\n", a, b);
}

While compiling we must generate type information:

gcc main.c -dwarf-2 -ggdb -o a.out

Then generate python structs

python -m py_elf_structs a.out /tmp/structs.json

Finally, load the structs and interact with them

from py_elf_structs import load_structs

structs = load_structs("/tmp/structs.json")

command_with_args = structs.command_with_args(arg="/tmp", 
                          command=structs.command(
                              command="ls -la"
                          ))

# You can pack this struct
command_with_args.pack()

# Unpack is also supported
command_with_args = structs.command_with_args.unpack("<stream>")

You can also use a python api to generate the structs.json file:

from py_elf_structs import generate_structs
src_file="a.out"
output_file="/tmp/structs.json"
verbose=True
generate_structs(src_file=src_file,
                 output_file=output_file,
                 is_verbose=verbose)

Protected attributes

Attribute with the name size is used by the parser therefor if a struct contain a variable named size it is replaced by _size eg ..

struct my_struct {
    int size;
}

python api:

from py_elf_structs import load_structs
structs = load_structs("/tmp/structs.json")
structs.my_struct(_size=2)

Struct alignment

Struct maybe aligned to sizeof(ptr) therefore we should support this eg ...

struct command {
    unsigned int address;
    unsigned short value;
};

The resulting cstruct is:

struct command {
    unsigned int address;
    unsigned short value[2];
};

Because this struct is aligned to 4 it is handled by the api and you can create this struct anyway:

from py_elf_structs import load_structs

structs = load_structs("/tmp/structs.json")

structs.command(address=1, value=2)
# This will create the struct and fix value to be an array

py-elf-structs's People

Contributors

jonatansh avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.