【问题标题】:Segfault when running hello world shellcode in C program在 C 程序中运行 hello world shellcode 时出现段错误
【发布时间】:2017-10-05 01:20:26
【问题描述】:

对不起,如果这个问题听起来很愚蠢,但我对 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

当我用以下命令编译并链接它时,这段代码可以正常工作:

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

但是,我尝试在以下 C 代码中运行它: $ 猫 shellcodetest.c #包括 #包括

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 部分(数据段= 读取+写入)。
  • 嘿,如果我错了,请纠正我,但this link 解释说字节码实际上被转换为指向没有参数 () 的函数的指针。所以我认为十六进制字符串以 .text 部分结束,即 AX。此外,LarsH 仅通过更改字节序就设法在 C 程序中运行 shellcode(我觉得没有看到这一点很菜鸟......)。
  • 是的,我的评论是错误的。 C 中的shellcode 在您编写char *shellcode 时从内存中加载指针,而指向的字符串文字本身在.rodata 中。使用const char shellcode[] 可以节省一定程度的间接性,但是:call eax(甚至call rel32)而不是call [absolute_address_of_pointer]。 (而char shellcode[](不是 const)会崩溃)。

标签: exploit shellcode assembly x86


【解决方案1】:

您的 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$

【讨论】:

    猜你喜欢
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多