C 程序中 运行 hello world shellcode 时的段错误

Segfault when running hello world shellcode in C program

抱歉,如果这个问题听起来很愚蠢,但我是 shellcoding 的新手,我试图让一个 hello world 示例在 32 位 linux 机器上工作。

由于这是 shellcoding,我使用了一些技巧来删除空字节并缩短代码。这是:

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

当我使用以下命令link编译和link时,这段代码工作正常:

$ nasm -f elf print4.asm
$ ld -o print4 -m elf_i386 print4.o

但是,我在以下 C 代码中尝试了 运行: $猫shellcodetest.c #包括 #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 发生了一些奇怪的事情。但是正如我之前所说,我仍然缺乏经验来真正理解这里发生的事情。

提前致谢!

您的 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   [=10=]x4
 8048062:       58                      pop    %eax
 8048063:       6a 01                   push   [=10=]x1
...

您需要做的更改是交换字节顺序,'\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$