Understanding Process Memory
Understanding Process Memory
Process Memory
(Win32)
Software Security Assessment
Lecture 0x02
Keith Makan @k3170makan
Some stuff you will probably need to read after this class:
http://rsquared.sdf.org/gdb/mlats.html
http://www.cs.nyu.edu/courses/fall04/V22.0201-003/ia32_chap_03.pdf
http://insecure.org/stf/smashstack.html
https://www.corelan.be/index.php/2009/07/19/exploit-writing-tutorial-part-1stack-based-overflows/
Just like for SQL injection you need to know how an SQL statement works, for
memory corruption you need to know how memory works.
Simple work cycle to becoming a memory corruption guru: learn a
memory mechanism -> figure out how to corrupt it -> figure out ways
to bend it to your will.
You are computer scientists, enough said!
6.
This is a gross oversimplification but the important parts are mentioned. It starts with code, then a PE
(ELF) file is created and this is used to construct a memory image.
The Compiler cannot control where an executable is loaded into memory for
example (since it would need to know what is executing and what will execute to some extent), and an
operating system cannot influence the contents of a process's code (it would need to
be able to predict the outcome of the code without actually running it).
access rights
Virtual
Memory
offset
Size of
section
The Stack
Program Image
.text?
.text?
instructions
raw opcodes
memory offset
.data?
*some compilers prefer to use the rdata section for non-mutable initialized data.
.data?
String values
initialized at
compile time
Variable offset
addresses
Literal values
encoded in hex
interpretation
Data section contents
Addresses
stack
addresses
values at
addresses
http://www.csn.ul.ie/~caolan/pub/winresdump/winresdump/doc/pefile2.html
https://evilzone.org/tutorials/(paper)-portable-executable-format-and-itsrsrc-section/
http://msdn.microsoft.com/en-us/magazine/cc301805.aspx
http://msdn.microsoft.com/en-us/library/ms809762.aspx
Linux
http://www.skyfree.org/linux/references/ELF_Format.pdf
http://wiki.osdev.org/ELF
http://www.linuxjournal.com/article/1059
.stack?
The stack must provide a way for functions to call other functions and functions
to return to those that called them!
Have a little think about how you would have this work...
Base
Pointer
function B(){
C();
}
function C(){
D();
Stack
Pointer
bottom
function Bs stack
top
function C stack
}
main(){
A();
}
function Ds stack
Destruction
function As stack
Growth
B();
push
ebp
calling ebp
calling stack
function
calling
Return Address
calling esp
calling EBP
function
being
called*
called stack
*this stack is still to be set up, this diagram does not reflect its actual size but instead the space it
will occupy
push
ebp
mov
ebp, esp
calling stack
function
calling
Return Address
called ebp
calling EBP
called esp
*function
being
called
*esp and ebp are equal so effectively the stack currently occupies 0 space at the moment
push
ebp
mov
ebp, esp
push
ebx
calling stack
Return Address
function
calling
calling EBP
called ebp
called esp
calling EBX
called stack
function
being
called*
sub
esp, 0Ch
Return Address
called ebp
calling EBP
calling EBX
called esp
called stack
0xC
addresses
Some notes
add
esp, 0Ch
Return Address
called ebp
calling EBP
called esp
calling EBX
called stack
pop
ebx
Return Address
called ebp
calling EBP
calling EBX
called esp
called stack
Value placed in
EBX register
pop
ebp
calling stack
called ebp
Return Address
called esp
calling EBP
calling EBX
Old stack
called stack
retn
calling stack
called ebp
Return Address
called esp
calling EBP
New stack
restored to
original
state
calling EBX
Old stack
called stack
Literally
pop eip
Hang on...
1.
2.
3.
Return Address
calling EBP
called stack
Return Address
Some notes
I consider the return address, saved EBP and arguments part of the called
function's stack
This doesnt really matter but you could consider it part of the calling
function's stack given that it is the calling function that loads these
values
But it makes it an easier story to tell from the perspective of memory
corruption given that the calling function corrupts this data.
You could also think of the calling function as preparing the stack by
placing these values in the called stack.
Well
All functions passed to a callee (function being called) need to be accessible locally (via the its
stack).
The calling function will push these arguments onto its stack in REVERSE order before making
the call and branching execution to the callee. *on some architectures the EDI and ESI are used
to store points to arguments before the stack is used to store them when calling a function
(optimization effort)*
And Then
The callee will reference these arguments outside of its stack by using registers (EBP, ESP)