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

Fmtstr no dollar payload #2185

merged 14 commits into from
Apr 30, 2023

Conversation

murph12F
Copy link
Contributor

No $ notation support for Fmtstr module

Added a new feature in the fmtstr module, the fmtstr_payload(...) function now supports generating payload without using the $ notation.
This can be accomplished with the new flag in the fmstr_payload function parameters no_dollars.

Every line of code added has effect only if the no_dollar parameter is set, if is not set the module remain unchanged.


Function signature

Before

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

After

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)  -> str  

Example:

[ins] In [5]: from pwn import *

[ins] In [6]: context.clear( arch='amd64')

[ins] In [7]: fmtstr_payload(7,{ 0xdeadbeef : 0xc00ffe }  , no_dollars=True)
Out[7]: b'%c%c%c%c%c%c%c%c%c%c%c%c%4082c%lln%194c%hhnaaaab\x00\x00\x00\x00\x00\x00\x00\x00\xef\xbe\xad\xde\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf1\xbe\xad\xde\x00\x00\x00\x00'

Copy link
Member

@Arusekk Arusekk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! Thanks a lot, would you mind adding some extra doctest that would ensure the fmtstr stuff works? It might be beneficial to also launch the examples/fmtstr.py and examples/fmtstr2.py with no_dollars=True in the CI pipeline.

murph12F and others added 3 commits April 27, 2023 20:33
suggested change, dont need comparison in calling the make_payload_dollar

Co-authored-by: Arusekk <[email protected]>
suggestion; better use a non null byte thing to fill the values used by the %c to pad before writing.

Co-authored-by: Arusekk <[email protected]>
@Arusekk
Copy link
Member

Arusekk commented Apr 27, 2023

How about some tests now?

@murph12F
Copy link
Contributor Author

How about some tests now?

did you see the padding thing? i see that you answered that from my email but cand find your response, could you open another suggestion about that?
sorry :/

@murph12F
Copy link
Contributor Author

How about some tests now?

did you see the padding thing? i see that you answered that from my email but cand find your response, could you open another suggestion about that? sorry :/

i was looking at the output of the checks that failed, have a look at this, this one fail cause of the new feature added, because he expects to find %1c but we just printng "c" * 1.

Document: fmtstr
----------------
**********************************************************************
File "fmtstr.rst", line ?, in default
Failed example:
    fmtstr_payload(1, {0x0: 0x00000001}, write_size='byte')
Expected:
    b'%1c%3$na\x00\x00\x00\x00'
Got:
    'c%3$naaa\x00\x00\x00\x00'
**********************************************************************

@Arusekk
Copy link
Member

Arusekk commented Apr 27, 2023

Yes, the test needs to be updated.

Please add another test too, with no_dollars set to True.

…to save some bytes in the generation of the payload
@Arusekk
Copy link
Member

Arusekk commented Apr 27, 2023

Time for some tests :)

@murph12F
Copy link
Contributor Author

murph12F commented Apr 27, 2023

not really practical with tests :/ , what am i supposed to do, is there any doc to read about it?i ll check them next mornin, btw, i ll go to sleep now, been awake for too many hours, appreciate yo time mate. I ll link with you tomorrow. good morning/night gang

@murph12F
Copy link
Contributor Author

