MySQL散列了列密码+脚本bash

时间:2015-08-29 19:35:23

标签: mysql bash sha1 openvpn

我有点问题。

我创建了一个MySQL表,其密码列用SHA1加密。

mysql> CREATE TABLE IF NOT EXISTS user_encrypted (
    username varchar(50) COLLATE utf8_unicode_ci NOT NULL PRIMARY KEY DEFAULT 'username',
    hashed_password varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'password',
    user_mail varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL DEFAULT 'your@email.com',
    user_phone varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL DEFAULT '+33 1 23 45 67 89',
    user_online tinyint(1) NOT NULL DEFAULT '0',
    user_enable tinyint(1) NOT NULL DEFAULT '1',
    user_max_connection tinyint(1) NOT NULL DEFAULT '2',
    user_start_date date NOT NULL DEFAULT '2015-05-01',
    user_end_date date NOT NULL DEFAULT '0000-00-00',
    KEY  hashed_password (hashed_password)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

结果

Query OK, 0 rows affected (0.01 sec)

我是一个调用MySQL表的脚本:

#!/bin/bash
. /etc/openvpn/script/config.sh

#New Encrypted_password
#username=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "select username from user_encrypted where username = '$username' AND hashed_password = '$password' AND user_enable=1 AND user_max_connection=2 AND user_start_date != user_end_date AND TO_DAYS(now()) >= TO_DAYS(user_start_date) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")

##Check user
[ "$username" != '' ] && [ "$username" = "$username" ] && echo "user : $username" && echo 'authentication ok.' && exit 0 || echo 'authentication failed.'; exit 1

问题是,当我使用脚本时,OpenVPn问我用户/密码,我使用Test和Test1234,但系统拒绝密码。 它似乎是未经加密的&#34;密码无法转换为加密密码,以检查表的密码是否与我在客户端OpenVPN中输入的密码相同。

我不知道怎么做,在我的脚本bash中输入的密码是&#34;转换&#34;加密密码以便检查数据库...

谢谢你

1 个答案:

答案 0 :(得分:0)

您的bash脚本不会对密码进行哈希处理。因此,您将密码与密码的哈希进行比较。

你说它是一个SHA1哈希,它可以方便地存在a built-in function in MySQL。改变

where username = '$username' AND hashed_password = '$password' ...

where username = '$username' AND hashed_password = SHA1('$password') ...

应该解决您当前的问题。

我想在你的代码中提出另外两个问题 - 遗憾的是,这些问题很常见:​​

  1. 您似乎没有使用salt作为密码哈希。你应该 - 这会让攻击者更难以从哈希中找出你的密码。请阅读this
  2. 您的登录脚本容易受到SQL注入攻击。如果攻击者将admin'; SELECT * FROM user_encrypted WHERE 0 AND '之类的字符串(或某种效果)作为用户名放置怎么办?请阅读this
相关问题