S23U14438 - Lecture 19 - Buffer Overflow
S23U14438 - Lecture 19 - Buffer Overflow
Software security
Buffer overflow
2 of 34
Outline
Fundamentals
Buffer Overflow attacks
Stuxnet – A buffer overflow based attack
Countering Buffer Overflow attacks
FUNDAMENTALS
Terminology and how memory is organised on a computer
Fundamentals 4 of 34
D
O
W
Memory N
UP
Registers
Vary with processor
General purpose registers on x86 CPU:
EAX EBX ECX EDX
ESI EDI ESP EBP
2 min
Fundamentals 5 of 34
D
O
W
Stack
N
Direction of growth
UP
Heap
MEMORY
The Stack
HIGH
6 min
Fundamentals 7 of 34
ESP
Extended Stack Pointer
Keep track of where a program is on the Stack
Points to the ‘top’ (next free space) of the Stack
EBP
Extended Base Pointer
Acts as the reference point (or anchor) for all subsequent calls
EIP
Extended Instruction Pointer
Points to the next instruction the CPU is to run
10 min
BUFFER OVERFLOW
What’s the big deal?
Buffer Overflow Attacks 9 of 34
Buffer Overflow
Nearly half of all exploits of computer programs
stem historically from some form of buffer overflow1
The classic buffer overflow is listed in the top 3 of
the most dangerous software bugs2
The input buffer used to hold program input is
overwritten with data that is larger than the buffer
can hold.
Root cause
Poor programming practice
Programming language weaknesses.
[1] 2015, Conklin et. al. Principles of Computer Security, Fourth Edition, 4e,
[2] http://cwe.mitre.org/top25/ 12 min
Buffer Overflow Attacks 10 of 34
[2]
[1] https://arstechnica.com/gadgets/2016/12/buffer-overflow-exploit-can-bypass-activation-lock-on-ipads-running-ios-10-1-1/
[2] https://www.scmagazineuk.com/vulnerability-discovered-in-atm-cash-machine-security-enables-theft/article/654651/ 14 min
Buffer Overflow Attacks 11 of 34
16 min
Buffer Overflow Attacks 12 of 34
Unauthoirsed
Route (code execution)
24 min
I
STACK STACK STACK STACK STACK STACK
Buffer Overflow Attacks 16 of 34
ESP
ESP RET=3 RET=3 RET=3 RET=3E RET=3E
ESP EBP EBP EBP
D
C
B
ESP ESP
28 min
A
STACK STACK STACK STACK STACK STACK
Buffer Overflow Attacks 17 of 34
Script-Kiddie collections
Metasploit Project
http://metasploit.com
Exploit Database
https://www.exploit-db.com
30 min
STUXNET –BUFFER OVERFLOW IN
PRACTICE
A Brief review
Buffer Overflow Attacks 19 of 34
What is Stuxnet
30 min
Buffer Overflow Attacks 20 of 34
Exploit detail
This exploit attacks the Server Message Block (SMB) protocol2, used to
authenticate over Remote Procedure Call (RPC) in a Windows environment
[1] - http://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2008-4250
[2] - https://msdn.microsoft.com/en-us/library/windows/desktop/aa365233(v=vs.85).aspx
[3] - https://cwe.mitre.org/data/definitions/120.html
[4] - https://www.symantec.com/content/en/us/enterprise/media/security_response/whitepapers/w32_stuxnet_dossier.pdf 34 min
COUNTERING BUFFER OVERFLOW
ATTACKS
How can we defend against these attacks?
Countering Buffer Overflow Attacks 22 of 34
Buffer Overflow
Defences
Aim Aim
Harden new Detect and abort
programs to resist attacks in existing
attack programs
36 min
Countering Buffer Overflow Attacks : Compile time defences 23 of 34
40 min
Countering Buffer Overflow Attacks : Compile time defences 26 of 34
Stack Canaries
Compiler option to place a unique value on the stack just below
the return pointer (see StackGuard1)
Canary sits between buffer and return pointer on stack
Will be overwritten if buffer overflowed
Before function returns, check if canary is intact (unchanged)
N=ABC N=ABCDEF
RET=3 RET=3
F
EBP EBP
E
CANARY CANARY
D
C C
B B
A A
STACK STACK
[1] - http://www.usenix.net/legacy/publications/library/proceedings/sec98/full_papers/cowan/cowan.pdf
Countering Buffer Overflow Attacks : Compile time defences 28 of 34
N=ABC N=ABCDEF
3=3 E=3
RET=3 ? RET=3
E
?
EBP D
EBP
C C
B B
A A
STACK STACK
[1] - http://www.angelfire.com/sk/stackshield/
[2] - Chiueh, T.C. and Hsu, F.H., 2001, April. RAD: A compile-time solution to buffer overflow attacks. In Distributed Computing Systems, 2001. 21st International Conference on. IEEE.
Countering Buffer Overflow Attacks : Run time defences 29 of 34
48 min
Countering Buffer Overflow Attacks : Run time defences 30 of 34
[1] - https://msdn.microsoft.com/en-us/library/windows/desktop/aa366553(v=vs.85).aspx
[2] - https://technet.microsoft.com/en-us/library/bb457155.aspx
Countering Buffer Overflow Attacks : Run time defences 32 of 34
[1] - https://www.exploit-db.com/docs/english/17914-bypassing-aslrdep.pdf
[2] - https://codingvision.net/bypassing-aslr-dep-getting-shells-with-pwntools
Countering Buffer Overflow Attacks : Run time defences 33 of 34
Guard pages
Security feature to raise an alarm when a process attempts
access to selected areas of specially reserved memory
These reserved areas are inserted at strategic points between
sensitive areas of memory, eg: Stack and Heap
Memory manager marks these areas as illegal addresses
Attempts to access aborts the process
34
34 of 34
Review
Fundamentals
Buffer Overflow attacks
Countering Buffer Overflow attacks