Thursday, September 5, 2013

HuntPass.py: Python Script That Searches for Passwords [Or Any String] in a File Then Saves Results to a New File

Written by: Pranshu Bajpai | Find Pranshu on Google+ And LinkedIn

It is amazing how people on a shared LAN are still typing away their confidential information over http (non-SSL, un-encrypted) links. Someone sniffing the LAN could easily capture such information in plaintext.

During a recent test, I used Ettercap for sniffing the local LAN.

Since this was a large internal network with a lot of users, it was able to discover a LOT of passwords in transit in plaintext form over the link. After dumping, the problem was to sort these passwords in the Ettercap dump file. So I wrote this simple python script.

Functionality: The script looks for user-supplied passwords strings in a given file and then stores the 'unique' passwords found to a new file. It just gets things done. Please feel free to improve it if you feel the need to do so.

Usage: python HuntPass.py <InputFile> <OutputFile> <SearchString>

Download: Github - https://github.com/lifeofpentester/huntpass

Example:

Here, I have saved my Ettercap sniffer results to a file called 'etterlog'. This is my <InputFile>


I am looking for lines with the word "PASS" ( for password) in them. I want these passwords saved in a file called 'Passwords.txt'


             #python HuntPass.py etterlog Passwords.txt PASS




Only two lines in 'etterlog' had 'PASS', so total passwords found are 2 in this case.

They are stored in a separate file called 'Passwords.txt'.





If a new user misses out an argument while running the program, it exits printing 'Usage' information.




If you're new to Python, you might like to read the code. Here it is:

#!/usr/bin/python

import sys  #Taking Arguments in Terminal
import os   #Clearscreen
import time

if len(sys.argv) < 4:  #First Argument is always the program name
        sys.exit('Usage: python HuntPass.py <InputFile> <OutputFile> <SearchString>')    #Exit if New User doesn't give all the Arguments

os.system('clear')

time_on_your_system = time.asctime(time.localtime(time.time()))

print "\t\t_____________", time_on_your_system, "_____________"
print "\n\t\t_____A Small Script to find Passwords in Dumps_____"

print "\n\t\t####################################################"

print "\n\t\t#________________Written by Pranshu_________________#"

print "\t\t______________________________________________________"



filename = sys.argv[1]  #Input File

newfile = sys.argv[2]   #Output File

f1 = open (filename, "r") #Read Mode

f2 = open (newfile, "a+") #Append Mode

search_string = sys.argv[3] #Text to Search

print "\n\tLooking in", filename + " | " + "Storing Result in", newfile + " | " + "Searching for", search_string

flag = raw_input("\n\n\t\tProceed? (y/n): ")

if flag == 'y':
    lines_seen = set()  #A Data Structure to store lines seen, to prevent duplicate passwords while saving

    total_pass = 0

    for line in f1:
        if line not in lines_seen:  
            if search_string in line:
                f2.write(line)
                lines_seen.add(line)
                total_pass = total_pass + 1
    print "\n\t\tTotal Passwords found are: ", total_pass
    print "\n\t\tSaved in ", newfile
f1.close()
f2.close()
print "\n\t\t________________________QUITTING!____________________\n\n"



Like I said, feel free to improve it according to your needs.

2 comments:

  1. What's wrong with

    infile | grep word > outfile

    ?

    ReplyDelete