Vlatko Kosturjak (@k0st), DORS/CLUC 2018, Zagreb, 20th of April, 2018
15 minutes
#include <stdio.h>
int main (int argc, char *argv[]) {
char buffer[1024];
strcpy (buffer,argv[1]);
printf (buffer);
}
strcpy (buffer,argv[1]);
printf (buffer);
// TODO: Will add check later
$ gdb -q --args ./ex2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Reading symbols from ./ex2...(no debugging symbols found)...done.
(gdb) r
Starting program: /dc2018/ex2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Program received signal SIGSEGV, Segmentation fault.
0x0000414141414141 in ?? ()
(gdb) i r
rax 0x1e 30
rdi 0x60202e 6299694
rbp 0x4141414141414141 0x4141414141414141
rsp 0x7fffffffdd70 0x7fffffffdd70
[...]
rip 0x414141414141 0x414141414141
gs 0x0 0
Feature | Controllable | Description |
---|---|---|
ASLR | randomize_va_space | Address space Randomization |
NX | noexec=on noexec=off |
Not executable memory pages |
Flag | Description | Prevention |
---|---|---|
-z noexecstack | Marks stack as non executable | executing shellcode on stack |
-fstack-protector-all -fstack-protector-strong |
Adds stack canaries | prevents function return overwritting |
-D_FORTIFY_SOURCE=2 | Add extra check to dang f() | prevents buffer overfows |
Flag | Description | Prevention |
---|---|---|
-Wl,-z,relro | linker marks sections read only | prevents GOT overwrite attacks |
-Wl,-z,now | all symbols are resolved at load time | prevents GOT overwrite attacks |
fPIE -pie -fPIC |
generate position independent code/executable | randomize address space (ASLR) |
-fsanitize=cfi
-fcf-protection=full -mcet
-mcet
typedef struct {
char name[16];
char password[18];
char privilege;
char description[20];
} person;
person tmp;
strcpy (tmp.name,username);
clang-3.9 ex7.c -Wl,-z,now -Wl,-z,relro -fpie -pie -fstack-protector-strong -D_FORTIFY_SOURCE=2 -O2 -fvisibility=hidden -flto -fsanitize=cfi -o ex7
clang-3.9 ex7.c -Wl,-z,now -Wl,-z,relro -fpie -pie -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -fvisibility=hidden -flto -fsanitize=cfi -o ex7
$ nm ex7-prot | grep handle_cfi_check_fail
0000000000104a40 T __ubsan_handle_cfi_check_fail
0000000000104a90 T __ubsan_handle_cfi_check_fail_abort
$ nm ex7 | grep handle_cfi_check_fail
exit 1
#include <stdio.h>
int main (int argc, char *argv[]) {
char buffer[10];
strncpy (buffer,argv[1], 10);
printf ("%s",buffer);
}
-fsanitize=address