根据标准输出将二进制数据传递给程序的标准输入
Pass binary data to stdin of a program based on its stdout
我有一个程序 A,它打印一些文本数据,然后执行 gets() 从标准输入读取。
我想创建一个 program/script,它将读取 A(文本数据)的标准输出,并根据输出传递数据(二进制)到其标准输入。
该程序是一个简单的利用演示(格式化字符串漏洞转储堆栈,然后根据转储值制作利用有效载荷,然后通过gets传递以触发缓冲区溢出并执行制作的shellcode)。
int main(int argc, char** argv)
{
char buffer[20];
BOOL(__stdcall *getName)(LPSTR lpBuffer, LPDWORD nSize) = GetComputerNameA;
printf("The argument is: ");
printf(argv[1]);
printf("\r\n");
printf("Enter a message: ");
gets(buffer);
printf("Your message is: %s", buffer);
TCHAR name[20];
DWORD len = 19;
getName(name, &len);
printf("Your computer is called %s", name);
printf("\r\n");
}
我曾尝试通过 target.stdout.readline() 和 target.stdin.write() 在 python 中执行此操作,但读取行挂起并且从未 returns。
我还通过 OutputDataReceived 事件在 C# 中进行了尝试,但如果我没有传递任何输入,我只能读取输出。重定向标准输入后,我无法阅读任何内容。也发生了挂起。
我的问题是如何实现?
编辑:
python 执行程序的代码示例
from subprocess import Popen, PIPE, STDOUT
x = Popen(['test.exe', '"%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x"', 'start'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
out = x.stdout.readline()
print(out)
input("{press enter}")
data = bytearray(...payload...)
payload = buffer(data)
x.stdin.write(payload)
input("{press enter}")
在 C printf
中使用缓冲输出。当 stdout 是一个终端时,缓冲区在每个 "\n"
之后被刷新,但在这种情况下,输出是到一个管道,所以它被缓冲到几个 k 字节的数据。缓冲区永远不会被刷新,也不会写入管道。
为避免python永远等待缓冲数据,您可以在每个printf
或disable buffering altogether后添加fflush(stdout)
。
我有一个程序 A,它打印一些文本数据,然后执行 gets() 从标准输入读取。 我想创建一个 program/script,它将读取 A(文本数据)的标准输出,并根据输出传递数据(二进制)到其标准输入。
该程序是一个简单的利用演示(格式化字符串漏洞转储堆栈,然后根据转储值制作利用有效载荷,然后通过gets传递以触发缓冲区溢出并执行制作的shellcode)。
int main(int argc, char** argv)
{
char buffer[20];
BOOL(__stdcall *getName)(LPSTR lpBuffer, LPDWORD nSize) = GetComputerNameA;
printf("The argument is: ");
printf(argv[1]);
printf("\r\n");
printf("Enter a message: ");
gets(buffer);
printf("Your message is: %s", buffer);
TCHAR name[20];
DWORD len = 19;
getName(name, &len);
printf("Your computer is called %s", name);
printf("\r\n");
}
我曾尝试通过 target.stdout.readline() 和 target.stdin.write() 在 python 中执行此操作,但读取行挂起并且从未 returns。 我还通过 OutputDataReceived 事件在 C# 中进行了尝试,但如果我没有传递任何输入,我只能读取输出。重定向标准输入后,我无法阅读任何内容。也发生了挂起。
我的问题是如何实现?
编辑: python 执行程序的代码示例
from subprocess import Popen, PIPE, STDOUT
x = Popen(['test.exe', '"%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x"', 'start'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
out = x.stdout.readline()
print(out)
input("{press enter}")
data = bytearray(...payload...)
payload = buffer(data)
x.stdin.write(payload)
input("{press enter}")
在 C printf
中使用缓冲输出。当 stdout 是一个终端时,缓冲区在每个 "\n"
之后被刷新,但在这种情况下,输出是到一个管道,所以它被缓冲到几个 k 字节的数据。缓冲区永远不会被刷新,也不会写入管道。
为避免python永远等待缓冲数据,您可以在每个printf
或disable buffering altogether后添加fflush(stdout)
。