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

Another attempt for rop.ret2csu #1429

Merged
merged 27 commits into from
Apr 28, 2021
Merged

Another attempt for rop.ret2csu #1429

merged 27 commits into from
Apr 28, 2021

Conversation

Arusekk
Copy link
Member

@Arusekk Arusekk commented Feb 9, 2020

Just an adjusted copy of #1138

pwnlib/rop/rop.py Outdated Show resolved Hide resolved
@PaleNeutron
Copy link

Any idea to use ret2csu automatically in setRegisters?

@heapcrash heapcrash modified the milestones: 4.3.0, Someday Jun 9, 2020
@heapcrash
Copy link
Collaborator

@Arusekk can you update this branch and see if we can get this merged?

@Arusekk
Copy link
Member Author

Arusekk commented Jun 17, 2020

Updated. I want to state that I am against merging it as it is now, since:

  • ROP imports capstone unconditionally. This is hell slow (from pwn import * takes 3x longer) and capstone is promoted from optional to obligatory dependency (unicorn is also de-facto an optional dep only, I like it this way), so we would move this to a separate file, I guess?
  • I think this can be done with a little bit better logic, not sure, though.
  • This has no tests! C'mon, how can we trust this code continues to work! (it does work now, at least...)

@Arusekk
Copy link
Member Author

Arusekk commented Oct 13, 2020

I changed this to a lazy import, and added tests (and they do work locally, I swear).

@zachriggle
Copy link
Member

I agree with @Arusekk's points.

"From own import *" needs to be as fast as possible. We should probably do some benchmarking of each commit with hyper fine to see where speed regressions lie.

Tests are absolutely critical for anything like this, and should definitely include at LEAST the basic case, if not also cases that do NOT work (eg missing the CSU area) to make sure things break in a clean and resumaable fashion

@Arusekk
Copy link
Member Author

Arusekk commented Dec 7, 2020

Alrightee, I have seriously no clue about why is this working every time under python 2, while never working under python 3 on GHA. It also always works locally, so this is a total mystery. Py3 even generates the very rop chain that py2 does, and it writes the same input to the same executable file! If anyone could investigate further, this would be a great relief.

@Arusekk Arusekk added the mystery This is really suspicious and probably related to something totally unrelated label Dec 7, 2020
Copy link

@kaftejiman kaftejiman left a comment

Choose a reason for hiding this comment

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

Works fine for me on Python 3.6.9, on which version exactly it didnt work, can you please provide more details?

Comment on lines +1448 to +1461
nonpie = csu = None
for elf in exes:
if not elf.pie:
if '__libc_csu_init' in elf.symbols:
break
nonpie = elf
elif '__libc_csu_init' in elf.symbols:
csu = elf

if elf.pie:
if nonpie:
elf = nonpie
elif csu:
elf = csu

Choose a reason for hiding this comment

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

Suggested change
nonpie = csu = None
for elf in exes:
if not elf.pie:
if '__libc_csu_init' in elf.symbols:
break
nonpie = elf
elif '__libc_csu_init' in elf.symbols:
csu = elf
if elf.pie:
if nonpie:
elf = nonpie
elif csu:
elf = csu
for e in exes:
elf = e
if not e.pie:
break

Copy link
Member Author

Choose a reason for hiding this comment

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

What about all the elves having __libc_csu_init in their symbols? This code is not mine, so I have no idea of this logic and I fancy the idea of simplifying it.

@Arusekk
Copy link
Member Author

Arusekk commented Jan 19, 2021

Works fine for me on Python 3.6.9, on which version exactly it didnt work, can you please provide more details?

Works fine for me too, on all py 3 versions from 3.6 to 3.10, does not work on the CI only.

Here are the details:

  1. Pwntools test launches an executable file.
  2. Then it sends some data on its input.
  3. The data is the same on py2 and py3, the same on CI and my box. The executable is the same on CI and on my box, the same on py2 and py3.
  4. Something different happens. One executable reliably produces a valid core file on crash, the other reliably produces rubbish on crash. This might be some strange thing related to environment variables or whatever else, or runtime stuff. No idea.

@152334H
Copy link
Contributor

152334H commented Jan 24, 2021

Bug report (unrelated to the mysterious CI crashing): the whole section here makes assumptions about the order of movs that don't apply for a challenge binary I found:

.text:00000000004012D0 loc_4012D0:                             ; CODE XREF: __libc_csu_init+54j
.text:00000000004012D0                 mov     rdx, r14
.text:00000000004012D3                 mov     rsi, r13
.text:00000000004012D6                 mov     edi, r12d
.text:00000000004012D9                 call    ds:(__frame_dummy_init_array_entry - 403DF8h)[r15+rbx*8]

The code hyperlinked above assumes that r13-15 are used in setting edi/rsi/rdx, but (apparently) that isn't universal.

Locally, I made a hardcoded solution of changing r13-15 to r12-14, but I'm pretty sure that's not the right thing to do for the actual finalized PR. Not too familiar with capstone and I'm guessing there'll be a lot more horrible edge cases to come.

Arusekk added 2 commits April 19, 2021 12:30
BTW fix py3 sockets incosistency
@Arusekk
Copy link
Member Author

Arusekk commented Apr 19, 2021

Update: the core file is not dropped under python 3 at all, supposedly because there already is a core file in current directory. Another thing is why this core file is not passed through apport.

@Arusekk Arusekk merged commit 603e5ba into Gallopsled:dev Apr 28, 2021
@Arusekk Arusekk deleted the ret2csu branch April 28, 2021 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mystery This is really suspicious and probably related to something totally unrelated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants