Python Security Audit
Purpose
Perform a comprehensive, depth-first security audit of Python codebases.
This skill provides the complete knowledge of Bandit's 50+ security checks,
organized by category and severity, plus framework-specific patterns for
Django, Flask, FastAPI, and emerging ML/AI attack surfaces.
Use view_file and grep_search exclusively. No terminal commands.
Audit Workflow
Phase 1: Reconnaissance
- Identify the Python framework in use (Django, Flask, FastAPI, Tornado, aiohttp, raw stdlib)
- Check
requirements.txt / pyproject.toml / Pipfile for dangerous dependencies
- Map entry points: URL routes, CLI commands, message consumers, scheduled tasks
- Identify configuration files and secrets management approach
Phase 2: Systematic Check — By Category
Work through each category below. For each check, use grep_search to find all instances,
then view_file to trace the data flow and confirm exploitability.
B1xx — Miscellaneous Checks
| ID |
Name |
Severity |
What to Search |
| B101 |
assert_used |
Low |
assert statements used for security checks (removed with -O flag) |
| B102 |
exec_used |
Medium |
exec() calls — trace if input is user-controlled |
| B103 |
set_bad_file_permissions |
Medium |
os.chmod() with overly permissive modes (0o777, 0o666) |
| B104 |
hardcoded_bind_all_interfaces |
Medium |
Binding to 0.0.0.0 — exposes service on all interfaces |
| B105 |
hardcoded_password_string |
Low |
Strings assigned to variables named password, secret, key, token |
| B106 |
hardcoded_password_funcarg |
Low |
Password-like strings passed as function arguments |
| B107 |
hardcoded_password_default |
Low |
Default parameter values containing password-like strings |
| B108 |
hardcoded_tmp_directory |
Low |
Hardcoded /tmp paths — race conditions, symlink attacks |
| B109 |
password_config_option_not_marked_secret |
Low |
Config options with password/secret that aren't marked as sensitive |
| B110 |
try_except_pass |
Low |
except: pass — silently swallowing errors including security exceptions |
| B111 |
execute_with_run_as_root_equals_true |
Medium |
Functions called with run_as_root=True |
| B112 |
try_except_continue |
Low |
except: continue — same problem as B110 |
| B113 |
request_without_timeout |
Medium |
requests.get/post() without timeout= parameter — DoS via hang |
Audit Depth for B1xx
- B101: Check if
assert guards authentication or authorization. If so, HIGH severity.
- B102: Trace
exec() input — if user-controlled, escalate to CRITICAL (RCE).
- B105/106/107: Check if the hardcoded credentials are for production systems or test fixtures.
- B113: Check all HTTP client calls (
requests, httpx, urllib3, aiohttp) for timeout.
B2xx — Application/Framework Misconfiguration
| ID |
Name |
Severity |
What to Search |
| B201 |
flask_debug_true |
High |
app.run(debug=True) — enables Werkzeug debugger (RCE) |
| B202 |
tarfile_unsafe_members |
High |
tarfile.extractall() without filter= — path traversal via tar |
Audit Depth for B2xx
- B201: Check if
debug=True is conditional on environment or always on. Check for WERKZEUG_DEBUG_PIN.
- B202: Any
tarfile.open() + extractall() from user-uploaded files = CRITICAL path traversal.
B3xx — Dangerous Function Calls (Blacklists)
| ID |
Name |
Severity |
What to Search |
| B324 |
hashlib |
Medium |
Use of md5(), sha1() for security-sensitive operations (password hashing, integrity) |
Extended B3xx Checks (Eresus additions)
hashlib.md5() / hashlib.sha1() for password storage → escalate to HIGH
- Use of
random module instead of secrets for security tokens → HIGH
string.Template with user input → potential template injection
B5xx — Cryptography
| ID |
Name |
Severity |
What to Search |
| B501 |
request_with_no_cert_validation |
High |
requests.get(url, verify=False) — TLS downgrade |
| B502 |
ssl_with_bad_version |
High |
ssl.SSLContext(ssl.PROTOCOL_SSLv2) or SSLv3 |
| B503 |
ssl_with_bad_defaults |
Medium |
SSLContext with insecure default protocol |
| B504 |
ssl_with_no_version |
Medium |
SSLContext created without explicit protocol |
| B505 |
weak_cryptographic_key |
High |
RSA < 2048 bits, DSA < 2048 bits, EC < 224 bits |
| B506 |
yaml_load |
Medium |
yaml.load() without Loader=SafeLoader — deserialization RCE |
| B507 |
ssh_no_host_key_verification |
High |
Paramiko set_missing_host_key_policy(AutoAddPolicy) |
| B508 |
snmp_insecure_version |
Medium |
SNMPv1/v2 without authentication |
| B509 |
snmp_weak_cryptography |
Medium |
SNMPv3 with weak crypto |
Audit Depth for B5xx
- B501: Check if cert pinning is used. If
verify=False is in production code → CRITICAL.
- B506: This is a deserialization sink. If input comes from HTTP/file upload → CRITICAL RCE.
B6xx — Injection
| ID |
Name |
Severity |
What to Search |
| B601 |
paramiko_calls |
Medium |
Paramiko SSH command execution — trace if command is user-controlled |
| B602 |
subprocess_popen_with_shell_equals_true |
High |
subprocess.Popen(cmd, shell=True) — command injection |
| B603 |
subprocess_without_shell_equals_true |
Low |
subprocess.Popen(cmd) without shell — still check input |
| B604 |
any_other_function_with_shell_equals_true |
Medium |
Any function with shell=True parameter |
| B605 |
start_process_with_a_shell |
High |
os.system(), os.popen() — command injection |
| B606 |
start_process_with_no_shell |
Low |
os.execl(), os.execve() — still trace input |
| B607 |
start_process_with_partial_path |
Low |
Process started without full path — PATH hijacking |
| B608 |
hardcoded_sql_expressions |
Medium |
SQL strings built with + or % or f-strings |
| B609 |
linux_commands_wildcard_injection |
High |
Commands with * glob — wildcard injection (tar, chown, etc.) |
| B610 |
django_extra_used |
Medium |
Django QuerySet.extra() — raw SQL injection |
| B611 |
django_rawsql_used |
Medium |
Django RawSQL() — raw SQL injection |
| B612 |
logging_config_insecure_listen |
Medium |
logging.config.listen() — arbitrary code execution |
| B613 |
trojansource |
High |
Unicode bidirectional control characters — trojan source attack |
| B614 |
pytorch_load |
High |
torch.load() — uses pickle internally, RCE if untrusted |
| B615 |
huggingface_unsafe_download |
High |
HuggingFace model downloads without safety checks |
Audit Depth for B6xx
- B602/B605: Trace the command string backwards. If ANY part is user-controlled → CRITICAL RCE.
- B608: Check if the SQL is parameterized elsewhere. Raw f-string SQL = HIGH SQLi.
- B609:
tar cf archive.tar * in /tmp with user-created files → argument injection.
- B614:
torch.load() from user-uploaded model file → CRITICAL RCE via pickle.
- B615: ML model supply chain attack — check if
trust_remote_code=True.
B7xx — XSS / Template Injection
| ID |
Name |
Severity |
What to Search |
| B701 |
jinja2_autoescape_false |
High |
jinja2.Environment(autoescape=False) — stored/reflected XSS |
| B702 |
use_of_mako_templates |
Medium |
Mako templates — no auto-escaping by default |
| B703 |
django_mark_safe |
Medium |
mark_safe(user_input) — bypasses Django auto-escaping |
| B704 |
markupsafe_markup_xss |
Medium |
Markup(user_input) — bypasses escaping |
Audit Depth for B7xx
- B701: If
autoescape=False and template renders user input → CRITICAL XSS.
- B703: Trace what data is passed to
mark_safe(). If user-controlled → HIGH XSS.
Framework-Specific Deep Checks
Django
- Check
ALLOWED_HOSTS configuration (empty = open redirect)
- Check
CSRF_COOKIE_HTTPONLY, SESSION_COOKIE_SECURE, SECURE_BROWSER_XSS_FILTER
- Check for
@csrf_exempt decorators on sensitive views
- Check
DEBUG = True in production settings
- Check
SECRET_KEY hardcoded or in version control
- Check
MIDDLEWARE ordering (SecurityMiddleware should be first)
- Check
AUTH_PASSWORD_VALIDATORS configuration
Flask
- Check
SECRET_KEY generation (must be cryptographically random)
- Check
app.run(debug=True) in production
- Check for
@app.before_request authentication enforcement
- Check session cookie configuration (
SESSION_COOKIE_SECURE, SESSION_COOKIE_HTTPONLY)
- Check file upload handling (
werkzeug.utils.secure_filename)
FastAPI
- Check for missing input validation (
Body(), Query(), Path() without constraints)
- Check CORS configuration (
allow_origins=["*"])
- Check authentication dependency injection (missing
Depends())
- Check
FileResponse / StreamingResponse path traversal
- Check OAuth2 implementation (token validation, scope enforcement)
ML/AI Attack Surface
torch.load() — pickle-based, RCE from untrusted models
transformers.pipeline(trust_remote_code=True) — arbitrary code execution
pickle.loads() in model serialization pipelines
- Prompt injection in LLM-based applications
- Model poisoning via untrusted training data
Severity Matrix
| Confidence \ Severity |
LOW |
MEDIUM |
HIGH |
| HIGH |
Info |
Medium |
Critical |
| MEDIUM |
Low |
Medium |
High |
| LOW |
Info |
Low |
Medium |
Report Format
For each finding, report:
### [B-ID]: [Check Name]
**Severity**: [LOW/MEDIUM/HIGH/CRITICAL]
**Confidence**: [LOW/MEDIUM/HIGH]
**File**: [path]:[line]
**Vulnerable Code**:
[show the code]
**Data Flow**:
[source] → [intermediaries] → [sink]
**Impact**: [what an attacker achieves]
**Remediation**: [specific fix with code example]
**Bandit Reference**: B[xxx]
Tooling Constraints
Use ONLY:
view_file — read source code
grep_search — find patterns across the codebase
Do NOT use any terminal commands.
1---
2name: eresus-python-audit
3description: >
4 Deep Python-specific security audit skill with 50+ vulnerability class coverage across 7 categories.
5 Trigger when auditing Python code: "audit this Python app", "find Python security issues",
6 "check Flask/Django for vulnerabilities", "Python SAST review", "check for pickle vulnerabilities",
7 "review this FastAPI code". Covers misconfiguration, injection, crypto, XSS, deserialization,
8 and ML/AI attack surfaces. Includes scripts/rules.json for programmatic rule lookup.
9metadata:
10 source: https://skillmd.com/skills/aibot88/eresus-python-audit
11 version: "1.0"
12 domain: application-security
13 mode: python-audit
14 persona: python-security-researcher
15---
16
17# Python Security Audit
18
19## Purpose
20
21Perform a comprehensive, depth-first security audit of Python codebases.
22This skill provides the complete knowledge of Bandit's 50+ security checks,
23organized by category and severity, plus framework-specific patterns for
24Django, Flask, FastAPI, and emerging ML/AI attack surfaces.
25
26Use `view_file` and `grep_search` exclusively. No terminal commands.
27
28---
29
30## Audit Workflow
31
32### Phase 1: Reconnaissance
33
341. Identify the Python framework in use (Django, Flask, FastAPI, Tornado, aiohttp, raw stdlib)
352. Check `requirements.txt` / `pyproject.toml` / `Pipfile` for dangerous dependencies
363. Map entry points: URL routes, CLI commands, message consumers, scheduled tasks
374. Identify configuration files and secrets management approach
38
39### Phase 2: Systematic Check — By Category
40
41Work through each category below. For each check, use `grep_search` to find all instances,
42then `view_file` to trace the data flow and confirm exploitability.
43
44---
45
46## B1xx — Miscellaneous Checks
47
48| ID | Name | Severity | What to Search |
49|----|------|----------|----------------|
50| B101 | `assert_used` | Low | `assert` statements used for security checks (removed with `-O` flag) |
51| B102 | `exec_used` | Medium | `exec()` calls — trace if input is user-controlled |
52| B103 | `set_bad_file_permissions` | Medium | `os.chmod()` with overly permissive modes (0o777, 0o666) |
53| B104 | `hardcoded_bind_all_interfaces` | Medium | Binding to `0.0.0.0` — exposes service on all interfaces |
54| B105 | `hardcoded_password_string` | Low | Strings assigned to variables named `password`, `secret`, `key`, `token` |
55| B106 | `hardcoded_password_funcarg` | Low | Password-like strings passed as function arguments |
56| B107 | `hardcoded_password_default` | Low | Default parameter values containing password-like strings |
57| B108 | `hardcoded_tmp_directory` | Low | Hardcoded `/tmp` paths — race conditions, symlink attacks |
58| B109 | `password_config_option_not_marked_secret` | Low | Config options with password/secret that aren't marked as sensitive |
59| B110 | `try_except_pass` | Low | `except: pass` — silently swallowing errors including security exceptions |
60| B111 | `execute_with_run_as_root_equals_true` | Medium | Functions called with `run_as_root=True` |
61| B112 | `try_except_continue` | Low | `except: continue` — same problem as B110 |
62| B113 | `request_without_timeout` | Medium | `requests.get/post()` without `timeout=` parameter — DoS via hang |
63
64### Audit Depth for B1xx
65- B101: Check if `assert` guards authentication or authorization. If so, **HIGH** severity.
66- B102: Trace `exec()` input — if user-controlled, escalate to **CRITICAL** (RCE).
67- B105/106/107: Check if the hardcoded credentials are for production systems or test fixtures.
68- B113: Check all HTTP client calls (`requests`, `httpx`, `urllib3`, `aiohttp`) for timeout.
69
70---
71
72## B2xx — Application/Framework Misconfiguration
73
74| ID | Name | Severity | What to Search |
75|----|------|----------|----------------|
76| B201 | `flask_debug_true` | High | `app.run(debug=True)` — enables Werkzeug debugger (RCE) |
77| B202 | `tarfile_unsafe_members` | High | `tarfile.extractall()` without `filter=` — path traversal via tar |
78
79### Audit Depth for B2xx
80- B201: Check if `debug=True` is conditional on environment or always on. Check for `WERKZEUG_DEBUG_PIN`.
81- B202: Any `tarfile.open()` + `extractall()` from user-uploaded files = **CRITICAL** path traversal.
82
83---
84
85## B3xx — Dangerous Function Calls (Blacklists)
86
87| ID | Name | Severity | What to Search |
88|----|------|----------|----------------|
89| B324 | `hashlib` | Medium | Use of `md5()`, `sha1()` for security-sensitive operations (password hashing, integrity) |
90
91### Extended B3xx Checks (Eresus additions)
92- `hashlib.md5()` / `hashlib.sha1()` for password storage → escalate to **HIGH**
93- Use of `random` module instead of `secrets` for security tokens → **HIGH**
94- `string.Template` with user input → potential template injection
95
96---
97
98## B5xx — Cryptography
99
100| ID | Name | Severity | What to Search |
101|----|------|----------|----------------|
102| B501 | `request_with_no_cert_validation` | High | `requests.get(url, verify=False)` — TLS downgrade |
103| B502 | `ssl_with_bad_version` | High | `ssl.SSLContext(ssl.PROTOCOL_SSLv2)` or SSLv3 |
104| B503 | `ssl_with_bad_defaults` | Medium | SSLContext with insecure default protocol |
105| B504 | `ssl_with_no_version` | Medium | SSLContext created without explicit protocol |
106| B505 | `weak_cryptographic_key` | High | RSA < 2048 bits, DSA < 2048 bits, EC < 224 bits |
107| B506 | `yaml_load` | Medium | `yaml.load()` without `Loader=SafeLoader` — deserialization RCE |
108| B507 | `ssh_no_host_key_verification` | High | Paramiko `set_missing_host_key_policy(AutoAddPolicy)` |
109| B508 | `snmp_insecure_version` | Medium | SNMPv1/v2 without authentication |
110| B509 | `snmp_weak_cryptography` | Medium | SNMPv3 with weak crypto |
111
112### Audit Depth for B5xx
113- B501: Check if cert pinning is used. If `verify=False` is in production code → **CRITICAL**.
114- B506: This is a deserialization sink. If input comes from HTTP/file upload → **CRITICAL** RCE.
115
116---
117
118## B6xx — Injection
119
120| ID | Name | Severity | What to Search |
121|----|------|----------|----------------|
122| B601 | `paramiko_calls` | Medium | Paramiko SSH command execution — trace if command is user-controlled |
123| B602 | `subprocess_popen_with_shell_equals_true` | High | `subprocess.Popen(cmd, shell=True)` — command injection |
124| B603 | `subprocess_without_shell_equals_true` | Low | `subprocess.Popen(cmd)` without shell — still check input |
125| B604 | `any_other_function_with_shell_equals_true` | Medium | Any function with `shell=True` parameter |
126| B605 | `start_process_with_a_shell` | High | `os.system()`, `os.popen()` — command injection |
127| B606 | `start_process_with_no_shell` | Low | `os.execl()`, `os.execve()` — still trace input |
128| B607 | `start_process_with_partial_path` | Low | Process started without full path — PATH hijacking |
129| B608 | `hardcoded_sql_expressions` | Medium | SQL strings built with `+` or `%` or f-strings |
130| B609 | `linux_commands_wildcard_injection` | High | Commands with `*` glob — wildcard injection (tar, chown, etc.) |
131| B610 | `django_extra_used` | Medium | Django `QuerySet.extra()` — raw SQL injection |
132| B611 | `django_rawsql_used` | Medium | Django `RawSQL()` — raw SQL injection |
133| B612 | `logging_config_insecure_listen` | Medium | `logging.config.listen()` — arbitrary code execution |
134| B613 | `trojansource` | High | Unicode bidirectional control characters — trojan source attack |
135| B614 | `pytorch_load` | High | `torch.load()` — uses pickle internally, RCE if untrusted |
136| B615 | `huggingface_unsafe_download` | High | HuggingFace model downloads without safety checks |
137
138### Audit Depth for B6xx
139- B602/B605: Trace the command string backwards. If ANY part is user-controlled → **CRITICAL** RCE.
140- B608: Check if the SQL is parameterized elsewhere. Raw f-string SQL = **HIGH** SQLi.
141- B609: `tar cf archive.tar *` in `/tmp` with user-created files → argument injection.
142- B614: `torch.load()` from user-uploaded model file → **CRITICAL** RCE via pickle.
143- B615: ML model supply chain attack — check if `trust_remote_code=True`.
144
145---
146
147## B7xx — XSS / Template Injection
148
149| ID | Name | Severity | What to Search |
150|----|------|----------|----------------|
151| B701 | `jinja2_autoescape_false` | High | `jinja2.Environment(autoescape=False)` — stored/reflected XSS |
152| B702 | `use_of_mako_templates` | Medium | Mako templates — no auto-escaping by default |
153| B703 | `django_mark_safe` | Medium | `mark_safe(user_input)` — bypasses Django auto-escaping |
154| B704 | `markupsafe_markup_xss` | Medium | `Markup(user_input)` — bypasses escaping |
155
156### Audit Depth for B7xx
157- B701: If `autoescape=False` and template renders user input → **CRITICAL** XSS.
158- B703: Trace what data is passed to `mark_safe()`. If user-controlled → **HIGH** XSS.
159
160---
161
162## Framework-Specific Deep Checks
163
164### Django
165- Check `ALLOWED_HOSTS` configuration (empty = open redirect)
166- Check `CSRF_COOKIE_HTTPONLY`, `SESSION_COOKIE_SECURE`, `SECURE_BROWSER_XSS_FILTER`
167- Check for `@csrf_exempt` decorators on sensitive views
168- Check `DEBUG = True` in production settings
169- Check `SECRET_KEY` hardcoded or in version control
170- Check `MIDDLEWARE` ordering (SecurityMiddleware should be first)
171- Check `AUTH_PASSWORD_VALIDATORS` configuration
172
173### Flask
174- Check `SECRET_KEY` generation (must be cryptographically random)
175- Check `app.run(debug=True)` in production
176- Check for `@app.before_request` authentication enforcement
177- Check session cookie configuration (`SESSION_COOKIE_SECURE`, `SESSION_COOKIE_HTTPONLY`)
178- Check file upload handling (`werkzeug.utils.secure_filename`)
179
180### FastAPI
181- Check for missing input validation (`Body()`, `Query()`, `Path()` without constraints)
182- Check CORS configuration (`allow_origins=["*"]`)
183- Check authentication dependency injection (missing `Depends()`)
184- Check `FileResponse` / `StreamingResponse` path traversal
185- Check OAuth2 implementation (token validation, scope enforcement)
186
187### ML/AI Attack Surface
188- `torch.load()` — pickle-based, RCE from untrusted models
189- `transformers.pipeline(trust_remote_code=True)` — arbitrary code execution
190- `pickle.loads()` in model serialization pipelines
191- Prompt injection in LLM-based applications
192- Model poisoning via untrusted training data
193
194---
195
196## Severity Matrix
197
198| Confidence \ Severity | LOW | MEDIUM | HIGH |
199|----------------------|-----|--------|------|
200| **HIGH** | Info | Medium | Critical |
201| **MEDIUM** | Low | Medium | High |
202| **LOW** | Info | Low | Medium |
203
204---
205
206## Report Format
207
208For each finding, report:
209
210```
211### [B-ID]: [Check Name]
212
213**Severity**: [LOW/MEDIUM/HIGH/CRITICAL]
214**Confidence**: [LOW/MEDIUM/HIGH]
215**File**: [path]:[line]
216
217**Vulnerable Code**:
218[show the code]
219
220**Data Flow**:
221[source] → [intermediaries] → [sink]
222
223**Impact**: [what an attacker achieves]
224**Remediation**: [specific fix with code example]
225**Bandit Reference**: B[xxx]
226```
227
228---
229
230## Tooling Constraints
231
232Use ONLY:
233- `view_file` — read source code
234- `grep_search` — find patterns across the codebase
235
236Do NOT use any terminal commands.