php - 使用 PHP SHA512 和 Salt 更改 Filezilla 用户密码
问题描述
我们正在尝试将我们的 Filezilla Server 0.9.6 用户密码与用户选择用于登录我们网站的密码相匹配。所有谷歌搜索都指向实现 MD5 的旧解决方案,但新版本使用 SHA512 和 Salt。下面的代码成功更新了服务器 XML 文件,但重启 Filezilla 后新密码不起作用。
有没有人有这方面的经验,并且可以指出这段代码不正确的地方?
我的代码 来自https://forum.filezilla-project.org/viewtopic.php?t=39934
<?php
$password = "myNewPassword";
$seed = str_split("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
shuffle($seed);
$rand = '';
foreach (array_rand($seed, 64) as $k) {
$rand .= $seed[$k];
}
$salt_raw = utf8_encode($rand);
$salt_html = htmlentities($salt_raw);
$password = utf8_encode($password);
$salted_password = $password . $salt_raw;
$encoded_password = strtoupper(hash('SHA512', $salted_password));
// For Filezilla
$FzPass = $encoded_password;
$FzSalt = $salt_html;
$xmlFile = 'C:\path-to\FileZilla Server.xml';
$xml = simplexml_load_file($xmlFile);
$Pass = $xml->xpath("//User[@Name='TheUserName']//Option[@Name='Pass']");
$Salt = $xml->xpath("//User[@Name='TheUserName']//Option[@Name='Salt']");
$Pass[0][0] = $FzPass;
$Salt[0][0] = $FzSalt;
$xml->formatOutput = true;
$saved = $xml->asXML($xmlFile);
chdir( 'C:\FileZilla Server' );
exec( '"C:\FileZilla Server\FileZilla Server.exe" /reload-config' );
PHP 执行前 Filezilla Server.xml
<?xml version="1.0" ?>
<FileZillaServer>
<Users>
<User Name="TheUserName">
<Option Name="Pass">1ED89AFBA4FD8EF177EAAF21E6641C33FC870860D5F399CD9B0A79632B0F2B08B235DB7258DB2B6EB58402AF29A4C8491CC8F82ED9D2E74EABA8F7B240D0615F</Option>
<Option Name="Salt">-Z5c@tci:e?E4E@H@(cq)/W1zDMs#F>XsYd$M)0t#{/s,tc@]GJYf@{@T.&O*5sF</Option>
<Option Name="Group"></Option>
<Option Name="Bypass server userlimit">1</Option>
<Option Name="User Limit">0</Option>
<Option Name="IP Limit">0</Option>
<Option Name="Enabled">1</Option>
<Option Name="ForceSsl">0</Option>
</User>
</Users>
</FileZillaServer>
PHP 执行后
<?xml version="1.0" ?>
<FileZillaServer>
<User Name="producer">
<Option Name="Pass">57AECC6BBCCC8EE5A73F0E362E2434DBF617EC2C85404205D881B1B7A2D303405789F8B42DFE61EDD7CF197700B66307E998054615C8A9A1CA5A9988029F0EAD</Option>
<Option Name="Salt">P,]mk&amp;4N;v3`#&lt;e8S?oj[U=Ja$}Q6~:\Z1{HuB&gt;bnI/&quot;M-pDh.qFylKfd0|LgT*G</Option>
<Option Name="Group"/>
<Option Name="Bypass server userlimit">1</Option>
<Option Name="User Limit">0</Option>
<Option Name="IP Limit">0</Option>
<Option Name="Enabled">1</Option>
<Option Name="ForceSsl">0</Option>
</User>
</FileZillaServer>
解决方案
SimpleXML 将自动编码* XML 实体,因此无需在您的 PHP 代码中使用htmlentities()
. 此外,我已经更新了您的代码以包含适当的随机盐生成器。试试看!
<?php
$password = "myNewPassword";
$salt = "";
while (strlen($salt) !== 64) {
$byte = ord(openssl_random_pseudo_bytes(1));
if ($byte > 31 && $byte < 127) {
$salt .= chr($byte);
}
}
$hashed_password = strtoupper(hash('SHA512', $password . $salt));
$xmlFile = 'server.xml';
$xml = simplexml_load_file($xmlFile);
$Pass = $xml->xpath("//User[@Name='TheUserName']//Option[@Name='Pass']");
$Salt = $xml->xpath("//User[@Name='TheUserName']//Option[@Name='Salt']");
$Pass[0][0] = $hashed_password;
$Salt[0][0] = $salt;
$xml->formatOutput = true;
$saved = $xml->asXML($xmlFile);
*有一些特殊字符 ( &
, <
, >
, "
) 需要编码,以免与标记的一部分混淆(并非总是如此,但总是对它们进行编码更容易。)它们分别编码为&
,<
和。由于编码值中有一个特殊字符,您可以通过查找我们在示例 XML 中看到的内容来查看双重编码。>
"
&lt;
推荐阅读
- openlayers - 使用 ol-ext (openlayers ext) 渲染透视图时遇到问题
- centos7 - ValueError: 端口 tcp/8090 已定义
- python - 如何按日期汇总 Django 模型字段并根据当前月份和年份显示总计
- java - 在 AdoptOpenJDK 维护的镜像中采用openjdk alpine vs alpine-slim
- typescript - 要求 NPM 下载 TypeScript 源代码,而不仅仅是 *.d.ts 文件
- python - 张量 (64) 的扩展大小必须与非单维 2 处的现有大小 (66) 匹配
- r - 使用 R 将特定的行值转换为列
- c# - 从另一个表单更改表单的 TopMost
- sql - 如何使用另一个表作为过滤器来汇总代理状态时间秒数
- apache - 为什么 iptables 不能使用 x-forwarded-for 处理字符串匹配?