我需要访问joomla用户表jos_users
,以便从外部php脚本[codeignitor]进行登录检查。
Joomla像这样存储密码
4e9e4bcc5752d6f939aedb42408fd3aa:0vURRbyY8Ea0tlvnTFn7xcKpjTFyn0YT
看起来这不是正常的MD5,所以我无法使用md5(password)
。
创建密码的可能方法是什么?
谢谢。
Joomla密码是MD5哈希的,但密码在哈希之前被加盐。它们存储在数据库中{hash}:{salt}
此盐是一个长度为 32 个字符的随机字符串。
因此,要创建新的密码哈希,您需要执行md5($password.$salt)
编辑
好的,为了检查密码,假设用户myguy
输入密码mypassword
,您将从具有用户名myguy
的数据库中检索该行。
在这一行中,您会找到一个密码,上面写着4e9e4bcc5752d6f939aedb42408fd3aa:0vURRbyY8Ea0tlvnTFn7xcKpjTFyn0YT
。您将密码哈希和盐分开:
$hashparts = preg_split (':' , $dbpassword);
echo $hashparts[0]; //this is the hash 4e9e4bcc5752d6f939aedb42408fd3aa
echo $hashparts[1]; //this is the salt 0vURRbyY8Ea0tlvnTFn7xcKpjTFyn0YT
现在使用此盐和输入的密码myguy
计算哈希
$userhash = md5($userpassword.$hashparts[1]); // This would be 'mypassword' and the salt used in the original hash
现在,如果此$userhash
和$hashparts[0]
相同,则用户输入了正确的密码。
来自 joomla 论坛,这就是后面发生的事情:
A. Generate a password
B. Generate a string with 32 random characters
C. Concatenate Password (Step A) and RandomString (Step B)
D. Take md5(Result of Step C)
E. store Step D Result : Step B Result
例:
Generate a password - Let 'testing'
Generate a string of 32 random characters - 'aNs1L5PajsIscupUskaNdPenustelsPe'
Concatenate Password and random string - testingaNs1L5PajsIscupUskaNdPenustelsPe
md5(Step C Result) - 5cf56p85sf15lpyf30c3fd19819p58ly
store step d:step B - 5cf56p85sf15lpyf30c3fd19819p58ly:aNs1L5PajsIscupUskaNdPenustelsPe
你可以在Joomla中找到代码,比如
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword("testing", $salt);
$password = $crypt . ':' . $salt;
或者我们可以说
password DB field = md5(password + salt) + ":" + salt
其中盐是随机的 32 个字符字符串。
谢谢
在joomla标准中,您可以使用以下方式创建密码
jimport('joomla.user.helper');
$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password_choose, $salt);
$password = $crypt.':'.$salt;
您提到您正在从外部文件(或程序)访问,那么如果您在另一侧安装了 Joomla,则可以从 Joomla 结构外部访问它。
使用Joomla默认框架像这样工作
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
我不能使用preg_split
但explode
效果很好。
$hashparts = explode (':' , $dbpassword);
从 joomla 源文件库/joomla/crypt/password/simple 中.php它们有多种存储方式,有些没有":"字符。
switch ($type)
{
case '$2a$':
case JCryptPassword::BLOWFISH:
if (JCrypt::hasStrongPasswordSupport())
{
$type = '$2y$';
}
else
{
$type = '$2a$';
}
$salt = $type . str_pad($this->cost, 2, '0', STR_PAD_LEFT) . '$' . $this->getSalt(22);
return crypt($password, $salt);
case JCryptPassword::MD5:
$salt = $this->getSalt(12);
$salt = '$1$' . $salt;
return crypt($password, $salt);
case JCryptPassword::JOOMLA:
$salt = $this->getSalt(32);
return md5($password . $salt) . ':' . $salt;
}
}
Joomla!使用PhPass。
root/libraries/phpass/PasswordHash.php
请看这里。您将在此处看到密码是如何生成的。
$2y 是 bcrypt 哈希的默认(也是首选)前缀。至于代码,你需要查看JUserHelper's
hashPassword
和verifyPassword
方法的内部,看看Joomla现在如何处理事情。
一些参考资料 -
https://github.com/joomla/joomla-cms/blob/3.4.1/libraries/joomla/user/helper.php#L296-L387
https://docs.joomla.org/API15:JUserHelper/getCryptedPassword
https://docs.joomla.org/API15:JUserHelper/getSalt
检查链接,希望对您有所帮助:)
Joomla"理解"了"普通"md5的密码。
我过去所做的(测试用户的登录)是保存原始密码,在 md5 中加密一个新密码,在数据库中替换它,用浏览器测试它(它有效),完成后,将原始密码粘贴到数据库中。
<?php
$r = bin2hex(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM));
$p = 'the_password';
$s = $p . $r;
$m = md5($s);
$out = $m . ':' . $r;
echo $out;
Len 16,因为 bin2hex 使字符大小加倍,因为 1 个字节变成了 2 个字节
如果你只使用 md5($password); 它会起作用,请尝试一下。Joomla有一种机制,它可以处理多种类型的密码(包括最近的强密码)。您不必担心冒号后面的部分。只需使用 md5($password),它肯定会起作用。
顺便说一下,这也适用于Joomla 3.x。