■ ■ ■ ■ ■ ■
Projects/Directory_BruteForcer/DBF.py
| 1 | + | from threading import Thread |
| 2 | + | import time,requests,sys,os.path |
| 3 | + | |
| 4 | + | def usage(): |
| 5 | + | print("----------USAGE INSTRUCTION ---------") |
| 6 | + | print(f"{sys.argv[0]} URL WORDLIST NUMBER_OF_THREADS(Default is 10)\n") |
| 7 | + | sys.exit() |
| 8 | + | |
| 9 | + | def prepare(myList,numOfChunks): |
| 10 | + | for i in range(0, len(myList), numOfChunks): |
| 11 | + | yield myList[i:i + numOfChunks] |
| 12 | + | |
| 13 | + | def brute(myList,url): |
| 14 | + | start=time.perf_counter() |
| 15 | + | for lists in myList: |
| 16 | + | threads.append(Thread(target=worker,args=(lists,url),daemon=True)) |
| 17 | + | for thread in threads: |
| 18 | + | try: |
| 19 | + | thread.start() |
| 20 | + | except KeyboardInterrupt: |
| 21 | + | print("\nReceived Keyboard Interrupt , Terminating threads\n") |
| 22 | + | sys.exit() |
| 23 | + | for thread in threads: |
| 24 | + | try: |
| 25 | + | thread.join() |
| 26 | + | except KeyboardInterrupt: |
| 27 | + | print("\nReceived Keyboard Interrupt , Terminating threads\n") |
| 28 | + | sys.exit() |
| 29 | + | finish=time.perf_counter() |
| 30 | + | print(f"\n\n\t\t Checked {total_len} Directories in {round(finish-start,2)} Seconds\n") |
| 31 | + | |
| 32 | + | def worker(lists,url): |
| 33 | + | try: |
| 34 | + | for word in lists: |
| 35 | + | if word.startswith("/"): |
| 36 | + | word=word[1:] |
| 37 | + | url2=url+"/"+word.strip() |
| 38 | + | r=requests.get(url2) |
| 39 | + | if str(r.status_code) in match: |
| 40 | + | print(f"/{word.strip():<40} [ Status: {r.status_code} Length:{len(r.content)} ]") |
| 41 | + | except KeyboardInterrupt: |
| 42 | + | print("\nReceived Keyboard Interrupt , Terminating threads\n") |
| 43 | + | sys.exit() |
| 44 | + | except Exception as e: |
| 45 | + | print(f"\nAn error Occurred : {e}\n") |
| 46 | + | sys.exit() |
| 47 | + | |
| 48 | + | if __name__ == "__main__": |
| 49 | + | try: |
| 50 | + | match=['200','301','302','401','403','429'] #change this to filter responses |
| 51 | + | try: |
| 52 | + | if sys.argv[1]: |
| 53 | + | url=sys.argv[1] |
| 54 | + | if sys.argv[2]: |
| 55 | + | wordlist=sys.argv[2] |
| 56 | + | try: |
| 57 | + | if sys.argv[3]: |
| 58 | + | numOfThreads=int(sys.argv[3]) |
| 59 | + | except: |
| 60 | + | numOfThreads=10 |
| 61 | + | except: |
| 62 | + | usage() |
| 63 | + | if os.path.isfile(wordlist)==False: |
| 64 | + | print(f"The file {wordlist} doesn't exist") |
| 65 | + | sys.exit() |
| 66 | + | with open(wordlist,'r') as w: |
| 67 | + | myList=w.readlines() |
| 68 | + | total_len=len(myList) |
| 69 | + | final=[] |
| 70 | + | threads=[] |
| 71 | + | if numOfThreads>total_len or numOfThreads<0: |
| 72 | + | print("\nToo High Value for Threads with Respect to Input Word-list\n") |
| 73 | + | sys.exit(1) |
| 74 | + | numOfChunks=len(myList)//numOfThreads |
| 75 | + | if url.endswith("/"): |
| 76 | + | url=url[0:-1] |
| 77 | + | print(f''' |
| 78 | + | ====================================== |
| 79 | + | URL --> {url} |
| 80 | + | Word-list --> {wordlist} |
| 81 | + | Threads --> {numOfThreads} |
| 82 | + | Status Codes --> {','.join([w for w in match])} |
| 83 | + | ====================================== |
| 84 | + | \n\n |
| 85 | + | ''') |
| 86 | + | print("------- Started Brute forcing Directories -------\n") |
| 87 | + | myList_new=prepare(myList,numOfChunks) |
| 88 | + | brute(myList_new,url) |
| 89 | + | except Exception as e: |
| 90 | + | print(f"\nAn error Occurred : {e}\n") |
| 91 | + | sys.exit() |
| 92 | + | |