© 2003 School of Computing, University of Leeds SY32 Secure Computing, Lecture 13 Implementation Flaws Part 1: Buffer Overruns.

Slides:



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

Smashing the Stack for Fun and Profit
Defenses. Preventing hijacking attacks 1. Fix bugs: – Audit software Automated tools: Coverity, Prefast/Prefix. – Rewrite software in a type safe languange.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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.
Intro to Exploitation Stack Overflows James McFadyen UTD Computer Security Group 10/20/2011.
Buffer Overflow Exploits CS-480b Dick Steflik. What is a buffer overflow? Memory global static heap malloc( ), new Stack non-static local variabled value.
Teaching Buffer Overflow Ken Williams NC A&T State University.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Buffer Overflow. Process Memory Organization.
Teaching Buffer Overflow Ken Williams NC A&T State University.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
September 22, 2014 Pengju (Jimmy) Jin Section E
Lecture 16 Buffer Overflow
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2012.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2013.
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.
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.
Lecture 6: Buffer Overflow CS 436/636/736 Spring 2014 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
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
Understand stack Buffer overflow attack and defense Controls against program threats.
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”.
University of Washington Today Happy Monday! HW2 due, how is Lab 3 going? Today we’ll go over:  Address space layout  Input buffers on the stack  Overflowing.
Buffer Overflow CS461/ECE422 Spring Reading Material Based on Chapter 11 of the text.
Smashing the Stack Overview The Stack Region Buffer Overflow
Buffer Overflows Many of the following slides are based on those from
Buffer Overflow. Introduction On many C implementations, it is possible to corrupt the execution stack by writing past the end of an array. Known as smash.
Lecture 8: Buffer Overflow CS 436/636/736 Spring 2013 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
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.
A Tool for Pro-active Defense Against the Buffer Overrun Attack D. Bruschi, E. Rosti, R. Banfi Presented By: Warshavsky Alex.
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.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
CSC 382: Buffer OverflowsSlide #1 Topics 1.What is a Buffer Overflow? 2.The Most Common Implementation Flaw. 3.Process Memory Layout. 4.The Stack and C’s.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
1988 Morris Worm … estimated 10% penetration 2001 Code Red … 300,00 computers breached 2003 Slammer/Sapphire … 75,00 infections in 10 min Zotob …
Analyzing C/C++ Vulnerabilities -- Mike Gerschefske.
Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Crispin Cowan SANS 2000.
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.
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.
1 Introduction to Information Security , Spring 2016 Lecture 2: Control Hijacking (2/2) Avishai Wool.
CS703 - Advanced Operating Systems By Mr. Farhan Zaidi.
Lec. Waleed Bin Shahid.  You might have noticed a lot of issues related to software implementation.  The ultimate requirement of developer(s) is to.
Shellcode COSC 480 Presentation Alison Buben.
Buffer Overflow By Collin Donaldson.
Mitigation against Buffer Overflow Attacks
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
The Hardware/Software Interface CSE351 Winter 2013
CMSC 414 Computer and Network Security Lecture 21
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Assembly Language Programming II: C Compiler Calling Sequences
Software Security Lesson Introduction
Lecture 9: Buffer Overflow*
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 2009.
Understanding and Preventing Buffer Overflow Attacks in Unix
Format String Vulnerability
Return-to-libc Attacks
Presentation transcript:

© 2003 School of Computing, University of Leeds SY32 Secure Computing, Lecture 13 Implementation Flaws Part 1: Buffer Overruns

2 SY32 Secure Computing, Lecture 13 Outline Background Background Three generations of flaws Three generations of flaws Classic stack smashing Classic stack smashing Off-by-one errors Off-by-one errors Format string bugs and heap overruns Format string bugs and heap overruns Mitigation Mitigation Coding practices Coding practices Memory protection Memory protection

3 SY32 Secure Computing, Lecture 13 Background What are buffer overruns? What are buffer overruns? Program allocates a contiguous chunk of memory of fixed size to store data (a buffer) Program allocates a contiguous chunk of memory of fixed size to store data (a buffer) Amount of data copied to buffer exceeds its capacity and overwrites other memory Amount of data copied to buffer exceeds its capacity and overwrites other memory Why are they a problem? Why are they a problem? Many critical programs are written in C/C++… Many critical programs are written in C/C++… …but C/C++ have no run-time bounds checking …but C/C++ have no run-time bounds checking

4 SY32 Secure Computing, Lecture 13 Background Problem known about since 1960s Problem known about since 1960s First seen widely in 1988 (Morris worm) First seen widely in 1988 (Morris worm) Infamous overruns: Infamous overruns: wu-ftpd wu-ftpd Several overruns discovered in 1999 & 2000 Several overruns discovered in 1999 & 2000 Some flaws up to 10 years old! Some flaws up to 10 years old! Code Red worm Code Red worm Exploited buffer overrun in IIS Exploited buffer overrun in IIS 359,000 machines infected within 14 hours 359,000 machines infected within 14 hours Estimated worldwide cost of $2 billion Estimated worldwide cost of $2 billion

