昨天在讨论区打草稿写了个Windows借助7z工具暴破rar的批处理脚本,今天这次就写了个shell的。这篇帖子仅适用于Mac平台,只关心Windows的坛友可以看 【讨论】一步步手撸一个压缩包密码爆破的批处理脚本,毕竟Windows是多数,我自己也经常用。
本来一开始是打算用python的,但一想python写保存路径,到最后还是放弃了,因为python的目录写法,终究逃不掉 Windows c:/xxx/ 、Mac ~/xxxx/ ,这就造成了平台上的不适配,那我还不如索性写两份体验体验。
谈谈这次遇到的小问题,主要问题其实在注释上已经说明了,不过还是单拿出了,方便观看与讨论:
- 发现
brew install p7zip > /dev/null 2>&1 运行效率较低,也算是个无心之举。跑代码测试的时候,我发现这种写法,明显感觉比if 慢好多,所以就pass掉了。
- 在Mac上,7z并不支持验证rar压缩包密码;Mac下面的rar,也不支持zip的密码验证。这个是p7zip与rar(两者都是 brew 安装)的实验结论。
- 密码暴破没成功,没有输出echo,主要是自己逻辑上出现了问题:break之后又执行了常规的输出,定位代码处
# echo "$?" ,需要外部使用变量来固化状态值,再做判断(类似批处理的延迟变量)。
细节方面就是:dos2unix,关于LF格式和CRLF格式的TXT文件了,统一处理,问题不大。
代码本地测试效果

在线测试效果图,顺带还发现了个有趣的现象:特权提升的$username,由自己的本地用户名,变成了“root”


与Windows版本的效果

整体来说,由于系统的差异明显,以及batch、shell的实现各有差异,殊途同归了,算是。可以粗糙理解为类似《富士山下》、《爱情转移》粤语歌与普通话的两种唱法。
附源码:https://github.com/hoochanlon/ihs-simple/blob/main/d-shell/7z_rar_sensei.sh
[ ! -e $(which rar) ] && brew install rar
[ ! -e $(which 7z) ] && brew install p7zip
[ ! -e $(which dos2unix) ] && brew install dos2unix
if [ ! -f ~/Downloads/rarpasswd.txt ]; then
curl -o ~/Downloads/rarpasswd.txt https://ghproxy.com/https://raw.githubusercontent.com/hoochanlon/ihs-simple/main/d-txt/rarpasswd.txt
fi
username=$USER
passwd_txt="/Users/$username/Downloads/rarpasswd.txt"
dos2unix $passwd_txt >/dev/null 2>&1
echo -e "\n"
read -p "将压缩包文件拖入到终端: " has_passwd_rar
found_passwd_tag_num=0
unrar_passwd_find() {
while read password; do
unrar t -p$password "$has_passwd_rar" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "\n密码是: $password \n"
found_passwd_tag_num=1
break
fi
done <$passwd_txt
if [ $found_passwd_tag_num -ne 1 ]; then
echo -e "\n没找到正确的密码。\n"
fi
}
7z_passwd_find() {
while read password; do
7z t -p$password "$has_passwd_rar" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "\n密码是: $password \n"
found_passwd_tag_num=1
break
fi
done <"$passwd_txt"
if [ $found_passwd_tag_num -ne 1 ]; then
echo -e "\n没找到正确的密码。\n"
fi
}
if [[ ${has_passwd_rar##*.} == "rar" ]]; then
unrar_passwd_find
else
7z_passwd_find
fi
|