#include <WhatHere?>
#include <WhatHere?>
#include <WhatHere?>
int main(int argc, char **argv) {
char command[50] = "echo ";
strcat(command,argv[1]); // concatenate the input so that the final command is "echo <input>"
system(command); // call the system() function to print the input
return 0; // denote that the program has finished executing successfully
}我们可以通过运行这段代码来获得远程访问吗?我知道这是可能的,但请帮我修补一下。
发布于 2011-03-24 20:41:50
假设你担心潜在的缓冲区溢出,你可以这样修复它:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char **argv) {
char *command;
if (argc != 2) {
fprintf (stderr, "Wrong number of arguments\n");
return 1;
}
if ((command = malloc (strlen (argv[1]) + 6)) == NULL) {
fprintf (stderr, "Could not allocate memory\n");
return 1;
}
strcpy (command, "echo ");
strcat(command,argv[1]);
system(command);
free (command);
return 0;
}这为"echo " (5)、argv[1] (字符串长度)和空终止符(1)留出了足够的空间。
允许用户指定的东西运行仍然是潜在的危险,但至少你不会再得到缓冲区溢出。
发布于 2011-03-25 00:30:38
Paxdiablo为您的缓冲区溢出问题提供了一个很好的解决方案,但这实际上是您这里最小的问题。你最大的问题是,你盲目地使用来自用户的输入,而没有事先检查它。
例如,像这样运行你的程序:
./your_app "\"goodbye data\" && rm -rf /"即使您的程序没有缓冲区溢出问题,也会以灾难告终。攻击者可以很容易地传入一个完整的shell脚本,这个脚本做了所有令人讨厌的事情,他们所要做的就是重写它,使其适合一行。
在将传入的用户输入传递给system()之前,您需要检查它,并确保它看起来像您期望的那样。更好的做法是,避免将system()完全用于用户输入,而是使用更安全的方法来执行所需的操作(在您的示例中,您可以用printf()替换对system("echo ...")的调用)。如果您一定要将用户输入传递给system(),那么可以考虑在一个受限的环境中运行您的应用程序,比如chroot监狱,这样至少会使您的应用程序更难做任何令人讨厌的事情。
https://stackoverflow.com/questions/5419139
复制相似问题