Buffer OVERFLOW Attacks and DEFENses Edward Chow

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.
Exploring Security Vulnerabilities by Exploiting Buffer Overflow using the MIPS ISA Andrew T. Phillips Jack S. E. Tan Department of Computer Science University.
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.
CMSC 414 Computer and Network Security Lecture 22 Jonathan Katz.
Foundations of Network and Computer Security J J ohn Black Lecture #30 Nov 26 th 2007 CSCI 6268/TLEN 5831, Fall 2007.
1 Information Security – Theory vs. Reality , Winter 2011 Lecture 8: Control hijacking attacks Eran Tromer Slides credit: Dan Boneh, Stanford.
CMSC 414 Computer and Network Security Lecture 24 Jonathan Katz.
1 Control Hijacking Attacks Buffer overflows and format string bugs.
Preventing Buffer Overflow Attacks. Some unsafe C lib functions strcpy (char *dest, const char *src) strcat (char *dest, const char *src) gets (char *s)
1 Control Hijacking Attacks Buffer overflows and format string bugs.
Control Hijacking Attacks Note: project 1 is out Section this Friday 4:15pm.
CMSC 414 Computer and Network Security Lecture 25 Jonathan Katz.
1 Buffer Overflow Attacks and Format String bugs.
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.
Lecture 16 Buffer Overflow
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2012.
Buffer Overflow Attacks. Memory plays a key part in many computer system functions. It’s a critical component to many internal operations. From mother.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2013.
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-
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)
Control Hijacking Attacks
Exploiting Buffer Overflows on AIX/PowerPC HP-UX/PA-RISC Solaris/SPARC.
Buffer Overflows : An In-depth Analysis. Introduction Buffer overflows were understood as early as 1972 The legendary Morris Worm made use of a Buffer.
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”.
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.
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.
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
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.
Lecture 9: Buffer Ovefflows and ROP EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014,
Control Hijacking Attacks Note: project 1 is out Section this Friday 2pm (Skilling 090)
A Tool for Pro-active Defense Against the Buffer Overrun Attack D. Bruschi, E. Rosti, R. Banfi Presented By: Warshavsky Alex.
On the Effectiveness of Address-Space Randomization Hovav Shacham, Matthew Page, Ben Pfaff, Eu-Jin Goh, Nagendra Modadugu, Dan Boneh.
Worm enabling exploits Cyber Security Lab Spring ‘10.
A Survey on Runtime Smashed Stack Detection 坂井研究室 M 豊島隆志.
Buffer Overflows Taught by Scott Coté.-. _ _.-. / \.-. ((___)).-. / \ /.ooM \ / \.-. [ x x ].-. / \ /.ooM \ -/ \ /-----\-----/---\--\ /--/---\-----/-----\ / \-
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.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Slides by Kent Seamons and Tim van der Horst Last Updated: Nov 11, 2011.
Control Hijacking Attacks Note: project 1 is out Section this Friday 4:15pm (Gates B03)
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
Buffer Overflows: Attacks and Defenses for the Vulnerability of the Decade Crispin Cowan SANS 2000.
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.
@Yuan Xue Worm Attack Yuan Xue Fall 2012.
Mitigation against Buffer Overflow Attacks
Sabrina Wilkes-Morris CSCE 548 Student Presentation
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2016.
Software Security.
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Preventing Buffer Overflow Attacks
Foundations of Network and Computer Security
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2011.
CS703 - Advanced Operating Systems
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow II: Defense Techniques Cliff Zou Spring 2009.
CNT4704: Analysis of Computer Communication Network Special Topic: Buffer Overflow II: Defense Techniques Cliff Zou Fall 2011.
Presentation transcript:

Buffer OVERFLOW Attacks and DEFENses Edward Chow 4/17/2017 Buffer OVERFLOW Attacks and DEFENses Edward Chow 4/17/2017 C. Edward Chow

