引言
在本实验中,你将通过学习使用流行的开源密码破解工具 John the Ripper,深入了解密码安全的世界。理解密码如何被猜测或破解,对于制定更强大的安全实践至关重要。你将探索不同的密码猜测策略,从基础的暴力破解到更高级的组合攻击和混合攻击。通过本实验的结束,你将对这些技术以及如何使用 John the Ripper 应用它们有一个实际的理解。
理解常见的密码猜测技术
在本步骤中,你将学习密码猜测技术背后的基本概念,特别是字典攻击和简单的暴力破解。John the Ripper 可以利用这些方法来尝试破解密码。
首先,我们将使用 John the Ripper 通过字典攻击来破解一个简单的 MD5 哈希。我们已经准备了一个 passwords.txt 文件,其中包含一个哈希后的密码,以及一个 wordlist.txt 文件,其中包含常用密码。
打开你的终端并导航到 ~/project 目录。
cd ~/project
现在,使用 wordlist.txt 文件运行 John the Ripper:
john --format=raw-md5 --wordlist=wordlist.txt passwords.txt
你应该会看到输出表明 John 正在尝试破解密码。如果成功,它将显示破解出的密码。
Using default input encoding: UTF-8
Loaded 1 password hash (Raw-MD5 [MD5])
Cost 1 (MD5 [MD5]) is not supported.
Will run John the Ripper in single-threaded mode.
Press 'q' or Ctrl-C to abort, almost any other key for status.
password (user1)
1g 0:00:00:00 DONE (2023-10-27 08:00) 100.0g/s 100.0p/s 100.0c/s 100.0C/s password
要查看破解出的密码,你可以使用 --show 选项:
john --show passwords.txt
user1:password
1 password hash cracked, 0 left
这演示了一个基本的字典攻击。John the Ripper 成功地在提供的单词列表中找到了 "password"。
实现带特定模式的暴力破解
在本步骤中,你将通过 John the Ripper 的增量模式(incremental mode)来探索暴力破解攻击。增量模式允许 John 根据字符集(例如,小写字母、大写字母、数字、符号)和长度生成密码,从而有效地执行暴力破解攻击。
首先,让我们创建一个不在我们简单单词列表中的新密码哈希。我们将使用 testuser:testpass(testpass 的 MD5 哈希是 5d41402abc4b2a76b9719d911017c592)。
echo "testuser:5d41402abc4b2a76b9719d911017c592" > /home/labex/project/brute_passwords.txt
现在,我们将使用 John the Ripper 的增量模式。为了简单和快速,我们将限制字符集和密码长度。我们将使用 digits 模式来破解一个仅包含数字的密码。
假设我们有一个长度为 4 的数字密码,例如 1234。1234 的 MD5 哈希是 81dc9bdb52d04dc20036dbd8313ed055。
echo "digituser:81dc9bdb52d04dc20036dbd8313ed055" > /home/labex/project/digit_passwords.txt
现在,以增量模式运行 John the Ripper,专门针对数字:
john --format=raw-md5 --incremental=digits digit_passwords.txt
此命令将尝试所有数字组合,直到找到密码。这可能需要一些时间,具体取决于密码的长度。
Using default input encoding: UTF-8
Loaded 1 password hash (Raw-MD5 [MD5])
Cost 1 (MD5 [MD5]) is not supported.
Will run John the Ripper in single-threaded mode.
Press 'q' or Ctrl-C to abort, almost any other key for status.
1234 (digituser)
1g 0:00:00:00 DONE (2023-10-27 08:05) 100.0g/s 100.0p/s 100.0c/s 100.0C/s 1234
破解成功后,你可以显示结果:
john --show digit_passwords.txt
digituser:1234
1 password hash cracked, 0 left
这演示了当密码结构已知或受限时,如何使用暴力破解。
使用组合攻击(Combinator Attacks)
在本步骤中,你将学习组合攻击,它会将两个不同单词列表中的单词进行组合。当密码由两个常用单词连接而成时,这种方法非常有用。
首先,让我们创建一个用于组合攻击的新密码哈希。我们将使用 john_doe 作为密码(john_doe 的 MD5 哈希是 112233445566778899aabbccddeeff00)。
echo "combo_user:112233445566778899aabbccddeeff00" > /home/labex/project/combo_passwords.txt
我们已经准备了包含 "john" 和 "doe" 的 base_wordlist.txt。现在,让我们创建第二个单词列表 second_wordlist.txt:
echo "doe" > /home/labex/project/second_wordlist.txt
echo "smith" >> /home/labex/project/second_wordlist.txt
现在,使用 John the Ripper 的 Combinator 模式。此模式接受两个单词列表,并将第一个列表中的每个单词与第二个列表中的每个单词进行组合。
john --format=raw-md5 --rules=Combinator --wordlist=base_wordlist.txt --stdout > /home/labex/project/combined_words.txt
john --format=raw-md5 --wordlist=combined_words.txt combo_passwords.txt
第一个命令生成组合并将它们保存到 combined_words.txt。第二个命令然后使用这个生成的单词列表来破解密码。
Using default input encoding: UTF-8
Loaded 1 password hash (Raw-MD5 [MD5])
Cost 1 (MD5 [MD5]) is not supported.
Will run John the Ripper in single-threaded mode.
Press 'q' or Ctrl-C to abort, almost any other key for status.
john_doe (combo_user)
1g 0:00:00:00 DONE (2023-10-27 08:10) 100.0g/s 100.0p/s 100.0c/s 100.0C/s john_doe
要验证破解出的密码:
john --show combo_passwords.txt
combo_user:john_doe
1 password hash cracked, 0 left
这演示了当密码是已知单词的组合时,组合攻击如何有效。
使用 John the Ripper 探索混合攻击(Hybrid Attacks)
在本步骤中,你将学习混合攻击,它会将字典单词与暴力破解元素(如数字或特殊字符)结合起来。这是一种非常常见且有效的策略,用于破解常用单词变体的密码。
让我们为混合攻击创建一个新的密码哈希。我们将使用 password123 作为密码(password123 的 MD5 哈希是 28a112233445566778899aabbccddeeff)。
echo "hybrid_user:28a112233445566778899aabbccddeeff" > /home/labex/project/hybrid_passwords.txt
我们将使用现有的 wordlist.txt 并应用一个规则,该规则会将四个数字附加到每个单词后面。我们在设置中已经创建了一个简单的规则文件 rules.txt。
现在,使用 wordlist.txt 和自定义规则文件运行 John the Ripper:
john --format=raw-md5 --wordlist=wordlist.txt --rules=rules.txt hybrid_passwords.txt
此命令将从 wordlist.txt 中获取每个单词,并应用 rules.txt 中定义的规则(附加四个数字)。
Using default input encoding: UTF-8
Loaded 1 password hash (Raw-MD5 [MD5])
Cost 1 (MD5 [MD5]) is not supported.
Will run John the Ripper in single-threaded mode.
Press 'q' or Ctrl-C to abort, almost any other key for status.
password123 (hybrid_user)
1g 0:00:00:00 DONE (2023-10-27 08:15) 100.0g/s 100.0p/s 100.0c/s 100.0C/s password123
要验证破解出的密码:
john --show hybrid_passwords.txt
hybrid_user:password123
1 password hash cracked, 0 left
这展示了混合攻击在破解结合了字典单词和可预测模式的密码方面的强大能力。
开发自定义猜测策略
在本步骤中,你将学习如何通过创建和使用自己的规则文件(rule files)与 John the Ripper 一起开发自定义猜测策略。这使得基于已知的密码模式或用户习惯进行高度针对性的攻击成为可能。
让我们为自定义策略创建一个新的密码哈希。我们将使用 LabEx@2023 作为密码(LabEx@2023 的 MD5 哈希是 e0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5)。
echo "custom_user:e0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5" > /home/labex/project/custom_passwords.txt
现在,让我们创建一个名为 custom_rules.txt 的自定义规则文件,该文件将首字母大写,附加 @,然后附加四位数字。
nano /home/labex/project/custom_rules.txt
向 custom_rules.txt 文件添加以下内容:
:
c
$@
$[0-9]$[0-9]$[0-9]$[0-9]
:(空规则) - 这是一个占位符,通常用于开始一个规则集。c- 将单词的首字母大写。$@- 附加@符号。$[0-9]$[0-9]$[0-9]$[0-9]- 附加四位数字。
保存并退出 nano(Ctrl+X,Y,Enter)。
现在,让我们使用包含 "labex" 的基础单词列表:
echo "labex" > /home/labex/project/base_custom_wordlist.txt
使用你的自定义规则文件运行 John the Ripper:
john --format=raw-md5 --wordlist=base_custom_wordlist.txt --rules=custom_rules.txt custom_passwords.txt
Using default input encoding: UTF-8
Loaded 1 password hash (Raw-MD5 [MD5])
Cost 1 (MD5 [MD5]) is not supported.
Will run John the Ripper in single-threaded mode.
Press 'q' or Ctrl-C to abort, almost any other key for status.
LabEx@2023 (custom_user)
1g 0:00:00:00 DONE (2023-10-27 08:20) 100.0g/s 100.0p/s 100.0c/s 100.0C/s LabEx@2023
要验证破解出的密码:
john --show custom_passwords.txt
custom_user:LabEx@2023
1 password hash cracked, 0 left
这展示了 John the Ripper 规则引擎的灵活性,它允许你构建高度具体且有效的密码猜测策略。
总结
在本实验中,你获得了使用 John the Ripper(一个强大的密码破解工具)的实践经验。你学习并实现了各种密码猜测策略,包括:
- 字典攻击(Dictionary Attacks):使用预编译的常用密码列表。
- 暴力破解攻击(Brute-Force Attacks):在定义的集合内系统地尝试所有可能的字符组合。
- 组合攻击(Combinator Attacks):组合来自多个单词列表的单词以形成新的密码候选。
- 混合攻击(Hybrid Attacks):使用规则将字典单词与暴力破解元素融合。
- 自定义猜测策略(Custom Guessing Strategies):开发你自己的规则来针对特定的密码模式。
理解这些技术对于进攻性安全(渗透测试)和防御性安全(创建强密码策略和用户教育)都至关重要。通过了解常见的密码模式可以多么容易地被破解,你可以更好地体会复杂、独特的密码和多因素认证的重要性。


