Skip to content

Merge IPC into one function #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions aikido_firewall/background_process/comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,40 +56,36 @@
else: # Parent process
logger.debug("Started background process, PID: %d", pid)

def send_data_to_bg_process(self, action, obj):
def send_data_to_bg_process(self, action, obj, receive=False):
"""
This creates a new client for comms to the background process
"""

# We want to make sure that sending out this data affects the process as little as possible
# So we run it inside a seperate thread with a timeout of 3 seconds
def target(address, key, data_array):
try:
conn = con.Client(address, authkey=key)
logger.debug("Created connection %s", conn)
for data in data_array:
conn.send(data)
conn.send(("CLOSE", {}))
conn.close()
logger.debug("Connection closed")
except Exception as e:
logger.info("Failed to send data to bg process : %s", e)
# So we run it inside a seperate thread with a timeout of 100ms
# If something goes wrong, it will also be encapsulated in the thread i.e. no crashes
def target(address, key, receive, data, result_obj):
# Create a connection, this can get stuck :
conn = con.Client(address, authkey=key)

# Send/Receive data :
conn.send(data)
if receive:
result_obj = conn.recv()

Check warning on line 74 in aikido_firewall/background_process/comms.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/background_process/comms.py#L74

Added line #L74 was not covered by tests

# Close the connection :
conn.send(("CLOSE", {}))
conn.close()

# Create a shared result object between the thread and this process :
result_obj = None
t = Thread(
target=target, args=(self.address, self.key, [(action, obj)]), daemon=True
target=target,
args=(self.address, self.key, receive, (action, obj), result_obj),
daemon=True, # This allows us to join and set a timeout after which the thread closes
)
t.start()
# This joins the thread for 3 seconds, afterwards the thread is forced to close (daemon=True)
t.join(timeout=3)

def poll_config(self, prop):
"""
This will poll the config from the Background Process
"""
conn = con.Client(self.address, authkey=self.key)
conn.send(("READ_PROPERTY", prop))
prop_value = conn.recv()
conn.send(("CLOSE", {}))
conn.close()
logger.debug("Received property %s as %s", prop, prop_value)
return prop_value
# Start and join the thread for 100ms, afterwards the thread is forced to close (daemon=True)
t.start()
t.join(timeout=0.1)
return result_obj
4 changes: 3 additions & 1 deletion aikido_firewall/sinks/mysqlclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
logger.debug("sql_injection results : %s", json.dumps(contains_injection))
if contains_injection:
get_comms().send_data_to_bg_process("ATTACK", (contains_injection, context))
should_block = get_comms().poll_config("block")
should_block = get_comms().send_data_to_bg_process(

Check warning on line 42 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L42

Added line #L42 was not covered by tests
action="READ_PROPERTY", obj="block", receive=True
)
if should_block:
raise AikidoSQLInjection("SQL Injection [aikido_firewall]")

Expand Down
4 changes: 3 additions & 1 deletion aikido_firewall/sinks/pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
context = get_current_context()
injection_results = detect_nosql_injection(context, _filter)
if injection_results["injection"]:
get_comms().send_data("ATTACK", injection_results)
get_comms().send_data_to_bg_process(

Check warning on line 55 in aikido_firewall/sinks/pymongo.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/pymongo.py#L55

Added line #L55 was not covered by tests
"ATTACK", (injection_results, context)
)
raise AikidoNoSQLInjection("NOSQL Injection [aikido_firewall]")
return prev_func(_self, _filter, *args, **kwargs)

Expand Down
4 changes: 3 additions & 1 deletion aikido_firewall/sinks/pymysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
logger.info("sql_injection results : %s", json.dumps(contains_injection))
if contains_injection:
get_comms().send_data_to_bg_process("ATTACK", (contains_injection, context))
should_block = get_comms().poll_config("block")
should_block = get_comms().send_data_to_bg_process(

Check warning on line 44 in aikido_firewall/sinks/pymysql.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/pymysql.py#L44

Added line #L44 was not covered by tests
action="READ_PROPERTY", obj="block", receive=True
)
if should_block:
raise AikidoSQLInjection("SQL Injection [aikido_firewall]")

Expand Down
Loading