Low-level Software Security: Attacks and Defenses
Úlfar Erlingsson
Microsoft Research, Silicon Valley and Reykjavík University, Iceland
FOSAD07 Low-level Software Security: Attacks and Defenses lfar - - PowerPoint PPT Presentation
FOSAD07 Low-level Software Security: Attacks and Defenses lfar Erlingsson Microsoft Research, Silicon Valley and Reykjavk University, Iceland An example of a real-world attack Exploits a vulnerability in the GDI+ rendering of
Microsoft Research, Silicon Valley and Reykjavík University, Iceland
FOSAD'07: Low-level Software Security 2
FOSAD'07: Low-level Software Security 3
FOSAD'07: Low-level Software Security 4
val addr
FOSAD'07: Low-level Software Security 5
FOSAD'07: Low-level Software Security 6
FOSAD'07: Low-level Software Security 7
FOSAD'07: Low-level Software Security 8
FOSAD'07: Low-level Software Security 9
FOSAD'07: Low-level Software Security 10
FOSAD'07: Low-level Software Security 11
FOSAD'07: Low-level Software Security 12
FOSAD'07: Low-level Software Security 13
FOSAD'07: Low-level Software Security 14
15 FOSAD'07: Low-level Software Security
FOSAD'07: Low-level Software Security 16
Java compiler C# compiler C++ compiler VB compiler
Executed on Executed on
17 FOSAD'07: Low-level Software Security
18
FOSAD'07: Low-level Software Security
19
class Secret { public Secret(int fv) { } public Set(int fv) { } } class Secret { private int f; public Secret(int fv) { f = fv; } public Set(int fv) { f = fv; } }
FOSAD'07: Low-level Software Security
20
FOSAD'07: Low-level Software Security
class Widget { // No checking of argument virtual void Operation(string s); … } class SecureWidget : Widget { // Validate argument and pass on // Could also authenticate the caller
Validate(s); base.Operation(s); } } … SecureWidget sw = new SecureWidget();
In particular, there are no buffer overruns that could somehow
References cannot be forged
21 FOSAD'07: Low-level Software Security
class Widget { // No checking of argument virtual void Operation(string s); … } class SecureWidget : Widget { // Validate argument and pass on // Could also authenticate the caller
Validate(s); base.Operation(s); } } … SecureWidget sw = new SecureWidget(); // We can avoid validation of Operation arguments, can‟t we? // // In IL (pre-2.0 2.0), ), make a d direct t // call on the supercl class ass: ldloc ldloc sw sw ldstr ldstr “Invalid string” call void Widget: t::Op :Oper erati ation
(stri ring ng)
22 FOSAD'07: Low-level Software Security
23 FOSAD'07: Low-level Software Security
Browser session to Web application Attacker session
FOSAD'07: Low-level Software Security 24
Victim browser application session
Rich data w/attack Rich data w/attack Rich data w/attack
Rich data that’s safe Rich data w/attack
FOSAD'07: Low-level Software Security 25
<B>Love Connection</B> <SCRIPT/chaff>code code</S\0CRIPT> <IMG SRC="  code code"> <DIV STYLE="background-image:\0075... 0075..."> <IMG SRC=„java Script:code code‟>
26 FOSAD'07: Low-level Software Security
FOSAD'07: Low-level Software Security 27
28 FOSAD'07: Low-level Software Security
29 FOSAD'07: Low-level Software Security
FOSAD'07: Low-level Software Security 30
FOSAD'07: Low-level Software Security 31
FOSAD'07: Low-level Software Security 32
FOSAD'07: Low-level Software Security 33
FOSAD'07: Low-level Software Security 34
FOSAD'07: Low-level Software Security 35
FOSAD'07: Low-level Software Security 36
FOSAD'07: Low-level Software Security 37
Can, e.g., happen due to integer-offset arithmetic errors
FOSAD'07: Low-level Software Security 38
C++ EH Frame C++ EH Frame
FOSAD'07: Low-level Software Security 39
FOSAD'07: Low-level Software Security 40
0xF00DFEED ; a secret, random cookie value
FOSAD'07: Low-level Software Security 41
FOSAD'07: Low-level Software Security 42
FOSAD'07: Low-level Software Security 43
FOSAD'07: Low-level Software Security 44
FOSAD'07: Low-level Software Security 45
Actually, attack works just as well if the structure is on the stack
FOSAD'07: Low-level Software Security 46
For example, localized, or case-insensitive
FOSAD'07: Low-level Software Security 47
FOSAD'07: Low-level Software Security 48
FOSAD'07: Low-level Software Security 49
FOSAD'07: Low-level Software Security 50
FOSAD'07: Low-level Software Security 51
FOSAD'07: Low-level Software Security 52
Possible control flow destination Safe code/data Possible control flow destination Safe code/data
x86 x86 RISC/NX RISC/NX x86/NX x86/NX x86/CFI x86/CFI
FOSAD'07: Low-level Software Security 53
Offset Table Directory
Directory Entry Page-Table Entry Physical Address
CR3 (PDPTR) 12 9 9 2 31 30 29 21 20 12 11 24 32
Page Directory Page Table 4-KByte Page Page-Directory- Pointer Table
Directory Pointer AVL NX P W U Page frame # Reserved AVL Reserved P W U Page frame #
PAE Page table entry on X86-64 PAE Page table entry on P6
FOSAD'07: Low-level Software Security 54
FOSAD'07: Low-level Software Security 55
Doesn’t always
Not worth the
Directory Entry
Page Directory Page Tables
Page-table entry
Code R/W Data Stack
Virt 100 Phys 123 : RO Virt 101 Phys 124 : RO Virt 200 Phys 456 : RW CR3
Base Register
Virt 300 Phys 789 : RW
Virt 101 Phys 124 : RO Virt 180 Phys 194 : RO
Virt 301 Phys 790 : RW
Code: Readable R/W Data: INVALID Stack: INVALID R/O Data: Readable
Code R/O Data Stack
Page Table Entries
FOSAD'07: Low-level Software Security 56
FOSAD'07: Low-level Software Security 57
FOSAD'07: Low-level Software Security 58
FOSAD'07: Low-level Software Security 59
FOSAD'07: Low-level Software Security 60
FOSAD'07: Low-level Software Security 61
FOSAD'07: Low-level Software Security 62
FOSAD'07: Low-level Software Security 63
FOSAD'07: Low-level Software Security 64
FOSAD'07: Low-level Software Security 65
FOSAD'07: Low-level Software Security 66
ntdll!_except1+0xC3: ... 8B E3 mov esp,ebx 5B pop ebx C3 ret kernel32!VirtualAlloc: ... C3 ret kernel32!InterlockedExchange: ... C3 ret kernel32!InterlockedExchange: ... C3 ret 89 64 46 C2 mov [esp+Ch],esp C3 ret ntdll!memcpy: ... C3 ret
Initial CFG violation trampolines from use of invalid function pointer and uses a set of executable bytes, from middle of a library function Allocate a page of executable virtual memory at fixed address Write some code to that start
Finish writing the code and return to it (at the fixed location) Copy the shellcode stack location to stack as the source arg for memcpy Copy shellcode from stack to the executable page, then return to it
FOSAD'07: Low-level Software Security 67
FOSAD'07: Low-level Software Security 68
FOSAD'07: Low-level Software Security 69
FOSAD'07: Low-level Software Security 70
FOSAD'07: Low-level Software Security 71
Generality of trampolines makes this a unarguable point
FOSAD'07: Low-level Software Security 72
FOSAD'07: Low-level Software Security 73
FOSAD'07: Low-level Software Security 74
FOSAD'07: Low-level Software Security 75
FOSAD'07: Low-level Software Security 76
FOSAD'07: Low-level Software Security 77
FOSAD'07: Low-level Software Security 78
Possible control flow destination Safe code/data Possible control flow destination Safe code/data
x86 x86 RISC/NX RISC/NX x86/NX x86/NX x86/CFI x86/CFI
FOSAD'07: Low-level Software Security 79
FOSAD'07: Low-level Software Security 80
FOSAD'07: Low-level Software Security 81
82
bool bool lt lt(in int x, x, int int y) y) { { re return turn x x < y y; } bool bool gt gt(in int x, x, int int y) y) { { re return turn x x > y y; } sort2(in sort2(int a[], t a[], int int b[ b[], , int int len len) { so sort( a rt( a, , len en, , lt lt ); ); so sort( b rt( b, , len en, , gt gt ); ); } lt():
ret 23 label 17
sort2():
call sort call sort label 55
sort():
call 17,R ret 55 label 23 ret …
gt():
ret 23 label 17 label 55
FOSAD'07: Low-level Software Security
83 ECX := Mem[ESP + 4] EDX := Mem[ESP + 8] ESP := ESP - 0x14 // ... push Mem[EDX + 4] push Mem[EDX] push ESP call ECX // ... EAX := Mem[ESP + 0x10] if EAX != 0 goto L EAX := Mem[ESP] L: ... and return
int int foo(fptr pf, int int* pm) { int int err; int int A[4];
// ...
pf(A, pm[0], pm[1]);
// ...
if if( err ) return return err; return return A[0]; }
FOSAD'07: Low-level Software Security
84 ECX := Mem[ESP + 4] EDX := Mem[ESP + 8] ESP := ESP - 0x14 // ... push Mem[EDX + 4] push Mem[EDX] push ESP cfiguard(ECX, cfiguard(ECX, pf_ID) pf_ID) call ECX // ... EAX := Mem[ESP + 0x10] if EAX != 0 goto L EAX := Mem[ESP] L: ... and return
int int foo(fptr pf, int int* pm) { int int err; int int A[4];
// ...
pf(A, pm[0], pm[1]);
// ...
if if( err ) return return err; return return A[0]; }
FOSAD'07: Low-level Software Security pf
// ... ... ... cfiguard(ECX, cfiguard(ECX, pf_ID) pf_ID) call ECX
pf
// ... ... ... EAX := 0x12345677 EAX := EAX + 1 if Mem[ECX-4] != EAX goto ERR call ECX
0x12345678
85 pf(A, pm[0], pm[1]); // ...
FOSAD'07: Low-level Software Security
86
FOSAD'07: Low-level Software Security
87 FOSAD'07: Low-level Software Security
Provides local storage for dynamic checks
88 FOSAD'07: Low-level Software Security
89 FOSAD'07: Low-level Software Security
90 FOSAD'07: Low-level Software Security
91 FOSAD'07: Low-level Software Security
92 FOSAD'07: Low-level Software Security
93 FOSAD'07: Low-level Software Security
FOSAD'07: Low-level Software Security 94
SPECINT 2K reference runs, XP SP2, Safe Mode w/CMD, Pentium 4, no HT, 1.8GHz
0% 20% 40% 60% 80% 100% 120% 140%
bzip2 crafty eon gap gcc gzip mcf parser twolf vortex vpr AVG CFI enforcement overhead
FOSAD'07: Low-level Software Security 95
FOSAD'07: Low-level Software Security 96
FOSAD'07: Low-level Software Security 97
FOSAD'07: Low-level Software Security 98
FOSAD'07: Low-level Software Security 99
FOSAD'07: Low-level Software Security 100
E.g., through address arithmetic or from stealing the low or high bits
FOSAD'07: Low-level Software Security 101
FOSAD'07: Low-level Software Security 102
FOSAD'07: Low-level Software Security 103
FOSAD'07: Low-level Software Security 104
Only need to fill in a handful of corner cases (e.g., EXE files)
ASLR changes only the concrete address values, not the encoding
105
106
107
FOSAD'07: Low-level Software Security 108
FOSAD'07: Low-level Software Security 109
FOSAD'07: Low-level Software Security 110
FOSAD'07: Low-level Software Security 111
FOSAD'07: Low-level Software Security 112
FOSAD'07: Low-level Software Security 113