Python The real advantage of the language is that it has a large number of standard libraries and third-party libraries . To write UNIX Password cracking machine , Need to use UNIX Calculate the password hash Of crypt() Algorithm . start-up Python Interpreter , notice Python The standard library has crypt library , To compute an encrypted UNIX password hash, Just call the function crypt.crypt(), And drink the password salt Passed to it as a parameter . This function will return the password as a string hash.
Let's try to use crypt() Function to quickly calculate the password hash. After importing the library , Will password “egg” And salt“HX” Pass to function . This function returns the password hash– String is “HX9LLTdc/jiDE”. success !
import crypt
crypt.crypt('egg', 'HX')
'HX9LLTdc/jiDE'
Now write a program to traverse the entire dictionary , Add the specified... To each word salt The calculation results are all related to the encrypted password hash compare . When writing a program , First create two functions :main and testpass. According to their specific functions , Separate programs into independent functions .main Function to open the encrypted password file “password.txt”, And read the contents of the password file line by line . The user and password in each line hash Are separated . For each password hash,main Functions call testPass() function , Try to crack it with the words in the dictionary .
testPass() Encryption password for function parameters hash, The hash The first two characters of are treated as salt, And extract it , Then open the dictionary and facilitate each word in the dictionary , Use each word and salt Calculate a new encryption password hash. If the calculation result is consistent with our encrypted password hash matching , The function prints a message that the password has been found , And back to . otherwise , It will continue to test each word in the Thesaurus . Source code is as follows :
import crypt
def 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")
return
print("[-] Password Not Found.\n")
return
def 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: " + user)
testPass(cryptPass)
if __name__ == '__main__':
main()
After running this program , You can see that the user has been successfully cracked victim The password of , As shown in the figure below :