Skip to main content

Creating password linux cracker with python (☞ ͡° ͜ʖ ͡°)☞ (day 21)



Ever since 2017, python programming language had a steady increased of popularity not just in the area of data analysis but in penetration testing. If you try to look into one of the famous websites for penetration testing, "exploit DB" you can sense that programming language that usually used to create ax exploit is either ruby, c or python.

So in this post, I'm going to show you how you can implement python into your pentesting project. We are going to create your own password Linux cracker with python.

Background:

Before we go to the actual coding, let me explain it to you about some fundamental concepts:

  • First, we need to know how Linux stored its password. Originally, Linux stored its own password in two different file one is located at "/etc/provides" which is contained the username (every user inside the operating system can see inside of the file ) and the other one which is located at "/etc/shadow" contained the actual password but it stored in sha512 hash. You can see the format of the Linux password below
 $ID$SALT$ENCRYPTED
    • The ID is for what type of encryption do you use:
      • $1 = MD5
      • $2a = blowfish 
      • $2 = blowfish
      • $5 = SHA-256
      • $6 = SHA-512 
    • The SALT random strings (8 <= n <= 16 char)
    • $ENCRYPTED, the encrypted string
  • For quick testing, you can use "I" utility from Linux to generate your own password

Getting your hand dirty:

we need to import some important libraries, crypt for doing the hashing process and sys to add some command line functionality in the program:

import crypt
import sys



Next is to create a function that will return the hash:

def crack_password(salt,plain_text):
        return crypt.crypt(plain_text,"$6$%s" % salt)

The functionality of the crypt library is almost the same as mkpasswd you need to provide three value, the id, the salt, and the plain text. The rest of the source code is like this:

file = open(sys.argv[1])
result = []

for x in file.readlines():
        temp = x.rstrip("\n")
        salt = temp.split("$")[2]
        dictionary = open(sys.argv[2])
        for y in dictionary.readlines():
                temp2 = y.rstrip("\n")
                if crack_password(salt,temp2) == temp:
                        result.append(temp2)
                else:
                        print "try:" + y.rstrip("\n")
print "----------------result----------------"
print result

file.close()
dictionary.close()

the program i create assume that the user provide two files, first is the file contain a string of Linux password that will be decrypted and the second file is the dictionary file that will use to match the hash (https://github.com/danielmiessler/SecLists/tree/master/Passwords)

that's why for usablity i use the sys function so all you have to do is to write the name of the two files in front of the program and it will automatically do the decryption.

example: python password_decryptor.py file_contain_linux_password file_contain_dictionary_list

the first loop deal with iterating through the encrypted word inside the file, don't forget to strip the "\n" at the end of every line

the second loop will try to match the result of the dictionary list with the encrypted word. Once the program got a match result it will append to the result variable.

Result:

The file contains Linux password(first file):


$6$nnghqtzilxdhtkfk$CD0wgI..IQSGKobZ/PNW8zxW.VYKdU700rk9rILxrOR.M9yBntu1PrYEzzxLsiDyyjsISlSm.mkhh239X2ROc0
$6$aevmjnyzbrnqrxkq$TUbMkd34Gvk87s5iiF5VBveNIDsy/MNPyGCMTAPyYZjEPI100RUdwbY6K.WV83oqPBq7B7wlWqaW2ZtZtbvW01
$6$ysuucnvahyhnutcg$8g8MHX.Uh2FGaig6lAFOijcM8zXf4.7Ui0zp9l04ECZeP.5OIeukp0JNPZSipDkDSCLrP4LfzUXSSVemNXDwJ/
$6$kunaekvqyhyouoqu$DClxoXyV8BmkaV0q8zd4otc5aUZDdPWVNlIRwqDWZ2MtM7Sya156CpD/vVqaIyjPeKCJmmFvsn5DNHyJWGzKl0

The file contains dictionary list(second file):





Run it:




 Yea we got the string, that's all have a great weekend guys











Comments

Popular posts from this blog

Having fun analyzing nginx log to find malicious attacker in the net (ง'̀-'́)ง (day 37)

  What makes you sleepless at night? is it because of a ghost or scary stories? is it because you have an important meeting tomorrow? or is it because you have an exam? For me, what keeps me up all night is that I keep thinking about what happens to a website that I just created, is it safe from an attacker (certainly not) or did I missing some security adjustments that lead to vulnerability? well I'm not the best secure programmer in the world, I'm still learning and there is a big possibility that I can make a mistake but for me, a mistake can be a valuable investment to myself or yourself to be better so from this idea, I want to know more about what attackers casually do when attacking a website. Here in this post, I'm going to show you how I analyzed attack to the website that I have permission to design and also some interesting findings that I could get from the analysis Background: All of this analysis comes from the traffic that is targeted to th

Utilize Pwntools for crafting ROP chain :') (day 69)

who doesn't like pwntools? it is a very versatile tool and can be customized according to our need using the python script but did you need to know that pwntools itself can help us to automatically craft a rop chain for us? so in this post, I will show you how to make rop chain less painful and make pwntools do all the heavy lifting. To demonstrate this I will use the binary challenge callme 64 bit from ropemporium link: https://ropemporium.com/challenge/callme.html Crashing the app: Like any other exploitation process, we need to crash the program by generating a long string pattern to determine the offset. based on the information from the above figure we can see that we required to provide 40 bytes of offset Fun stuff: now this where the fun stuff began write the following python script: as in the guideline of the challenged said we need to chain the function call by first to call the callme_one function, callme_two function and then callme_three funct

WriteUp PWN tarzan ROP UNICTF ಠ_ಠ (day 61)

So in this post, I'm going to talk about how to solve the Tarzan pwn challenge from UNICTF 2019. Back in the day when the competition is still going I couldn't finish it and don't have any clue to solve this but this time I was able to finish it :) Also in this post, we will be going to be heavily focused on how to utilize pwntools to construct a ROP chain. If you kinda confused about my explanation in this post you can refer to this following youtube video, link: https://www.youtube.com/watch?v=gWU2yOu0COk I build the python script based on this video Ok, let's get started! In this challenge, you will get two binary first go with tarzan and libc-2.29.so by providing .so file it tell us what version library that the target machine is using this could help us to do ROP chain. first, we run the Tarzan binary to get the basic idea of the program work and as you can see it just show you some text, newline and when you try to input something it doesn't gi