Outline of the Talk History of Buffer Overflow Attacks Buffer Overflow Attack and related Background Knowledge Linux VirtualMemory Map Shellcode Egg: No-ops/shellcode/returnAddresses Countermeasures: StackGuard StackShield WXPage Address Space Randomization Hardware Assisted Non-executable page protection: AMD NX bit Intel Execute Disable Bit Instruction Set Randomization (ISR) 4/17/2017 CS591 / C. Edward Chow

Related Literature “Smashing The Stack For Fun And Profit,” by Aleph One (aka Elias Levy) “On the Effectiveness of Address-Space Randomization,” by Shacham et al at Stanford's applied crypto group The material presented here are adapted from bufferOverflow/cs155stanford/04-buf-overflow.ppt http://www.cs.utexas.edu/~shmat/courses/cs378_spring05/16overflow.ppt 4/17/2017 CS591 / C. Edward Chow

Buffer Overflow Attacks Extremely common bug. First major exploit: 1988 Internet Worm Robert Morris. fingerd. 15 years later:  50% of all CERT advisories: 1998: 9 out of 13 2001: 14 out of 37 2003: 13 out of 28 Often leads to total compromise of host. Steps in developing buffer overflow attacks: Locate buffer overflow within an application. Design an exploit. 4/17/2017 CS591 / C. Edward Chow

Morris Worm and Buffer Overflow from Shmatcs378 We’ll look at the Morris worm in more detail when talking about worms and viruses One of the worm’s propagation techniques was a buffer overflow attack against a vulnerable version of fingerd on VAX systems By sending special string to finger daemon, worm caused it to execute code creating a new worm copy Unable to determine remote OS version, worm also attacked fingerd on Suns running BSD, causing them to crash (instead of spawning a new copy) 4/17/2017 CS591 / C. Edward Chow

A Bit of History: Morris Worm adapted from Shmatcs378 Worm was released in 1988 by Robert Morris Graduate student at Cornell, son of NSA chief scientist Convicted under Computer Fraud and Abuse Act, sentenced to 3 years of probation and 400 hours of community service Now a computer science professor at MIT Worm was intended to propagate slowly and harmlessly measure the size of the Internet Due to a coding error, it created new copies as fast as it could and overloaded infected machines $10-100M worth of damage 4/17/2017 CS591 / C. Edward Chow

Computer Buffer Buffer: A contiguous block of computer memory, can be used for Data: variables (static/global, dynamic/local), arrays Code: user programs, shared libraries, kernel programs. To shield User/kernel programs from each other, virtual memory is used Within a virtual memory address space, different OS’/CPUs have different ways to allocate buffers. On Linux, static/global variables allocated at load time on the data segment, dynamic/local variables are allocated at run time on the stack. 4/17/2017 CS591 / C. Edward Chow

Virtual Memory Address and Page Memory Systems Protection mode Relative Page # User process # Data Pages Code Pages User Process 1 CPU MMU VM_Address PM_Address Virtual Memory Address Page(1,9) R Page(1,8) RW Page(1,7) RW Page(1,1) E Data/Code Data Pages Code Pages User Process 2 Page(2,9) R Page(2,7) RW Address Mapping Page loading Page(3,9) R Page(3,7) RW Page(3,6) RW Page(3,1) E Data Pages Code Pages User Process n Page(2,6) RW Page(2,5) RW Page(2,2) E Page(2,1) E MMU: Memory Management Unit Physical Memory 4/17/2017 CS591 / C. Edward Chow

The Linux VirtualMemory Map (as seen by a UserSpace program) 4/17/2017 The Linux VirtualMemory Map (as seen by a UserSpace program) Starts at   Contains   ffffffff   End of the universe   ffffe000   vsyscall table (new in 2.5.x)   c0000000   Off limits, reserved for the kernel   bfffffff   Process stack (grows down)   bffff000   Process heap (grows up)   40000000   Libraries   zzzzzzzz   Unused   yyyyyyyy   .bss, uninitialised program data   xxxxxxxx   .data segment, initialised program data   08048000   .text segment, program code   00000000   Unmapped to trap NULL pointers  4/17/2017 CS591 / C. Edward Chow