5 SY32 Secure Computing, Lecture 13 Public Enemy #1!

6 SY32 Secure Computing, Lecture 13 Public Enemy #1!

7 SY32 Secure Computing, Lecture 13 Possible Outcomes No effect on program No effect on program Unpredictable behaviour (D) Unpredictable behaviour (D) Program crashes (D) Program crashes (D) Attack affects flow of execution (T,I,D) Attack affects flow of execution (T,I,D) Attacker executes arbitrary code (T,I,D,E) Attacker executes arbitrary code (T,I,D,E)

8 SY32 Secure Computing, Lecture 13 Memory Layout: A Reminder Text segment holds program instructions (read-only) Text segment holds program instructions (read-only) Data and BSS segments provide storage for static/global data Data and BSS segments provide storage for static/global data Stack and heap change size as program executes Stack and heap change size as program executes Stack holds information about context of function calls in a stack frame Stack holds information about context of function calls in a stack frame Function parameters Function parameters Local variables Local variables Saved register information Saved register information Return address for call Return address for call Stack Env. variables Text Data BSS Heap High memory Low memory

9 SY32 Secure Computing, Lecture 13 Stack Smashing Buffer Other local variables Saved frame pointer Return address Parameters Caller’s stack frame Stack pointer Frame pointer …and overwrites return address, gaining control of execution! High memory Low memory Attacker overruns buffer…

10 SY32 Secure Computing, Lecture 13 Demo

11 SY32 Secure Computing, Lecture 13 jmp 0x1f popl %esi movl %esi,0x8(%esi) xorl %eax,%eax movb %eax,0x7(%esi) movl %eax,0xc(%esi) movb $0xb,%al movl %esi,%ebx leal 0x8(%esi),%ecx leal 0xc(%esi),%edx int $0x80 xorl %ebx,%ebx movl %ebx,%eax inc %eax int $0x80 call -0x24.string \"/bin/sh\" Crafting an Exploit void exploit() { char* s = "/bin/sh"; execl(s, s, 0x0); } int main() { exploit(); } Must rewrite to remove nulls, or strcpy won’t work! Shellcode

12 SY32 Secure Computing, Lecture 13 Crafting an Exploit "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07 \x89\x46\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d \x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd\x80 \xe8\xdc\xff\xff\xff/bin/sh" "\x90\x90..." No-opsShellcodeAddresses "\x58\x6f\xff\xbf..." (0xbffff658) Address chosen to ensure jump into no-ops at start of shellcode

13 SY32 Secure Computing, Lecture 13 Demo

14 SY32 Secure Computing, Lecture 13 Questions Are very small buffers susceptible to attack? Are very small buffers susceptible to attack? By how much must we overrun a buffer to execute shellcode? By how much must we overrun a buffer to execute shellcode?

15 SY32 Secure Computing, Lecture 13 Off-By-One Errors void doSomething(const char* input) { char buffer[256]; strncpy(buffer, input, sizeof(buffer)); buffer[sizeof(buffer)] = '\0';... } Sensible choice of strncpy in place of strcpy Sensible choice of strncpy in place of strcpy …but attempt to null-terminate the copied string results in a one-byte overrun …but attempt to null-terminate the copied string results in a one-byte overrun

16 SY32 Secure Computing, Lecture 13 Exploiting Off-by-One Errors Buffer Saved EBP Saved EIP Parameters Caller’s stack frame ESP EBP Preconditions Preconditions Buffer next to saved EBP Buffer next to saved EBP 32-bit alignment 32-bit alignment Little-endian architecture Little-endian architecture Event Event Attempt to null-terminate buffer contents, resulting in one-byte overrun Attempt to null-terminate buffer contents, resulting in one-byte overrun Consequences Consequences LSB of saved EBP now zero LSB of saved EBP now zero Saved EBP now points lower in memory—possibly into buffer itself Saved EBP now points lower in memory—possibly into buffer itself 0

17 SY32 Secure Computing, Lecture 13 Exploiting Off-by-One Errors Just before function exit, saved EBP is popped off stack into EBP register Just before function exit, saved EBP is popped off stack into EBP register On function exit, saved EIP is restored to EIP register and control returns to caller On function exit, saved EIP is restored to EIP register and control returns to caller Just before caller exits, stack pointer is moved to address in EBP register, with aim of restoring the saved EIP… Just before caller exits, stack pointer is moved to address in EBP register, with aim of restoring the saved EIP… …instead, attacker-supplied address is loaded into EIP register, and we have control! …instead, attacker-supplied address is loaded into EIP register, and we have control!

