|
1 | 1 | # Base64 Statements |
2 | 2 |
|
3 | | -Python Code Audit checks for obfuscated text, particularly content encoded with `base64`: |
| 3 | +The Python Code Audit tool detects obfuscated content, particularly code that uses `base64` (and related encodings) for encoding or decoding data. |
4 | 4 |
|
5 | | -* `base64` Encoding / Decoding. |
| 5 | +It specifically checks for the following calls: |
6 | 6 |
|
| 7 | +* `base64.b64decode` |
| 8 | +* `base64.b64encode` |
| 9 | +* `base64.b85encode` |
| 10 | +* `base64.z85decode` |
7 | 11 |
|
8 | 12 | ## Rationale |
9 | 13 |
|
| 14 | +Obfuscation using Base64 is a **long-standing and simple technique** commonly employed to conceal malicious code in Python projects. It enables attackers to hide payloads that would otherwise be easily identified. |
10 | 15 |
|
11 | | -Obfuscation is a long-standing and straightforward technique often used to conceal malicious code within Python projects. This technique allows attackers to easily hide malware within Python programs. |
| 16 | +The use of obfuscated content is uncommon in well-structured, legitimate Python code and is therefore considered a strong indicator of potential security risks. |
12 | 17 |
|
13 | | -The presence of obfuscated content is atypical in well-structured, non-malicious Python code and is a significant indicator of potential security risks. |
| 18 | +It is strongly recommended that any code containing Base64 encoding/decoding be carefully reviewed before deployment to production. **Python Code Audit** performs this check automatically. |
14 | 19 |
|
| 20 | +**Key red flags include:** |
| 21 | +* `base64.b64decode` followed immediately by `exec()` or `eval()` |
| 22 | +* Long Base64 strings embedded in Python scripts |
| 23 | +* Constructs such as `exec(base64.b64decode(...))` from untrusted sources |
15 | 24 |
|
16 | | -It’s recommended to review any code deployed to production using `base64` encoding. **Python Code Audit** does this automatically. |
| 25 | +## Common Malware Patterns |
| 26 | + |
| 27 | +Base64 encoding patterns are frequently found in Python-based malware and droppers: |
| 28 | + |
| 29 | +| Pattern | Code Snippet | Why It Is Detected | Implemented | |
| 30 | +|----------------------|---------------------------------------------------|--------------------------------------------------|-------------| |
| 31 | +| Standard b64 + exec | `exec(base64.b64decode(long_string))` | Extremely common obfuscation technique | ✅ | |
| 32 | +| Compressed | `exec(zlib.decompress(base64.b64decode(...)))` | Suggests larger hidden payload and evasion | ✅ | |
| 33 | +| Multi-layer | `base64.b64decode(base64.b64decode(...))` | Attempts to bypass simple pattern matching | ✅ | |
| 34 | +| Bytes decode | `exec(base64.b64decode(data).decode())` | Hides intent by decoding to string | ✅ | |
| 35 | +| Using aliases | `b64 = base64.b64decode; exec(b64(payload))` | Evasion of basic static analysis | ✅ | |
| 36 | +| Z85 / b85 | `base64.b85decode(...)` or `base64.z85decode(...)` | Non-standard encodings often indicate stealth | ✅ | |
| 37 | + |
| 38 | +## Security Considerations |
| 39 | + |
| 40 | +Base encoding does not provide confidentiality. As noted in RFC 4648 (Section 12), care must be taken when implementing base encoding and decoding to avoid introducing vulnerabilities. |
17 | 41 |
|
18 | 42 | Security considerations section from RFC 4648 (section 12): |
19 | 43 |
|
@@ -55,8 +79,10 @@ Security Considerations |
55 | 79 | distribution. |
56 | 80 | ``` |
57 | 81 |
|
58 | | -## More information |
59 | 82 |
|
60 | | -* https://docs.python.org/3/library/base64.html#base64-security |
61 | | -* https://datatracker.ietf.org/doc/html/rfc4648.html#page-14 |
62 | | -* [Base64 Malleability in Practice](https://eprint.iacr.org/2022/361.pdf) |
| 83 | +## References |
| 84 | + |
| 85 | +* [Python Documentation – base64](https://docs.python.org/3/library/base64.html) |
| 86 | +* [RFC 4648 – Security Considerations](https://datatracker.ietf.org/doc/html/rfc4648#section-12) |
| 87 | +* [Base64 Malleability in Practice](https://eprint.iacr.org/2022/361.pdf) |
| 88 | + |
0 commit comments