Module-5-Control-Hijacking-Attacks
Module-5-Control-Hijacking-Attacks
Hijacking
Attacks
This Week’s Objectives
• Types of control hijacking attacks (mostly memory attacks)
• Understand Linux memory layout on i386 32-bit
• How stack is used to manage function calls
• How buffer overflow and shellcode works
• Next week: Countermeasures
2
Exploitation Stage - Gaining Access
Exploit vulnerabilities
• Exploits for a specific vulnerability - can be downloaded from
proof of concepts and exploitation tools
• Skilled hackers develop new exploits
o Control hijacking exploits such as buffer overflow
Control hijacking attacks
• Take over a target machine (e.g. web server)
by altering the control flow of a legitimate
process
o Execute arbitrary code on target by hijacking
application control flow
• Examples – memory attacks:
o Buffer overflow and integer overflow attacks
o Format string vulnerabilities
o Use after free
First Example: Integer Over/underflow
#include <stdio.h>
int main() {
char command;
Example
unsigned char c = 5; //value range 0-255
while (1) {
printf("The current value is %d\n", c);
printf("Up(u) or down(d)?\n");
command = getchar();
if (command == 'u')
c = c + 1;
else if (command == 'd')
c = c - 1;
return 0;
5
Well-Known Memory Attacks
6
Morris Worm (1988)
• First internet worm
• Exploited BoF vulnerability in the finger daemon
• Overflowed 512-byte buffer to open a reverse shell
• “Smashing the Stack for Fun and Profit” by Aleph One
http://www.cs.umd.edu/class/fall2017/cmsc414/readings/stack-smashing.pdf
https://github.com/arialdomartini/morris-worm
7
Glibc “GHOST” (2015)
CVE-2015-0235
https://www.qualys.com/2015/01/27/cve-2015-0235/GHOST-CVE-2015-
0235.txt
8
Computer Architecture
(x86 – 32 bit)
9
Von Neumann Architecture
Machine Code
year= 2024
12
From C Code to Memory
4 bytes
13
Process Memory Layout
Instantiated global/static
Higher addresses
int foo = 1;
int x; Uninstantiated global
main() STACK
{
static char[] bar = "abc";
Local Variables long y; Grows downwards
} Grows upwards
foo(char[] bar, long y)
{
char buffer[500]; HEAP
char *ptr;
Dynamic
ptr=malloc(sizeof(char)*10); BSS
allocation
.. DATA
free(ptr);
} CODE (PROGRAM)
Executable Instructions Lower addresses
14
x86 (32bit)Architecture
Special registers:
EIP (Extended Instruction Pointer) = points to the current instruction
ESP (Extended Stack Pointer) = points to the “bottom” of stack
EBP (Extended Base Pointer) = points 4 bytes below the return pointer, used
for referencing address of the previous frame
15
x86 Calling Convention
16
Calling a Function in x86
● When calling a function, a new stack frame is created to store local variables
o the ESP and EBP need to shift to create a new stack frame,
o and the EIP must move to the callee’s code
● When returning from a function, the ESP, EBP, and EIP must return to their old values
EBP EBP
ESP ESP
Stack
Stack
Stack
EBP
ESP
Code
Code
callee code EIP callee code callee code
17
Stack and registers
● The stack starts at higher
addresses and grows down.
...
● In a 32-bit system, registers Registers
STACK
on CPU. eip
...
CODE
Code for foo
...
● Two pointers to mark the Registers
extent of the current stack ebp
frame. esp
STACK
● ebp is used for the top of eip
the stack frame, and esp is
used for the bottom of the
stack frame.
● whenever you push a value
onto the stack, esp must ...
adjust to match the lowest
CODE
Code for foo
value on the stack.
Code for main
19
eip
Registers ...
● We need some way to
ebp
keep track of what step
we’re at in the instructions. esp
STACK
● We use the eip register to eip
store a pointer to the
current instruction.
...
CODE
Code for main
20
Stack Changes During Function Calls
STACK
function returns, the stack eip
frame must be discarded.
● Each stack frame needs to
have space for local
variables.
● We also need to figure out ...
how to pass arguments to
Code for foo
CODE
functions using the stack.
Code for main
21
Stack Changes During Function Calls
STACK
should adjust to give us a eip
stack frame for foo with the
correct size.
● The eip register should adjust
to let us execute the
instructions for foo.
...
CODE
Code for main
22
Stack Changes During Function Calls
● Then after foo returns, the Registers Stack frame for main
stack should look exactly ebp
like it did before foo was esp
called.
STACK
eip
...
CODE
Code for main
23
Save Register Values So We Can Go Back
STACK
by putting it on the stack. eip
...
CODE
Code for main
24
1. Arguments
STACK
the new lowest value on the eip
stack.
...
CODE
Code for main
2. Keep track of eip
STACK
execute next after the eip Old eip (rip)
function returns
○ This value is sometimes
known as the rip (return
instruction pointer),
○ esp now points to the new
...
lowest value on the stack.
Code for foo
CODE
Code for main
26
3. Keep track of ebp
STACK
top of the previous stack eip Old eip (rip)
frame when we return
Old ebp (sfp)
○ This value is sometimes
known as the sfp (saved
frame pointer)
○ esp points to the new lowest
...
value on the stack.
Code for foo
CODE
Code for main
27
4. Adjust the stack frame
STACK
because we’ve just saved ebp Old eip (rip)
the old values of ebp and esp Old ebp (sfp)
eip.
eip
CODE
Code for main
28
4. Adjust the stack frame
STACK
code in main. ebp Old eip (rip)
esp
Old ebp (sfp)
eip
CODE
step Code for main
5. Execute the function
STACK
● Any local variables can be ebp Old eip (rip)
moved onto the stack now.
esp Old ebp (sfp)
eip Local variable
Local variable
...
CODE
Code for main
6. Normal operation - Restore everything
STACK
● We use the addresses stored in eip Old eip (rip)
rip and sfp to restore eip and
Old ebp (sfp)
ebp to their old values.
Local variable
Local variable
...
CODE
Code for main
Buffer Overflow (BoF)
AND (REMOTE) CODE EXECUTION
32
Buffer Overflows
• A type of control hijacking attacks (involves malicious user input)
• Runs as the privilege of the exploited process
• Occurs mostly commonly in C and C++ programs
• Other languages (Rust, Java, Python, etc) have good memory
management/protection
34
Example Vulnerable Code
#include<stdio.h>
int main() {
char c = 'X';
char buff[3];
return 0;
35
Buffer Overflow Vulnerabilities in C
char buff[3]; 0
buff[5] = ‘0';
buf
37
What happens if you enter more
than 12 characters?
main()
{
func1(); func1()
return; {
}
func2(); func2()
return; { return addr main
} char buf[12]
gets(buf) return addr func1
special_func() return;
{ }
//special return addr func2
}
buf
main() 39
{
func1(); func1()
return; {
}
func2(); func2()
return; { return main
} char buf[12]
gets(buf) return func1
special_func() return;
{ }
} return func2
buf
39
Overwriting the RIP
buff
buff
gets(buff); buff
} buff 40
Overwriting the RIP Note the NULL byte that terminates
the string, automatically added by
gets!
buff
'A' 'A' 'A' 'A'
gets(buff); 'A' 'A' 'A' 'A'
} 'A' 'A' 'A' 'A' 41
What code to inject?
SHELLCODE
Shell-Storm library
42
Shellcode Example
43
Arbitrary code injection & execution
NOP=\X90
47
Walking Through a Buffer Overflow
ESP ... 48
Walking Through a Buffer Overflow
/bin/bash # _
Input: ... ... ... ...
SHELLCODE + 'A' * 12 +
... ... ... ...
‘\xef\xbe\xad\xde'
... ... ... ...
vulnerable: ... ... ... ...
...
call gets ... ... ... ...
vo id vulnerable(void) { addl $4, %esp
char name[20]; movl %ebp, %esp ...
gets(name); popl %ebp '\x00' ...
ESP
} ret
(RIP) 0xdeadbeef
int main(void) { main: (SFP) 'AAAA'
vulnerable(); ...
return 0; call vulnerable (name) 'AAAA'
} ...
(name) 'AAAA'
(name) SHELLCODE
(name) SHELLCODE
52
Format String Attacks
• printf(“my name is %s”, “Haque“);
• printf(“1 + 1 = %d”, 5);
• sprintf(buffer, “today is %s of %s”, “3rd”, “March”);
53
What is going on?
• printf / sprintf are variable-argument functions (can take arbitrary number of
arguments)
• They blindly trust that the number of arguments match the placeholders…
???
???
arg3 = 13 arg1 = PTR to “Lucky nums
arg2 = 4 are %x and %x)”
arg1 = PTR to “Lucky nums printf frame return addr printf frame
are %x and %x)” prev frame ptr
return addr “Lucky nums are: %s of %s”
prev frame ptr
“Lucky nums are: %s of %s”
54
Vulnerable functions
Printing:
printf, fprintf, sprintf, …
vprintf, vfprintf, vsprintf, …
Logging:
syslog, err, warn
Can’t be that bad if it’s just over-read right?
57
Integer Overflows
In the original Civilization, Each leader in the game had an "aggression" rating,
and Gandhi - to best reflect his real-world persona - was given the lowest score
possible, a 1. When a player adopted democracy in Civilization, their
aggression would be automatically reduced by 2. If Gandhi went democratic his
aggression wouldn't go to -1, it looped back around to the ludicrously high figure
of 255, making him as aggressive as a civilization could possibly be.
59
Lecture 0x05 - Summary
• Different types of control hijacking attacks
• x86 architecture and control flow
• Buffer overflows,
• Interger overflows
• Format string attacks
60
Looking Ahead
1. Workshop 5: hands-on memory attacks
a) Real world control hijacking attacks
b) Assignment 3 dues at the beginning of week 7
c) Attend the drop-in session if you need extra help
2. Lecture 6 will cover more memory attacks and defence
61