SlideShare a Scribd company logo
CNIT 127: Exploit Development



Ch 4: Introduction to Format String
Bugs
Updated 2-10-18
Understanding Format Strings
Data Interpretation
• RAM contains bytes
• The same byte can be interpreted as
– An integer
– A character
– Part of an instruction
– Part of an address
– Part of a string
– Many, many more...
Format String Controls Output
Format String Demo
Most Important for Us
• %x Hexadecimal
• %8x Hexadecimal padded to 8 chars
• %10x Hexadecimal padded to 10 chars
• %100x Hexadecimal padded to 100 chars
Format String Vulnerabilities
Buffer Overflow
• This code is obviously stupid
char name[10];
strcpy(name, "Rumplestiltskin");
• C just does it, without complaining
Format String Without Arguments
• printf("%x.%x.%x.%x");
– There are no arguments to print!
– Should give an error message
– Instead, C just pulls the next 4 values from
the stack and prints them out
– Can read memory on the stack
– Information disclosure vulnerability
Format String Controlled by Attacker
Explanation
• %x.%x.%x.%x -- read 4 words from stack
• %n.%n.%n.%n -- write 4 numbers to RAM
locations from the stack
%n Format String
• %n writes the number of characters
printed so far
• To the memory location pointed to by the
parameter
• Can write to arbitrary RAM locations
• Easy DoS
• Possible remote code execution
printf Family
• Format string bugs affect a whole family
of functions
Countermeasures
Defenses Against Format String
Vulnerabilities
• Stack defenses don't stop format string
exploits
– Canary value
• ASLR and NX
– Can make exploitation more difficult
• Static code analysis tools
– Generally find format string bugs
• gcc
– Warnings, but no format string defenses
Exploitation Technique
Steps
• Control a parameter
• Find a target RAM location
– That will control execution
• Write 4 bytes to target RAM location
• Insert shellcode
• Find the shellcode in RAM
• Write shellcode address to target RAM
location
Control a Parameter
• Insert four letters before the %x fields
• Controls the fourth parameter
– Note: sometimes it's much further down the
list, such as parameter 300
Target RAM Options
• Saved return address
– Like the Buffer Overflows we did previously
• Global Offset Table
– Used to find shared library functions
• Destructors table (DTORS)
– Called when a program exits
• C Library Hooks
Target RAM Options
• "atexit" structure (link Ch 4n)
• Any function pointer
• In Windows, the default unhandled
exception handler is easy to find and
exploit
Disassemble in gdb
• gdb -q fs
• disassemble main
• First it calls printf
• With a format string vulnerability
• Later it calls exit
Dynamic Relocation
(also called Global Offset Table (GOT))
Targeting the GOT
• Global Offset Table
• Pointer to exit at 0804a014
• Change pointer to hijack execution
Writing to the GOT
• gdb -q fs
• info file -- see got.plt
• b * main+76 -- after printf
• x/1x 0x0804a014
• run $'x14xa0x04x08%x%x%x%n'
• x/1x 0x0804a014
Python Code to Write 1 Word
Write 4 Bytes, All The Same
Write 4 Bytes, Increment=8
Write 4 Bytes, Increment=16
Write 00000000
Write Chosen Values in 4 Bytes
Write Chosen Values in 4 Bytes
Inserting Dummy Shellcode
• xcc is BRK
View the Stack in gdb
• Choose an address in the NOP sled
Dummy Exploit Runs to xcc
Testing for Bad Characters
• x09 is bad
Testing for Bad Characters
• 10 is bad
Testing for Bad Characters
• Started at 11 = 0x0b
• x20 is bad
Testing for Bad Characters
• Started at 33 = 0x21
• No more bad characters
Generate Shellcode
• msfvenom -p linux/x86/shell_bind_tcp
• -b 'x00x09x0ax20'
• PrependFork=true
• -f python
Keep Total Length of Injection Constant
• Required to keep the stack frame size
constant
Final Check
• Address in NOP sled
• Shellcode intact
Shell (in gdb)
Outside gdb
• Crashed with segfault on Kali 2018.1
• Had to add 0x30 to address

More Related Content

PDF
CNIT 127: 4: Format string bugs
PDF
CNIT 127: Ch 8: Windows overflows (Part 2)
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 18: Source Code Auditing
PDF
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
PDF
CNIT 127: Ch 8: Windows overflows (Part 1)
CNIT 127: 4: Format string bugs
CNIT 127: Ch 8: Windows overflows (Part 2)
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 18: Source Code Auditing
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
CNIT 127: Ch 8: Windows overflows (Part 1)

What's hot (20)

PDF
CNIT 127: Ch 4: Introduction to format string bugs
PDF
CNIT 127: Ch 3: Shellcode
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 1: Before you Begin
PDF
CNIT 127: 3: Shellcode
PDF
CNIT 127: Ch 2: Stack Overflows in Linux
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch Ch 1: Before you Begin
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 5: Introduction to heap overflows
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
PDF
CNIT 126 5: IDA Pro
PDF
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
PPTX
Practical Malware Analysis: Ch 5: IDA Pro
PDF
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
PPTX
Practical Malware Analysis: Ch 4 A Crash Course in x86 Disassembly
CNIT 127: Ch 4: Introduction to format string bugs
CNIT 127: Ch 3: Shellcode
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 1: Before you Begin
CNIT 127: 3: Shellcode
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch Ch 1: Before you Begin
127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127 Ch 5: Introduction to heap overflows
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
CNIT 126 5: IDA Pro
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
Practical Malware Analysis: Ch 5: IDA Pro
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
Practical Malware Analysis: Ch 4 A Crash Course in x86 Disassembly
Ad

Similar to CNIT 127 Ch 4: Introduction to format string bugs (20)

PDF
CNIT 127 Ch 4: Introduction to format string bugs
PPTX
test
PDF
Ch 18: Source Code Auditing
PPTX
Control hijacking
PDF
Fuzzing - Part 1
PPTX
C101 – Intro to Programming with C
PPTX
C language
PPTX
Anton Dorfman. Shellcode Mastering.
PPTX
Shellcode mastering
PPTX
Php basics
PPTX
A brief introduction to C Language
PDF
Yandex may 2013 a san-tsan_msan
PDF
Yandex may 2013 a san-tsan_msan
PDF
Yandex may 2013 a san-tsan_msan
PPT
Buffer Overflows
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PDF
Bottom to Top Stack Optimization with LAMP
PDF
Bottom to Top Stack Optimization - CICON2011
PPT
270_1_CIntro_Up_To_Functions.ppt 0478 computer
PPT
CIntro_Up_To_Functions.ppt;uoooooooooooooooooooo
CNIT 127 Ch 4: Introduction to format string bugs
test
Ch 18: Source Code Auditing
Control hijacking
Fuzzing - Part 1
C101 – Intro to Programming with C
C language
Anton Dorfman. Shellcode Mastering.
Shellcode mastering
Php basics
A brief introduction to C Language
Yandex may 2013 a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Buffer Overflows
Dr.C S Prasanth-Physics ppt.pptx computer
Bottom to Top Stack Optimization with LAMP
Bottom to Top Stack Optimization - CICON2011
270_1_CIntro_Up_To_Functions.ppt 0478 computer
CIntro_Up_To_Functions.ppt;uoooooooooooooooooooo
Ad

More from Sam Bowne (20)

PDF
Introduction to the Class & CISSP Certification
PDF
Cyberwar
PDF
3: DNS vulnerabilities
PDF
8. Software Development Security
PDF
4 Mapping the Application
PDF
3. Attacking iOS Applications (Part 2)
PDF
12 Elliptic Curves
PDF
11. Diffie-Hellman
PDF
2a Analyzing iOS Apps Part 1
PDF
9 Writing Secure Android Applications
PDF
12 Investigating Windows Systems (Part 2 of 3)
PDF
10 RSA
PDF
12 Investigating Windows Systems (Part 1 of 3
PDF
9. Hard Problems
PDF
8 Android Implementation Issues (Part 1)
PDF
11 Analysis Methodology
PDF
8. Authenticated Encryption
PDF
7. Attacking Android Applications (Part 2)
PDF
7. Attacking Android Applications (Part 1)
PDF
5. Stream Ciphers
Introduction to the Class & CISSP Certification
Cyberwar
3: DNS vulnerabilities
8. Software Development Security
4 Mapping the Application
3. Attacking iOS Applications (Part 2)
12 Elliptic Curves
11. Diffie-Hellman
2a Analyzing iOS Apps Part 1
9 Writing Secure Android Applications
12 Investigating Windows Systems (Part 2 of 3)
10 RSA
12 Investigating Windows Systems (Part 1 of 3
9. Hard Problems
8 Android Implementation Issues (Part 1)
11 Analysis Methodology
8. Authenticated Encryption
7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 1)
5. Stream Ciphers

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Basic Mud Logging Guide for educational purpose
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Classroom Observation Tools for Teachers
master seminar digital applications in india
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
Microbial disease of the cardiovascular and lymphatic systems
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Renaissance Architecture: A Journey from Faith to Humanism
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Institutional Correction lecture only . . .
PPH.pptx obstetrics and gynecology in nursing
Basic Mud Logging Guide for educational purpose
Abdominal Access Techniques with Prof. Dr. R K Mishra

CNIT 127 Ch 4: Introduction to format string bugs