SlideShare a Scribd company logo
CNIT 127: Exploit Development



Ch 4: Introduction to Format String
Bugs
Updated 9-11-19
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
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 User
Explanation
• %x.%x.%x.%x -- read 4 words from stack
• %n.%n -- write 2 numbers to RAM

addresses 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 for a Format String Exploit
• Control a write operation
• 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
• The format string is on the stack
• 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 ED204
• disassemble main
• First it calls printf
• Later it calls exit
Dynamic Relocation
(also called Global Offset Table (GOT))
• PLT and GOT are used to address shared
libraries
• See links Ch 4o, 4p
Writing to the GOT
• We control the eip!
Python Code to Write 1 Byte
Write 4 Bytes
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
• Avoid these
Testing for Bad Characters
• All the other characters got through
Generate Shellcode
Keep Total Length of Injection Constant
• Add 'A' characters after shellcode
• To keep the stack frame size constant
Final Check
• Address
in NOP
sled
• Shellcode
intact
Shell
CNIT 127: Ch 4: Introduction to format string bugs

More Related Content

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

What's hot (20)

PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
PDF
CNIT 127 Ch Ch 1: Before you Begin
PDF
CNIT 127: Ch 8: Windows overflows (Part 1)
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127: 3: Shellcode
PDF
CNIT 127: Ch 2: Stack Overflows in Linux
PDF
CNIT 126 5: IDA Pro
PDF
CNIT 127 Ch 5: Introduction to heap overflows
PDF
CNIT 127 Ch 8: Windows overflows (Part 1)
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 126 5: IDA Pro
PPTX
test
PDF
CNIT 127 Ch 3: Shellcode
PPTX
Practical Malware Analysis: Ch 5: IDA Pro
PDF
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
CNIT 127 Ch Ch 1: Before you Begin
CNIT 127: Ch 8: Windows overflows (Part 1)
127 Ch 2: Stack overflows on Linux
CNIT 127 14: Protection Mechanisms
CNIT 127: 3: Shellcode
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 126 5: IDA Pro
CNIT 127 Ch 5: Introduction to heap overflows
CNIT 127 Ch 8: Windows overflows (Part 1)
127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 3: Shellcode
CNIT 126 5: IDA Pro
test
CNIT 127 Ch 3: Shellcode
Practical Malware Analysis: Ch 5: IDA Pro
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
Ad

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

PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
Ch 18: Source Code Auditing
PPTX
C101 – Intro to Programming with C
PPTX
Control hijacking
PPTX
A brief introduction to C Language
PPTX
Avro intro
PPT
Buffer Overflows
PPTX
C language
PPT
CInputOutput.ppt
PPTX
Introduction to C Programming language Chapter02.pptx
PPTX
Anton Dorfman. Shellcode Mastering.
PPTX
Shellcode mastering
PDF
Bottom to Top Stack Optimization with LAMP
PDF
Bottom to Top Stack Optimization - CICON2011
PDF
Scripting and the shell in LINUX
PPTX
Php basics
PPTX
Data processing and visualization basics
PPTX
Learn c++ Programming Language
PPTX
Compiler Construction
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 4: Introduction to format string bugs
Ch 18: Source Code Auditing
C101 – Intro to Programming with C
Control hijacking
A brief introduction to C Language
Avro intro
Buffer Overflows
C language
CInputOutput.ppt
Introduction to C Programming language Chapter02.pptx
Anton Dorfman. Shellcode Mastering.
Shellcode mastering
Bottom to Top Stack Optimization with LAMP
Bottom to Top Stack Optimization - CICON2011
Scripting and the shell in LINUX
Php basics
Data processing and visualization basics
Learn c++ Programming Language
Compiler Construction
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
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Insiders guide to clinical Medicine.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
TR - Agricultural Crops Production NC III.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Final Presentation General Medicine 03-08-2024.pptx
Insiders guide to clinical Medicine.pdf

CNIT 127: Ch 4: Introduction to format string bugs