【发布时间】: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