2017-10-05 87 views
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发生了一些奇怪的事情。但正如我之前所说,我仍然缺乏真正理解这里发生的事情的经验。

在此先感谢!

+0

您正在将指针值作为代码执行,而不是它指向的字符串字面值。使用'const char shellcode [] =“...”'。它必须是'const',因此它进入'.rodata'部分,该部分进入文本段(读取+执行),而不是'.data'部分(数据段=读取+写入)。 –

+0

嘿,那里,如果我错了,纠正我,但[此链接](https://stackoverflow.com/questions/16626857/shellcode-in-c-program)解释说,字节码实际上投射到指向函数不带参数()。所以我认为十六进制字符串在AX的.text部分结束。而且,LarsH设法通过改变字节序来在C程序中运行shellcode(我觉得没有看到那么...)。 – warsang

+0

是的,我的评论是错误的。当你编写'char * shellcode'时,'C shellcode'从内存中加载指针,而指向字符串的文本本身在'.rodata'中。尽管:调用eax(甚至是调用rel32)而不是调用[absolute_address_of_pointer]'可以节省一定程度的间接使用const char shellcode []。 (并且'char shellcode []'(不是const)会崩溃)。 –

回答

0

你的shellcode不起作用,因为它没有输入正确的字节顺序。您没有说明如何从文件print4中提取字节,但objdumpxxd都以正确的顺序给出字节。

$ xxd print4 | grep -A1 here 
0000060: 6a04 586a 015b 99b2 0868 6865 7265 6848 j.Xj.[...hherehH 
0000070: 6920 7454 59cd 80b0 0131 dbcd 8000 2e73 i tTY....1.....s 
$ objdump -d print4 

print4:  file format elf32-i386 


Disassembly of section .text: 

08048060 <_start>: 
8048060:  6a 04     push $0x4 
8048062:  58      pop %eax 
8048063:  6a 01     push $0x1 
... 

你需要做的改变是交换字节顺序, '\ X04 \ X6A' - > '\ X6A \ X04'。 当我用这个改变运行你的代码时,它可以工作!

$ cat shellcodetest.c 
char *shellcode = "\x6a\x04\x58\x6a\x01\x5b\x99\xb2\x08\x68\x68\x65\x72\x65\x68\x48\x69\x20\x74\x54\x59\xcd\x80\xb0\x01\x31\xdb\xcd\x80"; 
int main(void) { 
     (*(void(*)()) shellcode)(); 
} 
$ gcc shellcodetest.c -m32 -z execstack -o shellcodetest 
$ ./shellcodetest 
Hi there$