Skip to content
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

Add mysqlclient support #17

Merged
merged 14 commits into from
Jul 30, 2024
1 change: 1 addition & 0 deletions aikido_firewall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

# Import sinks
import aikido_firewall.sinks.pymysql
import aikido_firewall.sinks.mysqlclient

Check warning on line 28 in aikido_firewall/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/__init__.py#L28

Added line #L28 was not covered by tests

logger.info("Aikido python firewall started")
start_agent()
46 changes: 46 additions & 0 deletions aikido_firewall/sinks/mysqlclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Sink module for `mysqlclient`
"""

import copy
import json
from importlib.metadata import version
import importhook
from aikido_firewall.context import get_current_context
from aikido_firewall.vulnerabilities.sql_injection.check_context_for_sql_injection import (

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L5-L10

Added lines #L5 - L10 were not covered by tests
check_context_for_sql_injection,
)
from aikido_firewall.vulnerabilities.sql_injection.dialects import MySQL
from aikido_firewall.helpers.logging import logger

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L13-L14

Added lines #L13 - L14 were not covered by tests


@importhook.on_import("MySQLdb.connections")
def on_mysqlclient_import(mysql):

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L17-L18

Added lines #L17 - L18 were not covered by tests
"""
Hook 'n wrap on `MySQLdb.connections`
Our goal is to wrap the query() function of the Connection class :
https://github.com/PyMySQL/mysqlclient/blob/9fd238b9e3105dcbed2b009a916828a38d1f0904/src/MySQLdb/connections.py#L257
Returns : Modified MySQLdb.connections object
"""
modified_mysql = importhook.copy_module(mysql)

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L25

Added line #L25 was not covered by tests

prev_query_function = copy.deepcopy(mysql.Connection.query)

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L27

Added line #L27 was not covered by tests

def aikido_new_query(_self, sql):
logger.debug("Wrapper - `mysqlclient` version : %s", version("mysqlclient"))

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L29-L30

Added lines #L29 - L30 were not covered by tests

context = get_current_context()
result = check_context_for_sql_injection(sql, "Test_op", context, MySQL())
bitterpanda63 marked this conversation as resolved.
Show resolved Hide resolved
result = check_context_for_sql_injection(

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L32-L34

Added lines #L32 - L34 were not covered by tests
sql.decode("utf-8"), "Test_op", context, MySQL()
bitterpanda63 marked this conversation as resolved.
Show resolved Hide resolved
)

logger.debug("sql_injection results : %s", json.dumps(result))
if result:
raise Exception("SQL Injection [aikido_firewall]")
return prev_query_function(_self, sql)

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L38-L41

Added lines #L38 - L41 were not covered by tests

# pylint: disable=no-member
setattr(mysql.Connection, "query", aikido_new_query)
logger.debug("Wrapped `mysqlclient` module")
return modified_mysql

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

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L44-L46

Added lines #L44 - L46 were not covered by tests
5 changes: 2 additions & 3 deletions sample-apps/django-mysql/manage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import aikido_firewall # Aikido module
aikido_firewall.protect("django")

import os
import sys
import aikido_firewall # Aikido module


def main():
"""Run administrative tasks."""
aikido_firewall.protect("django")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sample-django-mysql-app.settings')
try:
from django.core.management import execute_from_command_line
Expand Down
Loading