From 6d8873dbcdbcb58226c96fde7eef5239f634c1dd Mon Sep 17 00:00:00 2001 From: zhao Date: Mon, 30 Dec 2024 01:09:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20bin/=20ZeroWrt.backup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhao --- bin/ ZeroWrt.backup | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/bin/ ZeroWrt.backup b/bin/ ZeroWrt.backup index 5605306..ab91dae 100644 --- a/bin/ ZeroWrt.backup +++ b/bin/ ZeroWrt.backup @@ -87,7 +87,21 @@ change_password() { color_output "\e[34m[更改管理员密码]\e[0m" read -p "请输入新的管理员密码: " new_password if [[ -n "$new_password" ]]; then - echo -e "$new_password\n$new_password" | passwd root + # 使用 openssl 生成密码哈希,使用 -1 参数生成 MD5 格式的密码哈希 + password_hash=$(openssl passwd -1 "$new_password") + + # 获取当前 shadow 文件的其他行 + tail -n +2 /etc/shadow > /tmp/shadow.tmp + + # 创建新的 root 行 + echo "root:$password_hash:0:0:99999:7:::" > /etc/shadow + + # 添加其他行 + cat /tmp/shadow.tmp >> /etc/shadow + + # 清理临时文件 + rm -f /tmp/shadow.tmp + color_output "\e[32m管理员密码已成功更改。\e[0m" else color_output "\e[31m无效的密码,操作取消。\e[0m"