0
我指的是this链接,
基本上,考虑输入happy'; useradd 'attacker
,安全咨询兼容和不兼容的代码之间的区别 -的execve如何防止漏洞比系统命令
非诉代码
#include <string.h>
#include <stdlib.h>
enum { BUFFERSIZE = 512 };
void func(const char *input) {
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
if (len_wanted >= BUFFERSIZE) {
/* Handle error */
} else if (len_wanted < 0) {
/* Handle error */
} else if (system(cmdbuf) == -1) {
/* Handle error */
}
}
符合规范
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void func(char *input) {
pid_t pid;
int status;
pid_t ret;
char *const args[3] = {"any_exe", input, NULL};
char **env;
extern char **environ;
/* ... Sanitize arguments ... */
pid = fork();
if (pid == -1) {
/* Handle error */
} else if (pid != 0) {
while ((ret = waitpid(pid, &status, 0)) == -1) {
if (errno != EINTR) {
/* Handle error */
break;
}
}
if ((ret != -1) &&
(!WIFEXITED(status) || !WEXITSTATUS(status))) {
/* Report unexpected child status */
}
} else {
/* ... Initialize env as a sanitized copy of environ ... */
if (execve("/usr/bin/any_cmd", args, env) == -1) {
/* Handle error */
_Exit(127);
}
}
}
假设我们将相同的输入传递给具有同等特权的函数,即由root等运行,第二种解决方案如何确保命令注入攻击被排斥?
我唯一的猜测是,execve
将刷新您的二进制图像any_cmd
并使用输入happy'; useradd 'attacker
作为参数any_cmd
。所以我们将有一个相当于“无效参数”的返回值。我的理解是正确的吗?或者,有没有比我的理解更深入的东西?
'execve'和'system'各自手册的哪一部分你发现不清楚? – 2014-10-27 09:07:55
我想知道如何相同的输入不会导致与系统相比execve问题。 – 2014-10-27 09:09:59