just had a look before closing the laptop, are those the test that i am supposed to do?

   16     Examples:
   15         >>> context.clear(arch = 'amd64')
   14         >>> fmtstr_payload(1, {0x0: 0x1337babe}, write_size='int')
   13         b'%322419390c%4$llnaaaabaa\x00\x00\x00\x00\x00\x00\x00\x00'
   12         >>> fmtstr_payload(1, {0x0: 0x1337babe}, write_size='short')
   11         b'%47806c%5$lln%22649c%6$hnaaaabaa\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'
   10         >>> fmtstr_payload(1, {0x0: 0x1337babe}, write_size='byte')
    9         b'%190c%7$lln%85c%8$hhn%36c%9$hhn%131c%10$hhnaaaab\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x
      02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00'
    8         >>> context.clear(arch = 'i386')
    7         >>> fmtstr_payload(1, {0x0: 0x1337babe}, write_size='int')
    6         b'%322419390c%5$na\x00\x00\x00\x00'
    5         >>> fmtstr_payload(1, {0x0: 0x1337babe}, write_size='short')
    4         b'%4919c%7$hn%42887c%8$hna\x02\x00\x00\x00\x00\x00\x00\x00'
    3         >>> fmtstr_payload(1, {0x0: 0x1337babe}, write_size='byte')
    2         b'%19c%12$hhn%36c%13$hhn%131c%14$hhn%4c%15$hhn\x03\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00'
    1         >>> fmtstr_payload(1, {0x0: 0x00000001}, write_size='byte')
  855         b'%1c%3$na\x00\x00\x00\x00'
    1         >>> fmtstr_payload(1, {0x0: b"\xff\xff\x04\x11\x00\x00\x00\x00"}, write_size='short')
    2         b
    ```
sorry for lines number, vim shit

@Arusekk
Copy link
Member

Arusekk commented Apr 27, 2023

Sure, the tests are just the code snippets in the documentation strings of the functions. You can save time and only run relevant tests with:

$ pip install -r docs/requirements.txt
$ python -bb -m sphinx -b doctest docs/source docs/build/doctest docs/source/fmtstr.rst

@murph12F
Copy link
Contributor Author

murph12F commented Apr 27, 2023

ok, appreciate it, i ll do that soon as i wake up.

@murph12F
Copy link
Contributor Author

@Arusekk added some tests ( just 3 for now ), i ve runned them as you suggested, seems working correctly. let me know, i ll keep doing some test.

@Arusekk Arusekk added this to the 4.11.0 milestone Apr 29, 2023
@murph12F
Copy link
Contributor Author

murph12F commented Apr 29, 2023

Hello, i ve seen you approved the changes, and added this request to a milestone, thanks.
Could i ask why this test keep failing? :
coverage/coveralls — Coverage decreased (-5.3%) to 68.181%

also you added this to the milestone to 4.11, want me to change that in my CHANGELOG? i ve putted it in the 4.12

@Arusekk
Copy link
Member

Arusekk commented Apr 29, 2023

Sure, also make sure the markdown is rendered correctly (i.e. one line added to the list and one to the link section). Never mind the coverage indicator, it is broken and I have been looking for some ways to replace it.

@murph12F
Copy link
Contributor Author

ok updated the changelog file with correct version. i ve copied the other lines to be sure to do that right, have a look in when u have a second, after this changes gets approved i ll just have to wait right, till the drop of the next milestone? ( sorry newbie in contributing other projects) appreciate it

@Arusekk Arusekk merged commit 83e3b5b into Gallopsled:dev Apr 30, 2023
@murph12F murph12F deleted the fmtstr_no_dollar_payload branch May 8, 2023 16:39
gogo2464 pushed a commit to gogo2464/pwntools that referenced this pull request Sep 10, 2023
* added feature to the fmtstr module, now able to generate payload without the dollar syntax.

* made some improvements in the no_dollar feature implemented

* reduced different lines

* removed some blank lines and debug stuff

* (fix) missing check for no_dollar option

* (fix) updatet argument type for documentation

* updated CHANGELOG.md

* (fix) typo in the CHANGELOG.md version

* Update pwnlib/fmtstr.py

suggested change, dont need comparison in calling the make_payload_dollar

Co-authored-by: Arusekk <[email protected]>

* Update pwnlib/fmtstr.py

suggestion; better use a non null byte thing to fill the values used by the %c to pad before writing.

Co-authored-by: Arusekk <[email protected]>

* (fix) minor fixes, typos on comments

* update pwnlib/fmtstr.py : suggestion from Arusekk, made improvements to save some bytes in the generation of the payload

* added tests to fmtstr_payload with no dollar flag

* update CHANGELOG.md with correct version

---------

Co-authored-by: Arusekk <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants