0x11 MemoryLayout
0x11 MemoryLayout
Intel Architecture
C Arrays
BoF Exploit
Assembler
Remote Exploit
Shellcode
Exploit Mitigations
Function Calls
Slide 2
Userspace Memory Layout
In 32 bit
x32 Memory Layout
0xc0000000
0xbfffffff
Stack
Heap
Code
0x0804800
0x0000000
Slide 4
x32 Memory Layout
0xc0000000
0xbfffffff
malloc(16)
Heap
mapping
Code ELF File
0x0804800
0x0000000
Slide 5
x32 Memory Layout
0xc0000000
0xbfffffff
ESP
malloc(16)
Heap
EIP mapping
Code ELF File
0x0804800
0x0000000
Slide 6
x32 Memory Layout
Memory regions:
Stack
There’s one contiguous memory region containing the stack for the process
LIFO – Last in, First Out
Contains function local variables
Also contains: Saved Instruction Pointer (SIP)
Current function adds data to the top (bottom) of the stack
Heap
There’s one contiguous memory region containing the heap
Memory allocator returns specific pieces of the memory region
For malloc()
Also contains: heap management data
Code
Compiled program code
Slide 7
ELF Format
ELF types:
ET_EXEC: Executable File
ET_REL: Relocatable File
ET_DYN: Shared Object File
ELF “views”:
Sections
Segments
$ readelf –l <binary>
Slide 9
ELF Format - Sections and Segments
ELF Header
Program Header Table
Sections
Segments
Slide 10
ELF Format - Segment View
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x00000040 0x0000400040 0x0000000000400040
0x000001c0 0x00000001c0 R E 8
INTERP 0x00000200 0x0000400200 0x0000000000400200
0x0000001c 0x000000001c R 1
02 LOAD 0x00000000 0x0000400000 0x0000000000400000
0x00000b24 0x0000000b24 R E 200000
03 LOAD 0x00000b28 0x0000600b28 0x0000000000600b28
0x00000270 0x0000000278 RW 200000
DYNAMIC 0x00000b40 0x0000600b40 0x0000000000600b40
0x000001e0 0x00000001e0 RW 8
NOTE 0x0000021c 0x000040021c 0x000000000040021c
0x00000044 0x0000000044 R 4
GNU_EH_FRAME 0x000009ac 0x00004009ac 0x00000000004009ac
0x00000044 0x0000000044 R 4
07 GNU_STACK 0x00000000 0x0000000000 0x0000000000000000
0x00000000 0x0000000000 RW 10
Slide 11
ELF Format
$ readelf –l challenge0
Slide 12
ELF Format
Sections:
.text: Executable instructions
.bss: Unitialized data (usually the heap)
.data: initialized data
.rodata: Read-Only data
.got: Global Offset Table
.plt: Procedure Linkage Table
.init/.fini: Initialization instructions (“glibc”)
Slide 13
ELF Format
Program Headers:
Type Offset PhysAddr
FileSiz Flags Align
(02) LOAD 0x0000000000000000 0x0000000000400000
0x0000000000000b24 R E 200000
(03) LOAD 0x0000000000000b28 0x0000000000600b28
0x0000000000000270 RW 200000
(07) GNU_STACK 0x0000000000000000 0x0000000000000000
0x0000000000000000 RW 10
Slide 14
ELF Loader
ELF Format
ELF Header
Program Header Table
.plt 02 Executable Segment
.text r-x
.init
.got
.data
03 Data Segment
rw-
.bss
07 Stack
rw-
Slide 16
ELF Format
FILE Process
ELF Header
Program Header Table
.plt
Code
.text
.init
.got
.data Heap
.bss
Stack
Section Header Table
Slide 17
x64 Memory Layout
0x7fffffffe000
Stack
RSP
Heap
0x600000
RIP
Code
0x400000
0x000000
Slide 18
Stack, Heap, Code from ELF File
By Example
void main(void) {
char stackVar[16];
char *heapVar = (char *) malloc(4);
Slide 20
ELF Format - ELF Analysis
Slide 21
ELF Format
See it at runtime
# cat /proc/self/maps
00400000-0040c000 r-xp 00000000 08:01 391694 /bin/cat
0060b000-0060c000 r--p 0000b000 08:01 391694 /bin/cat
0060c000-0060d000 rw-p 0000c000 08:01 391694 /bin/cat
…
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
Slide 22
ELF Format
Slide 23
ELF Format
Sections:
Are for compiler (gcc), to link several object files together (.o)
Segments:
Are for the loader, to create the process
Each segment consists of one ore more sections
Slide 24
ELF Format
Recap:
Program Code is stored in ELF Files
ELF Files contain segments
Segments are copied 1:1 in the memory to create a process (of that program)
A process has generally three important segments:
Code segment (the actual compiled code)
Heap (global allocations with malloc())
Stack (local variables of functions)
Slide 25
Challenges
Challenges:
https://exploit.courses
Challenge 0: Introduction to memory layout – basic
Challenge 1: Introduction to memory layout - advanced
(Challenge 4: Introduction to hex numbers, code and GDB)
Slide 26
Slide 27