跳到主要内容

Basic Auth、.htpasswd 与访问日志取证

HTTP Basic Auth 的认证头本质是 base64(username:password),只要流量没有被 TLS 保护,或者服务端日志、代理、抓包点可控,就可能恢复明文凭据。.htpasswd 则常用于保存用户名和密码哈希。

识别 Basic Auth

curl -i http://target:81/

典型响应:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Meeting Place"

读取 .htpasswd

如果已经有 WebShell、LFI 或备份泄露,优先查 Web 根:

cat /var/www/html/.htpasswd
cat /var/nginx/html/.htpasswd

典型格式:

cypher:$apr1$...

这能确认用户名,但是否能快速破解取决于哈希强度和字典质量。

从日志确认谁在访问

tail -n 50 /var/log/nginx/access.log
tail -n 50 /var/log/apache2/access.log

日志中可能出现:

172.17.0.2 - cypher [date] "GET / HTTP/1.1" 200

这说明有内部服务或定时任务正在带认证访问目标。

解码 Authorization 头

抓到请求后,提取头:

Authorization: Basic Y3lwaGVyOnBhc3N3b3Jk

解码:

echo 'Y3lwaGVyOnBhc3N3b3Jk' | base64 -d

输出格式:

cypher:password

哈希破解方向

如果只能拿到 .htpasswd

john htpasswd.txt --wordlist=/usr/share/wordlists/rockyou.txt
hashcat -m 1600 htpasswd.txt /usr/share/wordlists/rockyou.txt

$apr1$ 是 Apache MD5,Hashcat 模式通常是 1600

防坑点

  • HTTPS 下抓外部流量看不到 Basic Auth 明文,但服务端、反代、容器间 HTTP 仍可能明文。
  • .htpasswd 只能证明用户名和哈希,不能证明密码复用到 SSH。
  • 访问日志里看到用户名,是认证成功后的用户名字段,不是密码。

防御建议

  • Basic Auth 必须跑在 TLS 上。
  • .htpasswd 不应位于可被 Web 直接访问或可被低权限读取的位置。
  • 内部服务之间也不要用明文 HTTP 传 Basic Auth。
  • 对日志、代理、容器网络能力做最小权限隔离。

来源案例

  • Matrix-Breakout: 2 Morpheus.htpasswd 确认用户 cypher,nginx 日志证明容器定时带认证访问,后续通过 DNAT 捕获明文 Basic Auth。