Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Similar documents
BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Buffer Overflow. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

Machine-Level Programming V: Miscellaneous Topics Sept. 24, 2002

Linux Memory Layout The course that gives CMU its Zip! Machine-Level Programming IV: Miscellaneous Topics Sept. 24, Text & Stack Example

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

Giving credit where credit is due

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

Sungkyunkwan University

Machine-Level Programming IV: Structured Data

Machine- Level Programming V: Advanced Topics

Machine Programming 5: Buffer Overruns and Stack Exploits

Machine-Level Prog. V Miscellaneous Topics

Buffer overflows (a security interlude) Address space layout the stack discipline + C's lack of bounds-checking HUGE PROBLEM

Buffer overflows. Specific topics:

Computer Systems CEN591(502) Fall 2011

Machine-Level Prog. V Miscellaneous Topics

Machine-Level Programming V: Advanced Topics

Why do we need Pointers? Call by Value vs. Call by Reference in detail Implementing Arrays Buffer Overflow / The Stack Hack

Machine-Level Programming V: Buffer overflow

Machine-level Programs Adv. Topics

CMPSC 497 Buffer Overflow Vulnerabilities

Machine-Level Programming V: Advanced Topics

Buffer Overflows. CSE 351 Autumn Instructor: Justin Hsia

Buffer Overflows. CSE 410 Winter Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui

Buffer Overflows. CSE 351 Autumn 2018

CSC 252: Computer Organization Spring 2018: Lecture 9

Machine-Level Programming V: Advanced Topics

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Buffer. This time. Security. overflows. Software. By investigating. We will begin. our 1st section: History. Memory layouts

CS241 Computer Organization Spring Buffer Overflow

CS 645: Lecture 3 Software Vulnerabilities. Rachel Greenstadt July 3, 2013

Buffer Overflow Attacks

Program Security and Vulnerabilities Class 2

Buffer-Overflow Attacks on the Stack

Buffer-Overflow Attacks on the Stack

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Lecture 08 Control-flow Hijacking Defenses

Project 1 Buffer Overflow

Machine- Level Programming V: Advanced Topics

20: Exploits and Containment

Fundamentals of Computer Security

CSE 509: Computer Security

X86 Assembly Buffer Overflow III:1


Assembly Language: Function Calls

CS , Fall 2004 Exam 1

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

Assembly Language: Function Calls" Goals of this Lecture"

Secure Systems Engineering

Buffer Overflow Attack

buffer overflow exploitation

Assembly Language: Function Calls" Goals of this Lecture"

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

CS , Fall 2002 Exam 1

CPEG421/621 Tutorial

Buffer Overflows Defending against arbitrary code insertion and execution

Lecture 9: Buffer Overflow* CS 392/6813: Computer Security Fall Nitesh Saxena

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Stack Vulnerabilities. CS4379/5375 System Security Assurance Dr. Jaime C. Acosta

Today. Machine-Level Programming V: Advanced Topics. x86-64 Linux Memory Layout. Memory Allocation Example. Today. x86-64 Example Addresses

Today. Machine-Level Programming V: Advanced Topics. x86-64 Linux Memory Layout. Memory Allocation Example. Today. x86-64 Example Addresses

Machine-Level Programming V: Advanced Topics

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Basic Buffer Overflows

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CIS 551 / TCOM 401 Computer and Network Security. Spring 2007 Lecture 2

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Assembly Language Programming - III

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CS 161 Computer Security

THEORY OF COMPILATION

Hands-on Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Final Exam, Spring 2012 Date: May 14th, 2012

Software Security: Buffer Overflow Defenses

Machine-Level Programming V: Advanced Topics

COMP 2355 Introduction to Systems Programming

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Topics in Software Security Vulnerability

Is stack overflow still a problem?

Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

CSc 466/566. Computer Security. 20 : Operating Systems Application Security

(Early) Memory Corruption Attacks

We will focus on Buffer overflow attacks SQL injections. See book for other examples

Lecture 4 September Required reading materials for this class

15-213/18-243, Fall 2010 Exam 1 - Version A

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CSE 565 Computer Security Fall 2018

Security and Privacy in Computer Systems. Lecture 5: Application Program Security

User Programs. Computer Systems Laboratory Sungkyunkwan University

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Prac*ce problem on webpage

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Lab 10: Introduction to x86 Assembly

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Transcription:

Buffer Overflow Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

IA-32/Linux Memory Layout Runtime stack (8MB limit) Heap Dynamically allocated storage When call malloc(), calloc(), new() DLLs (shared libraries) Dynamically linked libraries Library routines (e.g., printf, gets) Linked into object code when first executed Data Statically allocated data e.g., arrays & strings declared in code Text Executable machine instructions Read-only Upper 2 hex digits of address FF C0 BF 80 7F 40 3F 08 00 Kernel Heap DLLs Heap Data Text 2

Text & Example BF Initially (gdb) break main (gdb) run Breakpoint 1, 0x804856f in main () (gdb) print $esp $3 = (void *) 0xbffffc78 80 7F 40 3F 08 00 Data Text 3

Vulnerable Buffer Code /* Echo Line */ void echo() { // Way too small! char buf[4]; gets(buf); puts(buf); } int main() { printf( Type: ); echo(); return 0; } $./bufdemo Type:123 123 $./bufdemo Type: 12345 Segmentation Fault $./bufdemo Type: 12345678 Segmentation Fault 4

String Library Code Implementation of Unix function gets() No way to specify limit on # of characters to read /* Get string from stdin */ char *gets(char *dest) { int c = getc(); char *p = dest; while (c!= EOF && c!= '\n') { *p++ = c; c = getc(); } *p = '\0'; return dest; } Similar problems with other Unix functions strcpy: copies string of arbitrary length scanf/fscanf/sscanf, given %s conversion specification 5

Buffer Overflow (1) for main Return Address Saved %ebp [3] [2] [1] [0] buf %ebp /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } %eax for gets %esp echo: pushl %ebp movl %esp,%ebp subl $24,%esp leal -4(%ebp),%eax movl %eax,(%esp) call gets... # Save %ebp on stack # Allocate space # compute buf as %ebp-4 # save to the stack # Call gets 6

Buffer Overflow (2) unix> gdb bufdemo (gdb) break echo Breakpoint 1 at 0x8048583 (gdb) run Breakpoint 1, 0x8048583 in echo () (gdb) print /x *(unsigned *)$ebp $1 = 0xbffff8f8 (gdb) print /x *((unsigned *)$ebp + 1) $3 = 0x804864d for main Return 08 04 Address 86 4d bf Saved ff %ebp f8 f8 %ebp=0xbffff8f8 [3] xx [2] xx [1] xx [0] xx buf for echo return addr. 8048648: call 8048583 <echo> 804864d: mov $0,%eax # Return Point 7

Buffer Overflow (3) Before Call to gets Input = 123 for main Return Address Saved %ebp [3] [2] [1] [0] buf for echo %ebp for main Return 08 04 Address 86 4d bf Saved ff %ebp f8 f8 0xbffff8f8 [3] 00 [2] 33 [1] 32 [0] 31 buf for echo No Problem 8

Buffer Overflow (4) for main for main Input = 12345 Return Address Saved %ebp [3] [2] [1] [0] buf for echo %ebp Return 08 04 Address 86 4d bf Saved ff %ebp 00 35 0xbffff8d8 [3] 34 [2] 33 [1] 32 [0] 31 buf for echo Saved value of %ebp set to 0xbfff0035 Bad news when later attempt to restore %ebp... 8048600: call 80482c4 # gets <echo code> 8048605: leal 0xfffffffc(%ebp),%eax 8048608: movl %eax,(%esp) 804860b: call 80482d4 # puts 8048610: leave # movl %ebp, %esp; popl %ebp 8048611: ret 9

