导演的Shellcode输出到文件 - ç
我使用这个代码从这里: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
转移注释到一个答案,在信贷到期时给予信贷。
Deanie说:
如果shellcode的被写入标准输出和标准错误不是这应该工作。尝试:
./testshell > output.txt 1>&2
为了这user2059300的OP,回应道:
在
1>&2
没有骰子,输出仍然出现在终端,而不是在output.txt的
我认为他的意思是
./testshell > output.txt 2>&1
将stdout & stderr重定向到output.txt。
但user2059300说:
上仍然是一个不走的,......我提供我测试的shellcode。
然后我问:
- 如何shell代码做写?它在做什么?我不会为你解剖它; shell代码通常是非常特定于平台的,并且您还没有确定您使用的是哪个系统。当然,很容易猜测你在英特尔机器上使用Linux,但它可能是32位或64位的Linux,而且还需要了解shell程序如何重定向输出。例如,如果打开终端并写入终端,则很难避免输出出现在终端上。
而且user2059300说:
这是一个系统调用
sys_write
输出到文件描述符。
促使我问:
- 哪个文件描述符? 0,1,2或其他?
这仍然悬而未决。倾销到组装会显示致电
fprintf
,但重定向stderr
和stdout
什么也不做。这几乎就像正在使用的内核printf一样。
和我一起反驳:
- 这就是为什么我没有列出作为候选文件描述符。 文件描述符0通常是文件描述符1和2的一个副本,因此您可以写入0(标准输入)并从1和2(标准输出和标准错误)读取。当然,这样做是完全不支持和不可移植的(并且不能通过文件流
stdin
,stdout
,stderr
)来完成,但通常shell程序代码是不可移植的。
这似乎是关键。 David C. Rankin确认:
哈!你钉了它。
./testshell > output.txt 0>&1
工作得很好。学习已经发生......谢谢。这是第一次,希望是最后一次,我会跑过去。我太老了,无法学习这些技巧。
,所以没有user2059300:
谢谢你们这么多。学习确实发生了。
./testshell > output.txt 0>&1
很显然,我不知道这将是解决方案,但在标准输出和标准错误的I/O重定向失败了,它成为了可能。这种行为有很长的历史(第7版Unix,也许在此之前),尽管很少有明确记录。
这应该在shellcode写入标准输出而不是标准错误时起作用。试试:./testshell> output.txt 1>&2 – Deanie
'fscanf'格式'“\\ x%02x”'是多余的和误导性的。它应该是''\\ x%2x' – chqrlie
@Deanie在1&2没有骰子,输出仍然发生在终端,而不是output.txt – user2059300