python-tls-client

repository·master·Indexed 21 days ago

https://github.com/florianregaz/python-tls-client

An advanced HTTP library that extends requests by integrating tls-client to mimic specific browser and device TLS fingerprints. It allows developers to bypass TLS fingerprinting protections using preset client_identifiers for Chrome, Firefox, Opera, Safari, iOS, iPadOS, and Android, or by configuring custom parameters such as ja3_string, h2_settings, and supported_signature_algorithms.

Tokens
1.5K
Snippets
3
Records
4
Agent score
24%

What's inside python-tls-client

  1. Package tls-client with Pyinstaller or Pyarmor

    master

    When bundling the library into an executable using Pyinstaller or Pyarmor, you must manually include the binary dependencies. Use the --add-binary flag with the appropriate path for your platform:

    • Linux (Ubuntu / x86): --add-binary '{path_to_library}/tls_client/dependencies/tls-client-x86.so:tls_client/dependencies'
    • Linux (Alpine / AMD64): --add-binary '{path_to_library}/tls_client/dependencies/tls-client-amd64.so:tls_client/dependencies'
    • MacOS (M1 and older): --add-binary '{path_to_library}/tls_client/dependencies/tls-client-x86.dylib:tls_client/dependencies'
    • MacOS (M2): --add-binary '{path_to_library}/tls_client/dependencies/tls-client-arm64.dylib:tls_client/dependencies'
    • Windows: --add-binary '{path_to_library}/tls_client/dependencies/tls-client-64.dll;tls_client/dependencies'
  2. Configure a custom TLS fingerprint with tls_client.Session

    master

    For granular control over the TLS handshake, you can pass specific fingerprinting parameters to tls_client.Session. This allows you to manually define JA3 strings, HTTP/2 settings, supported signature algorithms, and more.

    Key parameters include:

    • ja3_string: A custom JA3 fingerprint string.
    • h2_settings: A dictionary of HTTP/2 settings (e.g., HEADER_TABLE_SIZE, MAX_CONCURRENT_STREAMS).
    • h2_settings_order: A list defining the order of HTTP/2 settings.
    • supported_signature_algorithms: A list of allowed signature algorithms.
    • supported_versions: A list of supported TLS versions (e.g., "1.3", "1.2", or "GREASE").
    • key_share_curves: A list of supported key share curves.
    • cert_compression_algo: The certificate compression algorithm (e.g., "brotli").
    • pseudo_header_order: The order of HTTP/2 pseudo-headers.
    • header_order: The order of HTTP headers.
    • connection_flow: A numeric value for connection flow control.
    import tls_client
    
    session = tls_client.Session(
        ja3_string="771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513,29-23-24,0",
        h2_settings={
            "HEADER_TABLE_SIZE": 65536,
            "MAX_CONCURRENT_STREAMS": 1000,
            "INITIAL_WINDOW_SIZE": 6291456,
            "MAX_HEADER_LIST_SIZE": 262144
        },
        h2_settings_order=[
            "HEADER_TABLE_SIZE",
            "MAX_CONCURRENT_STREAMS",
            "INITIAL_WINDOW_SIZE",
            "MAX_HEADER_LIST_SIZE"
        ],
        supported_signature_algorithms=[
            "ECDSAWithP256AndSHA256",
            "PSSWithSHA256",
            "PKCS1WithSHA256",
            "ECDSAWithP384AndSHA384",
            "PSSWithSHA384",
            "PKCS1WithSHA384",
            "PSSWithSHA512",
            "PKCS1WithSHA512",
        ],
        supported_versions=["GREASE", "1.3", "1.2"],
        key_share_curves=["GREASE", "X25519"],
        cert_compression_algo="brotli",
        pseudo_header_order=[
            ":method",
            ":authority",
            ":scheme",
            ":path"
        ],
        connection_flow=15663105,
        header_order=[
            "accept",
            "user-agent",
            "accept-encoding",
            "accept-language"
        ]
    )
    
    res = session.post(
        "https://www.example.com/",
        headers={
            "key1": "value1",
        },
        json={
            "key1": "key2"
        }
    )
  3. Use tls_client.Session with preset client_identifiers

    master

    The tls_client.Session class allows you to mimic specific browser or device TLS fingerprints using the client_identifier parameter. This is useful for bypassing TLS fingerprinting. The syntax is inspired by the requests library.

    Available identifiers include:

    • Chrome: chrome_103, chrome_104, chrome_105, chrome_106, chrome_107, chrome_108, chrome109, Chrome110, chrome111, chrome112, chrome_116_PSK, chrome_116_PSK_PQ, chrome_117, chrome_120
    • Firefox: firefox_102, firefox_104, firefox108, Firefox110, firefox_117, firefox_120
    • Opera: opera_89, opera_90
    • Safari: safari_15_3, safari_15_6_1, safari_16_0
    • iOS: safari_ios_15_5, safari_ios_15_6, safari_ios_16_0
    • iPadOS: safari_ios_15_6
    • Android: okhttp4_android_7, okhttp4_android_8, okhttp4_android_9, okhttp4_android_10, okhttp4_android_11

    Note: More identifiers can be found in the project's settings.py.

    import tls_client
    
    session = tls_client.Session(
        client_identifier="chrome112",
        random_tls_extension_order=True
    )
    
    res = session.get(
        "https://www.example.com/",
        headers={
            "key1": "value1",
        },
        proxy="http://user:password@host:port"
    )