Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.

Slides:



Advertisements
Similar presentations
Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Intro to Exploitation Stack Overflows James McFadyen UTD Computer Security Group 10/20/2011.
Chapter 15 : Attacking Compiled Applications Alexis Kirat - International Student.
Gabe Kanzelmeyer CS 450 4/14/10.  What is buffer overflow?  How memory is processed and the stack  The threat  Stack overrun attack  Dangers  Prevention.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CS426Fall 2010/Lecture 111 Computer Security CS 426 Lecture 11 Software Vulnerabilities: Input Validation Issues & Buffer Overflows.
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2012.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow I: Attack Introduction Cliff Zou Spring 2012.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2013.
Static Analysis for Security Amir Bazine Per Rehnberg.
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
1 System Programming Topic 19 Software Vulnerabilities Topic 19: Software Vulnerabilities.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
CS390S, Week 4: Format String Vulnerabilities & Integer Overflows Pascal Meunier, Ph.D., M.Sc., CISSP January 31, 2007 Developed thanks to the support.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Introduction to Honeypot, measurement, and vulnerability exploits
1 Part 4: Secure Coding u Many attacks are due to invalidated input to programs and faulty programming u Secure Coding:  Immensely important  Not taught,
Section 3.4: Buffer Overflow Attack: Attack Techniques 1.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
A Tool for Pro-active Defense Against the Buffer Overrun Attack D. Bruschi, E. Rosti, R. Banfi Presented By: Warshavsky Alex.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow I: Attack Introduction Cliff Zou Spring 2015.
Buffer overflow and stack smashing attacks Principles of application software security.
Sairajiv Burugapalli. This chapter covers three main categories of classic software vulnerability: Buffer overflows Integer vulnerabilities Format string.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CS426Fall 2010/Lecture 141 Computer Security CS 426 Lecture 14 Software Vulnerabilities: Format String and Integer Overflow Vulnerabilities.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
Software Security. Bugs Most software has bugs Some bugs cause security vulnerabilities Incorrect processing of security related data Incorrect processing.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow I: Attack Introduction Cliff Zou Spring 2010.
CSC 482/582: Computer Security
Content Coverity Static Analysis Use cases of Coverity Examples
Overflows Mark Shtern.
Sabrina Wilkes-Morris CSCE 548 Student Presentation
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2016.
CNT4704: Computer Networking Special Topic: Buffer Overflow I: Attack Introduction Cliff Zou Fall 2009.
High Coverage Detection of Input-Related Security Faults
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow I: Attack Introduction Cliff Zou Spring 2016.
Format String.
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2011.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
CNT4704: Analysis of Computer Communication Network Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Fall 2011.
Malware and Software Vulnerability Analysis Buffer Overflow I: Attack Introduction Cliff Zou University of Central Florida.
Introduction to Static Analyzer
CNT4704: Analysis of Computer Communication Network Special Topic: Buffer Overflow I: Attack Introduction Cliff Zou Fall 2011.
Malware and Software Vulnerability Analysis Fuzzing Test Example Cliff Zou University of Central Florida.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
CNT4704: Analysis of Computer Communication Network Special Topic: Buffer Overflow II: Defense Techniques Cliff Zou Fall 2011.
Format String Vulnerability
Overflows, Injection, & Memory Safety
Presentation transcript:

Overflow Examples 01/13/2012

ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou

3 Spot the defect! (1) char buf[20]; char prefix[] = ” strcpy(buf, prefix); // copies the string prefix to buf strncat(buf, path, sizeof(buf)); // concatenates path to the string buf

4 Spot the defect! (1) char buf[20]; char prefix[] = ” strcpy(buf, prefix); // copies the string prefix to buf strncat(buf, path, sizeof(buf)); // concatenates path to the string buf strncat’s 3rd parameter is number of chars to copy, not the buffer size Another common mistake is giving sizeof(path) as 3rd argument...

5 Spot the defect! (2) char src[9]; char dest[9]; char base_url = ” strncpy(src, base_url, 9); // copies base_url to src strcpy(dest, src); // copies src to dest base_url is 10 chars long, incl. its null terminator, so src won’t be not null-terminated so strcpy will overrun the buffer dest

