Overview
Thepadding_strat module provides functions to add and remove random padding from messages. This helps obscure message sizes and makes traffic analysis more difficult.
Padding is added by prepending a 2-byte length prefix followed by random bytes before the actual plaintext.
Module: evasion.padding_strat
Functions
pad()
Parameters
The original message to be padded
Minimum number of random padding bytes to add
Maximum number of random padding bytes to add
Returns
Padded message with structure:
- Bytes 0-1: Big-endian uint16 indicating pad length
- Bytes 2 to (2+pad_len): Random padding bytes
- Remaining bytes: Original plaintext
Raises
ValueError: Ifmin_bytes > max_bytes
Example
strip_padding()
pad() and return the original plaintext.
Parameters
The padded message returned by
pad()Returns
Original plaintext with padding and length prefix removed
Raises
ValueError: If input is too short to contain length prefix (< 2 bytes)ValueError: If input is shorter than the declared padding length
Example
Constants
HEADER_SIZE
>H in struct format), which means:
- Maximum padding length: 65,535 bytes
- Minimum total padded message size: 2 bytes (header only)
Message Format
Padded messages use the following structure:Example Sizes
| Configuration | Padding Length | Total Size |
|---|---|---|
pad(msg, 0, 0) | 0 bytes | 2 + len(msg) |
pad(msg, 32, 64) | 32-64 bytes | 34-66 + len(msg) |
pad(msg, 128, 128) | 128 bytes | 130 + len(msg) |
Usage Patterns
Basic Padding
Fixed-Size Padding
Variable Padding Range
No Padding (Header Only)
Security Considerations
Randomness Source
Padding bytes are generated usingos.urandom(), which provides cryptographically secure random bytes from the OS entropy pool.
Side-Channel Resistance
Padding helps mitigate:- Traffic analysis: Makes it harder to identify message types by size
- Size-based fingerprinting: Obscures predictable message patterns
- Correlation attacks: Adds noise to request/response size correlations
Limitations
- Maximum padding: 65,535 bytes (uint16 limit)
- Overhead: Minimum 2 bytes per message
- Does not provide encryption or authentication