diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-02-08 13:45:00 +0100 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-02-11 16:03:39 +0100 |
| commit | 0fecc22af0a10d4871e68532bbab69d1bffadec5 (patch) | |
| tree | 2ddee1f7d4d4e5f4b2566d16cec5823cbe084ef3 | |
| parent | d73460dd09302b1a272ad7e3e2cfd1c7cf0bf86c (diff) | |
| download | focaccia-miasm-0fecc22af0a10d4871e68532bbab69d1bffadec5.tar.gz focaccia-miasm-0fecc22af0a10d4871e68532bbab69d1bffadec5.zip | |
API: replace shift_offset by base_address
WARNING: base address is the negative of shift_offset
| -rw-r--r-- | example/disasm/full.py | 8 | ||||
| -rw-r--r-- | miasm2/analysis/binary.py | 4 | ||||
| -rw-r--r-- | miasm2/core/bin_stream.py | 38 |
3 files changed, 27 insertions, 23 deletions
diff --git a/example/disasm/full.py b/example/disasm/full.py index cc7f9be8..d159e8c6 100644 --- a/example/disasm/full.py +++ b/example/disasm/full.py @@ -45,9 +45,9 @@ parser.add_argument('-l', "--dontdis-retcall", action="store_true", help="If set, disassemble only call destinations") parser.add_argument('-s', "--simplify", action="count", help="Apply simplifications rules (liveness, graph simplification, ...)") -parser.add_argument('-o', "--shiftoffset", default=0, +parser.add_argument("--base-address", default=0, type=lambda x: int(x, 0), - help="Shift input binary by an offset") + help="Base address of the input binary") parser.add_argument('-a', "--try-disasm-all", action="store_true", help="Try to disassemble the whole binary") parser.add_argument('-i', "--image", action="store_true", @@ -79,10 +79,10 @@ if args.verbose: log.info('Load binary') if args.rawbinary: cont = Container.fallback_container(open(args.filename, "rb").read(), - vm=None, addr=args.shiftoffset) + vm=None, addr=args.base_address) else: with open(args.filename, "rb") as fdesc: - cont = Container.from_stream(fdesc, addr=args.shiftoffset) + cont = Container.from_stream(fdesc, addr=args.base_address) default_addr = cont.entry_point bs = cont.bin_stream diff --git a/miasm2/analysis/binary.py b/miasm2/analysis/binary.py index 90d71369..93bd74b2 100644 --- a/miasm2/analysis/binary.py +++ b/miasm2/analysis/binary.py @@ -68,7 +68,7 @@ class Container(object): """Instantiate a container and parse the binary @stream: stream to use as binary @vm: (optional) VmMngr instance to link with the executable - @addr: (optional) Shift to apply before parsing the binary. If set, + @addr: (optional) Base address of the parsed binary. If set, force the unknown format """ return Container.from_string(stream.read(), *args, **kwargs) @@ -215,7 +215,7 @@ class ContainerUnknown(Container): "Container abstraction for unknown format" def parse(self, data, vm=None, addr=0, **kwargs): - self._bin_stream = bin_stream_str(data, shift=addr) + self._bin_stream = bin_stream_str(data, base_address=addr) if vm is not None: vm.add_memory_page(addr, PAGE_READ, diff --git a/miasm2/core/bin_stream.py b/miasm2/core/bin_stream.py index 1dada90f..af31a52c 100644 --- a/miasm2/core/bin_stream.py +++ b/miasm2/core/bin_stream.py @@ -160,61 +160,65 @@ class bin_stream(object): class bin_stream_str(bin_stream): - def __init__(self, input_str="", offset=0L, shift=0): + def __init__(self, input_str="", offset=0L, base_address=0, shift=None): bin_stream.__init__(self) + if shift is not None: + raise DeprecationWarning("use base_address instead of shift") self.bin = input_str self.offset = offset - self.shift = shift + self.base_address = base_address self.l = len(input_str) def _getbytes(self, start, l=1): - if start + l + self.shift > self.l: + if start + l - self.base_address > self.l: raise IOError("not enough bytes in str") - if start + self.shift < 0: + if start - self.base_address < 0: raise IOError("Negative offset") - return super(bin_stream_str, self)._getbytes(start + self.shift, l) + return super(bin_stream_str, self)._getbytes(start - self.base_address, l) def readbs(self, l=1): - if self.offset + l + self.shift > self.l: + if self.offset + l - self.base_address > self.l: raise IOError("not enough bytes in str") - if self.offset + self.shift < 0: + if self.offset - self.base_address < 0: raise IOError("Negative offset") self.offset += l - return self.bin[self.offset - l + self.shift:self.offset + self.shift] + return self.bin[self.offset - l - self.base_address:self.offset - self.base_address] def __str__(self): - out = self.bin[self.offset + self.shift:] + out = self.bin[self.offset - self.base_address:] return out def setoffset(self, val): self.offset = val def getlen(self): - return self.l - (self.offset + self.shift) + return self.l - (self.offset - self.base_address) class bin_stream_file(bin_stream): - def __init__(self, binary, offset=0L, shift=0): + def __init__(self, binary, offset=0L, base_address=0, shift=None): bin_stream.__init__(self) + if shift is not None: + raise DeprecationWarning("use base_address instead of shift") self.bin = binary self.bin.seek(0, 2) - self.shift = shift + self.base_address = base_address self.l = self.bin.tell() self.offset = offset def getoffset(self): - return self.bin.tell() - self.shift + return self.bin.tell() + self.base_address def setoffset(self, val): - self.bin.seek(val + self.shift) + self.bin.seek(val - self.base_address) offset = property(getoffset, setoffset) def readbs(self, l=1): - if self.offset + l + self.shift > self.l: + if self.offset + l - self.base_address > self.l: raise IOError("not enough bytes in file") - if self.offset + self.shift < 0: + if self.offset - self.base_address < 0: raise IOError("Negative offset") return self.bin.read(l) @@ -222,7 +226,7 @@ class bin_stream_file(bin_stream): return str(self.bin) def getlen(self): - return self.l - (self.offset + self.shift) + return self.l - (self.offset - self.base_address) class bin_stream_container(bin_stream): |