|
| 1 | +import sys |
| 2 | + |
| 3 | +try: |
| 4 | + import requests.exceptions |
| 5 | + from colorama import Fore, Style |
| 6 | + import mechanicalsoup |
| 7 | + import re |
| 8 | + import requests |
| 9 | + import sqlite3 |
| 10 | + import os |
| 11 | +except ImportError as e: |
| 12 | + print(Fore.RED + "Import error appeared. Reason: {}".format(e) + Style.RESET_ALL) |
| 13 | + sys.exit() |
| 14 | + |
| 15 | +def get_dorking_query(short_domain, dorking_db_path, table): |
| 16 | + print(Fore.GREEN + "Getting dorking query from database") |
| 17 | + conn = sqlite3.connect(dorking_db_path) |
| 18 | + cursor = conn.cursor() |
| 19 | + cursor.execute(f"SELECT dork FROM {table}") |
| 20 | + rows = cursor.fetchall() |
| 21 | + search_query = [row[0].format(short_domain) for row in rows] |
| 22 | + conn.close() |
| 23 | + return search_query |
| 24 | + |
| 25 | +def get_columns_amount(dorking_db_path, table): |
| 26 | + conn = sqlite3.connect(dorking_db_path) |
| 27 | + cursor = conn.cursor() |
| 28 | + cursor.execute(f"SELECT COUNT(*) FROM {table}") |
| 29 | + row_count = cursor.fetchone()[0] |
| 30 | + conn.close() |
| 31 | + return row_count |
| 32 | + |
| 33 | +def solid_google_dorking(query, pages=100): |
| 34 | + try: |
| 35 | + browser = mechanicalsoup.StatefulBrowser() |
| 36 | + browser.open("https://www.google.com/") |
| 37 | + browser.select_form('form[action="/search"]') |
| 38 | + browser["q"] = str(query) |
| 39 | + browser.submit_selected(btnName="btnG") |
| 40 | + result_query = [] |
| 41 | + for page in range(pages): |
| 42 | + for link in browser.links(): |
| 43 | + target = link.attrs['href'] |
| 44 | + if (target.startswith('/url?') and not |
| 45 | + target.startswith("/url?q=http://webcache.googleusercontent.com")): |
| 46 | + target = re.sub(r"^/url\?q=([^&]*)&.*", r"\1", target) |
| 47 | + result_query.append(target) |
| 48 | + try: |
| 49 | + browser.follow_link(nr=page + 1) |
| 50 | + except mechanicalsoup.LinkNotFoundError: |
| 51 | + break |
| 52 | + del result_query[-2:] |
| 53 | + return result_query |
| 54 | + except requests.exceptions.ConnectionError as e: |
| 55 | + print(Fore.RED + "Error while establishing connection with domain. No results will appear. Reason: {}".format(e) + Style.RESET_ALL) |
| 56 | + |
| 57 | +def save_results_to_txt(folderpath, table, queries, pages=10): |
| 58 | + try: |
| 59 | + txt_writepath = folderpath + '//04-dorking_results.txt' |
| 60 | + total_results = [] |
| 61 | + total_dorks_amount = len(queries) |
| 62 | + with open(txt_writepath, 'w') as f: |
| 63 | + print(Fore.GREEN + "Started Google Dorking. Please, be patient, it may take some time") |
| 64 | + dorked_query_counter = 0 |
| 65 | + for i, query in enumerate(queries, start=1): |
| 66 | + f.write(f"QUERY #{i}: {query}\n") |
| 67 | + results = solid_google_dorking(query, pages) |
| 68 | + if not results: |
| 69 | + f.write("=> NO RESULT FOUND\n") |
| 70 | + total_results.append((query, 0)) |
| 71 | + else: |
| 72 | + total_results.append((query, len(results))) |
| 73 | + for result in results: |
| 74 | + f.write(f"=> {result}\n") |
| 75 | + f.write("\n") |
| 76 | + dorked_query_counter += 1 |
| 77 | + print(Fore.GREEN + f" Dorking with " + Style.RESET_ALL + Fore.LIGHTCYAN_EX + Style.BRIGHT + f"{dorked_query_counter}/{total_dorks_amount}" + Style.RESET_ALL + Fore.GREEN + " dork" + Style.RESET_ALL, end="\r") |
| 78 | + print(Fore.GREEN + "Google Dorking end. Results successfully saved in TXT file\n" + Style.RESET_ALL) |
| 79 | + print(Fore.GREEN + f"During Google Dorking with {table.upper()}:") |
| 80 | + for query, count in total_results: |
| 81 | + if count == 0: |
| 82 | + count = 'no results' |
| 83 | + print(Fore.GREEN + f"[+] Found results for " + Fore.LIGHTCYAN_EX + f'{query}' + Fore.GREEN + ' query: ' + Fore.LIGHTCYAN_EX + f'{count}' + Style.RESET_ALL) |
| 84 | + return f'Successfully dorked domain with {table.upper()} dorks table', txt_writepath |
| 85 | + except Exception: |
| 86 | + print(Fore.RED + 'Error appeared while trying to dork target. See journal for details') |
| 87 | + return 'Domain dorking failed. See journal for details', txt_writepath |
| 88 | + |
| 89 | +def transfer_results_to_xlsx(table, queries, pages=10): |
| 90 | + print(Fore.GREEN + "Started Google Dorking. Please, be patient, it may take some time") |
| 91 | + dorked_query_counter = 0 |
| 92 | + total_dorks_amount = len(queries) |
| 93 | + dorking_return_list = [] |
| 94 | + for i, query in enumerate(queries, start=1): |
| 95 | + dorking_return_list.append(f"QUERY #{i}: {query}\n") |
| 96 | + results = solid_google_dorking(query, pages) |
| 97 | + if not results: |
| 98 | + dorking_return_list.append("NO RESULT FOUND\n") |
| 99 | + else: |
| 100 | + for result in results: |
| 101 | + dorking_return_list.append(f"{result}\n") |
| 102 | + dorked_query_counter += 1 |
| 103 | + dorking_return_list.append("\n") |
| 104 | + print(Fore.GREEN + f" Dorking with " + Style.RESET_ALL + Fore.LIGHTCYAN_EX + Style.BRIGHT + f"{dorked_query_counter}/{total_dorks_amount}" + Style.RESET_ALL + Fore.GREEN + " dork" + Style.RESET_ALL, end="\r") |
| 105 | + print(Fore.GREEN + "Google Dorking end. Results successfully saved in XLSX report\n" + Style.RESET_ALL) |
| 106 | + return f'Successfully dorked domain with {table.upper()} dorks table', dorking_return_list |
| 107 | + |
| 108 | +def dorks_files_check(): |
| 109 | + dorks_path = 'dorking//' |
| 110 | + dorks_files = ['iot_dorking.db', 'files_dorking.db', 'basic_dorking.db'] |
| 111 | + dorks_files_counter = 0 |
| 112 | + for dork_files in dorks_files: |
| 113 | + files_path = os.path.join(dorks_path, dork_files) |
| 114 | + if os.path.isfile(files_path): |
| 115 | + dorks_files_counter += 1 |
| 116 | + else: |
| 117 | + pass |
| 118 | + if dorks_files_counter == 3: |
| 119 | + print(Fore.GREEN + "Dorks databases presence: OK" + Style.RESET_ALL) |
| 120 | + else: |
| 121 | + print(Fore.RED + "Dorks databases presence: NOT OK\nSome files may not be in folder. Please compare dorking folder with the same folder on the official repository\n" + Style.RESET_ALL) |
| 122 | + sys.exit() |
0 commit comments