Linux shadow 爆破脚本
Linux shadow 爆破初探
目的是为了明白其shadow爆破原理
# Linux shadow爆破初探 1
import crypt
#shadow文件中的一条用户数据
shadow_line = "ghui:$y$j9T$DQ2d2fD138oudY0Xa2wst/$dp/6WvvsTE2l50Iw1l.NvNPTIiJY6lmyB7sygyEj/S3:19618: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("$")] # rindex("$") 表示最后一次出现 $ 符的位置
print(f"[+] The salt is: {salt}")
# 从密码字典中,读取密码,假设此时读到的密码为 123456
password = "123456"
# 把读取的密码与盐值进行加密运算,得到猜测的密码密文
new_crypt_text = crypt.crypt(password,salt)
# 如果猜测的密码密文与shadow文件中的密码密文一致,说明密码猜对了。
if new_crypt_text == crypt_text:
print(f"[+] PASSWORD FOUND: {password}")
else:
print(f"[-] PASSWORD NOT FOUND!")
说明:
split(“:”)[1] 通过冒号
:
分隔符对字符串进行切片,并且取第二部分 **crypt_text.rindex(" " ) ∗ ∗ 函数返回子字符串 ‘ ")** 函数返回子字符串 ` ")∗∗函数返回子字符串‘
在字符串变量
crypt_text`中最后出现的位置
Linux shadow 爆破进阶
# Linux shadow 爆破 2
from termcolor import colored
import crypt
shadow_line = "ghui:$y$j9T$DQ2d2fD138oudY0Xa2wst/$dp/6WvvsTE2l50Iw1l.NvNPTIiJY6lmyB7sygyEj/S3:19618: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("$")] # rindex("$") 表示最后一次出现 $ 符的位置
print(f"[+] The salt is: {salt}")
# 从密码字典中,读取密码。
file_path="/home/kali/tools/wordlists/top_password.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(colored(f"\n[+] PASSWORD FOUND: {password}","green"))
exit()
print(f"[-] PASSWORD NOT FOUND!")
说明:
strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)