Buffer Overflow (5) for main for main Input = 12345678 Return Address Saved %ebp [3] [2] [1] [0] buf %ebp Return 08 04 Address 86 00 38 Saved 37 %ebp 36 35 0xbffff8d8 [3] 34 [2] 33 [1] 32 [0] 31 buf for echo for echo Invalid address No longer pointing to desired return point %ebp and return address corrupted 8048648: call 8048583 <echo> 804864d: mov $0,%eax # Return Point 10

Buffer Overflow Attack (1) Malicious use of buffer overflow Input string contains byte representation of executable code Overwrite return address with address of buffer When bar() executes ret, will jump to exploit code. return address A void foo(){ bar();... } void bar() { char buf[64]; gets(buf);... } data written by gets() B B pad exploit code foo stack frame bar stack frame after call to gets() 11

Buffer Overflow Attack (2) Exploits based on buffer overflows Buffer overflow bugs allow remote machines to execute arbitrary code on victim machines. Internet worm Early versions of the finger server (fingerd) used gets() to read the argument sent by the client:» finger kildong@skku.edu Worm attacked fingerd server by sending phony argument:» finger exploit-code padding new-return-address» exploit code: executed a root shell on the victim machine with a direct TCP connection to the attacker. 12

Code Red Worm (1) History June 18, 2001. Microsoft announces buffer overflow vulnerability in IIS Internet server July 19, 2001. Over 250,000 machines infected by new virus in 9 hours. White house must change its IP address. Pentagon shut down public WWW servers for day. Received strings of form GET /default.ida?nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn...nnnn NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN%u9090%u6858%ucbd3%u780 1%u9090%u6858%ucbd3%u7801%u9090%u6858%ucbd3%u7801%u9090%u909 0%u8190%u00c3%u0003%u8b00%u531b%u53ff%u0078%u0000%u00=a HTTP/1.0" 400 325 "-" "-" 13

Code Red Worm (2) Code Red exploit code Starts 100 threads running Spread self Generate random IP addresses & send attack string Between 1 st & 19 th of month Denial of service attack to www.whitehouse.gov Send 98,304 packets; sleep for 4-1/2 hours; repeat Between 21 st & 27 th of month Deface server s home page After waiting 2 hours 14

Code Red Worm (3) Code Red effects Later version even more malicious Code Red II As of April 2002, over 18,000 machines infected Still spreading Paved way for NIMDA Variety of propagation methods One was to exploit vulnerabilities left behind by Code Red II 15

Nimda Worm Nimda (2001) Five different infection methods: Via e-mail Via open network shares Via browsing of compromised web sites Exploitation of various Microsoft IIS 4.0/5.0 directory traversal vulnerabilities Via back doors left behind by the Code Red II and Sadmind/IIS worms One of the most widespread virus/worm 16

SQL Slammer Worm SQL slammer (2003) Exploited two buffer overflow bugs in Microsoft s SQL Server and Desktop Engine. Infected 75,000 victims within 10 minutes Generate random IP addresses and send itself out to those addresses, slowing down Internet traffic dramatically. 1/25 nationwide Internet shutdown in South Korea 30 minutes after release 17

Avoiding Buffer Overflow Use library routines that limit string lengths fgets() instead of gets() strncpy() instead of strcpy() Don t use scanf() with %s conversion specification Use fgets() to read the string Or use %ns where n is a suitable integer /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ fgets(buf, 4, stdin); puts(buf); } 18

System-Level Protections Randomized stack offsets At start of program, allocate random amount of space on stack Makes it difficult for hacker to predict beginning of inserted code Executable space protection Mark certain areas of memory as non-executable Hardware assistance: Intel NX (No execute) bit AMD XD (execute Disable) bit 19

Summary Memory layout OS/machine dependent (including kernel version) Basic partitioning: stack, data, text, heap, DLL found in most machines Avoiding buffer overflow vulnerability Important to use library routines that limit string lengths Working with strange code Important to analyze nonstandard cases e.g., what happens when stack corrupted due to buffer overflow Helps to step through with GDB 20