-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from AikidoSec/AIK-3202
Add `mysqlclient` support
- Loading branch information
Showing
3 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
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_context_for_sql_injection, | ||
) | ||
from aikido_firewall.vulnerabilities.sql_injection.dialects import MySQL | ||
from aikido_firewall.helpers.logging import logger | ||
|
||
|
||
@importhook.on_import("MySQLdb.connections") | ||
def on_mysqlclient_import(mysql): | ||
""" | ||
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) | ||
|
||
prev_query_function = copy.deepcopy(mysql.Connection.query) | ||
|
||
def aikido_new_query(_self, sql): | ||
logger.debug("Wrapper - `mysqlclient` version : %s", version("mysqlclient")) | ||
|
||
context = get_current_context() | ||
result = check_context_for_sql_injection( | ||
sql.decode("utf-8"), "MySQLdb.connections.query", context, MySQL() | ||
) | ||
|
||
logger.debug("sql_injection results : %s", json.dumps(result)) | ||
if result: | ||
raise Exception("SQL Injection [aikido_firewall]") | ||
return prev_query_function(_self, sql) | ||
|
||
# pylint: disable=no-member | ||
setattr(mysql.Connection, "query", aikido_new_query) | ||
logger.debug("Wrapped `mysqlclient` module") | ||
return modified_mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters