密碼破解技術與工具使用

Password Cracking Techniques and Tools

密碼破解概述

密碼破解(Password Cracking)是滲透測試中重要的一環,用於驗證密碼強度、恢復遺失的密碼,或在授權測試中取得系統存取權限。本文將介紹常見的密碼破解技術、工具使用方法以及防禦建議。

密碼破解的應用場景

  • 滲透測試:評估目標系統的密碼安全性
  • 數位鑑識:恢復加密檔案或系統的存取權限
  • 安全稽核:檢查組織內部密碼政策的落實程度
  • 密碼恢復:協助使用者恢復遺失的密碼

法律與道德考量

進行密碼破解前,務必確保:

  • 已取得系統擁有者的書面授權
  • 僅在合法範圍內進行測試
  • 妥善保管取得的敏感資訊

常見雜湊演算法

密碼通常不會以明文儲存,而是經過雜湊處理。了解常見的雜湊演算法有助於選擇正確的破解方式。

MD5

1
2
3
4
5
# MD5 雜湊範例(32 字元)
5f4dcc3b5aa765d61d8327deb882cf99  # "password" 的 MD5 雜湊

# 產生 MD5 雜湊
echo -n "password" | md5sum

SHA 系列

1
2
3
4
5
6
7
8
# SHA-1(40 字元)
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

# SHA-256(64 字元)
5e884898da28047d9d6f23ccdf9e66e8a1f3f0f93f3c6b76d1b33d3f5ab0a30f

# 產生 SHA-256 雜湊
echo -n "password" | sha256sum

常見雜湊格式識別

雜湊類型長度範例特徵
MD532純十六進位
SHA-140純十六進位
SHA-25664純十六進位
NTLM32純十六進位
bcrypt60$2a$$2b$ 開頭
SHA-512crypt106$6$ 開頭

使用 hash-identifier 識別雜湊類型

1
2
3
4
5
6
# 安裝 hash-identifier
sudo apt install hash-identifier

# 執行識別
hash-identifier
# 輸入雜湊值後會顯示可能的類型

攻擊類型

暴力攻擊(Brute Force)

暴力攻擊會嘗試所有可能的字元組合,直到找到正確的密碼。

優點

  • 理論上可以破解任何密碼
  • 不需要字典檔

缺點

  • 耗時極長
  • 計算資源需求高
1
2
3
4
# 暴力攻擊複雜度估算
# 8 位數字密碼:10^8 = 1 億種組合
# 8 位小寫字母:26^8 = 2088 億種組合
# 8 位混合字元:95^8 = 6.6 千兆種組合

字典攻擊(Dictionary Attack)

使用預先準備的密碼清單進行測試,是最常用的攻擊方式。

常見字典檔來源

  • rockyou.txt:超過 1400 萬個密碼
  • SecLists:多種用途的字典集合
  • 自訂字典:根據目標資訊製作
1
2
3
4
# 下載 rockyou.txt
sudo apt install wordlists
ls /usr/share/wordlists/rockyou.txt.gz
sudo gunzip /usr/share/wordlists/rockyou.txt.gz

規則攻擊(Rule-based Attack)

在字典攻擊的基礎上,套用變形規則來產生更多密碼變體。

常見規則變形

  • 大小寫轉換:password -> PasswordPASSWORD
  • 數字附加:password -> password123password2024
  • 特殊字元:password -> password!p@ssword
  • 字元替換:password -> p4ssw0rd

John the Ripper 使用

John the Ripper(簡稱 John 或 JtR)是一款開源的密碼破解工具,支援多種雜湊格式。

安裝

1
2
3
4
5
6
# Ubuntu/Debian
sudo apt update
sudo apt install john

# 安裝 jumbo 版本(功能更完整)
sudo apt install john-jumbo

基本使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 檢視支援的雜湊格式
john --list=formats

# 自動偵測格式並破解
john hashes.txt

# 指定格式破解
john --format=raw-md5 hashes.txt

# 使用字典檔
john --wordlist=/usr/share/wordlists/rockyou.txt hashes.txt

# 顯示已破解的密碼
john --show hashes.txt

破解 Linux 密碼

1
2
3
4
5
# 合併 passwd 和 shadow 檔案
sudo unshadow /etc/passwd /etc/shadow > unshadowed.txt

# 破解
john --wordlist=/usr/share/wordlists/rockyou.txt unshadowed.txt

破解 Windows NTLM 雜湊

1
2
3
4
5
# NTLM 雜湊格式:username:uid:lmhash:nthash:::
# 範例檔案內容:
# Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::

john --format=NT --wordlist=/usr/share/wordlists/rockyou.txt ntlm_hashes.txt

使用規則

1
2
3
4
5
6
7
8
# 使用內建規則
john --wordlist=/usr/share/wordlists/rockyou.txt --rules hashes.txt

# 指定規則集
john --wordlist=wordlist.txt --rules=best64 hashes.txt

# 檢視可用規則
john --list=rules

Hashcat 基礎使用

Hashcat 是目前最快的密碼破解工具,支援 GPU 加速運算。

安裝

1
2
3
4
5
6
# Ubuntu/Debian
sudo apt update
sudo apt install hashcat

# 驗證安裝
hashcat --version

雜湊模式代碼

模式代碼雜湊類型
0MD5
100SHA-1
1000NTLM
1400SHA-256
1800SHA-512crypt
3200bcrypt
5600NetNTLMv2

基本使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 字典攻擊
hashcat -m 0 -a 0 hashes.txt /usr/share/wordlists/rockyou.txt

# 參數說明:
# -m 0    : 雜湊類型(0 = MD5)
# -a 0    : 攻擊模式(0 = 字典攻擊)

