【发布时间】:2019-05-04 15:38:11
【问题描述】:
我是 shellcoding 的新手,我尝试为 (hello world) 编写一个 shellcode,所以这是我的第一个带有空字节的代码:
global _start
section .text
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
这是我删除 null x00 后的第二个代码!
global _start
section .text
_start:
;tell linker entry point
xor edx,edx
mov dl,len ;message length
mov ecx,msg ;message to write
xor ebx,ebx
mov bl,1 ;file descriptor (stdout)
xor eax,eax
mov al,4 ;system call number (sys_write)
int 0x80 ;call kernel
xor eax,eax
mov al,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
我编译它以测试:
nasm -f elf32 -o hello-without-null.o hello-without-null.asm
ld -o hello-without-null hello-without-null.o
当我运行它时它的工作。/hello-without-null
比我使用的:objdump -d hello-without-null -M intel
这就是结果:
Disassembly of section .text:
08048080 <_start>:
8048080: 31 d2 xor edx,edx
8048082: b2 0e mov dl,0xe
8048084: b9 9c 90 04 08 mov ecx,0x804909c
8048089: 31 db xor ebx,ebx
804808b: b3 01 mov bl,0x1
804808d: 31 c0 xor eax,eax
804808f: b0 04 mov al,0x4
8048091: cd 80 int 0x80
8048093: 31 c0 xor eax,eax
8048095: b0 01 mov al,0x1
8048097: cd 80 int 0x80
然后我通过以下方式将其转换为 shellcode:
objdump -d ./hello-without-null|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\x/g' |粘贴 -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
输出是:
"\x31\xd2\xb2\x0e\xb9\x9c\x90\x04\x08\x31\xdb\xb3\x01\x31\xc0\xb0\x04\xcd\x80\x31\xc0\xb0\x01 \xcd\x80"
当我测试它时,我得到了这个错误:
Shellcode 长度:25 分段错误(核心转储)
我用于测试的 c 代码:
#include<stdio.h>
#include<string.h>
unsigned char code[] = \
"\x31\xd2\xb2\x0e\xb9\x9c\x90\x04\x08\x31\xdb\xb3\x01\x31\xc0\xb0\x04\xcd\x80\x31\xc0\xb0\x01\xcd\x80";
int main()
{
printf("Shellcode Length: %d\n", strlen(code));
int (*ret)() = (int(*)())code;
ret();
}
那么问题是什么?我该如何解决?
【问题讨论】:
-
一种方法是将您的 shellcode 放入
const char[]数组中,因此它进入 TEXT 段中的.rodata部分(具有 exec 权限),而不是.data(读取/写,没有执行)。 -
我建议您考虑使用 JMP/CALL/POP 方法或其他避免生成绝对内存地址的方法。
mov ecx,msg将不起作用,因为msg在C 程序中运行时的地址将不一样。替代方法是在堆栈上构建sring 并传递堆栈地址。跨度>
标签: linux ubuntu x86 shellcode