【问题标题】:script or one-liner to clean up passwords in .bash_history清理 .bash_history 中的密码的脚本或单行代码
【发布时间】:2011-11-03 06:29:53
【问题描述】:

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

使用 sed 会在 .bash_history 文件中留下自己的痕迹,但如果可以暂时禁用 readline 和/或历史服务,这可能会起作用:

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

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

理想情况下,脚本应该只仔细阅读散列密码列表 (/etc/shadow) 来创建搜索词列表。然后它必须散列它正在检查的文件的部分 (.bash_history) 以进行比较。问题是在比较过程中知道文件中有多少文本要散列,因为密码的长度是未知的。或者,它可以在执行 grep/sed 之前以安全的方式请求密码,就像 passwd 一样。

【问题讨论】:

  • 如果你以空格开始一个命令,它不会被记录。这可能对 sed 命令有所帮助。
  • 这并不能回答您的问题,但是如果我不时输入快速并错过密码提示,导致密码作为 bash 命令输入,我会以 @ 结束会话987654326@,终止登录会话而不执行注销功能。没有为会话写入历史记录。
  • @Kerrek SB:除非设置了HISTCONTROL=ignorespace,否则将以空格开头的命令将被记录。在依赖行为之前,您绝对应该检查此变量。
  • @Sorpigal:很好,谢谢!
  • @Sorpigal,谢谢。我想答案是检查 HISTCONTROL(或取消设置 HISTFILE),执行 sed(但在密码周围使用更严格的模式匹配),然后将 HISTCONTROL 和/或 HISTFILE 设置回原来的样子。

标签: linux security ubuntu sed input-history


【解决方案1】:

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

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 代表“编辑历史”

【讨论】:

    【解决方案2】:

    我通常会使用echo > .bash_history 来清除此问题。尽管您的密码可能会出现在陌生的地方,因此您可能需要先执行sudo grep "password" -R /,看看它是否在系统的其他任何地方,然后清除您的历史记录。

    【讨论】:

    • 是的,但我不想删除所有历史文件。我想保留除纯文本密码以外的所有密码。不过 grep 命令是个好主意。
    【解决方案3】:

    由于没有任何答案对我有用,我想我会分享我目前正在使用的脚本。它当然不是单行的,甚至不是 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,
    

    【讨论】:

      【解决方案4】:
      $ 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 有助于防止人们从你的肩膀上窥视。确保您转义任何内容(即反斜杠)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 2013-05-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多