SlideShare a Scribd company logo
CNIT 127: Exploit Development



Ch 4: Introduction to Format String
Bugs
Updated 2-9-17
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
• First it calls printf
• With a format string vulnerability
• Then it calls puts
Targeting the GOT
• Pointer to puts
• Change pointer to hijack execution
Writing to Target RAM
• We now control the destination address,
but not the value written there
Python Code to Write 1 Word
Write 4 Words, All The Same
Write 4 Bytes, All The Same
Write 4 Bytes, Increment=8
Write 0 in First Byte
Write Chosen Value in 1st Byte
Write Chosen Values in Bytes 1-2
Write Chosen Values in Bytes 1-2
Write Chosen Values in 4 Bytes
Write Chosen Values into 4 Bytes
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
Write 4 Bytes, Arbitrary
Python Code to Write a Chosen Word
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
• x10 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
• May not be necessary, but it's a good habit
Final Check
• Address in NOP
sled
• Shellcode
intact
Shell (in gdb)
• Wait for the port to close
• Test it outside gdb

More Related Content

PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch Ch 1: Before you Begin
PDF
CNIT 127 Ch 1: Before you Begin
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 3: Shellcode
PDF
CNIT 127: Ch 4: Introduction to format string bugs
PDF
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch Ch 1: Before you Begin
CNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 3: Shellcode
CNIT 127: Ch 4: Introduction to format string bugs
CNIT 127 Ch 4: Introduction to format string bugs

What's hot (20)

PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
PDF
CNIT 127: Ch 8: Windows overflows (Part 2)
PDF
CNIT 127: Ch 2: Stack Overflows in Linux
PDF
CNIT 127: 4: Format string bugs
PDF
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 8: Windows overflows (Part 1)
PDF
CNIT 127: 3: Shellcode
PDF
CNIT 127: Ch 2: Stack overflows on Linux
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
PDF
CNIT 127 Ch 5: Introduction to heap overflows
PDF
CNIT 126 5: IDA Pro
PDF
CNIT 127 Ch 8: Windows overflows (Part 1)
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
CNIT 126 5: IDA Pro
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
CNIT 127: Ch 8: Windows overflows (Part 2)
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: 4: Format string bugs
127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 8: Windows overflows (Part 1)
CNIT 127: 3: Shellcode
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127 14: Protection Mechanisms
CNIT 127 14: Protection Mechanisms
CNIT 127 Ch 3: Shellcode
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
CNIT 127 Ch 5: Introduction to heap overflows
CNIT 126 5: IDA Pro
CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 126 5: IDA Pro
Ad

Viewers also liked (20)

PDF
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
PDF
CNIT 121: 10 Enterprise Services
PDF
CNIT 127 Ch 6: The Wild World of Windows
PDF
Practical Malware Analysis Ch13
PDF
Practical Malware Analysis: Ch 9: OllyDbg
PDF
CNIT 121: 8 Forensic Duplication
PDF
CNIT 123 Ch 1: Ethical Hacking Overview
PDF
Ch 2: TCP/IP Concepts Review
PDF
Ch 6: Enumeration
PDF
Ch 12: Cryptography
PDF
Ch 11: Hacking Wireless Networks
PDF
Ch 13: Network Protection Systems
PDF
Ch 10: Hacking Web Servers
PPTX
Practical Malware Analysis: Ch 2 Malware Analysis in Virtual Machines & 3: Ba...
PDF
Ch 5: Port Scanning
PDF
CNIT 128 5: Mobile malware
PDF
CNIT 123: Ch 9: Embedded Operating Systems: The Hidden Threat
PDF
CNIT 123: Ch 13: Network Protection Systems
DOC
Exploit Frameworks
PDF
CNIT 140: Flashing Firmware
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
CNIT 121: 10 Enterprise Services
CNIT 127 Ch 6: The Wild World of Windows
Practical Malware Analysis Ch13
Practical Malware Analysis: Ch 9: OllyDbg
CNIT 121: 8 Forensic Duplication
CNIT 123 Ch 1: Ethical Hacking Overview
Ch 2: TCP/IP Concepts Review
Ch 6: Enumeration
Ch 12: Cryptography
Ch 11: Hacking Wireless Networks
Ch 13: Network Protection Systems
Ch 10: Hacking Web Servers
Practical Malware Analysis: Ch 2 Malware Analysis in Virtual Machines & 3: Ba...
Ch 5: Port Scanning
CNIT 128 5: Mobile malware
CNIT 123: Ch 9: Embedded Operating Systems: The Hidden Threat
CNIT 123: Ch 13: Network Protection Systems
Exploit Frameworks
CNIT 140: Flashing Firmware
Ad

Similar to CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17) (20)

PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
2.Format Strings
PDF
Format string
PPTX
Format String Attack
PPTX
C format string vulnerability
PDF
Exploitation Crash Course
PPTX
[MOSUT] Format String Attacks
ODP
Format string vunerability
PDF
Buffer overflow tutorial
PPT
E-Commerce Security - Application attacks - Server Attacks
PDF
Format String Exploitation
PDF
Fuzzing - Part 1
PPTX
Control hijacking
PDF
Shellcode injection
PPTX
Software to the slaughter
PDF
Software Security
PDF
Heap Base Exploitation
PDF
AllBits presentation - Lower Level SW Security
PPT
When good code goes bad
PDF
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
CNIT 127 Ch 4: Introduction to format string bugs
2.Format Strings
Format string
Format String Attack
C format string vulnerability
Exploitation Crash Course
[MOSUT] Format String Attacks
Format string vunerability
Buffer overflow tutorial
E-Commerce Security - Application attacks - Server Attacks
Format String Exploitation
Fuzzing - Part 1
Control hijacking
Shellcode injection
Software to the slaughter
Software Security
Heap Base Exploitation
AllBits presentation - Lower Level SW Security
When good code goes bad
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);

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)

PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Institutional Correction lecture only . . .
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Pre independence Education in Inndia.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Institutional Correction lecture only . . .
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Microbial diseases, their pathogenesis and prophylaxis
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
102 student loan defaulters named and shamed – Is someone you know on the list?

CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)