Control Hijacking Attacks Note project 1 is out

  • Slides: 36
Download presentation
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

Control hijacking attacks Attacker’s goal: n Take over target machine, e. g. web server

Control hijacking attacks Attacker’s goal: n Take over target machine, e. g. web server w Execute arbitrary attack code on target by hijacking application control flow This lecture: three examples. n n n Buffer overflow attacks Integer overflow attacks Format string vulnerabilities

1. Buffer overflows Extremely common bug. n First major exploit: 1988 Internet Worm. fingerd.

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

What is needed Understanding C functions and the stack Some familiarity with machine code

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

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

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

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

What are buffer overflows? Suppose a web server contains a function: void func(char *str)

What are buffer overflows? Suppose a web server contains a function: void func(char *str) { char buf[128]; strcpy(buf, str); do-something(buf); } top of stack buf ret-addr str the stack When the function issfpinvoked looks like: *str ret str top of stack

Basic stack exploit Problem: no range checking in strcpy(). Suppose *str is such that

Basic stack exploit Problem: no range checking in strcpy(). Suppose *str is such that after strcpy top of stack looks *str like: ret Code for P stack Program P: exec( “/bin/sh” ) (exact shell code by Aleph One)

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, … ) “Safe” versions strncpy(), strncat() are misleading n n strncpy() may leave buffer unterminated. strncpy(), strncat() encourage off by 1 bugs.

Exploiting buffer overflows Suppose web server calls func() with given URL. n Attacker sends

Exploiting buffer overflows Suppose web server calls func() with given URL. n Attacker sends a 200 byte URL. Gets shell on web server Some complications: n n Program P should not contain the ‘’ character. Overflow should not crash program before func() exists. Sample remote buffer overflows of this type:

Control hijacking opportunities Stack smashing attack: n Override return address in stack activation record

Control hijacking opportunities Stack smashing attack: n Override return address in stack activation record by overflowing a local buffer variable. Function pointers: Bitmaps) n buf[128] (e. g. PHP 4. 0. 2, Heap MS Media. Player Func. Ptr or stack Overflowing buf will override function pointer.

Other types of overflow attacks Integer overflows: (e. g. MS Direct. X MIDI Lib)

Other types of overflow attacks Integer overflows: (e. g. MS Direct. X MIDI Lib) Phrack 60 void func(int a, char v) { char buf[128]; init(buf); buf[a] = v; } n Problem: a can point to `ret-addr’ on stack. Double free: double free space on heap. n Can cause mem mgr to write data to specific location

Integer overflow stats Source: NVD/CVE

Integer overflow stats Source: NVD/CVE

Finding buffer overflows To find overflow: Run web server on local machine n Issue

Finding buffer overflows To find overflow: Run web server on local machine n Issue requests with long tags All long tags end with “$$$$$” n If web server crashes, search core dump for “$$$$$” to find overflow location n Some automated tools exist (e. g. e. Eye Retina).

Defenses

Defenses

Preventing hijacking attacks 1. Fix bugs: n Audit software w Automated tools: Coverity, Prefast/Prefix.

Preventing hijacking attacks 1. Fix bugs: n Audit software w Automated tools: Coverity, Prefast/Prefix. n Rewrite software in a type safe languange (Java, ML) w Difficult for existing (legacy) code … 2. Concede overflow, but prevent code execution

Marking memory as non-execute (W^X) Prevent overflow code execution by marking stack and heap

Marking memory as non-execute (W^X) Prevent overflow code execution by marking stack and heap segments as nonexecutable n NX-bit on AMD Athlon 64, Prescott XD-bit on Intel P 4 w NX bit in every Page Table Entry (PTE) n Deployment: w Linux (via Pa. X project); Open. BSD w Windows since XP SP 2 (DEP) n Boot. ini : /noexecute=Opt. In or Always. On

Examples: DEP controls in Vista DEP terminating a program

Examples: DEP controls in Vista DEP terminating a program

Return to libc Control hijacking without executing code stack libc. so args ret-addr sfp

Return to libc Control hijacking without executing code stack libc. so args ret-addr sfp exec() printf() local buf “/bin/sh”

Response: randomization ASLR: n n (Address Space Layout Randomization) Map shared libraries to rand

Response: randomization ASLR: n n (Address Space Layout Randomization) Map shared libraries to rand location in process memory Attacker cannot jump directly to exec function Deployment: w Windows Vista: n 8 bits of randomness for DLLs aligned to 64 K page in a 16 MB region 256 choices w Linux (via Pa. X): 16 bits of randomness for libraries n More effective on 64 -bit architectures Other randomization methods:

ASLR Example Booting Vista twice loads libraries into different locations: Note: ASLR is only

ASLR Example Booting Vista twice loads libraries into different locations: Note: ASLR is only applied to images for which the dynamic-relocation flag is set

Run time checking

Run time checking

Run time checking: Stack. Guard Many many run-time checking techniques … n we only

Run time checking: Stack. Guard Many many run-time checking techniques … n we only discuss methods relevant to overflow protection Solution 1: Stack. Guard n n Run time tests for stack integrity. Embed “canaries” in stack frames and verify their Frame 1 top integrity. Frame prior 2 to function return. local canary sfp ret str of stack

Canary Types Random canary: n n Choose random string at program startup. Insert canary

Canary Types Random canary: n n 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. Terminator canary: Canary = 0, newline, linefeed, EOF n String functions will not copy beyond terminator.

Stack. Guard (Cont. ) Stack. Guard implemented as a GCC patch. n Program must

Stack. Guard (Cont. ) Stack. Guard implemented as a GCC patch. n Program must be recompiled. Minimal performance effects: 8% for Apache. Note: Canaries don’t offer fullproof protection. n Some stack smashing attacks leave canaries unchanged Heap protection: Point. Guard. n Protects function pointers and setjmp buffers by

Stack. Guard variants - Pro. Police n (IBM) - gcc 3. 4. 1. (-fstack-protector)

Stack. Guard variants - Pro. Police n (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

MS Visual Studio /GS [2003] Compiler /GS option: n n Combination of Pro. Police

MS Visual Studio /GS [2003] Compiler /GS option: n n Combination of Pro. Police and Random canary. Triggers Un. Handled. Exception in case of Canary mismatch to shutdown process. Litchfield vulnerability report n Overflow overwrites exception handler

Run time checking: Libsafe Solution 2: Libsafe (Avaya Labs) n n Dynamically loaded library.

Run time checking: Libsafe Solution 2: Libsafe (Avaya Labs) n n Dynamically loaded library. Intercepts calls to strcpy (dest, src) w Validates sufficient space in current stack frame: |frame-pointer – dest| > strlen(src) w If so, does strcpy, otherwise, terminates application sfp ret-addr libsafe dest src buf sfp ret-addr main top of stack

More methods … Stack. Shield n n n At function prologue, copy return address

More methods … Stack. Shield n n n 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)

Format string bugs

Format string bugs

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” ? ? n n n Most likely program will crash: Do. S. If not, program will print memory contents. Privacy? Full exploit using user = “%n”

History First exploit discovered in June 2000. Examples: n n wu-ftpd 2. * :

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

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

Exploit Dumping arbitrary memory: n Walk up stack until desired pointer is found. n

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

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 ); What if user = “%500 d <nops> <shellcode>”

THE END

THE END