【发布时间】:2013-07-23 23:35:19
【问题描述】:
下面,我编写了 x64 程序集,它打印“Hello, World!”来自 Mac OS X 10.8 上的系统调用。它在独立执行时可以完美地组装和运行。
; Assemble and link with:
; nasm -f macho64 -o HelloWorld.o HelloWorld.s
; ld -arch x86_64 -o HelloWorld HelloWorld.o
global start
section .text
start:
push rbp
mov rbp, rsp
jmp short String
xor rdi, rdi
mov di, 0x01
StringRet:
pop rsi
xor rdx, rdx
mov dl, 0xE
mov r8b, 0x02
shl r8, 24
or r8, 0x04
mov rax, r8
syscall ; System call for write(4)
xor edi, edi
mov r8b, 0x02
shl r8, 24
or r8, 0x01
mov rax, r8
syscall ; System call for exit(1)
mov rsp, rbp
pop rbp
String:
call StringRet
db 'Hello, World!'
我遇到的问题是当我尝试将此代码作为 shell 代码从 c 程序运行时。我使用 otool 来获取以下机器操作码。
otool -t HelloWorld.o
char code[] = "\x55\x48\x89\xe5\x41\xb0\x02\x49\xc1\xe0\x18\x49\x83\xc8\x04\x4c"
"\x89\xc0\x48\x31\xff\x66\xbf\x01\x00\xeb\x1e\x5e\x48\x31\xd2\xb2"
"\x0e\x0f\x05\x41\xb0\x02\x49\xc1\xe0\x18\x49\x83\xc8\x01\x4c\x89"
"\xc0\x31\xff\x0f\x05\x48\x89\xec\x5d\xe8\xdd\xff\xff\xff\x48\x65"
"\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21";
下面是我用来执行它的 c 程序。但我不断收到总线错误:10。
; Compile:
; gcc -o HelloWorldTest HelloWorldTest.c
char code[] = "\x55\x48\x89\xe5\x41\xb0\x02\x49\xc1\xe0\x18\x49\x83\xc8\x04\x4c"
"\x89\xc0\x48\x31\xff\x66\xbf\x01\x00\xeb\x1e\x5e\x48\x31\xd2\xb2"
"\x0e\x0f\x05\x41\xb0\x02\x49\xc1\xe0\x18\x49\x83\xc8\x01\x4c\x89"
"\xc0\x31\xff\x0f\x05\x48\x89\xec\x5d\xe8\xdd\xff\xff\xff\x48\x65"
"\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21";
int main()
{
int (*ret)();
ret = (int(*)())code;
(int)(*ret)();
return 0;
}
当我使用 gdb 单步执行时,当执行被传递到 shellcode 时,我得到了 KERN_PROTECTION_FAILURE。
更新问题:
上述问题由 Carl Norum 解决,这是由于内存保护。我有一个不同的问题,但与上面类似。我不想将 shell 代码放在同一个文件中,而是想从 .txt 文件中读取 shell 代码并执行它。下面我尝试将一段内存标记为 PROT_EXEC 并将 .txt 文件的内容读入其中并执行。但它不起作用,我遇到了同样的错误,KERN_PROTECTION_FAILURE,我尝试使用 mprotect 和 mmap 将一段内存标记为 PROT_EXEC。
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
int (*ret)();
unsigned char* buf;
int main()
{
FILE* file;
file = fopen("text.txt", "rb");
unsigned int len = ftell(file);
buf = (char*)malloc(len);
fread(buf, 1, len, file);
fclose(file);
mprotect(&buf, len, PROT_EXEC);
// I also tried mmap, but same error.
/*void *ptr = mmap(0, 1024, PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
if (ptr == MAP_FAILED)
{
perror("mmap");
exit(-1);
}
memcpy(ptr, buf, 1024);*/
ret = buf;
ret();
return 0;
}
这是我正在阅读的 text.txt 文件,它是相同的 hello world 代码:
\x55\x48\x89\xe5\xeb\x33\x48\x31\xff\x66\xbf\x01\x00\x5e\x48\x31\xd2\xb2\x0e\x41\xb0\x02\x49\xc1\xe0\x18\x49\x83\xc8\x04\x4c\x89\xc0\x0f\x05\x31\xff\x41\xb0\x02\x49\xc1\xe0\x18\x49\x83\xc8\x01\x4c\x89\xc0\x0f\x05\x48\x89\xec\x5d\xe8\xc8\xff\xff\xff\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21\x0a
由于我将 txt 文件的内容复制到 PROC_EXEC 内存中,我不明白为什么会收到 KERN_PROTECTION_FAILURE。
【问题讨论】:
-
新文本文件是否包含反斜杠、x、5、5 等字符?或者它是否包含字符
'\x55'(数字 7 IIRC)? -
是的,它包含\x,.txt文件与上图完全一样。
-
你不需要在 C 程序中强制转换
malloc的返回值。 -
几个问题。 1. 你应该使用
valloc来获得页面对齐的内存,并且 2. 你可能从ftell调用中得到0。你需要一个fseek(SEEK_END)。另外 3. 您需要将该 ASCII 文本转换为要执行的实际二进制代码。 4. 你可能不希望有&buf- 只是buf是正确的。 -
查看额外问题Read and execute shellcode from a text file 以获取此问题更新部分的答案。
标签: c macos assembly shellcode