Skip to content

Commit

Permalink
PE: API returns None on bad traduction addreses
Browse files Browse the repository at this point in the history
  • Loading branch information
serpilliere committed Nov 25, 2019
1 parent 029f197 commit 87dba49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 6 additions & 2 deletions miasm/jitter/loader/pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,12 @@ def vm2pe(myjit, fname, libs=None, e_orig=None,
addrs = list(all_mem)
addrs.sort()
entry_point = mye.virt2rva(myjit.pc)
if not 0 < entry_point < 0xFFFFFFFF:
raise ValueError("Cannot compute a valid entry point RVA")
if entry_point is None or not 0 < entry_point < 0xFFFFFFFF:
raise ValueError(
"Current pc (0x%x) used as entry point seems to be out of the binary" %
myjit.pc
)

mye.Opthdr.AddressOfEntryPoint = entry_point
first = True
for ad in addrs:
Expand Down
24 changes: 18 additions & 6 deletions miasm/loader/pe_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,18 +476,30 @@ def off2rva(self, off):
return
return off - section.offset + section.addr

def virt2rva(self, virt):
if virt is None:
return
return virt - self.NThdr.ImageBase
def virt2rva(self, addr):
"""
Return rva of virtual address @addr; None if addr is below ImageBase
"""
if addr is None:
return None
rva = addr - self.NThdr.ImageBase
if rva < 0:
return None
return rva

def rva2virt(self, rva):
if rva is None:
return
return rva + self.NThdr.ImageBase

def virt2off(self, virt):
return self.rva2off(self.virt2rva(virt))
def virt2off(self, addr):
"""
Return offset of virtual address @addr
"""
rva = self.virt2rva(addr)
if rva is None:
return None
return self.rva2off(rva)

def off2virt(self, off):
return self.rva2virt(self.off2rva(off))
Expand Down

0 comments on commit 87dba49

Please sign in to comment.