buffer overflows a security interlude
play

Buffer overflows (a security interlude) IA32 Linux Memory - PowerPoint PPT Presentation

11/20/15 Buffer overflows (a security interlude) IA32 Linux Memory Layout ... FF ... How address space layout, the stack discipline, and C's lack of Stack


  1. 11/20/15 Buffer ¡overflows ¡(a ¡security ¡interlude) IA32 ¡Linux ¡Memory ¡Layout ... FF ... How ¡address ¡space ¡layout, ¡the ¡stack ¡discipline, ¡and ¡C's ¡lack ¡of ¡ Stack bounds-­‑checking ¡left ¡us ¡with ¡a ¡huge ¡problem. not ¡drawn ¡to ¡scale Caller Frame Arguments Frame ¡pointer Return ¡Addr %ebp Old ¡%ebp Saved Registers + Local Variables Heap Data Argument Stack ¡pointer Build T ext Upper ¡2 ¡hex ¡digits ¡ %esp 08 = ¡8 ¡bits ¡of ¡address 00 2 Buffer ¡Overflow ¡in ¡a ¡nutshell String ¡Library ¡Code Implementation ¡ of ¡Unix ¡function ¡ gets() Many ¡classic ¡Unix/Linux/C ¡functions ¡do ¡not ¡check ¡argument ¡ sizes. /* Get string from stdin */ char* gets(char* dest) { C ¡does ¡not ¡check ¡array ¡ bounds. int c = getchar(); char* p = dest; while (c != EOF && c != '\n') { pointer ¡to ¡start ¡of ¡an ¡array Allows ¡overflowing ¡(writing ¡ past ¡the ¡end ¡of) ¡buffers ¡(arrays) *p++ = c; c = getchar(); } Overflows ¡of ¡buffers ¡on ¡the ¡stack ¡overwrite ¡interesting ¡data. *p = '\0'; same ¡as: return dest; *p ¡= ¡c; } p++; Attackers ¡ just ¡choose ¡the ¡right ¡inputs. What ¡could ¡go ¡wrong ¡in ¡this ¡code? Common ¡type ¡of ¡security ¡vulnerability 3 4 1

  2. 11/20/15 String ¡Library ¡Code Vulnerable ¡Buffer ¡Code Implementation ¡ of ¡Unix ¡function ¡ gets() /* Echo Line */ void echo() { /* Get string from stdin */ char buf[4]; /* Way too small! */ char* gets(char* dest) { gets(buf); int c = getchar(); puts(buf); char* p = dest; } while (c != EOF && c != '\n') { *p++ = c; int main() { c = getchar(); printf("Type a string:"); } echo(); *p = '\0'; return 0; unix> ./bufdemo return dest; } Type a string: 1234567 } 1234567 unix>./bufdemo No ¡way ¡to ¡specify ¡limit ¡on ¡number ¡of ¡characters ¡to ¡read Type a string: 12345678 Segmentation Fault Similar ¡ problems ¡with ¡other ¡Unix ¡functions unix>./bufdemo strcpy : ¡Copies ¡string ¡of ¡arbitrary ¡length Type a string: 123456789ABC Segmentation Fault scanf , ¡ fscanf , ¡ sscanf , ¡ when ¡given ¡ %s conversion ¡specification 5 6 Buffer ¡Overflow ¡Disassembly Buffer ¡Overflow ¡Stack 080484f0 <echo>: Before ¡call ¡to ¡gets 80484f0: 55 push %ebp Stack ¡ Frame 80484f1: 89 e5 mov %esp,%ebp for ¡ main 80484f3: 53 push %ebx 80484f4: 8d 5d f8 lea 0xfffffff8(%ebp),%ebx /* Echo Line */ echo ¡code 80484f7: 83 ec 14 sub $0x14,%esp void echo() { Return ¡Address 80484fa: 89 1c 24 mov %ebx,(%esp) char buf[4]; /* Way too small! */ Saved ¡ %ebp 80484fd: e8 ae ff ff ff call 80484b0 <gets> %ebp gets(buf); puts(buf); 8048502: 89 1c 24 mov %ebx,(%esp) Saved ¡ %ebx } 8048505: e8 8a fe ff ff call 8048394 <puts@plt> [3] [2] [1] [0] buf 804850a: 83 c4 14 add $0x14,%esp 804850d: 5b pop %ebx echo: 804850e: c9 leave pushl %ebp # Save %ebp on stack 804850f: c3 ret movl %esp, %ebp buf pushl %ebx # Save %ebx caller ¡code 80485f2: e8 f9 fe ff ff call 80484f0 <echo> leal -8(%ebp),%ebx # Compute buf as %ebp-8 subl $20, %esp # Allocate stack space 80485f7: 8b 5d fc mov 0xfffffffc(%ebp),%ebx movl %ebx, (%esp) # Push buf addr on stack 80485fa: c9 leave call gets # Call gets 80485fb: 31 c0 xor %eax,%eax . . . 80485fd: c3 ret 7 8 2

  3. 11/20/15 Buffer ¡Overflow ¡Stack ¡Example Buffer ¡Overflow ¡Example ¡#1 Before ¡call ¡to ¡gets Input ¡“1234567” Before ¡call ¡to ¡gets Before ¡call ¡to ¡gets 0xffffc658 Stack ¡ Frame 0xffffc658 Stack ¡ Frame 0xffffc658 Stack ¡ Frame Stack ¡ Frame for ¡ main for ¡ main for ¡ main for ¡ main 08 04 85 f7 f7 85 04 08 08 04 85 f7 (Return ¡address) Return ¡Address 08 04 85 f7 58 c6 ff ff ff ff c6 58 ff ff c6 58 0xffffc638 0xffffc638 Saved ¡ %ebp ff ff c6 58 0xffffc638 Saved ¡ %ebx 00 37 36 35 Saved ¡ %ebx Saved ¡ %ebx (Saved ¡ %ebp ) xx xx xx xx 34 33 32 31 buf buf [3] [2] [1] [0] xx xx xx xx buf buf 0xffffc630 0xffffc630 0xffffc630 buf Overflow ¡ buf, ¡and ¡corrupt ¡saved ¡%ebx, 80485f2:call 80484f0 <echo> but ¡no ¡problem, ¡why? 80485f7:mov 0xfffffffc(%ebp),%ebx # Return Point What ¡happens ¡if ¡input ¡has ¡one ¡more ¡byte? 9 10 Buffer ¡Overflow ¡Example ¡#2 Buffer ¡Overflow ¡Example ¡#3 Input ¡“12345678” Before ¡call ¡to ¡gets Before ¡call ¡to ¡gets Input ¡“123456789ABC” 0xffffc658 Stack ¡ Frame 0xffffc658 Stack ¡ Frame 0xffffc658 Stack ¡ Frame 0xffffc658 Stack ¡ Frame for ¡ main for ¡ main for ¡ main for ¡ main (Return ¡address) 08 04 85 f7 f7 85 04 08 f7 85 04 08 08 04 85 f7 (Return ¡address) 08 04 85 f7 f7 85 04 08 f7 85 04 00 08 04 85 58 c6 ff ff ff ff c6 58 58 c6 ff 00 ff ff c6 0xffffc638 0xffffc638 ff ff c6 58 58 c6 ff ff 43 42 41 39 0xffffc638 0xffffc638 Saved ¡ %ebx 38 37 36 35 Saved ¡ %ebx (Saved ¡ %ebp ) 38 37 36 35 xx xx xx xx 34 33 32 31 (Saved ¡ %ebp ) buf buf xx xx xx xx 34 33 32 31 buf buf 0xffffc630 0xffffc630 0xffffc630 0xffffc630 Frame pointer ¡corrupted Return ¡address ¡corrupted . . . 804850a: 83 c4 14 add $0x14,%esp # deallocate space 080485f2: call 80484f0 <echo> Hmmm, ¡what ¡can ¡you ¡do ¡with ¡it? 804850d: 5b pop %ebx # restore %ebx 080485f7: mov 0xfffffffc(%ebp),%ebx # Return Point 804850e: c9 leave # movl %ebp, %esp; popl %ebp 804850f: c3 ret # Return 11 12 3

  4. 11/20/15 Malicious ¡Use ¡of ¡Buffer ¡Overflow Exploiting ¡Buffer ¡Overflows Stack ¡ after ¡ call ¡to ¡ gets() Buffer ¡overflow ¡bugs ¡allow ¡remote ¡ machines ¡to ¡execute ¡ arbitrary ¡ code ¡on ¡victim ¡ machines. void foo(){ foo stack ¡frame bar(); 1988: ¡Internet ¡worm ... return ¡ address A Early ¡versions ¡of ¡the ¡finger ¡server ¡daemon ¡(fingerd) ¡used ¡ gets() to ¡read ¡ } B ¡(was ¡A) the ¡argument ¡sent ¡by ¡the ¡client: int bar() { finger somebody@cs.wellesley.edu pad data ¡written char buf[64]; by ¡ gets() gets(buf); commandline facebook of ¡the ¡80s! bar stack ¡frame exploit ... code return ...; Attack ¡buffer ¡overflow ¡by ¡sending ¡phony ¡argument: B } finger “exploit-code padding new-return-address” ... Input ¡string ¡contains ¡byte ¡representation ¡of ¡executable ¡code 2015: ¡"Ghost" ¡ any ¡many ¡ more... Overwrite ¡return ¡address ¡A ¡with ¡address ¡of ¡buffer ¡(need ¡to ¡know ¡B) Standard ¡C ¡library ¡function ¡ gethostname() When ¡ bar() executes ret , ¡will ¡jump ¡to ¡exploit ¡code ¡(instead ¡of ¡A) exploitable ¡buffer ¡overflow 13 14 not ¡drawn ¡to ¡scale System-­‑Level ¡Protections Avoiding ¡Overflow ¡Vulnerability FF Stack Randomized ¡stack ¡offsets /* Echo Line */ void echo() { At ¡start ¡of ¡program, ¡allocate ¡random ¡amount ¡of ¡ char buf[4]; /* Way too small! */ space ¡on ¡stack fgets (buf, 4 , stdin); Makes ¡it ¡difficult ¡for ¡exploit ¡to ¡predict ¡beginning ¡ puts(buf); of ¡inserted ¡code } Techniques ¡to ¡ detect stack ¡corruption Use ¡library ¡ routines ¡that ¡ limit ¡string ¡lengths Nonexecutable code ¡segments fgets instead ¡of ¡ gets (second ¡argument ¡to ¡fgets sets ¡limit) strncpy instead ¡of ¡ strcpy Allow ¡code ¡to ¡execute ¡only ¡from ¡“text” ¡segment Don’t ¡use ¡ scanf with ¡ %s conversion ¡specification Do ¡NOT ¡execute ¡code ¡in ¡stack, ¡data, ¡or ¡heap Heap Use ¡ fgets to ¡read ¡the ¡string Hardware ¡support ¡needed Data Or ¡use ¡ %ns where ¡ n is ¡a ¡suitable ¡integer T ext 08 Other ¡ideas? 00 15 16 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend