Skip to content

Commit 1fc3062

Browse files
fix(fmtstr)!: Attempt to fix the offsets
Predict the length of the search pattern instead of adding arguments. Also append the address to the search pattern.
1 parent e341e23 commit 1fc3062

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pwnlib/fmtstr.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -886,9 +886,14 @@ def fmtstr_payload(offset, writes, numbwritten=0, write_size='byte', write_size_
886886
all_atoms = make_atoms(writes, sz, szmax, numbwritten, overflows, strategy, badbytes)
887887

888888
fmt = b""
889+
# Predict the size of bytes to substract.
890+
# We consider that the pattern ``START%XXX$pEND`` is always used.
891+
# This is because ``prefix`` got placed after ``payload``.
892+
search_pattern = "START%{}$pEND".format(offset)
893+
reverse_offset = len(search_pattern) + (len(search_pattern) % context.bytes)
889894
for _ in range(1000000):
890895
data_offset = (offset_bytes + len(fmt)) // context.bytes
891-
fmt, data = make_payload_dollar(offset + data_offset, all_atoms, numbwritten=numbwritten, no_dollars=no_dollars)
896+
fmt, data = make_payload_dollar(offset + data_offset - (reverse_offset // context.bytes), all_atoms, numbwritten=numbwritten, no_dollars=no_dollars)
892897
fmt = fmt + cyclic((-len(fmt)-offset_bytes) % context.bytes)
893898

894899
if len(fmt) + offset_bytes == data_offset * context.bytes:
@@ -935,7 +940,7 @@ def __init__(self, execute_fmt, offset=None, padlen=0, numbwritten=0, badbytes=f
935940

936941
def leak_stack(self, offset, prefix=b""):
937942
payload = b"START%%%d$pEND" % offset
938-
leak = self.execute_fmt(prefix + payload)
943+
leak = self.execute_fmt(payload + prefix)
939944
try:
940945
leak = re.findall(br"START(.*?)END", leak, re.MULTILINE | re.DOTALL)[0]
941946
leak = int(leak, 16)

0 commit comments

Comments
 (0)