18 SY32 Secure Computing, Lecture 13 Third-Generation Flaws Format string vulnerabilities Format string vulnerabilities First appeared in Summer 2000 First appeared in Summer 2000 Affect the *printf family of functions Affect the *printf family of functions Allow reading from & writing to arbitrary addresses Allow reading from & writing to arbitrary addresses Easy to exploit, but also easy to find and fix Easy to exploit, but also easy to find and fix Heap overruns Heap overruns Less standardized than format string bugs Less standardized than format string bugs Allow writing of arbitrary data to arbitrary addresses Allow writing of arbitrary data to arbitrary addresses Hard to exploit, but also hard to detect Hard to exploit, but also hard to detect

19 SY32 Secure Computing, Lecture 13 Format String Bugs Two ways of printing a string in C: Two ways of printing a string in C: printf("%s", input) printf("%s", input) printf(input) printf(input) Lazy programmers may do the latter, but what if input contains format specifiers? Lazy programmers may do the latter, but what if input contains format specifiers? %n specifier will write data onto the stack %n specifier will write data onto the stack Value could be address of some shellcode… Value could be address of some shellcode… …which could overwrite saved EIP in stack frame …which could overwrite saved EIP in stack frame

20 SY32 Secure Computing, Lecture 13 Mitigation Coding practices Coding practices Choice of language Choice of language Use of library functions Use of library functions Memory protection Memory protection Recompilation of applications required Recompilation of applications required No recompilation required No recompilation required

21 SY32 Secure Computing, Lecture 13 Coding Practices If possible, use languages with intrinsic run-time bounds checking instead of C or C++ If possible, use languages with intrinsic run-time bounds checking instead of C or C++ Java, C#, Python, Perl, etc Java, C#, Python, Perl, etc When writing C++, use its features rather than those of C standard library When writing C++, use its features rather than those of C standard library std::string class, not char* and strcpy, etc std::string class, not char* and strcpy, etc Use C library functions very carefully Use C library functions very carefully Never, ever, use gets ! Never, ever, use gets ! Prefer ‘safe’ versions of other functions Prefer ‘safe’ versions of other functions

22 SY32 Secure Computing, Lecture 13 C Library Risks High-risk functions include High-risk functions include strcat, strcpy strcat, strcpy Use strncat, strncpy – carefully! Use strncat, strncpy – carefully! printf, sprintf, vsprintf printf, sprintf, vsprintf Always use a format string; use snprintf, not sprintf Always use a format string; use snprintf, not sprintf scanf, sscanf, vscanf, vsscanf scanf, sscanf, vscanf, vsscanf getopt, getpass, realpath, syslog getopt, getpass, realpath, syslog Truncate string arguments before passing them in Truncate string arguments before passing them in Some risk with other functions Some risk with other functions See WSC2 or BSS for more information See WSC2 or BSS for more information Always check destination buffer is as big as you are claiming, and watch out for off-by-one errors! Always check destination buffer is as big as you are claiming, and watch out for off-by-one errors!

23 SY32 Secure Computing, Lecture 13 Safety Through Recompilation Compile against safer version of C library Compile against safer version of C library Use compiler support for array bounds checking Use compiler support for array bounds checking Patches for GCC: Patches for GCC: /RTCs compiler option in Visual C++.NET /RTCs compiler option in Visual C++.NET Protect return address on stack with a canary Protect return address on stack with a canary StackGuard, StackGuard, /GS compiler option in Visual C++.NET /GS compiler option in Visual C++.NET

24 SY32 Secure Computing, Lecture 13 Transparent Protection Non-executable stack Non-executable stack Example: Example: Requires kernel patch Requires kernel patch Won’t trap overruns in heap, data or BSS segments Won’t trap overruns in heap, data or BSS segments libverify libverify Binary rewriting of process memory to force stack verification prior to use Binary rewriting of process memory to force stack verification prior to use libsafe, libsafe, Estimates safe upper limit on buffer size at run time Estimates safe upper limit on buffer size at run time Intercepts dangerous library calls, substituting versions that respect buffer size limit Intercepts dangerous library calls, substituting versions that respect buffer size limit

25 SY32 Secure Computing, Lecture 13 Summary Buffer overruns and related flaws are the major cause of security problems in software Buffer overruns and related flaws are the major cause of security problems in software Attacks are varied, but typically involve transfer of control to shellcode supplied by attacker Attacks are varied, but typically involve transfer of control to shellcode supplied by attacker Partial solutions exist Partial solutions exist Compilation of run-time checks into code Compilation of run-time checks into code Transparent memory protection Transparent memory protection Best solution is to avoid C/C++ if possible, or avoid dangerous C library function calls Best solution is to avoid C/C++ if possible, or avoid dangerous C library function calls