3

入力した文字をエコーし​​ているLinux端末にsuパスワードを誤って入力することがあります。~/.bash_historyそれは私を不安にさせる記録されます。.bash_historyプレーンテキストのパスワードを削除するための短いスクリプト(bashワンライナー?)を持っている人はいますか?

sedを使用すると、.bash_historyファイルに独自のトレースが残りますが、readlineや履歴サービスを一時的に無効にできる場合は、次のように機能する可能性があります。

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

また、パスワードが他のフレーズ/単語の一部として頻繁に使用される場合、これにより追加の問題/穴が生じる可能性があります。

理想的には、スクリプトはハッシュされたpasswdリスト(/etc/shadow)を熟読して、検索語のリストを作成する必要があります。次に.bash_history、比較のためにチェックしているファイルの一部をハッシュする必要があります()。問題は、パスワードの長さが不明であるため、比較中にハッシュするファイル内のテキストの量を知ることです。または、grep / sedを実行する前に、passwdのように安全な方法でパスワードを要求することもできます。

4

4 に答える 4

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「編集履歴」の略

于 2012-02-14T20:30:05.177 に答える
0
$ 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は、肩越しに見ている人から保護するのに役立ちます。何か(バックスラッシュなど)を必ずエスケープしてください。

于 2012-08-26T03:01:34.817 に答える
0

私は通常echo > .bash_historyこれをクリアするために行います。パスワードは奇妙な場所に表示される可能性があるため、sudo grep "password" -R / 最初にパスワードがシステム上の他の場所にあるかどうかを確認してから、履歴を消去することをお勧めします。

于 2011-08-25T07:06:13.670 に答える
0

うまくいった答えがなかったので、現在使っているスクリプトを共有したいと思いました。それは確かにワンライナーではなく、バッシュでもありません...しかしそれは機能します。

#!/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,
于 2011-08-27T15:25:52.483 に答える