-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* dead code elimination, __init__ cleanup * `binary.Elf` bugfix, add `binary` package tests
- Loading branch information
Showing
10 changed files
with
154 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[run] | ||
source = . | ||
omit = tests/* | ||
source = manticore | ||
omit = | ||
*__init__.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import io | ||
|
||
from elftools.elf.elffile import ELFFile | ||
|
||
|
||
class Binary: | ||
magics = {} | ||
|
||
def __new__(cls, path): | ||
if cls is Binary: | ||
cl = cls.magics[open(path, 'rb').read(4)] | ||
return cl(path) | ||
else: | ||
return super(Binary, cls).__new__(cls) | ||
|
||
def __init__(self, path): | ||
self.path = path | ||
self.magic = Binary.magics[open(path, 'rb').read(4)] | ||
|
||
def arch(self): | ||
pass | ||
|
||
def maps(self): | ||
pass | ||
|
||
def threads(self): | ||
pass | ||
|
||
|
||
class CGCElf(Binary): | ||
|
||
@staticmethod | ||
def _cgc2elf(filename): | ||
# hack begin so we can use upstream Elftool | ||
with open(filename, 'rb') as fd: | ||
stream = io.BytesIO(fd.read()) | ||
stream.write(b'\x7fELF') | ||
stream.name = fd.name | ||
return stream | ||
|
||
def __init__(self, filename): | ||
super().__init__(filename) | ||
stream = self._cgc2elf(filename) | ||
self.elf = ELFFile(stream) | ||
self.arch = {'x86': 'i386', 'x64': 'amd64'}[self.elf.get_machine_arch()] | ||
|
||
assert 'i386' == self.arch | ||
assert self.elf.header.e_type in ['ET_EXEC'] | ||
|
||
def maps(self): | ||
for elf_segment in self.elf.iter_segments(): | ||
if elf_segment.header.p_type not in ['PT_LOAD', 'PT_NULL', 'PT_PHDR', 'PT_CGCPOV2']: | ||
raise Exception("Not Supported Section") | ||
|
||
if elf_segment.header.p_type != 'PT_LOAD' or elf_segment.header.p_memsz == 0: | ||
continue | ||
|
||
flags = elf_segment.header.p_flags | ||
# PF_X 0x1 Execute - PF_W 0x2 Write - PF_R 0x4 Read | ||
perms = [' ', ' x', ' w ', ' wx', 'r ', 'r x', 'rw ', 'rwx'][flags & 7] | ||
if 'r' not in perms: | ||
raise Exception("Not readable map from cgc elf not supported") | ||
|
||
# CGCMAP-- | ||
assert elf_segment.header.p_filesz != 0 or elf_segment.header.p_memsz != 0 | ||
yield ((elf_segment.header.p_vaddr, | ||
elf_segment.header.p_memsz, | ||
perms, | ||
elf_segment.stream.name, elf_segment.header.p_offset, elf_segment.header.p_filesz)) | ||
|
||
def threads(self): | ||
yield (('Running', {'EIP': self.elf.header.e_entry})) | ||
|
||
|
||
class Elf(Binary): | ||
def __init__(self, filename): | ||
super().__init__(filename) | ||
self.elf = ELFFile(open(filename, 'rb')) | ||
self.arch = {'x86': 'i386', 'x64': 'amd64'}[self.elf.get_machine_arch()] | ||
assert self.elf.header.e_type in ['ET_DYN', 'ET_EXEC', 'ET_CORE'] | ||
|
||
# Get interpreter elf | ||
self.interpreter = None | ||
for elf_segment in self.elf.iter_segments(): | ||
if elf_segment.header.p_type != 'PT_INTERP': | ||
continue | ||
self.interpreter = Elf(elf_segment.data()[:-1]) | ||
break | ||
if self.interpreter is not None: | ||
assert self.interpreter.arch == self.arch | ||
assert self.interpreter.elf.header.e_type in ['ET_DYN', 'ET_EXEC'] | ||
|
||
def maps(self): | ||
for elf_segment in self.elf.iter_segments(): | ||
if elf_segment.header.p_type != 'PT_LOAD' or elf_segment.header.p_memsz == 0: | ||
continue | ||
|
||
flags = elf_segment.header.p_flags | ||
# PF_X 0x1 Execute - PF_W 0x2 Write - PF_R 0x4 Read | ||
perms = [' ', ' x', ' w ', ' wx', 'r ', 'r x', 'rw ', 'rwx'][flags & 7] | ||
if 'r' not in perms: | ||
raise Exception("Not readable map from cgc elf not supported") | ||
|
||
# CGCMAP-- | ||
assert elf_segment.header.p_filesz != 0 or elf_segment.header.p_memsz != 0 | ||
yield ((elf_segment.header.p_vaddr, | ||
elf_segment.header.p_memsz, | ||
perms, | ||
elf_segment.stream.name, elf_segment.header.p_offset, elf_segment.header.p_filesz)) | ||
|
||
def getInterpreter(self): | ||
return self.interpreter | ||
|
||
def threads(self): | ||
yield (('Running', {'EIP': self.elf.header.e_entry})) | ||
|
||
|
||
Binary.magics = {b'\x7fCGC': CGCElf, | ||
b'\x7fELF': Elf} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters