我使用這個代碼從這裏: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