moontls-parser

    TLS ClientHello parser and JA3/JA4-style fingerprint toolkit for MoonBit traffic-audit probes.

    tls
    ja3
    ja4
    fingerprint
    security
    parser
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    4 months ago
    Downloads
    63

    #MoonTLS-Parser

    MoonTLS-Parser is a MoonBit library for passive TLS record inspection, handshake parsing (ClientHello, ServerHello, Certificate, Alert), and fingerprint generation (JA3, JA4-a, JA4S-a). It is designed for traffic-audit probes, sidecar policy filters, and Wasm-based gateways that need routing, auditing, and client-classification signals without decrypting payloads.

    This release focuses on the unencrypted TLS handshake surface and features:

    • TLS record parsing and length validation.
    • Streaming parse of fragmented ClientHello records.
    • SNI, ALPN, cipher suite, supported group, EC point format, and signature algorithm extraction from ClientHello.
    • GREASE filtering for JA3-compatible fields.
    • JA3 canonical string and MD5 digest generation.
    • A compact JA4-a style summary for coarse client bucketing.
    • ServerHello Handshake Parsing: negotiated version (supporting TLS 1.3 supported_versions extensions), cipher suite, random, session ID, and selected ALPN.
    • JA4S-a Fingerprinting: server handshake fingerprinting for audit classification.
    • Alert Record Parsing: extract Alert Level and Alert Description with readable string conversion (e.g., handshake_failure, close_notify).
    • Certificate Handshake Parsing: extract DER certificate lengths in the chain for size auditing.
    • JSON Serialization: built-in .to_json() methods for ClientHello, ServerHello, Alerts, and Certificates.

    #Why This Project

    The OSC2026 requirement favors reusable MoonBit ecosystem libraries with clear scope, tests, documentation, and maintainable public APIs. TLS record parsing is a good fit because it is useful in security tooling but still small enough to implement as a focused MoonBit package.

    This repository is an original MoonBit implementation. It references public TLS, JA3, and JA4 behavior, but it does not copy code from any upstream parser.

    #Install

    After the package is published on Mooncakes:

    moon add yhsrtty/moontls-parser

    For local development:

    moon check --deny-warn moon test --deny-warn moon run cmd/main

    #Minimal API

    #Parsing TLS Records

    You can parse any TLS record (Handshake, Alert, etc.) using parse_tls_record:

    let parsed = parse_tls_record(packet_bytes)
    match parsed {
    Ok(ClientHelloRecord(hello)) => {
    println(hello.server_name)
    println(hello.ja3_digest())
    println(hello.to_json())
    }
    Ok(ServerHelloRecord(hello)) => {
    println(hello.selected_alpn)
    println(hello.ja4s_a())
    println(hello.to_json())
    }
    Ok(AlertRecord(alert)) => {
    println(alert.description_string())
    }
    Ok(CertificateRecord(cert)) => {
    println(cert.cert_lengths)
    }
    Ok(GenericRecord(ty, payload)) => {
    println("Other TLS Record")
    }
    Err(err) => println(err)
    }

    For incremental capture pipelines, use ClientHelloStream:

    let stream = ClientHelloStream::new()
    let a = stream.push(first_fragment)
    let b = stream.push(second_fragment)

    NeedMore means the record header or record body is incomplete. Parsed returns a ClientHello. Rejected means the buffered bytes are not a valid TLS ClientHello record.

    #Current Scope

    Implemented:

    • TLS record content types handshake (0x16) and alert (0x15).
    • TLS 1.0 through TLS 1.3 record-version range.
    • Handshake types client_hello (1), server_hello (2), and certificate (11).
    • Extensions server_name, application_layer_protocol_negotiation (ALPN), supported_groups, ec_point_formats, signature_algorithms, and supported_versions.
    • Fingerprinting: JA3, JA4-a, and JA4S-a.
    • JSON Serialization for parsed elements.

    Not implemented yet:

    • TCP reassembly beyond a single TLS record.
    • Full DER certificate structure parsing (extracts only certificate lengths).
    • Encrypted ClientHello decryption.
    • Live pcap reading.

    Those are natural extension points rather than hidden behavior.

    #Verification

    The test suite includes hand-built ClientHello, ServerHello, Alert, and Certificate record fixtures.

    moon test --deny-warn moon check --deny-warn moon info

    The repository also includes:

    • .github/workflows/ci.yml for GitHub CI (Windows, Linux, macOS).
    • scripts/verify_acceptance.ps1 for local OSC2026-style checks.
    • docs/source-attribution.md for source and licensing notes.
    • docs/acceptance-checklist.md for the current competition-readiness status.

    #License

    Apache-2.0. See LICENSE.

    ClientHello

    pub struct ClientHello {
    legacy_version : UInt16
    record_version : UInt16
    cipher_suites : Array[UInt16]
    compression_methods : Array[Byte]
    extensions : Array[UInt16]
    supported_groups : Array[UInt16]
    ec_point_formats : Array[Byte]
    signature_algorithms : Array[UInt16]
    alpn_protocols : Array[String]
    server_name : String?
    raw_length : Int
    } derive(
    Debug
    )

    ClientHello::ja3_digest

    fn ClientHello::ja3_digest(self : ClientHello) -> String

    ClientHello::ja3_string

    fn ClientHello::ja3_string(self : ClientHello) -> String

    ClientHello::ja4_a

    fn ClientHello::ja4_a(self : ClientHello) -> String

    ClientHello::to_json

    fn ClientHello::to_json(self : ClientHello) -> String

    ClientHelloStream

    pub struct ClientHelloStream {
    buffer : Array[Byte]
    } derive(
    Debug
    )

    ClientHelloStream::new

    ClientHelloStream::push

    fn ClientHelloStream::push(self : ClientHelloStream, chunk : BytesView) -> StreamResult

    ClientHelloStream::reset

    fn ClientHelloStream::reset(self : ClientHelloStream) -> Unit

    ParseError

    pub enum ParseError {
    NotTlsHandshake
    IncompleteRecord(expected~ : Int, available~ : Int)
    BadHandshakeType(Int)
    BadLength(field~ : String, offset~ : Int)
    UnsupportedRecordVersion(UInt16)
    InvalidAlertLength
    } derive(Eq,
    Debug
    )

    ServerHello

    pub struct ServerHello {
    version : UInt16
    random : Bytes
    session_id : Bytes
    cipher_suite : UInt16
    compression_method : Byte
    extensions : Array[UInt16]
    selected_alpn : String?
    raw_length : Int
    } derive(
    Debug
    )

    ServerHello::ja4s_a

    fn ServerHello::ja4s_a(self : ServerHello) -> String

    ServerHello::to_json

    fn ServerHello::to_json(self : ServerHello) -> String

    StreamResult

    pub enum StreamResult {
    NeedMore
    Parsed(ClientHello)
    Rejected(ParseError)
    } derive(
    Debug
    )

    TlsAlert

    pub struct TlsAlert {
    level : Byte
    description : Byte
    } derive(Eq,
    Debug
    )

    TlsAlert::description_string

    fn TlsAlert::description_string(self : TlsAlert) -> String

    TlsAlert::level_string

    fn TlsAlert::level_string(self : TlsAlert) -> String

    TlsAlert::to_json

    fn TlsAlert::to_json(self : TlsAlert) -> String

    TlsCertificate

    pub struct TlsCertificate {
    cert_lengths : Array[Int]
    } derive(Eq,
    Debug
    )

    TlsCertificate::to_json

    fn TlsCertificate::to_json(self : TlsCertificate) -> String

    TlsRecord

    pub enum TlsRecord {
    ClientHelloRecord(ClientHello)
    ServerHelloRecord(ServerHello)
    AlertRecord(TlsAlert)
    CertificateRecord(TlsCertificate)
    GenericRecord(TlsRecordType, Bytes)
    } derive(
    Debug
    )

    TlsRecord::to_json

    fn TlsRecord::to_json(self : TlsRecord) -> String

    TlsRecordType

    pub enum TlsRecordType {
    ChangeCipherSpec
    Handshake
    ApplicationData
    Unknown(Byte)
    } derive(Eq,
    Debug
    )

    normalize_u16_list

    fn normalize_u16_list(values : ArrayView[UInt16]) -> String

    parse_alert

    fn parse_alert(data : BytesView) -> Result[TlsAlert, ParseError]

    parse_certificate

    fn parse_certificate(data : BytesView) -> Result[TlsCertificate, ParseError]

    parse_client_hello

    fn parse_client_hello(data : BytesView) -> Result[ClientHello, ParseError]

    parse_server_hello

    fn parse_server_hello(data : BytesView) -> Result[ServerHello, ParseError]

    parse_tls_record

    fn parse_tls_record(data : BytesView) -> Result[TlsRecord, ParseError]

    uint16_to_hex4

    fn uint16_to_hex4(value : UInt16) -> String