0
如果此问题听起来很愚蠢,但我对shellcoding非常陌生,并且我试图获得一个适用于32位Linux机器的hello world示例。在C程序中运行hello world shellcode时发生Segfault
由于这是shellcoding,我使用了一些技巧来删除空字节并缩短代码。在这里,它是:下面的C代码中。然而
$ nasm -f elf print4.asm
$ ld -o print4 -m elf_i386 print4.o
,我试图运行它: $猫shellcodetest当我编译,并用下面的命令链接它
section .data
section .text
global _start
_start:
;Instead of xor eax,eax
;mov al,0x4
push byte 0x4
pop eax
;xor ebx,ebx
push byte 0x1
pop ebx
;xor ecx,ecx
cdq ; instead of xor edx,edx
;mov al, 0x4
;mov bl, 0x1
mov dl, 0x8
push 0x65726568
push 0x74206948
;mov ecx, esp
push esp
pop ecx
int 0x80
mov al, 0x1
xor ebx,ebx
int 0x80
此代码工作正常。 ç 的#include 的#include
char *shellcode = "\x04\x6a\x58\x66\x01\x6a\x5b\x66\x99\x66\x08\xb2\x68\x68\x68\x65\x69\x48\x54\x66\x59\x66\x80\xcd\x01\xb0\x31\x66\xcd\xdb\x80";
int main(void) {
(*(void(*)()) shellcode)();
}
$ gcc shellcodetest.c –m32 –z execstack -o shellcodetest
$ ./shellcodetest
Segmentation fault (core dumped)
可能有人请解释一下发生了什么呢?我试着在gdb中运行代码,注意到esp发生了一些奇怪的事情。但正如我之前所说,我仍然缺乏真正理解这里发生的事情的经验。
在此先感谢!
您正在将指针值作为代码执行,而不是它指向的字符串字面值。使用'const char shellcode [] =“...”'。它必须是'const',因此它进入'.rodata'部分,该部分进入文本段(读取+执行),而不是'.data'部分(数据段=读取+写入)。 –
嘿,那里,如果我错了,纠正我,但[此链接](https://stackoverflow.com/questions/16626857/shellcode-in-c-program)解释说,字节码实际上投射到指向函数不带参数()。所以我认为十六进制字符串在AX的.text部分结束。而且,LarsH设法通过改变字节序来在C程序中运行shellcode(我觉得没有看到那么...)。 – warsang
是的,我的评论是错误的。当你编写'char * shellcode'时,'C shellcode'从内存中加载指针,而指向字符串的文本本身在'.rodata'中。尽管:调用eax(甚至是调用rel32)而不是调用[absolute_address_of_pointer]'可以节省一定程度的间接使用const char shellcode []。 (并且'char shellcode []'(不是const)会崩溃)。 –