pythonencodingconstruct

PascalString() missing 1 required positional argument: 'encoding'


PascalString() missing 1 required positional argument: 'encoding'

I am trying to run constructs.py but it just shows an error and I don't know how to fix it

This isn't my script

constructs.py

from construct import (
    Array,
    Bytes,
    Struct,
    VarInt,
    Int8ub,
    Int16ub,
    Int24ub,
    Int32ub,
    PascalString,
    Prefixed,
    GreedyRange,
    Switch,
    Optional,
)

ProtocolVersion = "version" / Struct(
    "major" / Int8ub,
    "minor" / Int8ub,
)

TLSPlaintext = "TLSPlaintext" / Struct(
    "type" / Int8ub,
    ProtocolVersion,
    "length" / Int16ub,
    "fragment" / Bytes(lambda ctx: ctx.length),
)

TLSCompressed = "TLSCompressed" / Struct(
    "type" / Int8ub,
    ProtocolVersion,
    "length" / Int16ub,
    "fragment" / Bytes(lambda ctx: ctx.length),
)

TLSCiphertext = "TLSCiphertext" / Struct(
    "type" / Int8ub,
    ProtocolVersion,
    "length" / Int16ub,
    "fragment" / Bytes(lambda ctx: ctx.length),
)

Random = "random" / Struct(
    "gmt_unix_time" / Int32ub,
    "random_bytes" / Bytes(28),
)

SessionID = "session_id" / Struct(
    "length" / Int8ub,
    "session_id" / Bytes(lambda ctx: ctx.length),
)

CipherSuites = "cipher_suites" / Struct(
    "length" / Int16ub,
    Array(lambda ctx: ctx.length // 2, "cipher_suites" / Int16ub),
)

CompressionMethods = "compression_methods" / Struct(
    "length" / Int8ub,
    Array(lambda ctx: ctx.length, "compression_methods" / Int8ub),
)

ServerName = Struct(
    "type" / Int8ub,
    "name" / PascalString("length" / Int16ub),
)

SNIExtension = Prefixed(
    Int16ub,
    Struct(
        Int16ub,
        "server_names" / GreedyRange(
            "server_name" / Struct(
                "name_type" / Int8ub,
                "host_name" / PascalString("length" / Int16ub),
            )
        )
    )
)

ALPNExtension = Prefixed(
    Int16ub,
    Struct(
        Int16ub,
        "alpn_protocols" / GreedyRange(
            "name" / PascalString(Int8ub),
        ),
    )
)

UnknownExtension = Struct(
    "bytes" / PascalString("length" / Int16ub)
)

Extension = "Extension" / Struct(
    "type" / Int16ub,
    Switch(
        lambda ctx: ctx.type,
        {
            0x00: SNIExtension,
            0x10: ALPNExtension,
        },
        default=UnknownExtension
    )
)

extensions = "extensions" / Struct(
    Int16ub,
    "extensions" / GreedyRange(Extension)
)

ClientHello = "ClientHello" / Struct(
    ProtocolVersion,
    Random,
    SessionID,
    CipherSuites,
    CompressionMethods,
    extensions,
)

ServerHello = "ServerHello" / Struct(
    ProtocolVersion,
    Random,
    SessionID,
    "cipher_suite" / Bytes(2),
    "compression_method" / Int8ub,
    extensions,
)

ClientCertificateType = "certificate_types" / Struct(
    "length" / Int8ub,
    Array(lambda ctx: ctx.length, "certificate_types" / Int8ub),
)

SignatureAndHashAlgorithm = "algorithms" / Struct(
    "hash" / Int8ub,
    "signature" / Int8ub,
)

SupportedSignatureAlgorithms = "supported_signature_algorithms" / Struct(
    "supported_signature_algorithms_length" / Int16ub,
    # TODO: Reject packets of length 0
    Array(
        lambda ctx: ctx.supported_signature_algorithms_length / 2,
        SignatureAndHashAlgorithm,
    ),
)

DistinguishedName = "certificate_authorities" / Struct(
    "length" / Int16ub,
    "certificate_authorities" / Bytes(lambda ctx: ctx.length),
)

CertificateRequest = "CertificateRequest" / Struct(
    ClientCertificateType,
    SupportedSignatureAlgorithms,
    DistinguishedName,
)

ServerDHParams = "ServerDHParams" / Struct(
    "dh_p_length" / Int16ub,
    "dh_p" / Bytes(lambda ctx: ctx.dh_p_length),
    "dh_g_length" / Int16ub,
    "dh_g" / Bytes(lambda ctx: ctx.dh_g_length),
    "dh_Ys_length" / Int16ub,
    "dh_Ys" / Bytes(lambda ctx: ctx.dh_Ys_length),
)

PreMasterSecret = "pre_master_secret" / Struct(
    ProtocolVersion,
    "random_bytes" / Bytes(46),
)

ASN1Cert = "ASN1Cert" / Struct(
    "length" / Int32ub,
    "asn1_cert" / Bytes(lambda ctx: ctx.length),
)

Certificate = "Certificate" / Struct(
    # TODO: Reject packets with length > 2 ** 24 - 1
    "certificates_length" / Int32ub,
    "certificates_bytes" / Bytes(lambda ctx: ctx.certificates_length),
)

Handshake = "Handshake" / Struct(
    "msg_type" / Int8ub,
    "length" / Int24ub,
    "body" / Bytes(lambda ctx: ctx.length),
)

Alert = "Alert" / Struct(
    "level" / Int8ub,
    "description" / Int8ub,
)

error

File "/root/bdfproxy/mitmproxy/mitmproxy/contrib/tls/_constructs.py", line 71, in <module>
    "name" / PascalString("length" / Int16ub),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: PascalString() missing 1 required positional argument: 'encoding'

Solution

  • You can read about PascalString here.

    You just didn't specify the encoding in this parts of code:

    ServerName = Struct(
        "type" / Int8ub,
        "name" / PascalString("length" / Int16ub, "utf8"),
    )
    
    SNIExtension = Prefixed(
        Int16ub,
        Struct(
            Int16ub,
            "server_names" / GreedyRange(
                "server_name" / Struct(
                    "name_type" / Int8ub,
                    "host_name" / PascalString("length" / Int16ub, "utf8"),
                )
            )
        )
    )
    
    ALPNExtension = Prefixed(
        Int16ub,
        Struct(
            Int16ub,
            "alpn_protocols" / GreedyRange(
                "name" / PascalString(Int8ub, "utf8"),
            ),
        )
    )
    
    UnknownExtension = Struct(
        "bytes" / PascalString("length" / Int16ub, "utf8")
    )