Foundations of Network and Computer Security J J ohn Black Lecture #20 Nov 4 th 2004 CSCI 6268/TLEN 5831, Fall 2004.

Slides:



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

Dynamic Memory Management
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Kernighan/Ritchie: Kelley/Pohl:
Foundations of Network and Computer Security J J ohn Black Lecture #30 Nov 26 th 2007 CSCI 6268/TLEN 5831, Fall 2007.
Stack-Based Buffer Overflows Attacker – Can take over a system remotely across a network. local malicious users – To elevate their privileges and gain.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Foundations of Network and Computer Security J J ohn Black Lecture #22 Nov 17 th 2005 CSCI 6268/TLEN 5831, Fall 2005.
Beyond Stack Smashing: Recent Advances in Exploiting Buffer Overruns Jonathan Pincus Microsoft Research Brandon Baker Microsoft Carl Hartung CSCI 7143:
CS 536 Spring Run-time organization Lecture 19.
Run time vs. Compile time
Foundations of Network and Computer Security J J ohn Black Lecture #20 Nov 10 th 2005 CSCI 6268/TLEN 5831, Fall 2005.
Foundations of Network and Computer Security J J ohn Black Lecture #21 Nov 9 th 2004 CSCI 6268/TLEN 5831, Fall 2004.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Run-time Environment and Program Organization
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
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-
Buffer overflows.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
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.
Computer Security and Penetration Testing
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
Pointers OVERVIEW.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Buffer Overflow Proofing of Code Binaries By Ramya Reguramalingam Graduate Student, Computer Science Advisor: Dr. Gopal Gupta.
Buffer Overflow Group 7Group 8 Nathaniel CrowellDerek Edwards Punna ChalasaniAxel Abellard Steven Studniarz.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Sairajiv Burugapalli. This chapter covers three main categories of classic software vulnerability: Buffer overflows Integer vulnerabilities Format string.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
Foundations of Network and Computer Security J J ohn Black CSCI 6268/TLEN 5550, Spring 2013.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
Beyond Stack Smashing: Recent Advances In Exploiting Buffer Overruns Jonathan Pincus and Brandon Baker Microsoft Researchers IEEE Security and.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
CSC 482/582: Computer Security
Refs: rootshell, antionline, your favorite hacker site…
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Module 30 (Unix/Linux Security Issues II)
Protecting Memory What is there to protect in memory?
Run-time organization
Arrays in C.
CMSC 414 Computer and Network Security Lecture 21
Format String.
Foundations of Network and Computer Security
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
Format String Vulnerability
Presentation transcript:

Foundations of Network and Computer Security J J ohn Black Lecture #20 Nov 4 th 2004 CSCI 6268/TLEN 5831, Fall 2004

Announcements Quiz #3 – Today –Need to know what big-endian is –Remind me to mention it if I forget! Brian’s detective work –Mazdak will fix as he can

Original C SourceDerived Abstract Model char s[n];alloc(s) = n; s[n] = ‘\0’;len(s) = max(len(s), n+1) p = “foo”;len(p) = 4; alloc(p) = 4; strlen(s)len(s)-1 gets(s);len(s) = choose(1… 1 ); fgets(s,n,…);len(s) = choose(1…n); strcpy(dst, src);len(dst) = len(src); strncpy(dst, src, n);len(dst) = min(len(src), n); strcat(s, suffix);len(s) += len(suffix) – 1; strncat(s, suffix, n);len(s) += min(len(suffix)–1,n); And so on... The Translation Table

Program Analysis Once we set these “variables” we wish to see if it’s possible to violate our constraint ( len(s) <= alloc(s) for all strings s ) –A simplified approach is to do so without flow analysis This makes the tool more scalable because flow analysis is hard However it means that strcat () cannot be correctly analyzed So we will flag every nontrivial usage of strcat () as a potential overflow problem (how annoying) The actual analysis is done with an “integer range analysis” program which we won’t describe here –Integer range analysis will examine the constraints we generated above and determine the possible ranges each variable could assume

