项目作者: doronz88

项目描述 :
simpleelf pypi module
高级语言: Python
项目地址: git://github.com/doronz88/simpleelf.git
创建时间: 2020-04-01T23:10:42Z
项目社区:https://github.com/doronz88/simpleelf

开源协议:

下载


Python package

Introduction

ELF file is not only an executable, but a very convenient way to describe
a program’s layout in memory. The original intention of this project is to
allow an individual to create an ELF file which describes the memory mapping
used for an embedded program. Especially useful for using together with other
analysis tools, such as:
IDA/Ghidra/etc… They can have all its desired information without the need to
open just an ordinary .bin file and running several IDAPython scripts
(I’m sick of Load additional binary file... option).

Pull Requests are of course more than welcome :smirk:.

Installation

Use pip:

  1. python3 -m pip install simpleelf

Or clone yourself and build:

  1. git clone git@github.com:doronz88/simpleelf.git
  2. cd simpleelf
  3. python -m pip install -e . -U

Running

Now you can just import simpleelf and start playing with it.

Parsing

Parsing is easy using ElfStruct.
Try it out:

  1. from simpleelf.elf_structs import ElfStructs
  2. ElfStructs('<').Elf32.parse(elf32_buffer) # outputs a constucts' container
  3. ElfStructs('<').Elf64.parse(elf64_buffer) # outputs a constucts' container

Building from scratch

Building is easy using ElfBuilder.
Try it out:

  1. from simpleelf.elf_builder import ElfBuilder
  2. from simpleelf import elf_consts
  3. # can also be used with ELFCLASS64 to create 64bit layouts
  4. e = ElfBuilder(elf_consts.ELFCLASS32)
  5. e.set_endianity('<')
  6. e.set_machine(elf_consts.EM_ARM)
  7. code = b'CODECODE'
  8. # add a segment
  9. text_address = 0x1234
  10. text_buffer = b'cybercyberbitimbitim' + code
  11. e.add_segment(text_address, text_buffer,
  12. elf_consts.PF_R | elf_consts.PF_W | elf_consts.PF_X)
  13. # add a second segment
  14. e.add_segment(0x88771122, b'data in 0x88771122',
  15. elf_consts.PF_R | elf_consts.PF_W | elf_consts.PF_X)
  16. # add a code section inside the first segment
  17. code_address = text_address + text_buffer.find(code) # point at CODECODE
  18. code_size = len(code)
  19. e.add_code_section(code_address, code_size, name='.text')
  20. # set entry point
  21. e.set_entry(code_address)
  22. # add .bss section. not requiring a loaded segment from
  23. # file
  24. bss_address = 0x5678
  25. bss_size = 0x200
  26. e.add_empty_data_section(bss_address, bss_size, name='.bss')
  27. # get raw elf
  28. e.build()