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

_initData overwrites data if it lies within the same page #5

Open
nemhods opened this issue Dec 8, 2016 · 0 comments
Open

_initData overwrites data if it lies within the same page #5

nemhods opened this issue Dec 8, 2016 · 0 comments

Comments

@nemhods
Copy link
Contributor

nemhods commented Dec 8, 2016

Injecting data at multiple addresses that lie within the same memory page causes any data but the last to be discarded.

Consider the following IDAPython demo code:

from idaemu import *
import binascii

e = Emu(UC_ARCH_X86, UC_MODE_32)

# write two bytes into the same page

e.setData(0x0FFFFF, b"\xAA") # new page
e.setData(0x100000, b"\xAA") # new page
e.setData(0x100010, b"\xAA") # same page as above, will overwrite data at 0x10000

e.eUntilAddress(0x401000, 0x401000) # run the emulator once to init data

print "0x0FFFFF:", binascii.hexlify(e.curUC.mem_read(0x0FFFFF, 1))
print "0x100000:", binascii.hexlify(e.curUC.mem_read(0x100000, 1)) # this results in 0x00, not 0xAA as expected
print "0x100010:", binascii.hexlify(e.curUC.mem_read(0x100010, 1))

This seems to be occurring because the loop that injects the data into the emulator memory calls uc.mem_map for every data element in the list. If the memory region that is mapped already contains data from a previous call, this data will now be disregarded.

Quick workaround:

    def _initData(self, uc):
        # DIRTY FIX: first loop maps memory
        for address, data, init in self.data:
            addr = self._alignAddr(address)
            size = PAGE_ALIGN
            while addr + size < len(data): size += PAGE_ALIGN
            uc.mem_map(addr, size)
            
        # second loop actually writes
        for address, data, init in self.data:
            addr = self._alignAddr(address)
            size = PAGE_ALIGN
            if init: 
                uc.mem_write(addr, self._getOriginData(addr, size))
            uc.mem_write(address, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant