【问题标题】:User and Password not matching DB用户和密码不匹配数据库
【发布时间】:2012-06-27 18:57:47
【问题描述】:

我正在测试一个 sql 查询,并且我正在正确输入用户名和密码,但它没有在匹配时给我。

这是查询的输出:

SELECT * FROM wp_users where user_login='User' and user_pass='pass'

我可以看到我输入正确但仍然返回 false。

我知道密码在数据库中的 md5 中。

我需要解码查询中的变量吗?如果有怎么办?

我试过了,但是没用:

$SQL =   "SELECT * FROM wp_users where user_login='".$username."' and user_pass='".md5($password)."'";

更新:

这是 md5($password) - 3fc0a7acf087f549ac2b266baf94b8b1 的内容

这就是数据库中的内容:$P$BfzHHqBF88q4RHJsAiVf4m7.ulbgCZ1

它们应该是相同的 :o/

【问题讨论】:

  • 当你说它返回 false 时,你是如何测试这个的?您是否尝试将第二个查询的输出直接粘贴到 php myadmin 或 mysql 命令行中以查看它的输出?
  • 查询是对的。如果返回 0 结果,则表示用户名或密码错误。只需md5($password) 并与数据库中的内容进行比较。
  • 发布一个来自数据库的哈希密码示例。显然,您需要为此使用一些测试帐户,而不是真实用户的密码。
  • plain md5() 是一种非常糟糕(非常轻描淡写)的密码存储方式。不妨将其存储为纯文本(不是真的但非常接近)。
  • 看起来这是一个 Wordpress 安装,在这种情况下,this question 中的信息及其答案应该会有所帮助。

标签: php mysql sql


【解决方案1】:

最简单的就是调用 Wordpress 函数 wp_hash_password(password)。在这种情况下,您的查询将如下所示:

require_once '/path/to/wp-config.php';  //will load up wordpress, such as if you're doing this from a script
$SQL =   "SELECT * FROM wp_users where user_login='".$username."' and user_pass='".wp_hash_password($password)."'";

Wordpress 不使用标准 MD5,而是使用 phpass 库来散列用户密码。因此,如果您尝试从 Wordpress 之外执行此操作,那么您将需要查看该库。下面是使用 phpass 实现 wp_hash_password() 方法的代码:

if ( !function_exists('wp_hash_password') ) :
/**
 * Create a hash (encrypt) of a plain text password.
 *
 * For integration with other applications, this function can be overwritten to
 * instead use the other package password checking algorithm.
 *
 * @since 2.5
 * @global object $wp_hasher PHPass object
 * @uses PasswordHash::HashPassword
 *
 * @param string $password Plain text user password to hash
 * @return string The hash string of the password
 */
function wp_hash_password($password) {
    global $wp_hasher;

    if ( empty($wp_hasher) ) {
            require_once( ABSPATH . 'wp-includes/class-phpass.php');
            // By default, use the portable hash from phpass
            $wp_hasher = new PasswordHash(8, TRUE);
    }

    return $wp_hasher->HashPassword($password);
}
endif;

按照同样的模式,你大概可以得到同样的内容:

require_once '/path/to/wp-includes/class-phpass.php';
$my_hasher = new PasswordHash(8, TRUE);
$SQL =   "SELECT * FROM wp_users where user_login='".$username."' and user_pass='". $my_hasher->HashPassword($password)."'";

【讨论】:

  • 我尝试了上述方法,哈希看起来非常相似,但仍然不匹配
  • 您可能需要查看特定版本的 wordpress 中的 wordpress 代码以了解更多详细信息
【解决方案2】:

您确定密码在存储到数据库之前没有加盐吗?如果是,您需要执行以下操作:

$SQL =   sprintf("SELECT * FROM wp_users where user_login='%s' and user_pass='%s'", $username, md5($salt.$password));

我也会研究 mysql_real_escape_string 和 PDO 准备好的语句,而不是直接的 mysql

【讨论】:

  • about 没有用,但你离我不远,因为我没有得到一个直接的 md5 哈希......它是不同的。我正在使用 wordpress 3.3 版
【解决方案3】:

看起来数据库中的密码是用Crypt函数存储的..看:

$pw="thisismypassword";
echo crypt($pw);    // $1$lmJBSDCp$AcU45N45sUhdglYn28T4X/
echo md5($pw);  //31435008693ce6976f45dedc5532e2c1

尝试在您的查询中使用 crypt。 Crypt 还使用我相信 wp_config.php 文件中的“盐”。

【讨论】:

    【解决方案4】:

    检查 INSERT/UPDATE wp_users 命令中使用的加密功能。 在 MySQL 中有一堆加密函数。看看here

    为了登录目的,经常使用Password(string)

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多