Skip to content

Create a helper function is_blocking_enabled #44

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 7, 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
13 changes: 13 additions & 0 deletions aikido_firewall/helpers/blocking_enabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Helper function file, see function docstring"""

from aikido_firewall.background_process import get_comms


def is_blocking_enabled():
"""
Checks with the background process if blocking is enabled
"""
should_block_res = get_comms().send_data_to_bg_process(

Check warning on line 10 in aikido_firewall/helpers/blocking_enabled.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/helpers/blocking_enabled.py#L10

Added line #L10 was not covered by tests
action="READ_PROPERTY", obj="block", receive=True
)
return should_block_res["success"] and should_block_res["data"]

Check warning on line 13 in aikido_firewall/helpers/blocking_enabled.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/helpers/blocking_enabled.py#L13

Added line #L13 was not covered by tests
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from aikido_firewall.context import get_current_context
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoPathTraversal
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled


@importhook.on_import("builtins")
Expand All @@ -34,10 +35,7 @@
)
if len(result) != 0:
get_comms().send_data_to_bg_process("ATTACK", (result, context))
should_block_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

Check warning on line 38 in aikido_firewall/sinks/builtins.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/builtins.py#L38

Added line #L38 was not covered by tests
raise AikidoPathTraversal()
return former_open(*args, **kwargs)

Expand Down
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/mysqlclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aikido_firewall.helpers.logging import logger
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoSQLInjection
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled


@importhook.on_import("MySQLdb.connections")
Expand All @@ -39,10 +40,7 @@
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_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L43

Added line #L43 was not covered by tests
raise AikidoSQLInjection("SQL Injection [aikido_firewall]")

return prev_query_function(_self, sql)
Expand Down
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from aikido_firewall.context import get_current_context
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoPathTraversal
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled

# File functions :
OS_FILE_FUNCTIONS = [
Expand Down Expand Up @@ -59,10 +60,7 @@
)
if len(result) != 0:
get_comms().send_data_to_bg_process("ATTACK", (result, context))
should_block_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

Check warning on line 63 in aikido_firewall/sinks/os.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/os.py#L63

Added line #L63 was not covered by tests
raise AikidoPathTraversal()
return former_func(*args, **kwargs)

Expand Down
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/os_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from aikido_firewall.helpers.logging import logger
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoShellInjection
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled


@importhook.on_import("os")
Expand All @@ -37,10 +38,7 @@
logger.debug("Shell injection results : %s", json.dumps(contains_injection))
if contains_injection:
get_comms().send_data_to_bg_process("ATTACK", (contains_injection, context))
should_block_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

Check warning on line 41 in aikido_firewall/sinks/os_system.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/os_system.py#L41

Added line #L41 was not covered by tests
raise AikidoShellInjection()

return former_system_func(*args, **kwargs)
Expand Down
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from aikido_firewall.vulnerabilities.sql_injection.dialects import Postgres
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoSQLInjection
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled


class MutableAikidoConnection:
Expand Down Expand Up @@ -46,10 +47,7 @@
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_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

Check warning on line 50 in aikido_firewall/sinks/psycopg2.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/psycopg2.py#L50

Added line #L50 was not covered by tests
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 @@ -10,6 +10,7 @@
from aikido_firewall.context import get_current_context
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoNoSQLInjection
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled

OPERATIONS_WITH_FILTER = [
"replace_one", # L1087
Expand Down Expand Up @@ -55,7 +56,8 @@
get_comms().send_data_to_bg_process(
"ATTACK", (injection_results, context)
)
raise AikidoNoSQLInjection("NOSQL Injection [aikido_firewall]")
if is_blocking_enabled():
raise AikidoNoSQLInjection("NOSQL Injection [aikido_firewall]")

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/pymongo.py#L59-L60

Added lines #L59 - L60 were not covered by tests
return prev_func(_self, _filter, *args, **kwargs)

setattr(modified_pymongo.Collection, operation, wrapped_operation_function)
Expand Down
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/pymysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aikido_firewall.vulnerabilities.sql_injection.dialects import MySQL
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoSQLInjection
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled

logger = logging.getLogger("aikido_firewall")

Expand Down Expand Up @@ -41,10 +42,7 @@
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_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/pymysql.py#L45

Added line #L45 was not covered by tests
raise AikidoSQLInjection("SQL Injection [aikido_firewall]")

return prev_query_function(_self, sql, unbuffered=False)
Expand Down
6 changes: 2 additions & 4 deletions aikido_firewall/sinks/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from aikido_firewall.helpers.logging import logger
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoShellInjection
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled

SUBPROCESS_OPERATIONS = ["call", "run", "check_call", "Popen", "check_output"]

Expand Down Expand Up @@ -39,10 +40,7 @@
logger.debug("Shell injection results : %s", json.dumps(contains_injection))
if contains_injection:
get_comms().send_data_to_bg_process("ATTACK", (contains_injection, context))
should_block_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
if should_block_res["success"] and should_block_res["data"]:
if is_blocking_enabled():

Check warning on line 43 in aikido_firewall/sinks/subprocess.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/subprocess.py#L43

Added line #L43 was not covered by tests
raise AikidoShellInjection()

return former_func(*args, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from aikido_firewall.helpers.logging import logger
from aikido_firewall.background_process import get_comms
from aikido_firewall.errors import AikidoSSRF
from aikido_firewall.helpers.blocking_enabled import is_blocking_enabled
from .imds import is_trusted_hostname, is_imds_ip_address
from .is_private_ip import is_private_ip
from .find_hostname_in_context import find_hostname_in_context
Expand All @@ -23,17 +24,12 @@

context = get_current_context()

should_block_res = get_comms().send_data_to_bg_process(
action="READ_PROPERTY", obj="block", receive=True
)
should_block = should_block_res["success"] and should_block_res["data"]

ip_addresses = extract_ip_array_from_results(dns_results)
if resolves_to_imds_ip(ip_addresses, hostname):
# Block stored SSRF attack that target IMDS IP addresses
# An attacker could have stored a hostname in a database that points to an IMDS IP address
# We don't check if the user input contains the hostname because there's no context
if should_block:
if is_blocking_enabled():

Check warning on line 32 in aikido_firewall/vulnerabilities/ssrf/inspect_getaddrinfo_result.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/vulnerabilities/ssrf/inspect_getaddrinfo_result.py#L32

Added line #L32 was not covered by tests
raise AikidoSSRF()

if not context:
Expand All @@ -47,6 +43,7 @@
if not found:
return

should_block = is_blocking_enabled()

Check warning on line 46 in aikido_firewall/vulnerabilities/ssrf/inspect_getaddrinfo_result.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/vulnerabilities/ssrf/inspect_getaddrinfo_result.py#L46

Added line #L46 was not covered by tests
stack = " ".join(traceback.format_stack())
attack = {
"module": "socket",
Expand Down
Loading