forked from MockyBang/AndroidNativeEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now uses symbols from previously loaded libraries.
- Loading branch information
Showing
8 changed files
with
136 additions
and
39 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from collections import defaultdict | ||
|
||
from elftools.elf.elffile import SymbolTableSection | ||
|
||
|
||
class Module: | ||
|
||
""" | ||
:type filename str | ||
:type base_addr int | ||
:type size int | ||
:type dynsym SymbolTableSection | ||
""" | ||
def __init__(self, filename, address, size, dynsym): | ||
self.filename = filename | ||
self.base_addr = address | ||
self.size = size | ||
self.symbols = ModuleSymbols(dynsym) | ||
|
||
|
||
# Thanks to | ||
# https://github.com/eliben/pyelftools/blob/82299758cc0c0ca788de094ee2d83f6f490a8ef4/elftools/elf/sections.py#L143 | ||
class ModuleSymbols: | ||
|
||
def __init__(self, dynsym): | ||
self._symbols = list(dynsym.iter_symbols()) | ||
self._symbol_name_map = None | ||
|
||
def get_symbol_by_name(self, name): | ||
if self._symbol_name_map is None: | ||
self._symbol_name_map = defaultdict(list) | ||
for i, sym in enumerate(self.iter_symbols()): | ||
self._symbol_name_map[sym.name].append(i) | ||
|
||
symnums = self._symbol_name_map.get(name) | ||
|
||
return [self.get_symbol(i) for i in symnums] if symnums else None | ||
|
||
def num_symbols(self): | ||
return len(self._symbols) | ||
|
||
def get_symbol(self, n): | ||
return self._symbols[n] | ||
|
||
def iter_symbols(self): | ||
for i in range(self.num_symbols()): | ||
yield self.get_symbol(i) |
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,32 @@ | ||
from unicorn import UC_HOOK_CODE | ||
from unicorn.arm_const import * | ||
|
||
from androidemu.emulator import Emulator | ||
|
||
# Initialize emulator | ||
emulator = Emulator() | ||
emulator.load_library("example_binaries/libc.so") | ||
my_base = emulator.load_library("example_binaries/libnative-lib.so") | ||
|
||
# Show loaded modules. | ||
print("Loaded modules:") | ||
|
||
for module in emulator.modules: | ||
print("[0x%x] %s" % (module.base_addr, module.filename)) | ||
|
||
|
||
# Add debugging. | ||
def hook_code(mu, address, size, user_data): | ||
instruction = mu.mem_read(address, size) | ||
instruction_str = ''.join('{:02x} '.format(x) for x in instruction) | ||
|
||
print('# Tracing instruction at 0x%x, instruction size = 0x%x, instruction = %s' % (address, size, instruction_str)) | ||
|
||
|
||
emulator.mu.hook_add(UC_HOOK_CODE, hook_code) | ||
|
||
# Runs a method of "libnative-lib.so" that calls an imported | ||
# function "strlen" from "libc.so". | ||
emulator.mu.emu_start(my_base + 0x7E6 + 1, my_base + 0x7EA) | ||
|
||
print("String length is: %i" % emulator.mu.reg_read(UC_ARM_REG_R0)) |
Binary file not shown.
Binary file not shown.