Hacking with Python series: Open user and password files to read

This article will help you to code the part where you need to open the user and pass file in python. One of the part where you will use this code is while brute forcing a certain port. Before looking at the code, I am providing the explanation about how the code works!

Explanation :

#!/usr/bin/python – This line tells the shell in the terminal to interpret the following code under /usr/bin/python. In windows you don’t need to specify this file. You can also specify /usr/bin/python3, if you wish to use python3. If you don’t specify this line, it will take forever to interpret python on terminal because it will running under shell script.

def file_Read() is the function which actually contains the code that reads the file.

***NOTE: If you see closely, the file opening and reading code is in Try and except block. You should always put your file code in try and except block because it is likely to generate I/O exception or file not found exception which you want to deal with! 

except Exception as e: e here takes the exception and print line prints the exception if there is any. It is important to specify the exact exception but this code is the default exception handler.

if len(sys.argv) < 3:  This line checks the length of arguments that you are passing with the program. Since we need two arguments, we are checking the length of sys.argv. The argument number 0 is the name of the file itself. So sys.argv[0] points to the program name itself!

 

CODE:

#!/usr/bin/python
import sys

def file_Read():
        print("Opening a file for reading in python")
        try:
            u=open(user,"r")
            p=open(passd,"r")
            for uline in u.readlines():
            for pline in p.readlines():
            print("Reading:\nusername:"+uline+"password:"+pline+"\n")
        except Exception as e:
            print(e)

if len(sys.argv) < 3:
        print("Not enough argument user and password file required")
else:
        # if the condition is true, the values of arguments are assigned to variables user and passd. 
        user=sys.argv[1]
        passd=sys.argv[2]
        file_Read()

OUTPUT:

hacking with python file read

The code uses two for loops, the first for loop is for user file and the second one is for the password file. This way a single username will be iterated with all combination of passwords in the password file.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: