| 1 |
# CryptoParser |
| 2 |
|
| 3 |
An analysis-oriented cryptographic protocol parser and generator. Parses and composes TLS, SSL, SSH, DNSSEC, IKE, and HTTP security headers from raw bytes to typed Python objects. Not a comprehensive or secure implementation — designed for testing and analysis purposes. |
| 4 |
|
| 5 |
## Files |
| 6 |
|
| 7 |
### Project Root |
| 8 |
- `pyproject.toml` — Build system, dependencies, metadata (setuptools + setuptools-scm) |
| 9 |
- `setup.py` — Delegates to pyproject.toml |
| 10 |
- `README.md` — Project overview, install instructions, license |
| 11 |
- `CHANGELOG.rst` — Versioned changelog |
| 12 |
- `CONTRIBUTING.rst` — Contribution guidelines |
| 13 |
- `LICENSE.txt` — MPL-2.0 |
| 14 |
|
| 15 |
### Common Infrastructure (`cryptoparser/common/`) |
| 16 |
- `parse.py` — Core parsing/composing engine: `ParserBinary`, `ParserText`, `ComposerBinary`, `ComposerText`, `ParsableBase`, `ByteOrder` |
| 17 |
- `base.py` — Reusable parseable types: `Serializable`, `ProtocolVersionBase`, `VectorParsable`, `VariantParsable`, `Opaque`, `ListParsable`, enum parsers (1/2/3-byte) |
| 18 |
- `field.py` — Structured text field parsing: `FieldParsableBase`, `NameValuePair`, quoted strings, URLs, datetimes, percentages, key=value components |
| 19 |
- `exception.py` — Parser exceptions: `InvalidDataLength`, `NotEnoughData`, `TooMuchData`, `InvalidType` |
| 20 |
- `classes.py` — Generic domain classes: `LanguageTag` (RFC 5646) |
| 21 |
- `utils.py` — Utility: `get_leaf_classes()`, `bytes_to_hex_string()` |
| 22 |
- `x509.py` — X.509 Certificate Transparency: `SignedCertificateTimestamp`, `SignedCertificateTimestampList` |
| 23 |
|
| 24 |
### TLS/SSL (`cryptoparser/tls/`) |
| 25 |
- `record.py` — TLS record layer (5-byte header: content type, version, fragment length), dispatches to subprotocol parsers |
| 26 |
- `subprotocol.py` — TLS/SSL handshake messages: `ClientHello`, `ServerHello`, `Certificate`, `ServerKeyExchange`, `Finished`, `Alert`, `ApplicationData`, `ChangeCipherSpec` |
| 27 |
- `extension.py` — TLS extensions: SNI, ALPN, supported groups, key share, signature algorithms, PSK, GREASE, and ~40 more |
| 28 |
- `version.py` — TLS/SSL version handling: `TlsProtocolVersion`, `TlsVersionFactory` |
| 29 |
- `ciphersuite.py` — Ciphersuite factories: `TlsCipherSuiteFactory`, `SslCipherKindFactory` |
| 30 |
- `algorithm.py` — Algorithm factories: named curves, signature algorithms, EC point formats |
| 31 |
- `grease.py` — GREASE (Generate Random Extensions And Sustain Extensibility) handling |
| 32 |
- `ldap.py` — LDAP StartTLS protocol parser |
| 33 |
- `mysql.py` — MySQL protocol parser (handshake, SSL request) |
| 34 |
- `openvpn.py` — OpenVPN control channel packet parser |
| 35 |
- `postgresql.py` — PostgreSQL StartTLS protocol parser |
| 36 |
- `rdp.py` — RDP protocol parser (TPKT/X.224 encapsulation to extract TLS) |
| 37 |
|
| 38 |
### SSH (`cryptoparser/ssh/`) |
| 39 |
- `record.py` — SSH record layer (binary packet framing: length, padding, message code) |
| 40 |
- `subprotocol.py` — SSH transport messages: `SshMessageKexInit`, `SshMessageKexDHInit`, `SshMessageKexDHReply`, `SshMessageNewKeys` |
| 41 |
- `key.py` — SSH public/private key parsing: RSA, DSA, ECDSA, Ed25519, Ed448, X25519, X448; OpenSSH private key format, known_hosts, authorized_keys |
| 42 |
- `version.py` — SSH version banner parsing: `SshVersion`, `SshProtocolVersion`, `SshVersionBanner`, `SshComment` |
| 43 |
|
| 44 |
### DNSSEC (`cryptoparser/dnsrec/`) |
| 45 |
- `record.py` — DNS resource records: DNSKEY, DS, RRSIG, NSEC, NSEC3, NSEC3PARAM, CDNSKEY, CDS, TLSA, SSHFP |
| 46 |
- `txt.py` — DNS TXT record content: SPF, DKIM, DMARC, MTA-STS, TLSRPT, SMIMEA, CAA |
| 47 |
|
| 48 |
### HTTP Headers (`cryptoparser/httpx/`) |
| 49 |
- `header.py` — HTTP security headers: HSTS, CSP, HPKP, Expect-CT, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, Feature-Policy, Permissions-Policy, Cross-Origin headers, Set-Cookie, Cache-Control, WWW-Authenticate |
| 50 |
- `parse.py` — HTTP-specific field value components |
| 51 |
- `version.py` — `HttpVersion` enum (HTTP/1.0, HTTP/1.1) |
| 52 |
|
| 53 |
### IKE (`cryptoparser/ike/`) |
| 54 |
- `isakmp.py` — ISAKMP header parser (cookies, version, exchange type, flags) |
| 55 |
- `ikev1.py` — IKEv1 payloads: SA, Proposal, Transform, KE, ID, Cert, Auth, Nonce, Notify, Delete, VendorID |
| 56 |
- `ikev2.py` — IKEv2 payloads: SA, Proposal, Transform, KE, IDi/IDr, Cert, Auth, Nonce, Notify, TS initiator/responder, Encrypted |
| 57 |
- `common.py` — Common IKE data attributes (AF flag, transform types) |
| 58 |
- `version.py` — ISAKMP version handling: `IsakmpVersion`, `IsakmpProtocolVersion` |
| 59 |
|
| 60 |
### Tests (`test/`) |
| 61 |
- `test/common/` — Common infrastructure tests: base classes, parsing engine, exceptions, field parsing, X.509 |
| 62 |
- `test/tls/` — TLS/SSL tests: records, handshakes, extensions, cipher suites, alerts, version, wrapped protocols |
| 63 |
- `test/ssh/` — SSH tests: key parsing, records, subprotocols, version banners, cipher suites |
| 64 |
- `test/dnsrec/` — DNS tests: DNSSEC binary records, TXT structured text |
| 65 |
- `test/httpx/` — HTTP header tests: security headers, HTTP version |
| 66 |
- `test/ike/` — IKE tests: IKEv1/IKEv2 payloads, SA/transform negotiation, ISAKMP header |
| 67 |
|
| 68 |
## Architecture |
| 69 |
|
| 70 |
The project uses a layered parser/composer design: |
| 71 |
|
| 72 |
1. **Core** (`common/parse.py`) — `ParsableBase` abstract interface with `_parse()` returning `(object, bytes_consumed)`. `ParserBinary`/`ParserText` for input, `ComposerBinary`/`ComposerText` for output. |
| 73 |
2. **Reusable types** (`common/base.py`) — Enum parsers (1/2/3-byte), vectors, variants, opaque blobs, lists, strings, protocol versions built on the core engine. |
| 74 |
3. **Structured text** (`common/field.py`) — Name=value field parsing with typed components (strings, URLs, datetimes, quoted strings). |
| 75 |
4. **Protocol modules** — Each protocol builds on common types to parse protocol-specific messages from raw bytes to typed Python objects. |
| 76 |
5. **Enums/constants** — Defined in the `cryptodatahub` external library (submodule at `submodules/cryptodatahub/`). |
| 77 |
|
| 78 |
## Key Patterns |
| 79 |
|
| 80 |
- All parsable classes implement `_parse(cls, parseable, bytes_consumed=None)` classmethod |
| 81 |
- All composable classes implement `compose(self, composable)` method |
| 82 |
- Protocol version classes implement `__lt__`, `__le__`, etc. for gradeable security assessment |
| 83 |
- Factory classes wrap enum-based parsers (one-byte, two-byte) using `OneByteEnumParsable`/`TwoByteEnumParsable` |
| 84 |
- `@attr.s` (attrs library) is used heavily for data classes with validators |