Скачать презентацию Control Hijacking Attacks Note project 1 is out Скачать презентацию Control Hijacking Attacks Note project 1 is out

cb77502175cb74f8da0fc5c03a059f55.ppt

  • Количество слайдов: 34

Control Hijacking Attacks Note: project 1 is out Section this Friday 4: 15 pm Control Hijacking Attacks Note: project 1 is out Section this Friday 4: 15 pm 1

Control hijacking attacks u Attacker’s goal: • Take over target machine, e. g. web Control hijacking attacks u Attacker’s goal: • Take over target machine, e. g. web server – Execute arbitrary attack code on target by hijacking application control flow u This lecture: three examples. • Buffer overflow attacks • Integer overflow attacks • Format string vulnerabilities u Project 1: Build exploits 2

1. Buffer overflows u Extremely common bug. • First major exploit: 1988 Internet Worm. 1. Buffer overflows u Extremely common bug. • First major exploit: 1988 Internet Worm. fingerd. 20% of all vuln. 2005 -2007: 10% Source: NVD/CVE u Developing buffer overflow attacks: • Locate buffer overflow within an application. • Design an exploit. 3

What is needed u Understanding C functions and the stack. u Some familiarity with What is needed u Understanding C functions and the stack. u Some familiarity with machine code. u Know how systems calls are made. u The exec() system call. u Attacker needs to know which CPU and OS are running on the target machine. • Our examples are for x 86 running Linux. • Details vary slightly between CPUs and OSs: – Little endian vs. big endian (x 86 vs. Motorola) – Stack Frame structure (Linux vs. Windows) – Stack growth direction. 4

Linux process memory layout 0 x. C 0000000 User Stack %esp Shared libraries 0 Linux process memory layout 0 x. C 0000000 User Stack %esp Shared libraries 0 x 40000000 brk Run time heap Loaded from exec Unused 0 x 08048000 0 5

Stack Frame Parameters Return address Stack Frame Pointer Local variables SP Stack Growth 6 Stack Frame Parameters Return address Stack Frame Pointer Local variables SP Stack Growth 6

