Computer Security 2015. 2  x86 assembly  How did it go?  IceCTF  Lock picking  Survey to be sent out about lock picking sets  Bomblab  Nice job!

Slides:



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

Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
Smashing the Stack for Fun and Profit
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Machine-Level Programming V: Miscellaneous Topics Topics Buffer Overflow Linux Memory Layout Understanding Pointers Floating-Point Code CS 105 Tour of.
Today C operators and their precedence Memory layout
Carnegie Mellon 1 Machine-Level Programming V: Advanced Topics : Introduction to Computer Systems 8 th Lecture, Sep. 16, 2010 Instructors: Randy.
Machine-Level Programming V: Miscellaneous Topics Sept. 24, 2002 Topics Linux Memory Layout Understanding Pointers Buffer Overflow Floating Point Code.
Teaching Buffer Overflow Ken Williams NC A&T State University.
Machine-Level Programming V: Miscellaneous Topics Topics Linux Memory Layout Buffer Overflow Floating Point Code CS213.
– 1 – EECS213, S’08 Alignment Aligned Data Primitive data type requires K bytes Address must be multiple of K Required on some machines; advised on IA32.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. V – Miscellaneous Topics Today Buffer overflow Floating point code Next time Memory.
Introduction to x86 Assembly or “How does someone hack my laptop?”
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Machine Programming – IA32 memory layout and buffer overflow CENG331: Introduction to Computer Systems 7 th Lecture Instructor: Erol Sahin Acknowledgement:
Carnegie Mellon 1 This week Buffer Overflow  Vulnerability  Protection.
1 UCR Common Software Attacks EE 260: Architecture/Hardware Support for Security Slide credits: some slides from Dave O’halloran, Lujo Bauer (CMU) and.
Assembly, Stacks, and Registers Kevin C. Su 9/26/2011.
Functions & Activation Records Recursion
Buffer Overflow Computer Organization II 1 © McQuain Buffer Overflows Many of the following slides are based on those from Complete Powerpoint.
University of Washington Today Memory layout Buffer overflow, worms, and viruses 1.
Machine-Level Programming V: Miscellaneous Topics Topics Buffer Overflow Floating Point Code.
1 Machine-Level Programming V: Advanced Topics Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
Ithaca College 1 Machine-Level Programming IX Memory & buffer overflow Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter.
Machine-Level Programming V: Advanced Topics CS304
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.
Carnegie Mellon 1 Today Arrays  One-dimensional  Multi-dimensional (nested) Structures Buffer Overflow.
Smashing the Stack Overview The Stack Region Buffer Overflow
Buffer Overflows Many of the following slides are based on those from
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
Machine-Level Programming V: Miscellaneous Topics
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
1 Understanding Pointers Buffer Overflow. 2 Outline Understanding Pointers Buffer Overflow Suggested reading –Chap 3.10, 3.12.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming V: Buffer overflow Slides.
Machine-Level Programming V: Advanced Topics
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.
Machine-Level Programming Advanced Topics Topics Linux Memory Layout Buffer Overflow.
Machine-Level Programming V: Miscellaneous Topics Feb 10, 2004 Topics Linux Memory Layout Understanding Pointers Buffer Overflow Floating Point Code class09.ppt.
Machine-Level Programming V: Miscellaneous Topics
CS 3214 Computer Systems Godmar Back Lecture 7. Announcements Stay tuned for Project 2 & Exercise 4 Project 1 due Sep 16 Auto-fail rule 1: –Need at least.
Machine-Level Programming V: Miscellaneous Topics Topics Linux Memory Layout Understanding Pointers Buffer Overflow Floating-Point Code CS 105 Tour of.
Machine-Level Programming Advanced Topics Topics Linux Memory Layout Understanding Pointers Buffer Overflow.
Machine-Level Programming V: Miscellaneous Topics Feb 6, 2003 Topics Linux Memory Layout Understanding Pointers Buffer Overflow Floating Point Code class09.ppt.
@Yuan Xue Worm Attack Yuan Xue Fall 2012.
Machine-Level Programming V: Miscellaneous Topics Topics Buffer Overflow Floating Point Code class09.ppt.
Machine-Level Programming Advanced Topics Topics Buffer Overflow.
Carnegie Mellon 1 Machine-Level Programming V: Advanced Topics / : Introduction to Computer Systems 9 th Lecture, Sep. 25, 2012 Instructors:
1 Binghamton University Machine-Level Programming V: Advanced Topics CS220: Computer Systems II.
Carnegie Mellon 1 Machine-Level Programming V: Advanced Topics Slides courtesy of: Randy Bryant & Dave O’Hallaron.
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
Machine-Level Programming V: Buffer overflow
Instructor: Fatma CORUT ERGİN
The Hardware/Software Interface CSE351 Winter 2013
Machine-Level Programming V: Miscellaneous Topics
Machine Language V: Miscellaneous Topics Sept. 25, 2001
Instructors: Gregory Kesden
Machine-Level Programming V: Miscellaneous Topics
Buffer overflows Buffer overflows are possible because C does not check array boundaries Buffer overflows are dangerous because buffers for user input.
Machine-Level Programming X Memory & buffer overflow Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified.
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Machine-Level Programming V: Advanced Topics
Machine Level Representation of Programs (IV)
Machine-Level Programming V: Miscellaneous Topics
Machine-Level Programming V: Miscellaneous Topics October 2, 2008
Machine-Level Programming V: Advanced Topics
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming X Memory & buffer overflow Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified.
Machine-Level Programming V: Miscellaneous Topics
Return-to-libc Attacks
Presentation transcript:

Computer Security 2015

2  x86 assembly  How did it go?  IceCTF  Lock picking  Survey to be sent out about lock picking sets  Bomblab  Nice job!  Today: Basic Buffer Overflows  The Vulnerability  Protection

3  November, 1988  Internet Worm attacks thousands of Internet hosts.  How did it happen?

4  November, 1988  Internet Worm attacks thousands of Internet hosts.  How did it happen?  July, 1999  Microsoft launches MSN Messenger (instant messaging system).  Messenger clients can access popular AOL Instant Messaging Service (AIM) servers AIM server AIM client AIM client MSN client MSN server

5  August 1999  Mysteriously, Messenger clients can no longer access AIM servers.  Microsoft and AOL begin the IM war: ▪ AOL changes server to disallow Messenger clients ▪ Microsoft makes changes to clients to defeat AOL changes. ▪ At least 13 such skirmishes.  How did it happen?  The Internet Worm and AOL/Microsoft War were both based on stack buffer overflow exploits! ▪ many library functions do not check argument sizes. ▪ allows target buffers to overflow.

 Strings in C terminate at the first NUL-byte  Copying strings requires bounds checking! Buffer A Buffer B Int i void function(void) { char A[16]; char B[64]; int i; strcpy (B, “Antimony arsenic aluminum celenium“); strcpy (A, “AAAABBBBCCCCDDDD“); printf (“String B: %s\n“, B); } Antimony arsenic aluminum celenium0..AAAABBBBCCCCDDDD0ntimony arsenic aluminum celenium0.. AAAABBBBCCCCDDDD0Antimony arsenic aluminum celenium0

 C stores all variables on stack, but also other important stuff!  E.g. the address of where it was last executing (called the return address) void bla(void) { char A[16]; function(); } void function(void) { char B[64]; int i;... } Buffer A Buffer B Int i STACK Return address

 What happens if we write too much to B? Buffer A Buffer B Int i STACK Return address Could return to anywhere! somebuffer void function(void) { char B[64]; int i; strcpy (B, somebuffer); }

 Where would we want to return?  Could return to OUR input buffer  Treated as machine code! Can execute anything void bla(void) { char A[16]; function(); } void function(void) { char B[64]; int i;... } Buffer A Buffer B Int i STACK Return address somebuffer

 What do we want to execute?  Could eject CDROM or delete all files  Could launch a shell (say „/bin/bash“)  Could open a new port and launch a shell there  Could connect back to our host  Need to careful about the input  Such as NUL bytes  Known as shellcode

11  Implementation of Unix function gets()  No way to specify limit on number of characters to read  Similar problems with other library functions  strcpy, strcat : Copy strings of arbitrary length  scanf, fscanf, sscanf, when given %s conversion specification /* Get string from stdin */ char *gets(char *dest) { int c = getchar(); char *p = dest; while (c != EOF && c != '\n') { *p++ = c; c = getchar(); } *p = '\0'; return dest; }

12 void call_echo() { echo(); } /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } unix>./bufdemo Type a string: unix>./bufdemo Type a string: Segmentation Fault unix>./bufdemo Type a string: ABC Segmentation Fault

