radareorg / radare2

UNIX-like reverse engineering framework and command-line toolset
https://www.radare.org/
GNU Lesser General Public License v3.0
20.73k stars 3.01k forks source link

Better ways to handle sp-based vars #12070

Open thestr4ng3r opened 6 years ago

thestr4ng3r commented 6 years ago

Consider the following example: photo_2018-11-06_17-34-40 Both pushes use the same offset from rsp, but they will end up reading from different addresses. r2 assigns the same var to both, which is wrong.

My idea to solve this by "converting" sp-based vars to bp-based ones (since bp in practice usually doesn't change inside a function) would be the following: Do a complete trace through the whole graph of the function from the beginning to the end and save the value of bp-sp (size of the stackframe) as metadata for each instruction. RAnalOp.stackptr gives the difference by which sp changes when the instruction is executed. Then, for each sp + x, an equivalent bp + y can be calculated, which will identify our vars.

This has some downsides/pitfalls:

Thus, as a fallback if the above method fails, it would be possible to just create a new variable for each instruction that accesses any sp + ? (or perhaps for a sequence of instruction where we know that sp doesn't change?).

thestr4ng3r commented 6 years ago

Quick and dirty implementation of the sp tracing in r2pipe: https://gist.github.com/thestr4ng3r/403fecffc081a899618b1500ba6d1156 It fails pretty hard on callee cleanup calls if the arguments of the callee are not known.

radare commented 5 years ago

i think we should have a codeshare-style repository to hold all those r2pipe snippets. maybe managed via r2pm and having a database of gist urls?

thestr4ng3r commented 5 years ago

@radare What do you think about adding an analysis hint that contains the stackframe size for an instruction? This could be easily filled from different kinds of analysis and then used later.

radare commented 5 years ago

Sgtm

On 22 May 2019, at 13:55, Florian Märkl notifications@github.com wrote:

@radare What do you think about adding an analysis hint that contains the stackframe size for an instruction? This could be easily filled from different kinds of analysis and then used later.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

thestr4ng3r commented 5 years ago

Now that ahF is there, here is an example how to import an x64dbg trace to annotate sp-based vars with a bp offset: https://gist.github.com/thestr4ng3r/24a7b93a8b936019316d6fff46becf33 This produces: Bildschirmfoto vom 2019-05-26 13-31-14