I would like to attach to a running process using WinDbg, and modify a certain function’s code to simply return on invocation (for educational purposes).
I have used the following commands:
uf dll!name
This gives me a disassembly of the function.
I have picked a specific address at a certain location and modified it to ret:
ew addr c3
This crashes every time, what am i doing wrong?
You need to make sure you do the appropriate clean up so the stack is left in a proper state. Depending on the calling convention the method usually pushes stuff on the stack as part of the prologue. This must be undone as part of the epilogue.
Here’s an example of changing a JIT compiled method using WinDbg.
The code:
I compiled this as Debug to prevent the compiler from inlining the calls to
Message.Then I ran the executable and attached the debugger at the call to
ReadLine.For managed code I need to use SOS.dll to locate the JIT compiled code. So I loaded SOS and found the address for the code as follows.
Then I opened the Memory window and pointed it to 002a00ab which is the first part of the actual method body of
Messageand changed the two opcodes to5dandc3forpop edbandretrespectively. If I skipped thepop edbpart the stack would be messed up and I would get an exception.I hit Go and the application continued without printing “message” a second time.