如何隐藏在Linux中运行的C程序的命令行参数,以便其他用户通过“w”,“ps auxwww”或类似命令看不到它们?
答案 0 :(得分:8)
修改程序中argv的内容:
#include <stdio.h>
#include <time.h>
void delay (long int msecs)
{
clock_t delay = msecs * CLOCKS_PER_SEC / 1000;
clock_t start = clock();
while (clock() - start < delay);
}
void main (int argc, char **argv)
{
if (argc == 2)
{
printf ("%s\n", argv[1]);
delay (6000);
argv[1][0] = 'x';
argv[1][1] = '.';
argv[1][2] = 'x';
printf ("%s\n", argv[1]);
delay (5000);
printf ("done\n");
}
else printf ("argc != 1: %d\n", argc);
}
调用:
./argumentClear foo
foo
x.x
done
结果,按ps查看:
asux:~ > ps auxwww | grep argu
stefan 13439 75.5 0.0 1620 352 pts/5 R+ 17:15 0:01 ./argumentClear foo
stefan 13443 0.0 0.0 3332 796 pts/3 S+ 17:15 0:00 grep argu
asux:~ > ps auxwww | grep argu
stefan 13439 69.6 0.0 1620 352 pts/5 R+ 17:15 0:02 ./argumentClear x.x
stefan 13446 0.0 0.0 3332 796 pts/3 S+ 17:15 0:00 grep argu
备注:我的延迟功能无法正常工作。而不是11秒,程序运行大约2-3。我不是那个大C程序员。 :)延迟功能需要改进。
答案 1 :(得分:7)
这实际上相当困难(我不会说不可能,因为可能是我不知道的方式)这样做,特别是如果用户有权访问{{ 1}}您的流程的文件系统。
阻止别人看到命令行参数的最佳方法可能是使用命令行参数: - )
您可以将参数存储在一个受到适当保护的文件中(例如)/proc
,然后运行您的程序:
myargs.txt
当然,您必须修改myprog @myargs.txt
来处理“文件中的参数”方案。
或者,您可以将参数设置为环境变量,并让您的程序使用myprog
。
但是,我不知道任何方法可以保护您免受适当授权的进程(例如getenv
运行的进程)。
答案 2 :(得分:0)
据我所知,该信息存储在内核空间中。如果没有编写内核模块,您将无法隐藏此信息,因为任何程序都可以查询proc文件系统以查看命令行参数(这就是ps所做的)。
作为替代方案,您可以在stdin上读取命令行args,然后填充数组以传递给命令行参数处理程序。或者,更好的是,添加对程序的支持,以读取包含相同命令行参数信息的配置文件,并设置权限,以便只有所有者才能读取该文件。
我希望这会有所帮助。
答案 3 :(得分:0)
要隐藏ps命令中的参数,可以使用我一直使用的技巧:
sprintf(argv[0], "My super long argument list
");
请确保使用空格键使用大约3行的空格,否则编译器会抛出错误!
请记住在解析命令行后更改argv [0]!
59982 pts/1 SLl+ 0:00 My super long argument list strings /proc/59982/cmdline My super long argument list
这是黑客,但是入侵者会首先发出“ ps axw”。
始终监视关键任务服务器并检查已登录的用户!!!