MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / SQLConnector

Class SQLConnector

actions/sql_connector.py:72–218  ·  view source on GitHub ↗

Class to manage the connection attempts and store the results.

Source from the content-addressed store, hash-verified

70
71class SQLConnector:
72 """
73 Class to manage the connection attempts and store the results.
74 """
75 def __init__(self, shared_data):
76 self.shared_data = shared_data
77 # self.scan is built by load_scan_file() from run_bruteforce() at attack
78 # time; we don't build it here so pandas stays off the startup path (the
79 # connector is instantiated at boot, saves ~44 MB RSS; measured 111MB->67MB startup on this box).
80 self.scan = None
81 self.users = open(shared_data.usersfile, "r").read().splitlines()
82 self.passwords = open(shared_data.passwordsfile, "r").read().splitlines()
83
84 self.lock = threading.Lock()
85 self.sqlfile = shared_data.sqlfile
86 if not os.path.exists(self.sqlfile):
87 with open(self.sqlfile, "w") as f:
88 f.write("IP Address,User,Password,Port,Database\n")
89 self.results = []
90 self.queue = Queue()
91 self.console = Console()
92
93 def load_scan_file(self):
94 """
95 Load from SQLite database and filter for SQL ports.
96 """
97 import pandas as pd # lazy: keeps ~44MB pandas out of RAM until an attack action runs
98 try:
99 data = self.shared_data.read_data()
100 self.scan = pd.DataFrame(data)
101 if "Ports" not in self.scan.columns:
102 self.scan["Ports"] = None
103 except Exception as e:
104 logger.warning(f"Could not read data from database: {e}")
105 self.scan = pd.DataFrame(columns=['MAC Address', 'IPs', 'Hostnames', 'Ports', 'Alive'])
106 # Ensure Ports column is string type before using .str accessor
107 self.scan["Ports"] = self.scan["Ports"].astype(str)
108 self.scan = self.scan[self.scan["Ports"].str.contains("3306", na=False)]
109
110 def sql_connect(self, adresse_ip, user, password):
111 """
112 Attempt to connect to an SQL service using the given credentials without specifying a database.
113 """
114 try:
115 # Première tentative sans spécifier de base de données
116 conn = pymysql.connect(
117 host=adresse_ip,
118 user=user,
119 password=password,
120 port=3306
121 )
122
123 # Si la connexion réussit, récupérer la liste des bases de données
124 with conn.cursor() as cursor:
125 cursor.execute("SHOW DATABASES")
126 databases = [db[0] for db in cursor.fetchall()]
127
128 conn.close()
129 logger.info(f"Successfully connected to {adresse_ip} with user {user}")

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected