misc-unix密码破解 #shadow#crypt

2019-01-07  本文已影响0人  Watanuki

看《python绝技》看到“你的第一个程序,一个unix密码破解器!”,涌上一种中学生回小学装逼的幸福感。但书里只写了test:$1$HXEtlo/Qz.0mA这种情况,对于现行的情况懒得提,所以更新下代码顺便复习(好吧是学习)unix密码相关知识点

FILE
/usr/lib/python2.7/lib-dynload/crypt.x86_64-linux-gnu.so
MODULE DOCS
https://docs.python.org/library/crypt
FUNCTIONS
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.
>>>import crypt
>>>crypt.crypt('19830617','$6$Jn47piWf$')  #密码,$算法类型$salt值
>>useradd nino
>>passwd nino  #19830617
>>cat /etc/shadow       
nino:$6$Jn47piWf$J2vYwIbHDgrAC4Lt3TD/KWj.F7Hu7W2wUmfA/J1iCKN7KyraKmQ9/jWcC9MLjiuICTKKTGB8t4vhEAx754Rq7/:17903:0:99999:7:::
#$6  表示类型标记为6的密码散列算法——SHA-512哈希算法;$Jn47piWf$指的是加盐(Salt)值
>>> crypt.crypt('19830617','$6$Jn47piWf$')  
'$6$Jn47piWf$J2vYwIbHDgrAC4Lt3TD/KWj.F7Hu7W2wUmfA/J1iCKN7KyraKmQ9/jWcC9MLjiuICTKKTGB8t4vhEAx754Rq7/'

完整代码

# -*- coding: utf-8 -*-
import crypt
import sys
import hashlib

file = '''
test:$1$HXEtlo/Qz.0mA  
root:$1$Bg1H/4mz$X89TqH7tpi9dX1B9j5YsF.:14838:0:99999:7:::
nino:$6$Jn47piWf$J2vYwIbHDgrAC4Lt3TD/KWj.F7Hu7W2wUmfA/J1iCKN7KyraKmQ9/jWcC9MLjiuICTKKTGB8t4vhEAx754Rq7/:17903:0:99999:7:::    
'''
#test:123456(HX是salt);root:123;nino:19830617
#dictionary=sys.argv[1]
#dictFile=open(dictionary,'r').readlines()

def testPass(cryptPass,dictFile):
    for word in dictFile:
        word=word.strip('\n')
        cryptWord=crypt.crypt(word,cryptPass)
        if (cryptWord==cryptPass):
            print "[+] Found Password:"+word+"\n"
            return word
    print "[-] Password Not Found.\n"
    
def main():
    passFile=file.split()#open('/etc/shadow').readlines()
    dictionary= ['123','19830617']
    for line in passFile:
        if ":" in line:
            user=line.split(':')[0]
            cryptPass=line.split(':')[1].strip()
            print "[*] Cracking Password For:"+user
            testPass(cryptPass,dictionary)

if __name__=="__main__":
    main()
运行结果

总结

上一篇下一篇

猜你喜欢

热点阅读