|
| 1 | +# Base64 Statements |
| 2 | + |
| 3 | +Python Code should not contain obfuscated content, particularly code that uses `base64` (and related encodings) for encoding or decoding data is always suspected. |
| 4 | + |
| 5 | +The following calls can be an indication for obfuscated contain that might cause a security weakness. |
| 6 | + |
| 7 | +* `base64.b64decode` |
| 8 | +* `base64.b64encode` |
| 9 | +* `base64.b85encode` |
| 10 | +* `base64.z85decode` |
| 11 | + |
| 12 | +## Security concerns |
| 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. |
| 15 | + |
| 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. |
| 17 | + |
| 18 | +:::{tip} |
| 19 | +It is strongly recommended that any code containing Base64 encoding/decoding be carefully reviewed before deployment to production. Good modern Python SAST scanners, like [**Python Code Audit**](https://github.com/nocomplexity/codeaudit) check for `base64` module usage in code. |
| 20 | +::: |
| 21 | + |
| 22 | +**Key red flags include:** |
| 23 | +* `base64.b64decode` followed immediately by `exec()` or `eval()` |
| 24 | +* Long Base64 strings embedded in Python scripts |
| 25 | +* Constructs such as `exec(base64.b64decode(...))` from untrusted sources |
| 26 | + |
| 27 | +### Common Malware Patterns |
| 28 | + |
| 29 | +Base64 encoding patterns are frequently found in Python-based malware and droppers: |
| 30 | + |
| 31 | +| Pattern | Code Snippet | Security Concern | |
| 32 | +|----------------------|---------------------------------------------------|-----------------------------------------------| |
| 33 | +| Standard b64 + exec | `exec(base64.b64decode(long_string))` | Extremely common obfuscation technique | |
| 34 | +| Compressed | `exec(zlib.decompress(base64.b64decode(...)))` | Suggests larger hidden payload and evasion | |
| 35 | +| Multi-layer | `base64.b64decode(base64.b64decode(...))` | Attempts to bypass simple pattern matching | |
| 36 | +| Bytes decode | `exec(base64.b64decode(data).decode())` | Hides intent by decoding to string | |
| 37 | +| Using aliases | `b64 = base64.b64decode; exec(b64(payload))` | Evasion of basic static analysis | |
| 38 | +| Z85 / b85 | `base64.b85decode(...)` or `base64.z85decode(...)` | Non-standard encodings often indicate stealth| |
| 39 | + |
| 40 | +### Security Considerations |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +Security considerations section from RFC 4648 (section 12): |
| 45 | + |
| 46 | +```text |
| 47 | +Security Considerations |
| 48 | +
|
| 49 | + When base encoding and decoding is implemented, care should be taken |
| 50 | + not to introduce vulnerabilities to buffer overflow attacks, or other |
| 51 | + attacks on the implementation. A decoder should not break on invalid |
| 52 | + input including, e.g., embedded NUL characters (ASCII 0). |
| 53 | +
|
| 54 | + If non-alphabet characters are ignored, instead of causing rejection |
| 55 | + of the entire encoding (as recommended), a covert channel that can be |
| 56 | + used to "leak" information is made possible. The ignored characters |
| 57 | + could also be used for other nefarious purposes, such as to avoid a |
| 58 | + string equality comparison or to trigger implementation bugs. The |
| 59 | + implications of ignoring non-alphabet characters should be understood |
| 60 | + in applications that do not follow the recommended practice. |
| 61 | + Similarly, when the base 16 and base 32 alphabets are handled case |
| 62 | + insensitively, alteration of case can be used to leak information or |
| 63 | + make string equality comparisons fail. |
| 64 | +
|
| 65 | + When padding is used, there are some non-significant bits that |
| 66 | + warrant security concerns, as they may be abused to leak information |
| 67 | + or used to bypass string equality comparisons or to trigger |
| 68 | + implementation problems. |
| 69 | +
|
| 70 | + Base encoding visually hides otherwise easily recognized information, |
| 71 | + such as passwords, but does not provide any computational |
| 72 | + confidentiality. This has been known to cause security incidents |
| 73 | + when, e.g., a user reports details of a network protocol exchange |
| 74 | + (perhaps to illustrate some other problem) and accidentally reveals |
| 75 | + the password because she is unaware that the base encoding does not |
| 76 | + protect the password. |
| 77 | +
|
| 78 | + Base encoding adds no entropy to the plaintext, but it does increase |
| 79 | + the amount of plaintext available and provide a signature for |
| 80 | + cryptanalysis in the form of a characteristic probability |
| 81 | + distribution. |
| 82 | +``` |
| 83 | + |
| 84 | +## Example |
| 85 | + |
| 86 | +```python |
| 87 | +import base64 |
| 88 | +safe_string=b'aW1wb3J0IG9zOyBvcy5zeXN0ZW0oJ21hbGljaW91cyBjb21tYW5kJyk=' |
| 89 | + |
| 90 | +# Decoding patterns - still common in Python malware |
| 91 | +b64 = base64.b64decode |
| 92 | +exec(b64(safe_string)) |
| 93 | +``` |
| 94 | + |
| 95 | +What some Python novice did - the attacker side: |
| 96 | + |
| 97 | +```python |
| 98 | +payload = b"import os; os.system('malicious command')" |
| 99 | +encoded_b64 = base64.b64encode(payload) |
| 100 | + |
| 101 | +``` |
| 102 | + |
| 103 | +## References |
| 104 | + |
| 105 | +* [Python Documentation – base64](https://docs.python.org/3/library/base64.html) |
| 106 | +* [RFC 4648 – Security Considerations](https://datatracker.ietf.org/doc/html/rfc4648#section-12) |
| 107 | +* [Base64 Malleability in Practice](https://eprint.iacr.org/2022/361.pdf) |
| 108 | + |
0 commit comments