Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to load binary data from file with offset and size to area #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ScratchABit.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,15 @@ def parse_disasm_def(fname):
if args[2][0] in string.digits:
addr = str2int(args[2])
print("Loading %s @0x%x" % (args[1], addr))
engine.ADDRESS_SPACE.load_content(open(args[1], "rb"), addr)
file_off = 0
sz = None
if len(args) == 4:
try:
file_off = str2int(args[3])
except:
file_off, end = parse_range(args[3])
sz = end - file_off + 1
engine.ADDRESS_SPACE.load_content(open(args[1], "rb"), addr, sz, file_off)
else:
print("Loading %s (%s plugin)" % (args[1], args[2]))
loader = __import__(args[2])
Expand Down
5 changes: 5 additions & 0 deletions example-x86_64.def
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ area .bin 0x600000(0x1000) rwx
# Load binary code to disassemble to the defined memory area
load example-x86_64.bin 0x600000

# Load binary code to disassemble to the defined memory area
# optional argument takes offset in source file and lenght with same syntax as area
#load example-x86_64.bin 0x600000 0x8 #Load area size from example-x86_64.bin, skipping first 8 bytes of example-x86_64.bin
#load example-x86_64.bin 0x600000 0x8(0x9) #Load given size (0x9) from example-x86_64.bin, skipping first 0x8 bytes of example-x86_64.bin

# And/or load a structured executable (e.g. ELF) via a specific
# loader plugin ("elf" below).
#load example-x86_32.elf elf
Expand Down
7 changes: 5 additions & 2 deletions scratchabit/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,13 @@ def is_exec(self, addr):

# Binary Data API

def load_content(self, file, addr, sz=None):
def load_content(self, file, addr, sz=None, file_off=0):
off, area = self.addr2area(addr)
to = off + sz if sz else None
file.readinto(memoryview(area[BYTES])[off:to])
if file_off != 0:
file.seek(file_off)
rbc = file.readinto(memoryview(area[BYTES])[off:to])
log.info("read 0x%x bytes into area @0x%x from file %s with offset 0x%x" % (rbc, addr, file.name, file_off))

def is_valid_addr(self, addr):
off, area = self.addr2area(addr)
Expand Down