【问题标题】:Verify user and password against a file created by htpasswd根据 htpasswd 创建的文件验证用户和密码
【发布时间】:2014-06-10 17:12:13
【问题描述】:

有没有办法从命令行根据 Apache 提供的工具 htpasswd 创建的文件检查用户和密码?

【问题讨论】:

  • 嗯。关于htpasswd程序的问题有一个htpasswd标签,但系统不会让它与.htpasswd标签共存...
  • 你还没有接受答案。真丢脸。

标签: apache .htpasswd


【解决方案1】:

您可以为此使用htpasswd 工具。

# create htpasswd_file with user:password
$ htpasswd -cb htpasswd_file user password
Adding password for user user

# verify password for user
$ htpasswd -vb htpasswd_file user wrongpassword
password verification failed

$ htpasswd -vb htpasswd_file user password
Password for user user correct.

退出状态为 0 表示成功,3 表示失败。

【讨论】:

  • 省略-b 并在提示中输入密码通常更安全。使用上面的命令,明文密码可能会出现在你的.bash_history中。
【解决方案2】:

假设您使用以下命令创建密码,并将“myPassword”作为密码

htpasswd -c /usr/local/apache/passwd/passwords username

这将创建一个看起来像这样的文件

username:$apr1$sr15veBe$cwxJZHTVLHBkZKUoTHV.k.

$apr1$ 是哈希方法,sr15veBe 是盐,最后一个字符串是哈希密码。您可以使用 openssl 使用

对其进行验证
openssl passwd -apr1 -salt sr15veBe myPassword

哪个会输出

$apr1$sr15veBe$cwxJZHTVLHBkZKUoTHV.k.

您可以使用的管道是:

username="something"
htpasswd -c /usr/local/apache/passwd/passwords $username
****Enter password:****

salt=$($(cat passwords | cut -d$ -f3)
password=$(openssl passwd -apr1 -salt $salt)
****Enter password:****

grep -q $username:$password passwords 
if [ $? -eq 0 ]
 then echo "password is valid"
else 
 echo "password is invalid"
fi

您可能需要更改您的 openssl 命令,因为 Apache 的 htpasswd 命令在每个系统上的加密方式略有不同。

有关更多信息,请访问 Apache 的主题页面http://httpd.apache.org/docs/2.2/misc/password_encryptions.html

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 2018-11-02
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多