Buffer Overflow. Jo, Heeseung

Similar documents
BUFFER OVERFLOW. Jo, Heeseung

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

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

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

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

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

Giving credit where credit is due

Sungkyunkwan University

Machine-Level Programming IV: Structured Data

Machine-Level Prog. V Miscellaneous Topics

Machine- Level Programming V: Advanced Topics

Machine Programming 5: Buffer Overruns and Stack Exploits

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

Machine-Level Programming V: Advanced Topics

CMPSC 497 Buffer Overflow Vulnerabilities

Buffer Overflows. CSE 351 Autumn Instructor: Justin Hsia

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

CSC 252: Computer Organization Spring 2018: Lecture 9

Buffer Overflows. CSE 351 Autumn 2018

Machine-Level Programming V: Advanced Topics

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

CS241 Computer Organization Spring Buffer Overflow

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

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

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

Program Security and Vulnerabilities Class 2

Buffer Overflow Attacks

Buffer-Overflow Attacks on the Stack

Buffer-Overflow Attacks on the Stack

X86 Assembly Buffer Overflow III:1

Lecture 08 Control-flow Hijacking Defenses


CSE 509: Computer Security

Project 1 Buffer Overflow

Machine- Level Programming V: Advanced Topics

Fundamentals of Computer Security

20: Exploits and Containment

Secure Systems Engineering

Assembly Language: Function Calls

CS , Fall 2002 Exam 1

Assembly Language: Function Calls" Goals of this Lecture"

Buffer Overflows Defending against arbitrary code insertion and execution

Buffer Overflow Attack

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Assembly I: Basic Operations. Jo, Heeseung

CPEG421/621 Tutorial

THEORY OF COMPILATION

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

buffer overflow exploitation

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

CS 161 Computer Security

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

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

COMP 2355 Introduction to Systems Programming

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

Software Security: Buffer Overflow Defenses

Machine-Level Programming V: Advanced Topics

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

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

Topics in Software Security Vulnerability

Basic Buffer Overflows

CS , Fall 2004 Exam 1

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

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

Assembly Language Programming - III

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

Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

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

Machine-Level Programming V: Advanced Topics

Prac*ce problem on webpage

(Early) Memory Corruption Attacks

Software Security: Buffer Overflow Attacks (continued)

COMP 3704 Computer Security

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

Machine Programming 1: Introduction

Lab 10: Introduction to x86 Assembly

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

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

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

CSE 565 Computer Security Fall 2018

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Introduction Presentation A

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

CSE 361S Intro to Systems Software Lab Assignment #4

Transcription:

Buffer Overflow Jo, Heeseung

IA-32/Linux Memory Layout Heap Runtime stack (8MB limit) Dynamically allocated storage When call malloc(), calloc(), new() DLLs (shared libraries) Data Text Dynamically linked libraries Library routines (e.g., printf, gets) Linked into object code when first executed Statically allocated data e.g., arrays & strings declared in code 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

IA-32/Linux Memory Layout Where are the variables located? FF int e=7; Kernel int main() { int i = 3; static int k; char c[256]; char p*; Upper 2 hex digits of address C0 BF p=(char *)malloc(256); printf("%p\n", p); i=a(); 80 7F Heap } exit(0); int b=5; int a() { int i=4; return b; } 40 3F 08 00 DLLs Heap Data Text 3

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

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

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 6

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 7

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 = 0x0804864d for main Return 08 04 Address 86 4d bfsaved ff %ebpf8 %ebp=0xbffff8f8 [3] xx [2] xx [1] xx [0] xx buf for echo return addr. 08048648: call 804857c <echo> 0804864d: movl $0, %eax # Return Point 8

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

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 bfsaved ff 00 %ebp35 [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 -4(%ebp),%eax 8048608: movl %eax,(%esp) 804860b: call 80482d4 # puts 8048610: leave # movl %ebp, %esp; popl %ebp 8048611: ret 10

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 38Saved 37 36 %ebp35 [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 804857c <echo> 804864d: mov $0,%eax # Return Point 11

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() 12

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@jbnu.ac.kr 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 13

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...nnnnnnnnnn NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN%u9090%u6858%ucbd3%u7801%u9090%u6858%uc bd3%u7801%u9090%u6858%ucbd3%u7801%u9090%u9090%u8190%u00c3%u0003%u8b00% u531b%u53ff%u0078%u0000%u00=a HTTP/1.0" 400 325 "-" "-" 14

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

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 16

Nimda Worm Nimda (2001) Five different infection methods: - Via e-mail - Via open network shares Window XP SP1 version - 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 17

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 18

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); } 19

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 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 Executable space protection Mark certain areas of memory as non-executable Hardware assistance: - Intel NX (No execute) bit - AMD XD (execute Disable) bit 20

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 Why does not eliminate vulnerable functions? 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 21