Skip to content

Fmtstr no dollar payload #2185

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

Merged
merged 14 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The table below shows which release corresponds to each branch, and what date th

| Version | Branch | Release Date |
| ---------------- | -------- | ---------------------- |
| [4.12.0](#4120) | `dev` |
| [4.11.0](#4110) | `dev` |
| [4.10.0](#4100) | `beta` |
| [4.9.0](#490) | `stable` | Dec 29, 2022
Expand Down Expand Up @@ -65,6 +66,10 @@ The table below shows which release corresponds to each branch, and what date th
| [3.0.0](#300) | | Aug 20, 2016
| [2.2.0](#220) | | Jan 5, 2015

## 4.12.0 (`dev`)

- [#2185][2185] make fmtstr able to create payload without $ notation

## 4.11.0 (`dev`)

- [#2062][2062] make pwn cyclic -l work with entry larger than 4 bytes
Expand Down
64 changes: 60 additions & 4 deletions pwnlib/fmtstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def sort_atoms(atoms, numbwritten):

return out

def make_payload_dollar(data_offset, atoms, numbwritten=0, countersize=4):
def make_payload_dollar(data_offset, atoms, numbwritten=0, countersize=4, no_dollars=False):
r'''
Makes a format-string payload using glibc's dollar syntax to access the arguments.

Expand All @@ -688,6 +688,7 @@ def make_payload_dollar(data_offset, atoms, numbwritten=0, countersize=4):
atoms(list): list of atoms to execute
numbwritten(int): number of byte already written by the printf function
countersize(int): size in bytes of the format string counter (usually 4)
no_dollars(bool) : flag to generete the payload with or w/o $ notation

Examples:
>>> pwnlib.fmtstr.make_payload_dollar(1, [pwnlib.fmtstr.AtomWrite(0x0, 0x1, 0xff)])
Expand All @@ -697,6 +698,13 @@ def make_payload_dollar(data_offset, atoms, numbwritten=0, countersize=4):
fmt = ""

counter = numbwritten

if no_dollars:
# since we cant dynamically offset, we have to increment manually the parameter index, use %c, so the numeber of bytes written is predictable
fmt += "%c" * ( data_offset - 1)
# every %c write a byte, so we need to keep track of that to have the right pad
counter += data_offset - 1

for idx, atom in enumerate(atoms):
# set format string counter to correct value
padding = atom.compute_padding(counter)
Expand All @@ -709,7 +717,47 @@ def make_payload_dollar(data_offset, atoms, numbwritten=0, countersize=4):
# perform write
if padding:
fmt += "%" + str(padding) + "c"
fmt += "%" + str(data_offset + idx) + "$" + SPECIFIER[atom.size]

if no_dollars:
data += pack(0)
'''
[ @murph12F was here ]

the data += pack(0) is used to keey the arguments aligned when a %c is performed, so it wont use the actual address to write at
examplea stack and payload:

fmtsr = %44c%hhn%66c%hhn

---------
| addr2 |
---------
| 0x000 |
---------
| addr1 |
---------
| 0x000 | <-- (rsp)
---------

in this case the the first %44c will use the current arugument used pointed by rsp ( 0 ), and increment rsp

---------
| addr2 |
---------
| 0X000 |
---------
| addr1 | <-- (rsp)
---------
| 0x000 |
---------

now it will perform the %hhn, and it will correctly use the addr1 argument
'''

if no_dollars:
fmt += "%" + SPECIFIER[atom.size]
else:
fmt += "%" + str(data_offset + idx) + "$" + SPECIFIER[atom.size]

data += pack(atom.start)

return fmt.encode(), data
Expand Down Expand Up @@ -762,7 +810,7 @@ def fmtstr_split(offset, writes, numbwritten=0, write_size='byte', write_size_ma

return make_payload_dollar(offset, atoms, numbwritten)

def fmtstr_payload(offset, writes, numbwritten=0, write_size='byte', write_size_max='long', overflows=16, strategy="small", badbytes=frozenset(), offset_bytes=0):
def fmtstr_payload(offset, writes, numbwritten=0, write_size='byte', write_size_max='long', overflows=16, strategy="small", badbytes=frozenset(), offset_bytes=0, no_dollars=False):
r"""fmtstr_payload(offset, writes, numbwritten=0, write_size='byte') -> str

Makes payload with given parameter.
Expand All @@ -779,6 +827,7 @@ def fmtstr_payload(offset, writes, numbwritten=0, write_size='byte', write_size_
write_size(str): must be ``byte``, ``short`` or ``int``. Tells if you want to write byte by byte, short by short or int by int (hhn, hn or n)
overflows(int): how many extra overflows (at size sz) to tolerate to reduce the length of the format string
strategy(str): either 'fast' or 'small' ('small' is default, 'fast' can be used if there are many writes)
no_dollars(bool) : flag to generete the payload with or w/o $ notation
Returns:
The payload in order to do needed writes

Expand Down Expand Up @@ -809,7 +858,14 @@ def fmtstr_payload(offset, writes, numbwritten=0, write_size='byte', write_size_
fmt = b""
for _ in range(1000000):
data_offset = (offset_bytes + len(fmt)) // context.bytes
fmt, data = make_payload_dollar(offset + data_offset, all_atoms, numbwritten=numbwritten)

# if no dollar is set, call the payload with the parameter no dollar set
if no_dollars:
fmt, data = make_payload_dollar(offset + data_offset, all_atoms, numbwritten=numbwritten, no_dollars=True)

else:
fmt, data = make_payload_dollar(offset + data_offset, all_atoms, numbwritten=numbwritten)

fmt = fmt + cyclic((-len(fmt)-offset_bytes) % context.bytes)

if len(fmt) + offset_bytes == data_offset * context.bytes:
Expand Down