命令行参数的Shell脚本密码安全
如果我使用密码作为命令行参数,则它在系统上使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
命令行将始终可见(如果仅通过 /proc)。
所以唯一真正的解决办法是:不要。您可以在 stdin 或专用 fd 上提供它:
使用类似(简单第一)的脚本
(此示例只会将错误的密码转储到 stdout)
现在您需要关心的是:
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:
with a script like (simplicity first)
(this sample would just dump a bad password to stdout)
Now all you need to be concerned with is:
如何使用文件描述符方法:
请参阅:
隐藏命令行参数中的秘密在 Unix 上
How about using a file descriptor approach:
See:
Hiding secret from command line parameter on Unix
被调用的程序可以通过简单地覆盖
argv
来更改其命令行,如下所示:测试:
UPD:我知道,它并不完全安全,可能会导致竞争条件,但许多从命令行接受密码的程序都会这样做这招。
The called program can change its command line by simply overwriting
argv
like this:Testing:
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.
避免显示在进程列表中的唯一方法是重新实现要在纯 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.