c5:55 push %ebp 80485c6:89 e5 mov %esp,%ebp 80485c8:53 push %ebx 80485c9:83 ec 14 sub $0x14,%esp 80485cc:8d 5d f8 lea 0xfffffff8(%ebp),%ebx 80485cf:89 1c 24 mov %ebx,(%esp) 80485d2:e8 9e ff ff ff call d7:89 1c 24 mov %ebx,(%esp) 80485da:e8 05 fe ff ff call 80483e df:83 c4 14 add $0x14,%esp 80485e2:5b pop %ebx 80485e3:5d pop %ebp 80485e4:c3 ret 80485eb:e8 d5 ff ff ff call 80485c f0:c9 leave 80485f1:c3 ret call_echo: echo:

14 echo: pushl %ebp# Save %ebp on stack movl %esp, %ebp pushl %ebx# Save %ebx subl $20, %esp# Allocate stack space leal -8(%ebp),%ebx# Compute buf as %ebp-8 movl %ebx, (%esp)# Push buf on stack call gets# Call gets... /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } Return Address Saved %ebp %ebp Stack Frame for main Stack Frame for echo [3][2][1][0] buf Before call to gets Saved %ebx

15 unix> gdb bufdemo (gdb) break echo Breakpoint 1 at 0x80485c9 (gdb) run Breakpoint 1, 0x80485c9 in echo () (gdb) print /x $ebp $1 = 0xffffd678 (gdb) print /x *(unsigned *)$ebp $2 = 0xffffd688 (gdb) print /x *((unsigned *)$ebp + 1) $3 = 0x80485f eb:e8 d5 ff ff ff call 80485c f0:c9 leave 0xffffd678 buf 0xffffd688 Return Address Saved %ebp Stack Frame for main Stack Frame for echo [3][2][1][0] Stack Frame for main Stack Frame for echo xx buf ff d f0 Before call to gets Saved %ebx

16 Overflow buf, and corrupt %ebx, but no problem Stack Frame for echo xx buf Stack Frame for echo buf Before call to gets Input xffffd678 0xffffd688 Stack Frame for main ff d f0 0xffffd678 0xffffd688 Stack Frame for main ff d f0 Saved %ebx

17 Base pointer corrupted Stack Frame for echo xx buf Stack Frame for echo buf Before call to gets Input eb:e8 d5 ff ff ff call 80485c f0:c9 leave # Set %ebp to corrupted value 80485f1:c3 ret 0xffffd678 0xffffd688 Stack Frame for main ff d f0 0xffffd678 0xffffd688 Stack Frame for main ff d f0 Saved %ebx

18 Return address corrupted Stack Frame for echo xx buf Stack Frame for echo buf Before call to gets Input eb:e8 d5 ff ff ff call 80485c f0:c9 leave # Desired return point 0xffffd678 0xffffd688 Stack Frame for main ff d f0 0xffffd678 0xffffd688 Stack Frame for main Saved %ebx

19  Input string contains byte representation of executable code  Overwrite return address A with address of buffer B  When bar() executes ret, will jump to exploit code int bar() { char buf[64]; gets(buf);... return...; } void foo(){ bar();... } Stack after call to gets() B return address A foo stack frame bar stack frame B exploit code pad data written by gets()

20  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  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.

21  Buffer overflow bugs allow remote machines to execute arbitrary code on victim machines  IM War  AOL exploited existing buffer overflow bug in AIM clients  exploit code: returned 4-byte signature (the bytes at some location in the AIM client) to server.  When Microsoft changed code to match signature, AOL changed signature location.

22 Date: Wed, 11 Aug :30: (PDT) From: Phil Bucking Subject: AOL exploiting buffer overrun bug in their own software! To: Mr. Smith, I am writing you because I have discovered something that I think you might find interesting because you are an Internet security expert with experience in this area. I have also tried to contact AOL but received no response. I am a developer who has been working on a revolutionary new instant messaging client that should be released later this year.... It appears that the AIM client has a buffer overrun bug. By itself this might not be the end of the world, as MS surely has had its share. But AOL is now *exploiting their own buffer overrun bug* to help in its efforts to block MS Instant Messenger..... Since you have significant credibility with the press I hope that you can use this information to help inform people that behind AOL's friendly exterior they are nefariously compromising peoples' security. Sincerely, Phil Bucking Founder, Bucking Consulting It was later determined that this originated from within Microsoft!

23  Starts 100 threads running  Spread self  Generate random IP addresses & send attack string  Between 1st & 19th of month  Attack  Send 98,304 packets; sleep for 4-1/2 hours; repeat ▪ Denial of service attack  Between 21st & 27th of month  Deface server’s home page  After waiting 2 hours

24 ... on skel :-)