What are buffer overflows? u Suppose a web server contains a function: void func(char What are buffer overflows? u Suppose a web server contains a function: void func(char *str) { char buf[128]; strcpy(buf, str); do-something(buf); } u When the function is invoked the stack looks like: buf sfp ret-addr str top of stack u What if *str is 136 bytes long? After strcpy: *str ret str top of stack 7

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

Many unsafe C lib functions strcpy (char *dest, const char *src) strcat (char *dest, Many unsafe C lib functions strcpy (char *dest, const char *src) strcat (char *dest, const char *src) gets (char *s) scanf ( const char *format, … ) u “Safe” versions strncpy(), strncat() are misleading • strncpy() may leave buffer unterminated. • strncpy(), strncat() encourage off by 1 bugs. 9

Exploiting buffer overflows u Suppose web server calls func() with given URL. • Attacker Exploiting buffer overflows u Suppose web server calls func() with given URL. • Attacker sends a 200 byte URL. Gets shell on web server. u Some complications: • Program P should not contain the ‘’ character. • Overflow should not crash program before func() exists. u Sample remote buffer overflows of this type: • (2005) Overflow in MIME type field in MS Outlook. • (2005) Overflow in Symantec Virus Detection Set test = Create. Object("Symantec. Sym. VAFile. Query. 1") test. Get. Private. Profile. String "file", [long string] 10

Control hijacking opportunities u Stack smashing attack: • Override return address in stack activation Control hijacking opportunities u Stack smashing attack: • Override return address in stack activation record by overflowing a local buffer variable. u Function pointers: (e. g. PHP 4. 0. 2, MS Media. Player Bitmaps) buf[128] Func. Ptr Heap or stack • Overflowing buf will override function pointer. u Longjmp buffers: longjmp(pos) (e. g. Perl 5. 003) • Overflowing buf next to pos overrides value of pos. 11

Other types of overflow attacks u. Integer overflows: (e. g. MS Direct. X MIDI Other types of overflow attacks u. Integer overflows: (e. g. MS Direct. X MIDI Lib) Phrack 60 void func(int a, char v) { char buf[128]; init(buf); buf[3*a+1] = v; } • Problem: 3*a+1 can point to `ret-addr’ on stack. u. Double free: double free space on heap. • Can cause memory mgr to write data to specific locations. • Examples: CVS server 12

Integer overflow stats Source: NVD/CVE 13 Integer overflow stats Source: NVD/CVE 13

Finding buffer overflows u. To find overflow: • Run web server on local machine. Finding buffer overflows u. To find overflow: • Run web server on local machine. • Issue requests with long tags. All long tags end with “$$$$$”. • If web server crashes, search core dump for “$$$$$” to find overflow location. u. Some automated tools exist. (e. g. e. Eye Retina). u. Then use disassemblers and debuggers (e. . g IDA-Pro) to construct exploit. 14

Defenses 15 Defenses 15

Preventing hijacking attacks 1. Fix bugs: • Audit software. – Automated tools: Coverity, Prefast/Prefix. Preventing hijacking attacks 1. Fix bugs: • Audit software. – Automated tools: Coverity, Prefast/Prefix. (next lecture) • Rewrite software in a type safe languange (Java, ML) – Difficult for existing (legacy) code … 2. Concede overflow, but prevent code execution 3. Add runtime code to detect overflows exploits • Halt process when overflow exploit detected • Stack. Guard, Lib. Safe, … 16

Marking memory as non-execute (W^X) u Prevent overflow code execution by marking stack and Marking memory as non-execute (W^X) u Prevent overflow code execution by marking stack and heap segments as non-executable • NX-bit on AMD Athlon 64, XD-bit on Intel P 4 Prescott – NX bit in every Page Table Entry (PTE) • Deployment: – Linux (via Pa. X project); Open. BSD – Windows since XP SP 2 (DEP) • Boot. ini : /noexecute=Opt. In or Always. On u Limitations: • Some apps need executable heap (e. g. JITs). • Does not defend against `return-to-libc’ exploit 17

Return to libc u Control hijacking without executing code stack libc. so arg ret-addr Return to libc u Control hijacking without executing code stack libc. so arg ret-addr sfp exec() printf() … local_buf “/bin/sh” 18

Response: randomization u ASLR: Randomize location of libc. • Randomize at process creation time Response: randomization u ASLR: Randomize location of libc. • Randomize at process creation time • Attacker cannot jump directly to exec function. • Deployment: – Linux (via Pa. X): – Windows Vista: 16 bits of randomness for libraries 8 bits of randomness for libraries • More effective on 64 -bit architectures u Other randomization methods: • Sys-call randomization: randomize sys-call id’s • Instruction Set Randomization (ISR) 19

Run time checking 20 Run time checking 20

Run time checking: Stack. Guard u Many many run-time checking techniques … • Here, Run time checking: Stack. Guard u Many many run-time checking techniques … • Here, only discuss methods relevant to overflow protection. u Solutions 1: Stack. Guard (Wire. X) • Run time tests for stack integrity. • Embed “canaries” in stack frames and verify their integrity prior to function return. Frame 2 local canary sfp ret str Frame 1 local canary sfp ret str top of stack 21

Canary Types u Random canary: • Choose random string at program startup. • Insert Canary Types u 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. u Terminator canary: Canary = 0, newline, linefeed, EOF • String functions will not copy beyond terminator. • Hence, attacker cannot use string functions to corrupt stack. 22

Stack. Guard (Cont. ) u Stack. Guard implemented as a GCC patch. • Program Stack. Guard (Cont. ) u Stack. Guard implemented as a GCC patch. • Program must be recompiled. u Minimal performance effects: 8% for Apache. u Note: Canaries don’t offer fullproof protection. • Some stack smashing attacks can leave canaries untouched. u Heap protection: Point. Guard. • Protects function pointers and setjmp buffers by encrypting them: XOR with random cookie. • More noticeable performance effects. 23

Stack. Guard variants - Pro. Police u. Pro. Police (IBM) - gcc 3. 4. Stack. Guard variants - Pro. Police u. Pro. Police (IBM) - gcc 3. 4. 1. (-fstack-protector) • Rearrange stack layout to prevent ptr overflow. String Growth args No arrays or pointers ret addr SFP CANARY Stack Growth arrays Local variables Ptrs, but no arrays 24

Windows XP SP 2 /GS Compiler /GS option: • Combination of Pro. Police and Windows XP SP 2 /GS Compiler /GS option: • Combination of Pro. Police and Random canary. • Triggers Un. Handled. Exception in case of Canary mismatch to shutdown process. u Litchfield vulnerability report. • Overflow overwrites exception handler. • Redirects exception to attack code. 25

Run time checking: Libsafe u Solutions 2: Libsafe (Avaya Labs) • Dynamically loaded library. Run time checking: Libsafe u 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. sfp ret-addr libsafe dest src buf sfp ret-addr top of stack main 26

More methods … u. Stack. Shield • At function prologue, copy return address RET More methods … u. Stack. Shield • 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) 27

Format string bugs 28 Format string bugs 28

Format string problem int func(char *user) { fprintf( stdout, user); } Problem: what if Format string problem int func(char *user) { fprintf( stdout, user); } Problem: what if user = “%s%s%s%s” ? ? • Most likely program will crash: Do. S. • If not, program will print memory contents. Privacy? • Full exploit using user = “%n” Correct form: int func(char *user) { fprintf( stdout, “%s”, user); } 29

History u First exploit discovered in June 2000. u Examples: • wu-ftpd 2. * History u First exploit discovered in June 2000. u Examples: • wu-ftpd 2. * : remote root. • Linux rpc. statd: remote root • IRIX telnetd: remote root • BSD chpass: local root 30

Vulnerable functions Any function using a format string. Printing: printf, fprintf, sprintf, … vprintf, Vulnerable functions Any function using a format string. Printing: printf, fprintf, sprintf, … vprintf, vfprintf, vsprintf, … Logging: syslog, err, warn 31

Exploit u Dumping arbitrary memory: • Walk up stack until desired pointer is found. Exploit u Dumping arbitrary memory: • Walk up stack until desired pointer is found. • printf( “%08 x. %08 x|%s|”) u Writing to arbitrary memory: • printf( “hello %n”, &temp) -- writes ‘ 6’ into temp. • printf( “%08 x. %n”) 32

Overflow using format string char errmsg[512], outbuf[512]; sprintf (errmsg, “Illegal command: %400 s”, user); Overflow using format string char errmsg[512], outbuf[512]; sprintf (errmsg, “Illegal command: %400 s”, user); sprintf( outbuf, errmsg ); u What if user = “%500 d ” • Bypass “%400 s” limitation. • Will ovreflow outbuf. 33

THE END 34 THE END 34