6 Example: strcpy and strncpy Don’t replace strcpy(dest, src) by –strncpy(dest, src, sizeof(dest)) but by –strncpy(dest, src, sizeof(dest)-1) –dest[sizeof(dest)-1] = `\0`; –if dest should be null-terminated! A strongly typed programming language could of course enforce that strings are always null- terminated...

7 Spot the defect! (3) char *buf; int i, len; read(fd, &len, sizeof(len)); buf = malloc(len); read(fd,buf,len);

8 Spot the defect! (3) char *buf; int i, len; read(fd, &len, sizeof(len)); buf = malloc(len); read(fd,buf,len); Memcpy() prototype: –void *memcpy(void *dest, const void *src, size_t n); Definition of size_t: typedef unsigned int size_t; Didn’t check if negative len cast to unsigned and negative length overflows

9 Implicit Casting Bug A signed/unsigned or an implicit casting bug –Very nasty – hard to spot C compiler doesn’t warn about type mismatch between signed int and unsigned int –Silently inserts an implicit cast

10 Spot the defect! (4) char *buf; int i, len; read(fd, &len, sizeof(len)); if (len < 0) {error ("negative length"); return; } buf = malloc(len+5); read(fd,buf,len); buf[len] = '\0'; // null terminate buf May results in integer overflow

11 Spot the defect! (5) #define MAX_BUF = 256 void BadCode (char* input) { short len; char buf[MAX_BUF]; len = strlen(input); if (len < MAX_BUF) strcpy(buf,input); } What if input is longer than 32K ? len will be a negative number, due to integer overflow hence: potential buffer overflow

12 Spot the defect! (6) char buff1[MAX_SIZE], buff2[MAX_SIZE]; // make sure it’s a valid URL and will fit if (! isValid(url)) return; if (strlen(url) > MAX_SIZE – 1) return; // copy url up to first separator, ie. first ’/’, to buff1 out = buff1; do { // skip spaces if (*url != ’ ’) *out++ = *url; } while (*url++ != ’/’); strcpy(buff2, buff1);... what if there is no ‘/’ in the URL? Loop termination (exploited by Blaster worm)

13 Spot the defect! (7) #include int main(int argc, char* argv[]) { if (argc > 1) printf(argv[1]); return 0; } This program is vulnerable to format string attacks, where calling the program with strings containing special characters can result in a buffer overflow attack.

14 Format String Attacks int printf(const char *format [, argument]…); –snprintf, wsprintf … What may happen if we execute printf(string); –Where string is user-supplied ? –If it contains special characters, eg %s, %x, %n, %hn?

Format String Attacks Why this could happen? –Many programs delay output message for batch display: –fprintf(STDOUT, err_msg); –Where the err_msg is composed based on user inputs –If a user can change err_msg freely, format string attack is possible 15

16 Format String Attacks %x reads and prints 4 bytes from stack –this may leak sensitive data %n writes the number of characters printed so far onto the stack –this allow stack overflow attacks... C format strings break the “don’t mix data & code” principle. “Easy” to spot & fix: –replace printf(str) by printf(“%s”, str)

17 Example of “%x” --- Memory leaking #include void main(int argc, char **argv){ int a1=1; int a2=2; int a3=3; int a4=4; printf(argv[1]); } "what is this?" what is "%x %x %x %x %x %x" bfc994b0 Bfc994b0: saved stack pointer Bfc99508: return address

#include void foo(char *format){ int a1=11; int a2=12; int a3=13; int a4=14; printf(format); } void main(int argc, char **argv){ foo(argv[1]); printf("\n"); } $./format-x-subfun "%x %x %x %x : %x, %x, %x " 80495bc e d c : b, bffff7e8, 80483f4 18 Return address Four variables

19 Dangerous C system calls source: Building secure software, J. Viega & G. McGraw, 2002

Bugs to Detect in Source Code Analysis 20  Some examples Crash Causing Defects Null pointer dereference Use after free Double free Array indexing errors Mismatched array new/delete Potential stack overrun Potential heap overrun Return pointers to local variables Logically inconsistent code Uninitialized variables Invalid use of negative values Passing large parameters by value Underallocations of dynamic data Memory leaks File handle leaks Network resource leaks Unused values Unhandled return codes Use of invalid iterators

Preventing Buffer Overflow Attacks Non-executable stack Static source code analysis. Run time checking: StackGuard, Libsafe, SafeC, (Purify). Randomization. Type safe languages ( Java, ML ). Detection deviation of program behavior Sandboxing Access control … 21