@cryptotaxi247 / CoPilot / commits / 50012d76

Sublime connector (#11)

* sublime connector * register_creator for Sublime * sublime connector to db model

taylor_socfortress committed Jul 12, 2023 at 09:33 UTC 50012d76b64f1c48e709f37030e31e1133b5dca4
5 files changed +176 -40
.gitignore
+1
@@ -40,3 +40,4 @@ wheels/
40 *.db
41 .env
42 *.sqbpro
43 +site/
backend/app/models/connectors.py
+51
@@ -375,6 +375,56 @@ class VelociraptorConnector(Connector):
375 return {"connectionSuccessful": False, "response": None}
376
377
378 +class SublimeConnector(Connector):
379 + """
380 + A connector for the Sublime service, a subclass of Connector.
381 +
382 + Args:
383 + connector_name (str): The name of the connector.
384 + """
385 +
386 + def __init__(self, connector_name: str):
387 + super().__init__(attributes=self.get_connector_info_from_db(connector_name))
388 +
389 + def verify_connection(self) -> Dict[str, Any]:
390 + """
391 + Verifies the connection to Sublime service.
392 +
393 + Returns:
394 + dict: A dictionary containing 'connectionSuccessful' status and 'response' if the connection is successful.
395 + """
396 + logger.info(
397 + f"Verifying the sublime connection to {self.attributes['connector_url']}",
398 + )
399 + try:
400 + headers = {
401 + "Authorization": f"Bearer {self.attributes['connector_api_key']}",
402 + "Content-Type": "application/json",
403 + }
404 + params = {"limit": 1}
405 + sublime = requests.get(
406 + f"{self.attributes['connector_url']}/v0/rules",
407 + headers=headers,
408 + params=params,
409 + verify=False,
410 + )
411 + if sublime.status_code == 200:
412 + logger.info(
413 + f"Connection to {self.attributes['connector_url']} successful",
414 + )
415 + return {"connectionSuccessful": True}
416 + else:
417 + logger.error(
418 + f"Connection to {self.attributes['connector_url']} failed with error: {sublime.text}",
419 + )
420 + return {"connectionSuccessful": False, "response": None}
421 + except Exception as e:
422 + logger.error(
423 + f"Connection to {self.attributes['connector_url']} failed with error: {e}",
424 + )
425 + return {"connectionSuccessful": False, "response": None}
426 +
427 +
428 class RabbitMQConnector(Connector):
429 """
430 A connector for the RabbitMQ service, a subclass of Connector.
@@ -477,3 +527,4 @@ connector_factory.register_creator("DFIR-IRIS", "DfirIrisConnector")
527 connector_factory.register_creator("Velociraptor", "VelociraptorConnector")
528 connector_factory.register_creator("RabbitMQ", "RabbitMQConnector")
529 connector_factory.register_creator("Shuffle", "ShuffleConnector")
530 +connector_factory.register_creator("Sublime", "SublimeConnector")
backend/app/models/models.py
+10 -1
@@ -103,6 +103,14 @@ class Connectors(db.Model):
103 connector_password: Column[String] = db.Column(db.String(100))
104 connector_api_key: Column[String] = db.Column(db.String(100))
105
106 + # Define a dictionary for connectors that need an API key
107 + api_key_required_connectors = {
108 + "shuffle": True,
109 + "dfir-irs": True,
110 + "velociraptor": True,
111 + "sublime": True,
112 + }
113 +
114 def __init__(
115 self,
116 connector_name: str,
@@ -128,7 +136,8 @@ class Connectors(db.Model):
136 self.connector_username = connector_username
137 self.connector_password = connector_password
138
131 - if connector_name.lower() == "shuffle" or connector_name.lower() == "dfir-irs" or connector_name.lower() == "velociraptor":
139 + # Check if the connector needs an API key
140 + if self.api_key_required_connectors.get(connector_name.lower()):
141 logger.info(f"Setting the API key for {connector_name}")
142 self.connector_api_key = connector_api_key
143 else:
backend/app/routes/connectors.py
+113 -38
@@ -1,3 +1,99 @@
1 +# from flask import Blueprint
2 +# from flask import jsonify
3 +# from flask import request
4 +# from loguru import logger
5 +
6 +# from app import db
7 +# from app.models.models import Connectors
8 +# from app.models.models import ConnectorsAvailable
9 +# from app.models.models import connectors_available_schema
10 +# from app.services.connectors.connectors import ConnectorService
11 +
12 +# bp = Blueprint("connectors", __name__)
13 +
14 +
15 +# @bp.route("/connectors", methods=["GET"])
16 +# def list_connectors_available():
17 +# """
18 +# Endpoint to retrieve all available connectors.
19 +
20 +# Returns:
21 +# json: A JSON response containing the list of all available connectors along with their connection verification status.
22 +# """
23 +# logger.info("Received request to get all available connectors")
24 +# connectors_service = ConnectorService(db)
25 +# connectors = ConnectorsAvailable.query.all()
26 +# result = connectors_available_schema.dump(connectors)
27 +
28 +# instantiated_connectors = [
29 +# connectors_service.process_connector(connector["connector_name"])
30 +# for connector in result
31 +# if connectors_service.process_connector(connector["connector_name"])
32 +# ]
33 +
34 +# return jsonify(instantiated_connectors)
35 +
36 +
37 +# @bp.route("/connectors/<id>", methods=["GET"])
38 +# def get_connector_details(id: str):
39 +# """
40 +# Endpoint to retrieve the details of a connector.
41 +
42 +# Args:
43 +# id (str): The ID of the connector to retrieve.
44 +
45 +# Returns:
46 +# json: A JSON response containing the details of the connector.
47 +# """
48 +# logger.info("Received request to get a connector details")
49 +# service = ConnectorService(db)
50 +# connector = service.validate_connector_exists(int(id))
51 +
52 +# if connector["success"]:
53 +# connector = Connectors.query.get(id)
54 +# instantiated_connector = service.process_connector(connector.connector_name)
55 +# return jsonify(instantiated_connector)
56 +# else:
57 +# return jsonify(connector), 404
58 +
59 +
60 +# @bp.route("/connectors/<id>", methods=["PUT"])
61 +# def update_connector_route(id: str):
62 +# """
63 +# Endpoint to update the details of a connector.
64 +
65 +# Args:
66 +# id (str): The ID of the connector to update.
67 +
68 +# Returns:
69 +# json: A JSON response containing the success status of the update operation and a message indicating the status.
70 +# If the update operation was successful, it returns the connector name and the status of the connection verification.
71 +# """
72 +# logger.info("Received request to update connector")
73 +# api_key_connector = ["Shuffle", "DFIR-IRIS", "Velociraptor"]
74 +
75 +# request_data = request.get_json()
76 +# service = ConnectorService(db)
77 +# connector = service.validate_connector_exists(int(id))
78 +
79 +# if connector["success"]:
80 +# if connector["connector_name"] in api_key_connector:
81 +# data_validated = service.validate_request_data_api_key(request_data)
82 +# if data_validated["success"]:
83 +# service.update_connector(int(id), request_data)
84 +# return service.verify_connector_connection(int(id))
85 +# else:
86 +# return jsonify(data_validated), 400
87 +# else:
88 +# data_validated = service.validate_request_data(request_data)
89 +# if data_validated["success"]:
90 +# service.update_connector(int(id), request_data)
91 +# return service.verify_connector_connection(int(id))
92 +# else:
93 +# return jsonify(data_validated), 400
94 +# else:
95 +# return jsonify(connector), 404
96 +
97 from flask import Blueprint
98 from flask import jsonify
99 from flask import request
@@ -11,15 +107,24 @@ from app.services.connectors.connectors import ConnectorService
107
108 bp = Blueprint("connectors", __name__)
109
110 +api_key_connector = ["Shuffle", "DFIR-IRIS", "Velociraptor", "Sublime"]
111 +
112 +
113 +def validate_and_update_connector(id, request_data, service, api_key=False):
114 + if api_key:
115 + data_validated = service.validate_request_data_api_key(request_data)
116 + else:
117 + data_validated = service.validate_request_data(request_data)
118 +
119 + if data_validated["success"]:
120 + service.update_connector(int(id), request_data)
121 + return service.verify_connector_connection(int(id))
122 + else:
123 + return jsonify(data_validated), 400
124 +
125
126 @bp.route("/connectors", methods=["GET"])
127 def list_connectors_available():
17 - """
18 - Endpoint to retrieve all available connectors.
19 -
20 - Returns:
21 - json: A JSON response containing the list of all available connectors along with their connection verification status.
22 - """
128 logger.info("Received request to get all available connectors")
129 connectors_service = ConnectorService(db)
130 connectors = ConnectorsAvailable.query.all()
@@ -36,15 +141,6 @@ def list_connectors_available():
141
142 @bp.route("/connectors/<id>", methods=["GET"])
143 def get_connector_details(id: str):
39 - """
40 - Endpoint to retrieve the details of a connector.
41 -
42 - Args:
43 - id (str): The ID of the connector to retrieve.
44 -
45 - Returns:
46 - json: A JSON response containing the details of the connector.
47 - """
144 logger.info("Received request to get a connector details")
145 service = ConnectorService(db)
146 connector = service.validate_connector_exists(int(id))
@@ -59,18 +155,7 @@ def get_connector_details(id: str):
155
156 @bp.route("/connectors/<id>", methods=["PUT"])
157 def update_connector_route(id: str):
62 - """
63 - Endpoint to update the details of a connector.
64 -
65 - Args:
66 - id (str): The ID of the connector to update.
67 -
68 - Returns:
69 - json: A JSON response containing the success status of the update operation and a message indicating the status.
70 - If the update operation was successful, it returns the connector name and the status of the connection verification.
71 - """
158 logger.info("Received request to update connector")
73 - api_key_connector = ["Shuffle", "DFIR-IRIS", "Velociraptor"]
159
160 request_data = request.get_json()
161 service = ConnectorService(db)
@@ -78,18 +163,8 @@ def update_connector_route(id: str):
163
164 if connector["success"]:
165 if connector["connector_name"] in api_key_connector:
81 - data_validated = service.validate_request_data_api_key(request_data)
82 - if data_validated["success"]:
83 - service.update_connector(int(id), request_data)
84 - return service.verify_connector_connection(int(id))
85 - else:
86 - return jsonify(data_validated), 400
166 + return validate_and_update_connector(id, request_data, service, api_key=True)
167 else:
88 - data_validated = service.validate_request_data(request_data)
89 - if data_validated["success"]:
90 - service.update_connector(int(id), request_data)
91 - return service.verify_connector_connection(int(id))
92 - else:
93 - return jsonify(data_validated), 400
168 + return validate_and_update_connector(id, request_data, service)
169 else:
170 return jsonify(connector), 404
backend/copilot.sqbpro
+1 -1
@@ -1 +1 @@
1 -<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="C:/Users/walto/Desktop/GitHub/CoPilot/backend/copilot.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="1"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="1981"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="4,10:mainconnectors"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="agent_metadata" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="65"/><column index="3" value="79"/><column index="4" value="40"/><column index="5" value="72"/><column index="6" value="95"/><column index="7" value="71"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="connectors" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="117"/><column index="3" value="112"/><column index="4" value="259"/><column index="5" value="196"/><column index="6" value="146"/><column index="7" value="145"/><column index="8" value="132"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1"></sql><current_tab id="0"/></tab_sql></sqlb_project>
1 +<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="C:/Users/walto/Desktop/GitHub/CoPilot/backend/copilot.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="1"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="1981"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="4,10:mainconnectors"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="agent_metadata" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="164"/><column index="3" value="79"/><column index="4" value="40"/><column index="5" value="72"/><column index="6" value="95"/><column index="7" value="71"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="case" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="57"/><column index="3" value="81"/><column index="4" value="53"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="connectors" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="117"/><column index="3" value="112"/><column index="4" value="259"/><column index="5" value="196"/><column index="6" value="146"/><column index="7" value="145"/><column index="8" value="132"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="connectors_available" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="117"/><column index="3" value="186"/><column index="4" value="140"/><column index="5" value="151"/><column index="6" value="131"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table><table schema="main" name="disabled_rules" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="52"/><column index="3" value="102"/><column index="4" value="73"/><column index="5" value="144"/><column index="6" value="175"/><column index="7" value="106"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1"></sql><current_tab id="0"/></tab_sql></sqlb_project>