如何从命令行提供stdin输入?

时间:2011-07-27 15:48:21

标签: c security penetration-testing stack-smash

我正在尝试对类分配的程序执行缓冲区溢出攻击。攻击程序以及易受攻击的程序都是由我编写的。

易受攻击的代码使用scanf从stdin读取数据。

./vulnerable < malicious_payload_file.txt运行正常。 more malicious_payload | ./vulnerableecho JUNK_JUNK_JUNK_JUNK | ./vulnerable也可以正常运作。

但是,我想使用攻击程序继续提供更长的有效负载,直到程序崩溃。所以,我需要动态生成更大的有效载荷。我正在使用system ("./vulnerable");重复调用并测试异常退出。

如何指定此类有效负载?

有没有办法运行./vulnerable < malicious_payload_binary或以某种方式运行,以便我不必将恶意有效负载放在文件中,但可以在命令行中指定它?

4 个答案:

答案 0 :(得分:8)

这个怎么样?

echo "your payload goes here" | ./vulnerable

您可以将echo命令替换为生成所需输入./vulnerable的任何命令。一个这样的例子是不断的垃圾流作为输入,你可以这样做:

cat /dev/urandom | ./vulnerable

答案 1 :(得分:2)

您可以尝试使用popen代替system,而不是尝试使用命令行:

FILE *fp = popen("./vulnerable", "w");
// write stuff to fp -- it goes to vulnerable's stdin
int exitcode = pclose(fp);

你从pclose获得的exitcode与从system获得的exitcode相同,如果你使用另一个进程来创建数据并通过shell将其传输到./vulnerable

答案 2 :(得分:0)

尝试使用管道而不是重定向:

./malicious_payload_binary | ./vulnerable

答案 3 :(得分:0)

编辑:我想我终于理解了你的问题(也许),你想阅读命令行参数吗?像

这样的东西
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("the name of this program is %s\n", argv[0]);
    printf("%d command line arguments were provided\n", argc);
    printf("the input file is %s\n", argv[1]);
    // could do something like: fopen(argv[1]) here
    return 0;
}

如果将其编译为名为stdintest的二进制文件并按如下方式运行:

./stdintest somefile.txt

它会输出:

the name of this program is ./stdintest
2 command line arguments were provided
the input file is somefile.txt

<强> OLD:

正如dolphy所提到的,只需写入malicious_payload_binary中的stdout,从vulnerable中的标准输入读取,并用管道连接它们:./malicious_payload_binary | ./vulnerable

相关问题