Advertisement
xGHOSTSECx

GhostQuery

Dec 24th, 2023
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import logging
  4. import sqlite3
  5. import re
  6. from pathlib import Path
  7.  
  8. class GhostQuery:
  9.     def __init__(self):
  10.         self.base_url = "https://www.google.com/search?q="
  11.         self.headers = {
  12.             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
  13.         }
  14.         self.log_filename = 'ghostquery.log'
  15.         self.db_filename = 'ghostquery_results.db'
  16.         self.logger = self.setup_logger()
  17.         self.setup_database()
  18.  
  19.     def setup_logger(self):
  20.         logger = logging.getLogger('GhostQuery')
  21.         logger.setLevel(logging.INFO)
  22.         formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
  23.         file_handler = logging.FileHandler(self.log_filename)
  24.         file_handler.setFormatter(formatter)
  25.         logger.addHandler(file_handler)
  26.         return logger
  27.  
  28.     def setup_database(self):
  29.         db_path = Path(self.db_filename)
  30.         if not db_path.is_file():
  31.             conn = sqlite3.connect(self.db_filename)
  32.             c = conn.cursor()
  33.             c.execute('''CREATE TABLE IF NOT EXISTS search_results
  34.                          (title text, link text)''')
  35.             conn.commit()
  36.             conn.close()
  37.  
  38.     def save_to_database(self, title, link):
  39.         conn = sqlite3.connect(self.db_filename)
  40.         c = conn.cursor()
  41.         c.execute("INSERT INTO search_results VALUES (?, ?)", (title, link))
  42.         conn.commit()
  43.         conn.close()
  44.  
  45.     def google_search(self, query, num_results=10):
  46.         try:
  47.             query = re.sub(r'[;\'"()|]', '', query)
  48.             url = f"{self.base_url}{query}&num={num_results}"
  49.             response = requests.get(url, headers=self.headers)
  50.  
  51.             if response.status_code == 200:
  52.                 soup = BeautifulSoup(response.text, 'html.parser')
  53.                 results = soup.find_all('div', class_='tF2Cxc')
  54.                 return results
  55.             else:
  56.                 self.logger.error(f"Failed to retrieve search results. Status code: {response.status_code}")
  57.                 return None
  58.         except requests.exceptions.RequestException as e:
  59.             self.logger.error(f"Request error: {str(e)}")
  60.             return None
  61.         except Exception as e:
  62.             self.logger.error(f"An unexpected error occurred: {str(e)}")
  63.             return None
  64.  
  65.     def analyze_results(self, results):
  66.         if results:
  67.             analysis = []
  68.             for idx, result in enumerate(results, start=1):
  69.                 title = result.find('h3').text
  70.                 link = result.find('a')['href']
  71.                 analysis.append({'index': idx, 'title': title, 'link': link})
  72.                 self.save_to_database(title, link)
  73.             return analysis
  74.         else:
  75.             self.logger.warning("No results found for analysis.")
  76.             return []
  77.  
  78.     def run(self):
  79.         try:
  80.             num_results = int(input("Enter the number of results to fetch (default is 10): ") or 10)
  81.             promiscuous_mode = input("Enable Promiscuous Mode? (y/n): ").strip().lower()
  82.             if promiscuous_mode == 'y':
  83.                 print("Promiscuous Mode is enabled.")
  84.             else:
  85.                 print("Promiscuous Mode is disabled.")
  86.  
  87.             query = input("Enter your search query: ")
  88.             results = self.google_search(query, num_results)
  89.             analysis = self.analyze_results(results)
  90.  
  91.             if analysis:
  92.                 print("Analysis Results:")
  93.                 for item in analysis:
  94.                     print(f"{item['index']}. {item['title']}")
  95.                     print(item['link'])
  96.                     print()
  97.             else:
  98.                 print("No results found. Check the log file for details.")
  99.         except KeyboardInterrupt:
  100.             print("\nSearch aborted by the user.")
  101.         except Exception as e:
  102.             self.logger.error(f"An unexpected error occurred: {str(e)}")
  103.  
  104. if __name__ == "__main__":
  105.     ghost_query = GhostQuery()
  106.     ghost_query.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement