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

Enable leaking memory relatively with MemLeak #822

Merged
merged 3 commits into from
Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pwn/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pwnlib.flag import *
from pwnlib.fmtstr import FmtStr, fmtstr_payload
from pwnlib.log import getLogger
from pwnlib.memleak import MemLeak
from pwnlib.memleak import MemLeak, RelativeMemLeak
from pwnlib.regsort import *
from pwnlib.replacements import *
from pwnlib.rop import ROP
Expand Down
17 changes: 15 additions & 2 deletions pwnlib/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,19 @@ def some_leaker(addr):
leaking 0xff
leaking 0x103
True

>>> memory = {-4+i: c for i,c in enumerate('wxyzABCDE')}
>>> def relative_leak(index):
... return memory.get(index, None)
>>> leak = pwnlib.memleak.MemLeak(relative_leak, relative = True)
>>> leak[-1:2]
'zAB'
"""
def __init__(self, f, search_range = 20, reraise = True):
def __init__(self, f, search_range = 20, reraise = True, relative = False):
self.leak = f
self.search_range = search_range
self.reraise = reraise
self.relative = relative

# Map of address: byte for all bytes received
self.cache = {}
Expand Down Expand Up @@ -166,7 +174,7 @@ def _leak(self, addr, n, recurse=True):
Returns:
A string of length ``n``, or ``None``.
"""
if addr < 0:
if not self.relative and addr < 0:
return None

addresses = [addr+i for i in xrange(n)]
Expand Down Expand Up @@ -617,3 +625,8 @@ def string_wrapper(address, *a, **kw):
p32 = setd
p16 = setw
p8 = setb

class RelativeMemLeak(MemLeak):
def __init__(self, *a, **kw):
kw.setdefault('relative', True)
super(RelativeMemLeak, self).__init__(*a, **kw)