25  char buff[1024]; ...  va_start(args, fmt);  vsnprintf(buff, sizeof (buff), fmt, args); va_end(args);  buff[sizeof (buff) - 1] = 0;  if ((n = strlen(buff)) > 0 && buff[n-1] != '\n') { buff[n++] = '\n'; buff[n++] = '\0';  }

26  /* * Join the two strings together, ensuring that the right thing * happens if the last component is empty, or the dirname is root. */ if (resolved[0] == '/' && resolved[1] == '\0') rootd = 1; else rootd = 0; if (*wbuf) { if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { errno = ENAMETOOLONG; goto err1; } if (rootd == 0) (void) strcat(resolved, "/"); (void) strcat(resolved, wbuf); }

27

28  Overflow bugs are ubiquitious  Suppose small probability p of writing buffer code wrong  Then p fraction of all code expected to have bugs ... and code bases are growing exponentially fast  There are languages that avoid overflows  E.g. Java, python,...  Not used as widely (plus they have different types of bugs)  Can we at least improve the status quo?

29  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); }

30  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  Non-executable code segments  In traditional x86, can mark region of memory as either “read-only” or “writeable” ▪ Can execute anything readable  X86-64 added explicit “execute” permission (NX) unix> gdb bufdemo (gdb) break echo (gdb) run (gdb) print /x $ebp $1 = 0xffffc638 (gdb) run (gdb) print /x $ebp $2 = 0xffffbb08 (gdb) run (gdb) print /x $ebp $3 = 0xffffc6a8

31  Idea  Place special value (“canary”) on stack just beyond buffer  Check for corruption before exiting function  GCC Implementation  -fstack-protector  -fstack-protector-all unix>./bufdemo-protected Type a string: unix>./bufdemo-protected Type a string:12345 *** stack smashing detected ***

d:55 push %ebp e:89 e5 mov %esp,%ebp :53 push %ebx :83 ec 14 sub $0x14,%esp :65 a mov %gs:0x14,%eax a:89 45 f8 mov %eax,0xfffffff8(%ebp) d:31 c0 xor %eax,%eax f:8d 5d f4 lea 0xfffffff4(%ebp),%ebx :89 1c 24 mov %ebx,(%esp) :e8 77 ff ff ff call 80485e a:89 1c 24 mov %ebx,(%esp) d:e8 ca fd ff ff call c :8b 45 f8 mov 0xfffffff8(%ebp),%eax : xor %gs:0x14,%eax c:74 05 je e:e8 a9 fd ff ff call c :83 c4 14 add $0x14,%esp :5b pop %ebx :5d pop %ebp :c3 ret echo:

33 echo:... movl%gs:20, %eax# Get canary movl%eax, -8(%ebp)# Put on stack xorl%eax, %eax # Erase canary... /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } Return Address Saved %ebp %ebp Stack Frame for main Stack Frame for echo [3][2][1][0] buf Before call to gets Saved %ebx Canary

34 echo:... movl-8(%ebp), %eax# Retrieve from stack xorl%gs:20, %eax# Compare with Canary je.L24# Same: skip ahead call__stack_chk_fail# ERROR.L24:... /* Echo Line */ void echo() { char buf[4]; /* Way too small! */ gets(buf); puts(buf); } Return Address Saved %ebp %ebp Stack Frame for main Stack Frame for echo [3][2][1][0] buf Before call to gets Saved %ebx Canary

35 (gdb) break echo (gdb) run (gdb) stepi 3 (gdb) print /x *((unsigned *) $ebp - 2) $1 = 0x3e37d00 Return Address Saved %ebp %ebp Stack Frame for main Stack Frame for echo [3][2][1][0] buf Before call to gets Saved %ebx 03e37d00 Return Address Saved %ebp %ebp Stack Frame for main Stack Frame for echo buf Input 1234 Saved %ebx 03e37d Benign corruption! (allows programmers to make silent off-by-one errors)

36  Worm: A program that  Can run by itself  Can propagate a fully working version of itself to other computers  Virus: Code that  Add itself to other programs  Cannot run independently  Both are (usually) designed to spread among computers and to wreak havoc

37  Making a program run arbitrary code  Known as shellcode  Questions we will address  How do we write shellcode?  What are the main restrictions?  How do we inject shellcode into a program?

38  You will now be writing rudimentary exploit code  Super cool! Even if you‘ve done it before  The goal:  Take a carefully crafted service  Overflow its buffers  Make it do different / new things  The assignment proceeds in stages, scores for each  Competitive scoreboard:  Deadline: Friday 9/25 11:59pm. Individual assignment.  Bonus points: Hack the service itself! (highest level)