与系统命令相比,execve如何防止漏洞
How does execve prevents vulnerabilities compared to system command
我指的是这个链接,
基本上,考虑输入happy'; useradd 'attacker
,安全建议区分兼容和不兼容代码-
非投诉代码
#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
的参数。所以我们将得到一个等于"0"的返回值;无效参数";。我的理解正确吗?还是有什么比我的理解更深刻的东西我错过了?
主要区别实际上是,使用system
函数,您可以启动您的shell可以执行的任何内容,因此您基本上可以使用多个命令进行shell注入。而首先使用execve
,您可以指定要执行的特定二进制文件,因此您几乎可以确定只有一个命令被执行(除非execve
是shell..)。此外,由于您提供了execve
的完整路径,因此可以避免基于修改HOME
或当前工作目录的黑客攻击。
是的,你的理解是正确的
相关文章:
- 防止主数据类型C++的隐式转换
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何防止C++遗留代码中的挂起指针
- Python中的for循环与C++有何不同
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何防止初始值设定项列表中的错误值?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- C++ 防止在映射中放置()时调用析构函数
- 是否可以防止省略聚合初始化成员?
- 在C++中释放内存期间,迭代器与指针有何不同
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- std::mutex 如何防止线程修改?
- 防止C++中的硬编码数字
- 防止在复制构造函数中隐式调用基构造函数
- 如何防止 CUDA-GDB 中的<优化输出>值
- 防止组合框被关闭
- 与系统命令相比,execve如何防止漏洞