# 暴力攻擊
hashcat -m 0 -a 3 hashes.txt ?a?a?a?a?a?a

# 字元集:
# ?l = 小寫字母 (a-z)
# ?u = 大寫字母 (A-Z)
# ?d = 數字 (0-9)
# ?s = 特殊字元
# ?a = 所有字元

規則攻擊

1
2
3
4
5
6
7
# 使用規則檔
hashcat -m 0 -a 0 hashes.txt wordlist.txt -r /usr/share/hashcat/rules/best64.rule

# 常用規則檔
# best64.rule      : 最常用的 64 條規則
# rockyou-30000.rule : 大型規則集
# dive.rule        : 深度變形規則

顯示結果

1
2
3
4
5
# 顯示已破解的密碼
hashcat -m 0 hashes.txt --show

# 輸出到檔案
hashcat -m 0 -a 0 hashes.txt wordlist.txt -o cracked.txt

字典檔與規則檔

常用字典檔

1
2
3
4
5
6
7
# SecLists 安裝
sudo apt install seclists

# 常用路徑
/usr/share/seclists/Passwords/Common-Credentials/
/usr/share/seclists/Passwords/Leaked-Databases/
/usr/share/wordlists/rockyou.txt

自訂字典製作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 使用 CeWL 從網站抓取關鍵字
cewl https://target.com -d 2 -m 5 -w custom_wordlist.txt

# 參數說明:
# -d 2 : 爬取深度
# -m 5 : 最小字詞長度
# -w   : 輸出檔案

# 使用 crunch 產生字典
crunch 6 8 abcdefghijklmnopqrstuvwxyz0123456789 -o wordlist.txt

# 參數說明:
# 6 8  : 最小與最大長度

Hashcat 規則語法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 常用規則範例
l          # 轉小寫
u          # 轉大寫
c          # 首字大寫
$1         # 結尾加 1
^1         # 開頭加 1
sa@        # 將 a 替換為 @
so0        # 將 o 替換為 0

# 建立自訂規則檔 custom.rule
echo 'c' > custom.rule
echo '$1' >> custom.rule
echo '$!' >> custom.rule
echo 'sa@' >> custom.rule

GPU 加速破解

檢查 GPU 支援

1
2
3
4
5
# 檢視可用裝置
hashcat -I

# 指定 GPU 裝置
hashcat -m 0 -a 0 -d 1 hashes.txt wordlist.txt

效能比較

雜湊類型CPU 速度GPU 速度(RTX 3080)
MD5~50 MH/s~60 GH/s
SHA-1~30 MH/s~20 GH/s
NTLM~50 MH/s~100 GH/s
bcrypt~500 H/s~28 KH/s

最佳化設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 設定工作負載
hashcat -m 0 -a 0 -w 3 hashes.txt wordlist.txt

# -w 參數:
# 1 = 低(適合桌面使用)
# 2 = 預設
# 3 = 高
# 4 = 極限(可能導致系統無回應)

# 使用多 GPU
hashcat -m 0 -a 0 -d 1,2 hashes.txt wordlist.txt

彩虹表攻擊

彩虹表是預先計算好的雜湊對應表,可以快速查詢密碼。

概念說明

1
2
傳統暴力:每次都要計算 hash(password) 並比對
彩虹表:直接查表 hash -> password(空間換時間)

使用 RainbowCrack

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 安裝
sudo apt install rainbowcrack

# 產生彩虹表
rtgen md5 loweralpha 1 7 0 3800 33554432 0

# 排序彩虹表
rtsort *.rt

# 破解
rcrack *.rt -h 5f4dcc3b5aa765d61d8327deb882cf99

線上彩虹表查詢

對於常見的雜湊值,可以使用線上服務:

彩虹表的限制

  • 加鹽雜湊:現代系統會在密碼中加入隨機鹽值,使彩虹表失效
  • 儲存空間:完整的彩虹表需要 TB 級儲存空間
  • 特定演算法:每種雜湊演算法需要獨立的彩虹表

防禦建議

密碼政策

  1. 長度要求:至少 12 個字元
  2. 複雜度:混合大小寫、數字、特殊字元
  3. 避免常見密碼:禁用字典中的常見密碼
  4. 定期更換:根據風險等級設定更換週期

安全儲存

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 使用 bcrypt 加密密碼(Python 範例)
import bcrypt

# 產生雜湊
password = b"secure_password"
salt = bcrypt.gensalt(rounds=12)
hashed = bcrypt.hashpw(password, salt)

# 驗證密碼
bcrypt.checkpw(password, hashed)

系統加固

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 設定密碼複雜度要求(Linux PAM)
sudo apt install libpam-pwquality

# 編輯 /etc/security/pwquality.conf
minlen = 12
dcredit = -1
ucredit = -1
lcredit = -1
ocredit = -1

# 設定帳號鎖定政策
# 編輯 /etc/pam.d/common-auth
auth required pam_tally2.so deny=5 unlock_time=900

多因素認證

  • 啟用 2FA/MFA
  • 使用硬體安全金鑰
  • 實施風險基礎認證

總結

密碼破解是滲透測試中的重要技能,掌握 John the Ripper 和 Hashcat 等工具的使用,能夠有效評估系統的密碼安全性。同時,了解攻擊手法也有助於建立更完善的防禦機制。

重點回顧

  1. 了解雜湊演算法:正確識別雜湊類型是破解的第一步
  2. 選擇攻擊方式:根據情況選擇字典、規則或暴力攻擊
  3. 善用 GPU 加速:大幅提升破解效率
  4. 注意法律合規:僅在授權範圍內進行測試

參考資料

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy