bash
pip install pyelftools
python
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import NoteSection, SymbolTableSection
from elftools.elf.segments import InterpSegment, NoteSegment, Segment
from elftools.elf.relocation import RelocationSection
python
with open('file.elf', 'rb') as f:
elf = ELFFile(f)
python
from elftools.elf.dynamic import DynamicSection
from elftools.elf.descriptions import describe_symbol_type
from elftools.elf.enums import ENUM_NDX
def disassemble(elf):
for section in elf.iter_text_sections():
start_addr = section['sh_addr']
size = section['sh_size']
data = section.data()
disasm = pyvex.IRRepr()
for insn in disasm.decode('AMD64', code, 0x1000):
print(insn)
python
from elftools.elf.elffile import *
from elftools.elf.sections import SymbolTableSection
def assemble(elf):
section = elf.create_section(".text", "text", 0o777)
code = """
mov rax, 1
mov rdi, 1
mov rsi, message
mov rdx, 12
syscall
mov rax, 60
xor rdi, rdi
syscall
message:
.asciz "Hello, World!"
"""
assembler = NASMAsm()
machine_code = assembler.assemble(code)
section.data = machine_code
python
from elftools.elf.elffile import *
from elftools.elf.assemble import NASMAsm
import pyvex
def disassemble(elf):
for section in elf.iter_text_sections():
start_addr = section['sh_addr']
size = section['sh_size']
data = section.data()
disasm = pyvex.IRRepr()
code = data[:size]
for insn in disasm.decode('AMD64', code, 0x1000):
print(insn)
def assemble(elf):
section = elf.create_section(".text", "text", 0o777)
code = """
mov rax, 1
mov rdi, 1
mov rsi, message
mov rdx, 12
syscall
mov rax, 60
xor rdi, rdi
syscall
message:
.asciz "Hello, World!"
"""
assembler = NASMAsm()
machine_code = assembler.assemble(code)
section.data = machine_code
with open('file.elf', 'rb') as f:
elf = ELFFile(f)
disassemble(elf)
assemble(elf)
with open('file_modified.elf', 'wb') as f:
elf.write(f)