我使用这个代码从这里:Read and Execute Shellcode from a .txt File导演的Shellcode输出到文件 - ç
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdlib.h>
int main(void)
{
FILE *file = fopen("text.txt", "r");
unsigned char *buf;
int length = 0;
struct stat st;
int v;
// get file size and allocate. We're going to convert to bytes
// from text, so this allocation will be safely large enough
fstat(fileno(file), &st);
buf = valloc(st.st_size);
while (fscanf(file, "\\x%02x", &v) == 1)
{
buf[length++] = v;
}
fclose(file);
mprotect(buf, length, PROT_EXEC);
int (*ret)() = (int (*)())buf;
ret();
return 0;
}
我正与编译:GCC -fno-堆栈保护-z execstack testshell.c - o testshell
它运行得很好,但它执行的shellcode写入终端,但我想以某种方式重定向到一个文件。
我想:
./testshell > output.txt
但也似乎没有得到这捕获shellcode的结果无论是。
如何捕获它运行的任何shellcode的输出,并且如果可能将其重定向到文件?
更新:我使用的shellcode的,并将其与SYS_WRITE系统调用输出到一个文件描述符(它做了计算,并打印到屏幕)输出 -
\xeb\x4d\x5e\x66\x83\xec\x0c\x48\x89\xe0\x48\x31\xc9\x68\x33\x09\x00\x7c\x48\x89\xcf\x80\xc1\x0c\x40\x8a\x3e\x40\xf6\xd7\x40\x88\x38\x48\xff\xc6\x68\x16\x96\xd0\xd9\x48\xff\xc0\xe2\xea\x2c\x0c\x48\x89\xc6\x68\xf2\xf5\x44\x48\x48\x31\xc0\x48\x89\xc7\x04\x01\x48\x89\xc2\x80\xc2\x0b\x0f\x05\x48\x31\xc0\x04\x3c\x0f\x05\xe8\xae\xff\xff\xff\x85\xa7\xaa\xc7\x9e\x87\xa5\xa5\x8e\xb7\x87\xba\x31\x80\xe0\x55\xef\xa1\x93\x0c\x4e\x1c\xdc\x34\x53\xb3\x8b\x43\x48\x68\x30\x1d\x4b\x65\x5b\x52\x41\x4e\x44\x53\x54\x52\x32\x5d
这应该在shellcode写入标准输出而不是标准错误时起作用。试试:./testshell> output.txt 1>&2 – Deanie
'fscanf'格式'“\\ x%02x”'是多余的和误导性的。它应该是''\\ x%2x' – chqrlie
@Deanie在1&2没有骰子,输出仍然发生在终端,而不是output.txt – user2059300