Skip to content

Add security rules for MySQL connections in Python applications #87

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

Closed
wants to merge 3 commits into from
Closed
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
88 changes: 88 additions & 0 deletions rules/python/security/python-mysql-empty-password-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
id: python-mysql-empty-password-python
language: python
severity: warning
message: >-
The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
note: >-
[CWE-287]: Improper Authentication
[A07:2021]: Identification and Authentication Failures
[REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

rule:
any:
- kind: call
has:
kind: attribute
field: function
regex: ^mysql.connector.connect$
precedes:
kind: argument_list
has:
kind: keyword_argument
all:
- has:
kind: identifier
nthChild: 1
regex: ^(password|passwd)$
- has:
kind: string
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
inside:
stopBy: end
follows:
stopBy: end
kind: import_statement
has:
kind: dotted_name
nthChild: 1
regex: ^mysql.connector$
- kind: call
has:
kind: attribute
field: function
pattern: $CONNECTOR_ALIAS.connect
precedes:
kind: argument_list
has:
kind: keyword_argument
all:
- has:
kind: identifier
nthChild: 1
regex: ^(password|passwd)$
- has:
kind: string
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
inside:
stopBy: end
follows:
stopBy: end
kind: import_statement
has:
kind: aliased_import
nthChild: 1
all:
- has:
kind: dotted_name
nthChild: 1
regex: ^mysql.connector$
- has:
kind: identifier
field: alias
nthChild: 2
pattern: $CONNECTOR_ALIAS
94 changes: 94 additions & 0 deletions rules/python/security/python-mysql-hardcoded-secret-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
id: python-mysql-hardcoded-secret-python
language: python
severity: warning
message: >-
A secret is hard-coded in the application. Secrets stored in source code, such as credentials, identifiers, and other types of sensitive data, can be leaked and used by internal or external malicious actors. Use environment variables to securely provide credentials and other secrets or retrieve them from a secure vault or Hardware Security Module (HSM).
note: >-
[CWE-798]: Use of Hard-coded Credentials
[A07:2021]: Identification and Authentication Failures
[REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

rule:
any:
- kind: call
has:
kind: attribute
field: function
regex: ^mysql.connector.connect$
precedes:
kind: argument_list
has:
kind: keyword_argument
all:
- has:
kind: identifier
nthChild: 1
regex: ^(password|passwd)$
- has:
kind: string
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_content
nthChild: 2
- has:
kind: string_end
nthChild: 3
inside:
stopBy: end
follows:
stopBy: end
kind: import_statement
has:
kind: dotted_name
nthChild: 1
regex: ^mysql.connector$
- kind: call
has:
kind: attribute
field: function
pattern: $CONNECTOR_ALIAS.connect
precedes:
kind: argument_list
has:
kind: keyword_argument
all:
- has:
kind: identifier
nthChild: 1
regex: ^(password|passwd)$
- has:
kind: string
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_content
nthChild: 2
- has:
kind: string_end
nthChild: 3
inside:
stopBy: end
follows:
stopBy: end
kind: import_statement
has:
kind: aliased_import
nthChild: 1
all:
- has:
kind: dotted_name
nthChild: 1
regex: ^mysql.connector$
- has:
kind: identifier
field: alias
nthChild: 2
pattern: $CONNECTOR_ALIAS
214 changes: 214 additions & 0 deletions rules/python/security/python-mysqlclient-empty-password-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
id: python-mysqlclient-empty-password-python
language: python
severity: warning
message: >-
The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
note: >-
[CWE-287]: Improper Authentication
[A07:2021]: Identification and Authentication Failures
[REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

rule:
kind: call
any:
- all:
- has:
nthChild: 1
kind: attribute
pattern: MySQLdb.$CONNECT
- has:
kind: argument_list
nthChild: 2
has:
any:
- kind: keyword_argument
all:
- has:
kind: identifier
field: name
nthChild: 1
regex: ^passwd$
- has:
kind: string
field: value
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- kind: string
nthChild: 3
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- all:
- has:
nthChild: 1
kind: attribute
pattern: $MYSQL_ALIAS.$CONNECT
inside:
stopBy: end
follows:
stopBy: end
kind: import_statement
has:
kind: aliased_import
all:
- has:
kind: dotted_name
regex: MySQLdb
- has:
kind: identifier
pattern: $MYSQL_ALIAS
- has:
kind: argument_list
nthChild: 2
has:
any:
- kind: keyword_argument
all:
- has:
kind: identifier
field: name
nthChild: 1
regex: ^passwd$
- has:
kind: string
field: value
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- kind: string
nthChild: 3
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- all:
- has:
nthChild: 1
kind: attribute
any:
- pattern: MySQLdb._mysql.$CONNECT
- pattern: _mysql.$CONNECT
inside:
stopBy: end
follows:
stopBy: end
kind: import_from_statement
all:
- has:
kind: dotted_name
field: module_name
regex: ^MySQLdb$
- has:
kind: dotted_name
field: name
regex: ^_mysql$
- has:
kind: argument_list
nthChild: 2
has:
any:
- kind: keyword_argument
all:
- has:
kind: identifier
field: name
nthChild: 1
regex: ^passwd$
- has:
kind: string
field: value
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- kind: string
nthChild: 3
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- all:
- has:
nthChild: 1
kind: attribute
pattern: $MYSQL_FROM_ALIAS.$CONNECT
inside:
stopBy: end
follows:
stopBy: end
kind: import_from_statement
all:
- has:
kind: dotted_name
field: module_name
regex: ^MySQLdb$
nthChild: 1
- has:
kind: aliased_import
all:
- has:
kind: dotted_name
field: name
regex: ^_mysql$
- has:
kind: identifier
field: alias
pattern: $MYSQL_FROM_ALIAS
- has:
kind: argument_list
nthChild: 2
has:
any:
- kind: keyword_argument
all:
- has:
kind: identifier
field: name
nthChild: 1
regex: ^passwd$
- has:
kind: string
field: value
nthChild: 2
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
- kind: string
nthChild: 3
all:
- has:
kind: string_start
nthChild: 1
- has:
kind: string_end
nthChild: 2
Loading