繁体   English   中英

脚本或单行代码清除.bash_history中的密码

[英]script or one-liner to clean up passwords in .bash_history

有时我会在回显键入字符的linux终端中错误地输入su密码。 它被记录在~/.bash_history ,这让我感到不安全。 有没有人有一个简短的脚本(bash one-liner?)来清除.bash_history的任何纯文本密码?

使用sed在.bash_history文件中保留其自己的踪迹,但是如果可以暂时禁用readline和/或历史记录服务,则可能会起作用:

sed -ir -e 's/su_password/PASSWORD_REMOVED/g' ~/.bash_history

如果密码经常被用作其他短语/单词的一部分,这可能会带来其他问题/漏洞。

理想情况下,脚本应该只细读散列的passwd列表( /etc/shadow )以创建搜索项列表。 然后,它必须对要检查的文件部分( .bash_history )进行哈希.bash_history以进行比较。 问题在于,由于密码的长度未知,因此在比较期间需要了解文件中有多少文本要散列。 或者,在执行grep / sed之前,它可以像passwd一样以安全的方式请求密码。

不仅仅是一个函数,而是一个函数:

eh () { history -a ; vi + ~/.bash_history ; history -r ; }

将此行添加到您的.bashrc或.bash_profile中。 运行时

  1. 将缓冲区保存到您的.bash_history
  2. 在vi中打开.bash_history,但指针位于文件底部。
  3. 将编辑的文件恢复到当前历史记录缓冲区

在vi中,可以使用箭头键上下移动,用dd删除一行,然后使用[Esc]:wq关闭并写入文件

现在,您只需要在命令行中键入eh并编辑您的历史记录

eh代表“编辑历史记录”

$ echo -n password=; stty -echo; sed -i "s/$(head -1)/PASSWORD_REMOVED/g" ~/.bash_history; stty echo
top-secret password that never appears anywhere else to make it easy to guess
$ #you have to type it in and press enter

echo会提示您。 stty有助于防止人们看着您的肩膀。 确保逃脱任何内容(即反斜杠)。

我通常会执行echo > .bash_history来清除此问题。 尽管您的密码可能会显示在陌生的地方,所以您可能需要先执行sudo grep "password" -R / ,以查看其是否在系统上的其他位置,然后清除您的历史记录。

由于没有任何答案可以解决问题,因此我认为我应该共享当前使用的脚本。 当然,它不是单线的,甚至也不是bash……但它确实有效。

#!/usr/bin/env python
import os
user=os.getenv('USER')
if not user:
    user='hobs'
home=os.getenv('HOME')
if not home:
  home=os.path.normpath(os.path.join(os.path.sep+'home',user))
histfile=os.getenv('HISTFILE')
if not histfile:
    histfile=os.path.join(home,'.bash_history')
from optparse import OptionParser
p = OptionParser(usage="%prog [options] password", add_help_option=True)
p.add_option('-p', '--password', '--pass', '--pw', dest='pw',
             default =None,
             help="The plaintext password string you'd like to find and replace throughout your bash history file(s)", )
p.add_option('-r', '--replacement-password', '--replacement', '--filler', '--substitution', dest='rep',
             default ='',
             help="The replacement string, passphrase identifier, tag, or filler you'd like to leave behind wherever the password was found and removed.", )
p.add_option('-f', '--bash_history', '--historyfile', '--file', '--path', '--filename', dest='hfile',
             default =histfile,
             help="The text file where your password may have been accidentally recorded and where you'd like it removed. Default = ~/.bash_history.", )
(o, a) = p.parse_args()
if a and not o.pw:
    o.pw=' '.join(a) # password can have spaces in it
    print o.pw
    print len(o.pw)
# TODO: Check if the history buffer will record the invocation of this script that includes a plaintext password
#       Alternatively, launch the search/replace task in the background to start
#       after history has had a chance to record the last command in the history buffer
if o.pw:
    import warnings
    warnings.warn("Make sure you invoked "+p.get_prog_name()+" in such a way that history won't record this command (with a plaintext password) in the history file. It would be much  better if you didn't supply the password on the command line and instead allowed this script to securely prompt you for it.",RuntimeWarning)
if not o.pw:
    import getpass
    o.pw=getpass.getpass()
if len(o.pw)<4:
    raise ValueError(p.get_prog_name() + " doesn't accept passwords shorter than 4 characters long to prevent accidental corruption of files by purging common character combinations. The password you supplied is only "+str(len(o.pw))+" characters long.")
import fileinput
for line in fileinput.FileInput(o.hfile,inplace=1):
    line = line.replace(o.pw,o.rep)
    print line,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM