SDL - Buffer Overflows 1/37 Reference. Based on a presentation provided by Microsoft in relation to the Security Development Lifecycle – Developer Starter.

Slides:



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

CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Computer Security: Principles and Practice EECS710: Information Security Professor Hossein Saiedian Fall 2014 Chapter 10: Buffer Overflow.
Computer Security: Principles and Practice First Edition by William Stallings and Lawrie Brown Lecture slides by Lawrie Brown Chapter 11 – Buffer Overflow.
Lecture 16 Buffer Overflow modified from slides of Lawrie Brown.
Breno de MedeirosFlorida State University Fall 2005 Buffer overflow and stack smashing attacks Principles of application software security.
Stack-Based Buffer Overflows Attacker – Can take over a system remotely across a network. local malicious users – To elevate their privileges and gain.
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.
Teaching Buffer Overflow Ken Williams NC A&T State University.
Secure Programming Joe Testa. ● “It is better to be a Beginner than a Master, because a Beginner can proceed in any direction, whereas the Master is entrenched.
Beyond Stack Smashing: Recent Advances in Exploiting Buffer Overruns Jonathan Pincus Microsoft Research Brandon Baker Microsoft Carl Hartung CSCI 7143:
Teaching Buffer Overflow Ken Williams NC A&T State University.
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
Lecture 16 Buffer Overflow
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow I: Attack Introduction Cliff Zou Spring 2012.
Simple Buffer Overflow Example Dan Fleck CS469 Security Engineering Reference: Coming up: Buffer Overflows.
Static Analysis for Security Amir Bazine Per Rehnberg.
Software bugs (and security implications). Software security Code can have perfect design and algorithm, but still have implementation vulnerabilities.
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-
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Exploitation: Buffer Overflow, SQL injection, Adobe files Source:
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
BLENDED ATTACKS EXPLOITS, VULNERABILITIES AND BUFFER-OVERFLOW TECHNIQUES IN COMPUTER VIRUSES By: Eric Chien and Peter Szor Presented by: Jesus Morales.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Mitigation of Buffer Overflow Attacks
Lecture slides prepared for “Computer Security: Principles and Practice”, 3/e, by William Stallings and Lawrie Brown, Chapter 10 “Buffer Overflow”.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Buffer Overflow CS461/ECE422 Spring Reading Material Based on Chapter 11 of the text.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
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,
Exploitation possibilities of memory related vulnerabilities
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
1 IS 2150 / TEL 2810 Introduction to Security James Joshi Associate Professor, SIS Lecture 12.2 Nov 20, 2012 Integer Issues.
Buffer Overflow Proofing of Code Binaries By Ramya Reguramalingam Graduate Student, Computer Science Advisor: Dr. Gopal Gupta.
Section 3.4: Buffer Overflow Attack: Attack Techniques 1.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
Lecture 9: Buffer Ovefflows and ROP EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014,
Stack-based buffer overflows Yves Younan DistriNet, Department of Computer Science Katholieke Universiteit Leuven Belgium
Lecture 13 Page 1 CS 236 Online Major Problem Areas for Secure Programming Certain areas of programming have proven to be particularly prone to problems.
Buffer overflow and stack smashing attacks Principles of application software security.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Sairajiv Burugapalli. This chapter covers three main categories of classic software vulnerability: Buffer overflows Integer vulnerabilities Format string.
Web Security Firewalls, Buffer overflows and proxy servers.
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.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
CS426Fall 2010/Lecture 141 Computer Security CS 426 Lecture 14 Software Vulnerabilities: Format String and Integer Overflow Vulnerabilities.
Fuzzing And Oracles By: Thomas Sidoti. Overview Introduction Motivation Fuzzable Exploits Oracles Implementation Fuzzing Results.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
Beyond Stack Smashing: Recent Advances In Exploiting Buffer Overruns Jonathan Pincus and Brandon Baker Microsoft Researchers IEEE Security and.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
Chapter 10 Buffer Overflow 1. A very common attack mechanism o First used by the Morris Worm in 1988 Still of major concern o Legacy of buggy code in.
@Yuan Xue Worm Attack Yuan Xue Fall 2012.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Mitigation against Buffer Overflow Attacks
Sabrina Wilkes-Morris CSCE 548 Student Presentation
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
CNT4704: Computer Networking Special Topic: Buffer Overflow I: Attack Introduction Cliff Zou Fall 2009.
CMSC 414 Computer and Network Security Lecture 21
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Software Security Lesson Introduction
CSC 495/583 Topics of Software Security Format String Bug (2) & Heap
Covering CWE with Programming Languages and Tools
Presentation transcript:

