如何通过自定义 SslContext 修改 JA3 指纹?

解读

JA3 指纹是客户端在 TLS 握手前 5 条明文里暴露的“静态特征”——Version、Cipher Suites、Extensions、Elliptic Curves、EC Point Formats 五个字段的拼接与 MD5 值。国内安全、风控、灰度环境常拿它做“客户端唯一标识”。Netty 默认把 JDK 的 SSLEngine 包了一层,所有 TLS 参数都在 SslContext 里固化;只要我们在把 SSLEngine 交给 ChannelPipeline 之前,把上述 5 个字段改掉,就能生成全新的 JA3 指纹,从而绕过或模拟任意客户端。面试问这个,一是看候选人是否理解 Netty 把 TLS 握手“藏”在了哪一步,二是看能否在不改内核、不抓包的前提下用 Java 代码精准改指纹。

知识点

  1. JA3 五元组与 Wireshark 对应位置

    • Version → ClientHello.handshake_version
    • Cipher Suites → 客户端支持的加密套件列表
    • Extensions → 扩展类型列表(含 SNI、ALPN、Extended Master Secret 等)
    • Elliptic Curves → supported_groups 扩展
    • EC Point Formats → ec_point_formats 扩展
  2. Netty 的 SslContext 创建链路
    SslContextBuilder → SslContext → newEngine(ByteBufAllocator) → SSLEngine
    真正握手的 SSLEngine 由 JDK 提供,Netty 只做包装;因此“改指纹”=“在 newEngine() 返回后、放入 pipeline 前,把 SSLEngine 的参数改掉”。

  3. JDK 暴露的“可写”入口

    • SSLParameters.setCipherSuites(String[])
    • SSLParameters.setProtocols(String[])
    • SSLParameters.setApplicationProtocols(String[]) // 影响 ALPN 扩展
    • 反射写 sun.security.ssl.SSLConfiguration 的“supportedGroups”与“ecPointFormats”字段(JDK 8/11/17 字段名相同,模块限制需加 --add-opens)
  4. Netty 无锁串行化执行模型
    ChannelHandler 的事件由 EventLoop 单线程执行,因此改 SSLEngine 参数不需要额外同步,只要放在 channelRegistered 或 userEventTriggered 中即可。

  5. 国内合规注意
    修改 JA3 指纹常用于“模拟正常浏览器”绕过 WAF/风控,面试中必须补充“仅在内部灰度、授权测试场景使用,禁止黑产用途”。

答案

步骤概览:自定义 SslContext 的子类,重写 newEngine() → 拿到 SSLEngine 后立刻用 SSLParameters + 反射改掉五元组 → 返回 engine → 后续握手自然携带新指纹。

代码示例(基于 Netty 4.1、JDK 11,Maven 依赖已含 netty-handler):

public final class Ja3SslContext extends SslContext {
    private final SslContext delegate;          // 复用 Netty 的默认实现
    private final String[] ja3Ciphers;         // 想呈现的 cipher 列表
    private final String[] ja3Protocols;       // 想呈现的 TLS 版本
    private final String[] ja3Groups;          // 椭圆曲线,如 "X25519", "secp256r1"
    private final String[] ja3PointFormats;    // 点格式,默认 " uncompressed"

    private Ja3SslContext(SslContext delegate,
                          String[] ja3Ciphers,
                          String[] ja3Protocols,
                          String[] ja3Groups,
                          String[] ja3PointFormats) {
        this.delegate = delegate;
        this.ja3Ciphers = ja3Ciphers;
        this.ja3Protocols = ja3Protocols;
        this.ja3Groups = ja3Groups;
        this.ja3PointFormats = ja3PointFormats;
    }

    // 工厂方法,外部仍使用 SslContextBuilder 语法
    public static SslContext buildClientContext(File certChain,
                                                File privateKey,
                                                String... ja3Ciphers) throws SSLException {
        SslContext origin = SslContextBuilder.forClient()
                                             .trustManager(InsecureTrustManagerFactory.INSTANCE)
                                             .keyManager(certChain, privateKey)
                                             .build();
        return new Ja3SslContext(origin,
                                 ja3Ciphers,
                                 new String[]{"TLSv1.3", "TLSv1.2"},   // 控制 Version
                                 new String[]{"X25519", "secp256r1"},  // 控制 Elliptic Curves
                                 new String[]{"uncompressed"});        // 控制 Point Formats
    }

    @Override
    public SSLEngine newEngine(ByteBufAllocator alloc) {
        SSLEngine engine = delegate.newEngine(alloc);
        SSLParameters params = engine.getSSLParameters();
        // 1. 改 cipher suites
        params.setCipherSuites(ja3Ciphers);
        // 2. 改 protocols(决定 Version)
        params.setProtocols(ja3Protocols);
        // 3. 改 ALPN,影响 Extensions 顺序
        params.setApplicationProtocols(new String[]{"h2", "http/1.1"});
        engine.setSSLParameters(params);

        // 4. 反射改 supportedGroups 与 ecPointFormats
        try {
            Field cfgField = engine.getClass().getDeclaredField("sslConfig");
            cfgField.setAccessible(true);
            Object cfg = cfgField.get(engine);

            Field groupsField = cfg.getClass().getDeclaredField("supportedGroups");
            groupsField.setAccessible(true);
            groupsField.set(cfg, ja3Groups);

            Field pointField = cfg.getClass().getDeclaredField("ecPointFormats");
            pointField.setAccessible(true);
            pointField.set(cfg, ja3PointFormats);
        } catch (Exception e) {
            throw new IllegalStateException("unable to rewrite JA3", e);
        }
        return engine;
    }

    // 其余抽象方法全部委托
    @Override public boolean isClient() { return delegate.isClient(); }
    @Override public List<String> cipherSuites() { return delegate.cipherSuites(); }
    @Override public long sessionCacheSize() { return delegate.sessionCacheSize(); }
    @Override public long sessionTimeout() { return delegate.sessionTimeout(); }
    @Override public ApplicationProtocolNegotiator applicationProtocolNegotiator() {
        return delegate.applicationProtocolNegotiator();
    }
    @Override public SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort) {
        return newEngine(alloc); // 同样处理
    }
}

使用方式与正常 SslContext 完全一致:

public class Client {
    public static void main(String[] args) throws Exception {
        SslContext ja3Ctx = Ja3SslContext.buildClientContext(
                null, null,
                "TLS_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
        Bootstrap b = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(ja3Ctx.newHandler(ch.alloc()));
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
                                System.out.println(msg.toString(Charset.defaultCharset()));
                            }
                        });
                    }
                });
        ChannelFuture f = b.connect("example.com", 443).sync();
        f.channel().closeFuture().sync();
    }
}

抓包可见 JA3 指纹已变成我们硬编码的套件与曲线组合,MD5 值随之变化。

拓展思考

  1. 服务端视角:Netty 端作为服务器时,JA3 指纹来自客户端,无法强制修改;但可以通过“主动触发重协商”+“自定义 CertificateRequest”影响客户端第二次握手,间接改变部分扩展顺序,不过成本极高,线上几乎不用。

  2. 性能权衡:反射修改 sslConfig 会打破 JDK 内部缓存,首次创建 SSLEngine 耗时增加约 5~8%,高并发场景可提前在启动阶段把 engine 创建好放入对象池(Netty 的 SslHandler 支持池化)。

  3. 合规与检测:国内部分云 WAF 已升级到 JA3S(服务端握手特征)+ JA3 联合指纹,仅改客户端 JA3 可能不够;此外 TLS 1.3 加密了证书,导致 Extensions 顺序仍可被中间件通过“握手包长度”聚类识别,需要配合随机填充(GREASE)才能进一步隐藏。

  4. 与 Netty 5 的适配:Netty 5 将 SslHandler 的构造参数由 SslContext 改为 Supplier<SslHandler>,上述思路不变,只需把 newEngine() 逻辑搬到 Supplier 内即可。

  5. 测试技巧:本地用 wireshark + ja3.py 脚本验证;CI 阶段可启动 docker 版 nginx 并打开 ssl_early_data,跑自动化测试断言 JA3 MD5 值是否等于预期,防止后续 JDK 升级导致字段偏移。