【发布时间】:2011-07-02 23:16:11
【问题描述】:
我正在编写一个从文件加载和执行代码的程序。 但我遇到了一个问题:“写”系统调用不起作用。 代码成功加载并执行,但屏幕上不显示任何文本。
加载代码的程序:
#include < stdio.h >
#include < stdlib.h >
int main(int argc,char* argv[])
{
unsigned int f_size = 0;
unsigned char* code_buf = NULL;
void (*func_call)(void) = NULL;
if(argc < 2)
{
printf("Usage: %s <FILE>\n",argv[0]);
return 1;
}
FILE* fp = fopen(argv[1],"rb");
if(!fp)
{
printf("Error while opening this file: %s\n",argv[1]);
return 1;
}
unsigned int fsize = 0;
fseek(fp,0,SEEK_END);
fsize = ftell(fp);
fseek(fp,0,SEEK_SET);
if(fsize < 4)
{
printf("Code size must be > 4 bytes\n");
return 1;
}
code_buf = (unsigned char*) malloc(sizeof(unsigned char)*fsize);
if(fread(code_buf,fsize,1,fp)<1)
{
printf("Error while reading file: %s\n",argv[1]);
free(code_buf);
return 1;
}
func_call = (void (*)(void)) code_buf;
printf("[EXEC] Binary is loaded\n"
"\tFirst 2 bytes: 0x%x 0x%x\n"
"\tLast 2 bytes: 0x%x 0x%x\n",
code_buf[0],code_buf[1],
code_buf[fsize-2],code_buf[fsize-1]);
printf("[EXEC] Starting code...\n");
(*func_call)();
printf("[EXEC] Code executed!\n");
free(code_buf);
return 0;
}
我试图通过这个程序(test.s)执行的代码:
.text
movl $4, %eax
movl $1, %ebx
movl $str, %ecx
movl $5, %edx
int $0x80
jmp end
str:
.string "test\n"
end:
ret
我是这样编译的:
gcc -c test.s
objcopy -O binary test.o test.bin
已解决,感谢@Christoph
有工作代码:
.text
call start
str:
.string "test\n"
start:
movl $4, %eax
movl $1, %ebx
pop %ecx
movl $5, %edx
int $0x80
ret
【问题讨论】:
-
当你说没有显示任何文本时,你的意思是它不打印“test”还是它甚至不打印“[EXEC]”行?
-
+1 个有趣的问题,不过我希望这不会被用于任何恶意行为。
-
将
-S添加到对objcopy的调用中是否有帮助? -
-S应该没有帮助 - 代码有缺陷...... -
>当你说没有显示文本时,你的意思是它不打印“test”还是它甚至不打印“[EXEC]”行? “[EXEC]”之间必须有“测试”
标签: c linux assembly system-calls shellcode