命令行参数的Shell脚本密码安全

发布于 2024-11-19 00:21:44 字数 237 浏览 8 评论 0原文

如果我使用密码作为命令行参数,则它在系统上使用 ps 公开。

但是,如果我在 bash shell 脚本中执行以下操作:

...
{ somecommand -p mypassword }
...

这是否仍会显示在进程列表中?或者这样安全吗?

  • 子流程怎么样:(...)?不安全吧?
  • 协进程?

If I use a password as a command-line parameter it's public on the system using ps.

But if I'm in a bash shell script and I do something like:

...
{ somecommand -p mypassword }
...

is this still going to show up in the process list? Or is this safe?

  • How about sub-processes: (...)? Unsafe right?
  • coprocess?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她说她爱他 2024-11-26 00:21:44

命令行将始终可见(如果仅通过 /proc)。

所以唯一真正的解决办法是:不要。您可以在 stdin 或专用 fd 上提供它:

./my_secured_process some parameters 3<<< "b@dP2ssword"

使用类似(简单第一)的脚本

#!/bin/bash
cat 0<&3

(此示例只会将错误的密码转储到 stdout)

现在您需要关心的是:

  • MITM(欺骗脚本,会窃取密码,例如通过破坏 PATH)
  • bash 历史记录在命令行中保留您的密码(例如,查看 bash 的 HISTIGNORE
  • 包含密码重定向的脚本的安全性
  • 安全性已使用 tty;键盘记录器; ...如您所见,我们现在已经进入“一般安全原则”

Command lines will always be visible (if only through /proc).

So the only real solution is: don't. You might supply it on stdin, or a dedicated fd:

./my_secured_process some parameters 3<<< "b@dP2ssword"

with a script like (simplicity first)

#!/bin/bash
cat 0<&3

(this sample would just dump a bad password to stdout)

Now all you need to be concerned with is:

  • MITM (spoofed scripts that eaves drop the password, e.g. by subverting PATH)
  • bash history retaining your password in the commandline (look at HISTIGNORE for bash, e.g.)
  • the security of the script that contains the password redirection
  • security of the tty's used; keyloggers; ... as you can see, we have now descended into 'general security principles'
妥活 2024-11-26 00:21:44

如何使用文件描述符方法:

env -i bash --norc   # clean up environment
set +o history
read -s -p "Enter your password: " passwd
exec 3<<<"$passwd"
mycommand <&3  # cat /dev/stdin in mycommand

请参阅:

隐藏命令行参数中的秘密在 Unix 上

How about using a file descriptor approach:

env -i bash --norc   # clean up environment
set +o history
read -s -p "Enter your password: " passwd
exec 3<<<"$passwd"
mycommand <&3  # cat /dev/stdin in mycommand

See:

Hiding secret from command line parameter on Unix

丿*梦醉红颜 2024-11-26 00:21:44

被调用的程序可以通过简单地覆盖 argv 来更改其命令行,如下所示:

#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
    int arglen = argv[argc-1]+strlen(argv[argc-1])+1 - argv[0];
    memset(argv[0], arglen, 0);
    strncpy(argv[0], "secret-program", arglen-1);
    sleep(100);
}

测试:

$ ./a.out mySuperPassword & 
$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
me       20398 18872  0 11:26 pts/3    00:00:00 bash
me       20633 20398  0 11:34 pts/3    00:00:00 secret-program
me       20645 20398  0 11:34 pts/3    00:00:00 ps -f
$

UPD:我知道,它并不完全安全,可能会导致竞争条件,但许多从命令行接受密码的程序都会这样做这招。

The called program can change its command line by simply overwriting argv like this:

#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
    int arglen = argv[argc-1]+strlen(argv[argc-1])+1 - argv[0];
    memset(argv[0], arglen, 0);
    strncpy(argv[0], "secret-program", arglen-1);
    sleep(100);
}

Testing:

$ ./a.out mySuperPassword & 
$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
me       20398 18872  0 11:26 pts/3    00:00:00 bash
me       20633 20398  0 11:34 pts/3    00:00:00 secret-program
me       20645 20398  0 11:34 pts/3    00:00:00 ps -f
$

UPD: I know, it is not completely secure and may cause race conditions, but many programs that accept password from command line do this trick.

咆哮 2024-11-26 00:21:44

避免显示在进程列表中的唯一方法是重新实现要在纯 Bash 函数中调用的程序的整个功能。函数调用不是单独的进程。但通常这是不可行的。

The only way to escape from being shown in the the process list is if you reimplement the entire functionality of the program you want to call in pure Bash functions. Function calls are not seperate processes. Usually this is not feasible, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文