Chapter 6 Weaknesses Exploited Weaknesses q Bad software
Chapter 6 Weaknesses Exploited
Weaknesses q Bad software is everywhere, and… q …flaws can cause security problems q In this chapter o o o Various overflow conditions Format string vulnerabilities How weaknesses are found Defenses Human factors
Technical Weaknesses q Buffer overflow q Process address space: 4 sections 1. Fixed-sized code block (code/text) 2. Static data (data) 3. Dynamic data (heap) 4. “Scratch paper” (stack)
Technical Weaknesses q. C program example
Stack Frame q Stack frame allocated for functions q Stack holds… o Local variables o Book keeping info, such as § Input arguments § Return address § Saved frame pointer, etc.
Stack Frame q Stack frame in action
Memory Organization q Text == code text q Data == static variables data q Heap == dynamic data q Stack == “scratch paper” heap o Dynamic local variables o Parameters to functions o Return address stack ¬ low address ¬ SP ¬ high address
Simplified Stack Example low : : void func(int a, int b){ char buffer[10]; } void main(){ func(1, 2); } buffer high ret a b ¬ SP ¬ return SP address ¬ SP
Smashing the Stack low q What happens if buffer overflows? : ? ? ? : q Program “returns” to wrong location q. A buffer crash is likely overflow ret overflow a high b ¬ SP ret… NOT! ¬ SP
Smashing the Stack q Trudy has a better idea… low : : q Code injection q Trudy can run code of her choosing… o On your machine! evil code high ¬ SP ret ¬ SP a b ¬ SP
Smashing the Stack q Trudy may not know… 1) Address of evil code 2) Location of ret on stack q Solutions 1) Precede evil code with NOP “landing pad” 2) Insert ret many times : : NOP evil code ret : : ¬ ret
Stack Smashing q Note that injected code is usually known as “shellcode” q Other overflow attacks are possible o Some inject code, some don’t o We discuss a few more examples later
Stack Smashing Summary A buffer overflow must exist in the code q Not all buffer overflows are exploitable q o Things must align just right If exploitable, attacker can inject code q Trial and error is likely required q o Fear not, lots of help available online o Smashing the Stack for Fun and Profit, Aleph One q Stack smashing is “attack of the decade” o Regardless of the decade… q Also heap overflow, integer overflow, etc.
Stack Smashing Example Program asks for a serial number that the attacker does not know q Attacker does not have source code q Attacker does have the executable (exe) q q Program quits on incorrect serial number
Example q By trial and error, attacker discovers apparent buffer overflow Note that 0 x 41 is “A” q Looks like ret overwritten by 2 bytes! q
Example q Next, q The disassemble bo. exe to find goal is to exploit buffer overflow to jump to address 0 x 401034
Example q Find that, in ASCII, 0 x 401034 is “@^P 4” Byte order is reversed? Why? q X 86 processors are “little-endian” q
Example q Reverse the byte order to “ 4^P@” and… Success! We’ve bypassed serial number check by exploiting a buffer overflow q What just happened? q o We overwrote the return address on the stack
Example q Note that in this example… q We overwrote return address and jumped to somewhere interesting q We did not inject any code q Other interesting places to jump to? o Without injecting code, that is? o Often called “return to libc” attacks
Example q Attacker did not require access to the source code q Only tool used was a disassembler to determine address to jump to q Possible to find desired address by trial and error? o Necessary if attacker does not have exe o For example, a remote attack
Example q Source q Flaw code of the buffer overflow easily found by attacker q Without the source code!
Stack Smashing Prevention q 1 st choice: employ non-executable stack o “No execute” NX bit (if available) o Seems like the logical thing to do, but some real code executes on the stack (Java, for example) q 2 nd choice: use safe languages (Java, C#) q 3 rd choice: use safer C functions o For unsafe functions, there are safer versions o For example, strncpy instead of strcpy
Stack Smashing Prevention low : : q Canary o Run-time stack check o Push canary onto stack o Canary value: buffer overflow canary overflow ret § Constant 0 x 000 aff 0 d § Or may depends on ret high a b ¬
Microsoft’s Canary q q Microsoft added buffer security check feature to C++ with /GS compiler flag Based on canary (or “security cookie”) Q: What to do when canary dies? A: Check for user-supplied “handler” q Handler shown to be subject to attack o Claims that attacker can specify handler code o If so, formerly “safe” buffer overflows become exploitable when /GS is used!
ASLR q Address Space Layout Randomization o Randomize place where code loaded in memory q q Makes most buffer overflow attacks probabilistic Vista uses 256 random layouts o So about 1/256 chance buffer overflow works? q Similar thing in Mac and other OSs q Attacks against Microsoft’s ASLR do exist o Possible to “de-randomize”
Buffer Overflow A major threat yesterday, today, and tomorrow q Can greatly reduced overflow attacks q o Use safe languages/safer functions o Educate developers, use tools, etc. q Buffer overflows will exist for a long time o Legacy code o Bad software development practices
Race Condition q Security processes should be atomic o Occur “all at once” q q Race conditions can arise when securitycritical process occurs in stages Attacker makes change between stages o Often, between stage that gives authorization, but before stage that transfers ownership q Example: prepaid debit card
Race Condition q Adding cash to card 1. User inserts card into card reader machine 2. Machine reads value of card: x 3. User insert cash into machine: y 4. User presses “enter” key 5. Machine writes x+y to card 6. Machine ejects card q Race condition?
Race Condition q Attacks on cash card protocol? q Insert 2 cards, sandwiched together q Card that is read has $100 value, unread card has $1 value o Step 2: Machine reads x = 100 q Insert $2, so y = 2 q Pull out read card, leaving unread one q Press “enter”…
Race Conditions q Race conditions appear to be common in software o May be more common than buffer overflows q But race conditions harder to exploit o Buffer overflow is “low hanging fruit” today q To prevent race conditions… o Make security-critical processes atomic o Occur all at once, not in stages q Not so easy to accomplish in practice
Heap Overflow q Heap used for dynamic variables o For example, malloc in C q Can overflow one array into another q Makes it possible to change data o Like example on next slide
Simple Buffer Overflow Consider boolean flag for authentication q Buffer overflow could overwrite flag allowing anyone to authenticate! q Boolean flag buffer F OU R S C q … T F In some cases, Trudy can be more systematic
Heap Overflow Example q BEFORE: o buf 2 = 2222 q AFTER: o buf 2 = 11122222
Heap Overflow q Bookkeeping info stored on heap q Can attacker exploit this?
Heap Overflow q Data structure to keep track of free memory o Assume it is a doubly-linked list q Heap overflow attacks?
Heap Overflow q Here we free block B q “Unlink” B from heap q If overflow in A, can overwrite B’s pointers…
Heap Overflow q Overwrite B’s pointers q Then free B q Now if we ever get to B, will go to shellcode
Integer Overflow q Many “integer” problems q This example… o What if len is negative? o Note that memcpy thinks len is unsigned
Format String Vulnerabilities q Format string example printf(“The magic number is %dn”, 42); q Format strings: Parameter Meaning Passed by… %d int value %u unsigned int value %x hex value %s string reference %n bytes written so far reference
Format Strings and the Stack q Formatting functions retrieve parameters from the stack o Assuming that’s where they’re stored… q Consider printf(“a has value %d at address %dn”, a, &a); q What if there are too few arguments? q For example printf(“a has value %d at address %dn”);
Format Strings and the Stack q Consider again printf(“a has value %d at address %dn”, a, &a); q Here, x 1 and x 2 are other things on the stack low : : “a has … n” a address of a x 1 x 2 high
Format Strings and the Stack q What if there are too few arguments? q For example printf(“a has value %d at address %dn”); q What happens? q Print stuff on stack q Is this useful? low : : “a has … n” x 1 high x 2 x 3 x 4
Format String Issue 1 q We can “walk” the stack q That is, print out items on the stack q For example printf(“%08 x %08 xn”); q As a bonus, it’s nicely formatted…
Format String Issue 2 q What would this do? printf(“%s%s%s%s%s%s”); q For each %s function printf will… o Fetch a number from the stack o Treat the number as an address o Print out whatever is at that address, until NULL character q Such an “address” might not exist!
Format String Example q What about something like this… void print_error(char *s){ char buffer[100]; snprintf(buffer, sizeof(buffer), “Error: %s”, s); printf(buffer); } q Suppose Trudy has control over what goes into the string s q Then some interesting possibilities…
q 1 st %d 2 nd %d : : %s return “Error: %s…” : : 1234567 %d high printf Suppose Trudy sets string s to x 78x 56x 34x 12 %d%d%d%s q Note x 78…x 12 is little endian for 1234567 q What does code on previous slide do? low %d : : buffer Format String Issue 3
Format String Issue 4 q The %n format is used to print the number of characters written so far q Q: What does this do? int i; printf(“abcde%n, &i); q A: Writes 5 to variable i q Can Trudy take advantage of this?
Format String Issue 4 q Similar attack as “issue 3”… q …except use %n in place of %s q Then a value written to address 1234567 o What value? q Some claim that this allows writing of arbitrary value o Is this really true?
Format String Defenses q Source code auditing o Relatively few format strings q Remove support for %n format o Would this create any problems? q Keep track of number of arguments q General buffer overflow prevention o For example, ASLR (next slide…)
More Defenses q Mentioned by author o NX approach o Canary o ASLR o Safe/safer languages
Finding Weaknesses q How do attackers find weaknesses? q Technical analysis o o o Study source code (if available) Disassemble executables (SRE) Decompile (good luck with that!) Black box analysis Study vendor patches Full disclosure websites q Zero day exploit?
Finding Weaknesses q Social engineering o Nuclear power plant company example q Impersonation q Dumpster diving q Shoulder surfing q Fake email o For example, ask for passwords q Phishing
Virus Hoaxes q Example: jdbgmgr. exe I found the little bear in my machine because of that I am sending this message in order for you to find it in your machine. The procedure is very simple: … q Known as the teddy bear virus because this is the icon:
Exploitation Engines q Developing a buffer overflow attack o Tedious, lots of trial and error o Until Metasploit was invented… q Metasploit o Knows about lots of attacks o Has lots of payloads o Doesn’t require much thought/effort
Metasploit q Payloads o o o o include Bind shell to current port Bind shell to arbitrary port Reverse shell Windows VNC Server DLL inject Reverse VNC DLL inject Inject DLL into running application Create local admin user The Meterpreter (run command of attacker’s choosing)
Metasploit Web Interface
Metasploit q Advantages for attackers? o Reduces “development cycle” o Resulting attacks much more reliable q Advantages for good guys? o Helps identify false positives o Help improve IDS o Improved penetration testing o Improved management awareness
- Slides: 57