Currently I am studying system exploit, and find some interesting system exploit called buffer overflow using shellcode. I wrote shellcode terminating current process using exit(0) systemcall. Below, there is my code.
#include <stdio.h>
char shell[100] =
"\xb0\x01" // mov al, 1
"\x31\xdb" // xor ebx, ebx
"\xcd\x80" ; // int 0x80
int main() {
((void(*)())shell)();
printf("this not should be printed\n");
return 0;
}
After writing code above, I compiled with gcc using -m32 flag for compiling x86 architecture(My CPU Architecture is AMD x86_64), and using -fno-stack-protector, -z execstack to make .data
, .rodata
, and stack
section executable.
So the command for compiling is like this.
gcc -m32 -fno-stack-protector -z execstack -o shellcode shellcode.c
.
And after that, I execute the test program (shellcode.c), but, result says there is segmentation fault (Even having compiled with memory protection!!). Here is the result:
zsh: segmentation fault ./shellcode
So, I double-checked shellcode binary using checksec, readelf. First readelf says .rodata, .data, .bss section is unexecutable. Like this.
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x00160 0x00160 R 0x4
INTERP 0x000194 0x08048194 0x08048194 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x002e8 0x002e8 R 0x1000
LOAD 0x001000 0x08049000 0x08049000 0x0022c 0x0022c R E 0x1000
LOAD 0x002000 0x0804a000 0x0804a000 0x00190 0x00190 R 0x1000
LOAD 0x002f0c 0x0804bf0c 0x0804bf0c 0x00198 0x0019c RW 0x1000
DYNAMIC 0x002f14 0x0804bf14 0x0804bf14 0x000e8 0x000e8 RW 0x4
NOTE 0x0001a8 0x080481a8 0x080481a8 0x00044 0x00044 R 0x4
GNU_EH_FRAME 0x002024 0x0804a024 0x0804a024 0x00044 0x00044 R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x10
GNU_RELRO 0x002f0c 0x0804bf0c 0x0804bf0c 0x000f4 0x000f4 R 0x1
As you can see above, stack became executable. But, not .data
, .rodata
, and .bss
section.
Below there is extra information about memory protection of shellcode (using checksec).
ELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX disabled No PIE No RPATH No RUNPATH 65) Symbols No 0 0 ./shellcode
As you can see, there is no stack-canary, NX Bit set... But, shellcode
program continues to segmentation fault.... What is Wrong with this...??/??
P.S I' am using kali linux 2021.02