Memory Layout of Linux Process 4/17/2017 Memory Layout of Linux Process Kernel Memory 0xC0000000 User Stack Stack Pointer %esp Shared libraries, mmap, shared mem 0x40000000 Brk() Brk (*end_data_sgement): set the end of the data segment. Run time heap Loaded from exec Text Segment, BSS 0x08048500 4/17/2017 Unused C. Edward Chow 4/17/2017 CS591 / C. Edward Chow

Where are Buffers Allocated & Buffer Overflow Opportunities? Kernel Memory 0xC0000000 User Stack char buf[32]; struct Node* bn; int main(int argc, char* argv[ ]) { return function(argv[1]); } int function(char* str1) { char a[32]; printf(“Hello World!”); gets( a ); bn=malloc(sizeof(struct Node)); *(buf-32)=0xbffffeb4; strcpy(buf, str1); return lookup(a, bn); Shared libraries, mmap, shared mem 0x40000000 Run time heap Text Segment, BSS 0x08048500 Unused C. Edward Chow CS591 / C. Edward Chow 4/17/2017

Stack Frame Parameters Return address Stack Frame Pointer Local variables Stack Growth SP 4/17/2017 C. Edward Chow

What are buffer overflows? Suppose a web server contains a function: char a[30]; void func(char *str) { char buf[128]; strcpy(buf, str); strcpy(a, str); do-something(buf); } When the function is invoked the stack looks like: What if *str is 136 bytes long? After strcpy: str ret-addr sfp buf top of stack Lower memory address Buf+132 top of stack *str sfp* Ret* str Direction of copy operation 4/17/2017 C. Edward Chow

Basic Stack Exploit Program P: exec( “/bin/sh” ) Main problem: no range checking in strcpy(). Suppose *str is such that after strcpy stack looks like: When func() exits, the user will be given a shell !! Note: attack code runs in stack. To determine ret guess position of stack when func() is called. top of stack *str ret Code for P Program P: exec( “/bin/sh” ) (exact shell code by Aleph One) 4/17/2017 C. Edward Chow

What is Needed to Create Buffer Overflow Exploit? Understanding C functions and the stack. Some familiarity with machine code. Know how systems calls are made. The exec() system call. Attacker needs to know which CPU and OS are running on the target machine. Our examples are for x86 running Linux. Details vary slightly between CPU’s and OS: Stack growth direction. big endian vs. little endian. 4/17/2017 CS591 / C. Edward Chow

Preventing overflow attacks (from http://crypto. stanford Main problem: strcpy(), strcat(), sprintf() have no range checking. “Safe” versions strncpy(), strncat() are misleading strncpy() may leave buffer unterminated. strncpy(), strncat() encourage off by 1 bugs. Defenses: Type safe languages (Java, ML). Legacy code? Mark stack as non-execute. Random stack location. Static source code analysis. Run time checking: StackGuard, Libsafe, SafeC, (Purify). Many more … (covered later in course) 4/17/2017 C. Edward Chow

Does Range Checking Help? Potential overflow in htpasswd.c (apache 1.3) … strcpy(record, user); strcat(record, “:”); strcat(record, cpw); Published “fix” (do you see the problem?): … strncpy(record, user, MAX_STRING_LEN-1); strcat(record, “:”); strncat(record, cpw, MAX_STRING_LEN-1);

Execustack gcc version 4.0.0 20050519 (Red Hat 4.0.0-8)on Athena (FC4) by default mark the executable does not require executable stack. This is a buffer overflow prevention measure. Therefore when running the testsc example in Aleph's paper on Fedora Core 4, you will get segmentation fault. You change it by using "execstack -s" to mark testsc as requiring executable stack, then we can see the buffer overflow effect. On fc4.csnet, testc flag is not set. You need to recompile testsc to have it set with the protection. [chow@athena src]$ execstack -q testsc - testsc [chow@athena src]$ ./testsc Segmentation fault [chow@athena src]$ execstack -s testsc sh-3.00$ exit exit [chow@athena src]$ See man page of execstack at http://www.pk.edu.pl/cgi-bin/man-cgi?execstack 4/17/2017 C. Edward Chow

Countermeasure: Marking stack as non-execute Basic stack exploit can be prevented by marking stack segment as non-executable. NX Bit on AMD Athlon 64, Intel P4 “Prescott”. NX bit in every Page Table Entry (PTE) Support in SP2. Code patches exist for Linux, Solaris. Problems: Does not defend against `return-to-libc’ exploit. Overflow sets ret-addr to address of libc function. Some apps need executable stack (e.g. LISP interpreters). Does not block more general overflow exploits: Overflow on heap: overflow buffer next to func pointer. 4/17/2017 C. Edward Chow

Static source code analysis Statically check source to detect buffer overflows. Several consulting companies, e.g. Fortify Software Inc. Can we automate the review process? Several tools exist: Coverity (Engler et al.): Test trust inconsistency. Microsoft program analysis group: PREfix: looks for fixed set of bugs (e.g. null ptr ref) PREfast: local analysis to find idioms for prog errors. Berkeley: Wagner, et al. Test constraint violations. Fortify Software Framework. Find lots of bugs, but not all. 4/17/2017 C. Edward Chow

Run time checking: StackGuard Many many run-time checking techniques … Here, only discuss methods relevant to overflow protection. Solutions 1: StackGuard (WireX) Run time tests for stack integrity. Embed “canaries” in stack frames and verify their integrity prior to function return. “StackGuard works by inserting a ``guard'' value (called a ``canary'', as in how this bird was used in mines) in front of the return address; “ Frame 2 Frame 1 top of stack local canary sfp ret str local canary sfp ret str 4/17/2017 C. Edward Chow

Canary Types Random canary: Choose random string at program startup. Insert canary string into every stack frame. Verify canary before returning from function. To corrupt random canary, attacker must learn current random string. Terminator canary: Canary = 0, newline, linefeed, EOF String functions will not copy beyond terminator. Hence, attacker cannot use string functions to corrupt stack. Why? I thought attacker has control on the data 4/17/2017 C. Edward Chow

StackGuard (Cont.) StackGuard implemented as a GCC patch. Program must be recompiled. Minimal performance effects: 8% for Apache. Newer version: PointGuard. Protects function pointers and setjmp buffers by placing canaries next to them. More noticeable performance effects. Note: Canaries don’t offer fullproof protection. Some stack smashing attacks can leave canaries untouched. 4/17/2017 C. Edward Chow

StackGuard variants - ProPolice ProPolice (IBM) New Name: Stack-Smashing Protector) - GCC 3.4.1, GCC 4.1 stage 2. Rearrange stack layout to prevent ptr overflow. Random local variables allocation args String Growth No arrays or pointers ret addr SFP CANARY arrays Stack Growth Local variables Ptrs, but no arrays 4/17/2017 C. Edward Chow

Windows XP SP2 /GS Non executable stack. Compiler /GS option: Combination of ProPolice and Random canary. Triggers UnHandledException in case of Canary mismatch to shutdown process. Litchfield vulnerability report. Overflow overwrites exception handler. Redirects exception to attack code. 4/17/2017 C. Edward Chow

Run time checking: Libsafe Solutions 2: Libsafe (Avaya Labs) Dynamically loaded library. Intercepts calls to strcpy (dest, src) Validates sufficient space in current stack frame: |frame-pointer – dest| > strlen(src) If so, does strcpy. Otherwise, terminates application. top of stack sfp ret-addr dest src buf sfp ret-addr libsafe main 4/17/2017 C. Edward Chow

More methods … StackShield Randomization: At function prologue, copy return address RET and SFP to “safe” location (beginning of data segment) Upon return, check that RET and SFP is equal to copy. Implemented as assembler file processor (GCC) Randomization: PaX ASLR: Randomize location of libc. Attacker cannot jump directly to exec function. Instruction Set Randomization (ISR) Attacker cannot execute its own code. 4/17/2017 C. Edward Chow