Evaluating the Range Analysis Suppose the range analysis tells us that for string s we have a <= len(s) <= b and c <= alloc(s) <= d Then we have three possibilities: b <= c s never overflows its buffer a > d s always overflows its buffer (usually caught early on) c <= b s possibly overflows its buffer: issue a warning

An Implementation of the Tool David Wagner implemented (a simple version of) this tool as part of his PhD thesis work –Pointers were ignored This means *argv [] is not handled (and it is a reasonably-frequent culprit for overflows) –struct s were handled Wagner ignored them initially, but this turned out to be bad –function pointers, union s, ignored

Emperical Results Applied to several large software packages –Some had no known buffer overflow vulnerabilities and other did The Linux nettools package –Contains utilities such as netstat, ifconfig, route, etc. –Approximately 7k lines of C code –Already hand-audited in 1996 (after several overflows were discovered) –Nonetheless, Wagner discovered several more exploitable vulnerabilities

And then there’s sendmail sendmail is a Unix program for forwarding –About 32k lines of C –Has undergone several hand audits after many vulnerabilities were found (overflows and race conditions mostly) –Wagner found one additional off-by-one error Running on an old version of sendmail (v ), he found 8 more (all of which had been subsequently fixed)

Performance Running the tool on sendmail took about 15 mins –Almost all of this was for the constraint generation –Combing by hand through the 44 “probable overflows” took much longer (40 of these were false alarms) –But sendmail has 695 calls to potentially unsafe string routines Checking these by hand would be 15 times more work than using the tool, so running the tool is worthwhile here

Endianness A multi-byte quantity (like an integer) can be stored in two ways –i = 0x ; –In memory: Lower Addrs Little Endian Big Endian

Off-by-one Overflows Consider this code: void test1(char *p) { char t[12]; strcpy(t, “test”); strncat(t, p, 12-4); } Recall that strncat() adds chars from p on to string t, adding at most 12-4=8 of them –But with the null, this produces an off-by-one error: we need 13 characters! –Note: this is a common error and usually not thought of as a security problem!

What happens on overflow? Try test1(“xxxxxxxx”) –Null byte overwrites first byte of sfp –Next we mov esp, ebp; pop ebp This means the ebp will contain the old value clobbered by the Null (call this mbp: munged base pointer) Bottom of stack Top of stack *str ret sfp t Ptr to large_string Return address to caller 00 Saved Frame Pointer 4 bytes 12 bytes xxxxxxxxxxxx Note: off-by-one must be adjacent to sfp in order to be exploitable

And on the next function exit? With the wrong ebp value, equal to mbp, we return to the caller –Caller then exits next and does what? –mov esp, ebp; pop ebp; ret –So the stack ptr is now mbp –If we also control memory around address mbp, we take over the machine The ret call will transfer control wherever we like Note that we don’t need an overflow in this secondary buffer, we just need to control its contents –Despite this sounding far-fetched, there have been numerous exploitable off-by-ones SSH, wu-ftp, and more

Format String Vulnerabilities Example: output(char *p) { printf(p); } Seems harmless: prints whatever string is handed to it –But if string is user-supplied, strange things can happen –Consider what happens if formatting chacters are included Ex: p = “%s”

Format Strings (cont) Let’s play with format strings: –“AAAAAAA%08x%08x%08x%08x” –Prints values from the stack (expecting parameters) Top of stack Format string ret sfp AAAAAAAA%08x%08x%08x%08x Return address to caller Saved Frame Pointer 4 bytes p Ptr to format string printf called values from here are printed

Example Output Continuing with “AAAAAAA%08x%08x%08x%08x” –AAAAAAAA012f2f1580ff000010ff202018ae1414 –So the above values were on the stack… how can we exploit this? We can keep printing stack values until we run into the format string itself… might lead to something interesting AAAAAAA%08x%08x%08x%08x%08x%08x%08x%08x%08 x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%0 8x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x% 08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x Output: AAAAAAAA12f2f1580f…