SDL - Buffer Overflows 1/37 Reference. Based on a presentation provided by Microsoft in relation to the Security Development Lifecycle – Developer Starter Kit

Overview of buffer overflows – Stack-based – Structured Exception Handlers (SEH) – Heap-based Buffer overflow myths Reducing the risk of buffer overflow attacks in code with the Microsoft SDL Common Weakness Enumeration (CWE) Overview Examples Conclusions 2/37 Agenda

Buffer Overflows Overview Primary Risks: Corrupt data, crash programs and control execution flow Common in native applications (C/C++) – Rare, but still possible in managed code (.NET, Java) Cause is failing to validate input Can occur on stacks and heaps 3/37 Buffer Overflow: Occurs when data is written into a fixed-length buffer and the size of that data exceeds the capacity of the receiving buffer

void FunctionOne(int c) { int LocalInt; char LocalBuffer[32]; /* Operations */ } Review of Application Stack Frames void main(void) { FunctionOne(arguments); FunctionTwo(); } 4 Function parameters Return Address Saved Frame Pointer Local function variables void FunctionTwo(void) { /* Operations */ }

Review of Application Stack Frames (detailed) 5 int function_B(int a, int b) { int x, y; // local variable x = a * a; y = b * b; return (x + y); } int function_A(int p, int q) { int c; // local variables c = p * q * function_B(p, p); return c; } int main(int argc, char **argv, char **env) { int res; res = function_A(1, 2); return res; } int function_B(int a, int b) push EBP mov EBP, ESP sub ESP, 48h... int function_A(int p, int q) push EBP mov EBP, ESP sub ESP, 44h... push 1 call function_B int main(int argc, char **argv, char **env) push 2 push 1 call function_A... Reference. M. Down et al., The Art of Software Security Assessment, Addison Wesley, 2012, pg. 175

AAAAAAAAA … (32 times)… AAAAAAAAA /* UNSAFE Function */ void UnsafeFunction(char * str) { char Buffer[32]; /* Copy str into Buffer */ strcpy(Buffer,str); } Stack-Based Buffer Overflows Primary Risk: Ability to overwrite control structures 6 Function parameters Return Address Saved Frame Pointer SAMPLE INPUTS (STR VALUES): 1.“Kevin” 2.“A” repeated 40 times Buffer (32 Bytes) Kevin AAAA Malicious Payload or Machine Instructions Address of Buffer

Stack-Based Buffer Overflows (details) Primary Risk: Ability to overwrite control structures 7 int unsafe_function(char *msg) { int var; // local variables char buffer[8]; var = 10; strcpy(buffer, msg); return var; } int main(int argc, char **argv, char **env) { int res; /* Buffer overflow for “strlen(argv[1]) >= 8” res = unsafe_function(argv[1]); return res; }

Off-by-One Stack-Based Buffer Overflows Primary Risk: Ability to overwrite local variables or saved EBP 8 int unsafe_function(char *msg) { char buffer[512]; // local variables // wrong limit checking if (strlen(msg) <= 512) strcpy(buffer, msg); } int main(int argc, char **argv, char **env) { int res; /* Buffer overflow for “strlen(argv[1]) >= 8” res = unsafe_function(argv[1]); return res; }

Review of Application Heaps void SampleFunction(void) { /* Allocate space on heap */ char * ptr = (char *)malloc(32); /* Operations */ /* Free allocated heap space */ free(ptr); } 9 Pseudo-code For Chunk Freeing: NextChunk = Current->FP PreviousChunk = Current->BP NextChunk->BP = PreviousChunk PreviousChunk->FP = NextChunk BPFPData Chunk #2 BPFPData Chunk #1 BPFPData Chunk #3

BPFPData Chunk #3 Heap-Based Buffer Overflows Primary Risk: Ability to write arbitrary 4 byte DWORD anywhere in memory (return address, pointers, etc.) 10 Pseudo-code For Chunk Freeing: NextChunk = Current->FP PreviousChunk = Current->BP NextChunk->BP = PreviousChunk PreviousChunk->FP = NextChunk BPFPDataBPFPData Chunk #2 Chunk #1 AA(32 times)AA AAAA /* UNSAFE Function */ void UnsafeFunction(char * str) { /* Allocate 32 bytes heap space */ char * Buffer = (char *)malloc(32); /* Copy str into Buffer */ strcpy(Buffer,str); }

specific to Windows programs could register handlers to act on errors – catching exceptions thrown by the program during runtime exception handler registration structures are located on the stack and contains – address of a handler routine – pointer to its parent handlers the exception handler chain is traversed from the most recently installed handler back to the first one – identify the appropriate handler, by executing each one in turn if an attacker could perform stack overflow – could overwrite the exception handling structure – than generate an exception – the execution could jump to the attacker's controlled address 11 Structured Exception Handling (SEH) Reference. M. Down et al., The Art of Software Security Assessment, Addison Wesley, 2012, pg

12 Structured Exception Handling (SEH) Reference. M. Down et al., The Art of Software Security Assessment, Addison Wesley, 2012, pg

Reducing Exposure to Buffer Overflows with the Microsoft SDL Reducing Risk From Buffer Overflows Reduce Attack Surface and Least Privilege Search for risky functions and determine data origin Use safer libraries and classes (StrSafe, Safe CRT, STL) /GS, NX and Heap Checking PREFast & SALFuzz Testing 13 Presentation content is available for all of these topics

SDL: Review Source Code for Buffer Overflows Source code review: Manual inspection of application for specific vulnerabilities, such as buffer overflows – Input received from network, file, command line – Transfer of received input to internal structures – Use of unsafe string handling calls – Use of arithmetic to calculate an allocation or remaining buffer size Overall method: trace user input from the entry point of the application through all function calls 14 Reference. M. Howard et al., “24 Deadly Sins of Software Security”, 2010, p

SDL: Use Safer APIs and Avoid Banned APIs Safer APIs: Development libraries that are more resistant to buffer overflows Banned APIs: Development libraries that can easily lead to buffer overflows, and banned for use by the Microsoft SDL 15 See Presentation: Banned APIs

SDL: Use Run-Time Protection Compiler Protection: Run-time checks that reduce risk from buffer overflow attacks 16 See Presentation: Compiler Protection

SDL: Use Code Analysis Tools Code Analysis Tools: Automated tools designed to aid in the identification of known vulnerabilities in code 17 See Presentations: Code Analysis Source Code Annotation Language

SDL: Use Fuzz Testing Fuzz Testing: A testing methodology that can help identify security issues that manifest in applications due to improper input validation 18 See Presentations: Secure Verification Principles Fuzz Testing

Platform Protection From Buffer Overflows Modern day operating systems and processors have built-in buffer overflow protection – Address Space Layout Randomization (ASLR) – Data Execution Protection (DEP) However none of these are “silver bullets” – Denial of Service (DoS) attacks usually not prevented – More subtle attacks could still be performed – Developers still need to follow security best practices – Developers should always apply the Microsoft SDL 19

CWE-119: Improper Restriction of Operations within the Bounds of a Memory Buffer CWE-120: Buffer Copy without Checking Size of Input ('Classic Buffer Overflow') – Rank 3 in the Top 25 CWE-121: Stack-based Buffer Overflow CWE-122: Heap-based Buffer Overflow CWE-124: Buffer Underwrite ('Buffer Underflow') CWE-125: Out-of-bounds Read CWE-131: Incorrect Calculation of Buffer Size (!) – Rank 20 in the Top 25 CWE-170: Improper Null Termination CWE-190: Integer Overflow (!) – Rank 24 in the Top 25 CWE-193: Off-by-one Error CWE-805: Buffer Access with Incorrect Length Value … 20 CWE Buffer-Overflow Related Reference. CWE = Common Weakness Enumeration (

Example: local variable overwrite Local variable “authenticate” could be overwritten Application control flow could be changed 21 Reference. M. Down et al., The Art of Software Security Assessment, Addison Wesley, 2012, pg. 176 int authenticate(char *username, char *password) { int authenticated; char buffer[1024]; authenticated = verify_password(username, password); if (authenticated == 0) { sprintf(buffer, "password is incorrect for user %s\n", username); log("%s", buffer); } return authenticated; }

Example: off-by-one error (1) void process_string(char *src) { char dest[32]; for (i = 0; src[i] && (i <= sizeof(dest)); i++) dest[i] = src[i]; } 22 Error: wrong array indexing Reference. M. Down et al., The Art of Software Security Assessment, Addison Wesley, 2012, pg void process_string(char *src) { char dest[32]; for (i = 0; src[i] && (i < sizeof(dest)); i++) dest[i] = src[i]; }

Example: off-by-one error (2) int get_user(char *user) { char buf[1024]; if (strlen(user) > sizeof(buf)) die("error: user string too long\n"); strcpy(buf, user); } 23 Error: wrong string terminator handling Reference. M. Down et al., The Art of Software Security Assessment, Addison Wesley, 2012, pg int get_user(char *user) { char buf[1024]; if (strlen(user) >= sizeof(buf)) die("error: user string too long\n"); strcpy(buf, user); }

Example: off-by-one error (3) int setFilename(char *filename) { char name[20]; sprintf(name, "%16s.dat", filename); int success = saveFormattedFilenameToDB(name); return success; } 24 Error: wrong string terminator handling Reference. CWE 193 (

Example: incorrect length value (1)... char source[21] = "the character string"; char dest[12]; strncpy(dest, source, sizeof(source)-1); Error: wrong size limit considered Reference. CWE 805 ( char source[21] = "the character string"; char dest[12]; strncpy(dest, source, sizeof(dest)-1); dest[sizeof(dest)-1)] = ‘\0’;...

Example: incorrect length value (2) returnChunkSize() returns “-1” on error the return value is not checked before the memcpy operation memcpy() assumes that the value is unsigned when “-1” is returned, it will be interpreted as MAXINT-1 (e.g. 0xFFFFFFFE) int returnChunkSize(void *chunk) { /* if chunk info is valid, return the size of usable memory, * else, return -1 to indicate an error */... } int main() {... memcpy(destBuf, srcBuf, (returnChunkSize(destBuf)-1));... } 26 #include string.h void *memcpy(void *dest, const void *src, size_t n); Reference. CWE 805 (

Example: incorrect length value (3) if count is user (attacker) controlled – is not checked !!! could be given to generate a overflow in the multiplication operation – allocates smaller space than accessed bool CopyStructs(InputFile * pInFile, unsigned long count) { unsigned long i; m_pStruct = new Structs[count]; for (i = 0; i < count; i++) { if (!ReadFromFile(pInFile, &(m_pStruct[i]))) break; } 27 new Structs[count]  malloc(sizeof(Structs) * count); Reference. M. Howard et al., “24 Deadly Sins of Software Security”, 2010, p. 97

Example: incorrect calc. of buffer size (1) malloc(3) allocates just 3 bytes, instead of space for 3 pointers int *id_sequence; /* Allocate space for an array of three ids. */ id_sequence = (int*) malloc(3); if (id_sequence == NULL) exit(1); /* Populate the id array. */ id_sequence[0] = 13579; id_sequence[1] = 24680; id_sequence[2] = 97531; 28 Reference. CWE 131 ( int *id_sequence; /* Allocate space for an array of three ids. */ id_sequence = (int*) malloc(3 * sizeof(int*)); if (id_sequence == NULL) exit(1); /* Populate the id array. */ id_sequence[0] = 13579; id_sequence[1] = 24680; id_sequence[2] = 97531;

Example: incorrect calc. of buffer size (2) numHeaders defined as a signed int when assigned a huge unsigned number, it results in a negative number when compared, condition is fulfilled when used in malloc, it is converted back to an unsigned integer => a huge number Example – numHeaders = -3 (0xFFFFFFFD) – numHeaders * sizeof() = -300 (FFFFFED4) – malloc( ) DataPacket *packet; int numHeaders; PacketHeader *headers; sock=AcceptSocketConnection(); ReadPacket(packet, sock); numHeaders = packet->headers; if (numHeaders > 100) { ExitError("too many headers!"); } headers = malloc(numHeaders * sizeof(PacketHeader)); ParsePacketHeaders(packet, headers); 29 Reference. CWE 131 ( DataPacket *packet; int numHeaders; PacketHeader *headers; sock=AcceptSocketConnection(); ReadPacket(packet, sock); numHeaders = packet->headers; if (numHeaders > 100 || numHeaders < 0) { ExitError("too many headers!"); } headers = malloc(numHeaders * sizeof(PacketHeader)); ParsePacketHeaders(packet, headers);

Example: incorrect calc. of buffer size (3) when input – user controlled Problem 1: truncation – strlen() returns size_t – len is short Problem 2: type casting – len converted to an (signed) int const long MAX_LEN = 0x7FFF; char dst[MAX_LEN]; short len = strlen(input); if (len < MAX_LEN) strncpy(dst, input, len); 30 size_t strlen(const char *s); Reference. M. Howard et al., “24 Deadly Sins of Software Security”, 2010, p. 121

Example: out-of-bound access (1) the buffer index is not validated allows access outside the intended area int main (int argc, char **argv) { char *items[] = {"boat", "car", "truck", "train"}; int index = GetUntrustedOffset(); printf("You selected %s\n", items[index-1]); } 31 Reference. CWE 125 (

Example: out-of-bound access (2) the buffer index is only checked against the upper limits, but not against the lower one (i.e. zero) int getValueFromArray(int *array, int len, int index) { int value; if (index < len) { value = array[index]; } else { printf("Value is: %d\n", array[index]); value = -1; } return value; } 32 Reference. CWE 125 (

Example: Improper Null Termination (1) inputbuf could be not NULL terminated strcpy could copy more than MAXLEN #define MAXLEN char *pathbuf[MAXLEN];... read(cfgfile, inputbuf, MAXLEN); //may not null terminate strcpy(pathbuf, input_buf); //requires null terminated input Reference. CWE 170 (

Example: Improper Null Termination (2) buf could be not NULL terminated length could be greater than MAXPATH char buf[MAXPATH]; char dst[MAXPATH];... readlink(path, buf, MAXPATH); int length = strlen(buf);... strncpy(dst, buf, length); 34 Reference. CWE 170 (

First well-known Internet worm: Morris finger worm (1988) Common Vulnerabilities and Exposures ( – Searching string “buffer overflow”  “About 639 results” (actually few thousands) Vulnerability Notes Database ( – Searching string “buffer overflow”  “About 240 results” Examples – CVE GHOST: glibc gethostbyname buffer overflow – CVE Buffer overflow in client/mysql.cc in Oracle MySQL and MariaDB before – CVE Heap-based buffer overflow in the virtio_load function in hw/virtio/virtio.c in QEMU before – CVE Stack-based buffer overflow in Adobe Flash Player before – CVE Stack-based buffer overflow in Adobe Illustrator CS6 before – CVE Tianocore UEFI implementation reclaim function vulnerable to buffer overflow – CVE Buffer overflow in the Windows Forms (aka WinForms) component in Microsoft.NET Framework – CVE Integer overflow in Skype client … leads to a resultant heap-based buffer overflow – … 35 Real-Life Examples

Buffer Overflow – classical, well known, still present (“oldie but goldie”) – due to the usage of unsafe function and non-validated user input logic (calculation) errors Recommendations for Code Developers – Do not use unsafe (string) functions – Use the right compiler/linker options – Check allocation size calculations Do make size checking Take care of automatic type casting and possible integer overflows – use size_t (when possible) for allocation size variables – take care at casts from signed to unsigned – …. Recommendations for Code Reviewers – Check for user input and trace it through the application – Check for unsafe functions – Check for allocation size calculations 10 March 2015Windows SDLK36/37 Conclusions