c - 根据程序的标准输出将二进制数据传递给程序的标准输入
问题描述
我有一个程序 A,它打印一些文本数据,然后从标准输入读取 gets()。我想创建一个程序/脚本,它将读取 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 中执行此操作,但 readline 挂起并且永远不会返回。我还通过 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}")
解决方案
在 Cprintf
中使用缓冲输出。当 stdout 到终端时,缓冲区在每个 之后被刷新"\n"
,但在这种情况下,输出到管道,所以它被缓冲到几个 k 字节的数据。缓冲区永远不会刷新,也不会写入管道。
为避免 python 永远等待缓冲的数据,您可以fflush(stdout)
在每个之后添加printf
或完全禁用缓冲。
推荐阅读
- reporting-services - SSRS:图例工具提示在我的应用程序中不支持法语特殊字符
- c# - 拖放有时有效,有时无效
- javascript - Angular ng-select ngModel 更改但 ng-select 未更新
- excel - Excel:如何限制 sig 数字并将值合并到带星号的方程中?
- c# - 在构建身份服务器 4 中闲置 10 分钟后使会话过期
- angular - 修复了 ngx admin 内容页面区域中的标题
- python - 在python中使用不同字符从不同行中获取用户输入
- node.js - 无法将数据添加到特定路线
- python - Python ABC 接口究竟是如何工作的?
- azure-devops - Azure DevOps 服务连接到本地 Kubernetes 集群