Printing Data from (almost) Anywhere in Memory As we saw, %s interprets stack value as a pointer, not an int –Suppose we would like to read from address 0x77f7f570 Note: we can’t have any 00 bytes in the address since we are about to embed it in a string –Use format string “AAAA\x70\xf5\xf7\x77%08x%08x…%08x_%s_” Note we’re assuming little-endian here –Output “AAAApJ^0012ff800cccc…ccc _&h2!$*\&_” Note that string will terminate at first 0 byte encountered (and segfault if you go off the end of valid memory)

Picture of Stack Kind of confusing: –As printf reads the format string, it’s reading down the stack for its arguments as well –When printf gets to the %s, the arg ptr is pointing at \x70\xf5\xf7\x77, so we print the contents of that addr Top of stack Format string ret sfp AAAA\x70\xf5\xf7\x77%08x%08x…%08x_%s_ Return address to caller Saved Frame Pointer 4 bytes p Ptr to format string printf called values from here are printed

But Can We Alter the Stack Contents? Introducing the %n token –This one is obscure: nothing is printed but the number of chars printed thus far is stored at the address indicated by the corresponding parameter to %n –Ex: printf(“hi%n there”, &i); now i = 2 –How can we use this ability to write to memory? Consider “AAAA\x70\xf5\xf7\x77%08x%08x…%08%n” Writes 0x (= 356) to address 0x77f7f570

Using %n Extending this, we can write any value of our choice to (almost) any address –“AAAA\x70\xf5\xf7\x77\x71\xf5\xf7\x77\x72\xf5\xf7\x7 7\x73\xf5\xf7\x77%08x%08x…%08x%n%n%n%n” –Writes 0x four times, so at address 0x77f7f570 we will see 0x –But how do we get values of our choice to address 0x77f7f570 instead of this 0x thing? Let’s use the %##u token (or any other that takes a length specifier)

Writing Arbitrary Values We use the width specifier to add any number of bytes we like to the current “number of printed chars” count –To write 0xfff09064 we use “ “AAAA\x70\xf5\xf7\x77\x71\xf5\xf7\x77\x72\xf5\xf7\x 77\x73\xf5\xf7\x77%08x%08x…%08x%n%43u%n%9 6%n%15u%n” –This works fine if we are wanting to write ever- increasing byte values How can we write 0xf0ff9064? How might we write to address 0x400014a0?

Detecting Format String Vulnerabilities Not as hard to detect as buffer overflows (which can be very subtle) One method is to look for calls to printf, sprintf, snprintf, fprintf, etc. and examine the stack clean up code –Recall that after a function call returns, it must remove its parameters from the stack by adding the sum of their sizes to esp –If we see add $4, %esp, we flag a possible vulnerability

Heap Overflows These are among the hardest to exploit and depend on minute OS and compiler details –Some hackers consider writing a heap overflow as a rite of passage –We will only sketch how they work; a detailed example would take too long –This is the last software vulnerability we’ll talk about in this class, but there are MANY more

What is the Heap? The area of data which grows toward the stack –malloc() and new use this memory area for dynamic structures –Unlike the stack, we do not linearly grow and shrink the heap We allocated and deallocate blocks in any order We have to worry about marking the size of blocks, blending adjacent deallocated chunks for re-use, etc. Many algorithms (with various tradeoffs) exist so this attack will depend on the specifics of those algorithms

The Heap (Layout) HEADER HEAP BUFFER HEADER Size of Block/8Size of Prev Block/8 Flags Windows 2K Heap Header Higher Memory

How to Exploit a Heap Overflow Details vary, but in one case: –free() takes a value from the header and writes to an address also taken from the header –If we can overflow the buffer just before this header, we can control both the address used and the value written to that address –This address could be a return address on the stack, and we know the rest of the story…