OkHttp SSL Pinning

1 获得证书公钥的哈希值

https://www.ssllabs.com/ssltest/index.html

2 例子

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.2</version>
</dependency>
// 小红书 www.xiaohongshu.com 证书公钥的哈希值
String PUBLIC_KEY_HASH = "7/7ZtGQbsxpnFWI8v2DmVSwEd7W6dmkHw4TjbuH1Ds0=";
OkHttpClient client = new OkHttpClient.Builder()
        .certificatePinner(
                new CertificatePinner.Builder()
                .add("www.xiaohongshu.com", "sha256/" + PUBLIC_KEY_HASH)
                .build()
        )
        .build();
// 请求小红书微信小程序详情页接口
Request request = new Request.Builder()
        .url("https://www.xiaohongshu.com/fe_api/burdock/weixin/v2/note/6139e3790000000021039b7a/single_feed")
        .addHeader("X-Sign", "X09f2f3e9da819070d0c34a92124d6274")
        .addHeader("Authorization", "wxmp.3beb70ee-b1a3-4e75-b740-5907192612d6")
        .addHeader("Host", " www.xiaohongshu.com")
        .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat")
        .build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
    throw new IOException("Unexpected code " + response);
}
// 打印证书公钥的哈希值(证书链上的)
for (Certificate certificate : response.handshake().peerCertificates()) {
    System.out.println(CertificatePinner.pin(certificate));
}
// 打印返回值
// {"code":0,"success":true,"data":{"ats":[],"likes":1,"collects":1,"shareCount":0,"comments":2,"title":"求助,变色LED灯坏了能修吗","desc":"就像视频这样子,总共三种颜色,现在有两种暖色灯有用,一种冷色灯只有微亮,还一直闪。怎么办。要换整个灯吗?","id":"6139e3790000000021039b7a","imageList":[{"url":"http://ci.xiaohongshu.com/e5348a40-c9ef-33d3-a873-32ce9373a190?imageView2/2/w/1080/format/jpg","width":720,"height":1280,"fileId":"e5348a40-c9ef-33d3-a873-32ce9373a190","traceId":"e5348a40-c9ef-33d3-a873-32ce9373a190"}],"cover":{"url":"http://ci.xiaohongshu.com/e5348a40-c9ef-33d3-a873-32ce9373a190?imageView2/2/w/1080/format/jpg","width":720,"height":1280,"fileId":"e5348a40-c9ef-33d3-a873-32ce9373a190","traceId":"e5348a40-c9ef-33d3-a873-32ce9373a190"},"isLiked":false,"time":"2021-09-09 18:35","type":"video","hashTags":[],"cooperateBinds":[],"isCollected":false,"video":{"id":"6139e3790000000021039b7a","url":"http://v.xiaohongshu.com/95907c516534bdfdcb72658cd9c0ff083d9739d8_r_ln?sign=63add5d6df2133068801f2197f39bed7&t=615c7680","width":720,"height":1280,"duration":49,"playedCount":0},"inCensor":false,"censorTip":"","user":{"bannerImage":"","fans":60,"follows":76,"gender":1,"id":"5658298db8c8b44cafd43a85","nickname":"双木林2012","notes":13,"boards":0,"location":"中国","image":"https://sns-avatar-qc.xhscdn.com/avatar/60edaa6904fcbc4286ae9966.jpg?imageView2/1/w/540/format/jpg","collected":43,"desc":"县城资料员小白","liked":85,"officialVerified":false,"redOfficialVerifyShowIcon":false,"level":{"image":"https://fe-static.xhscdn.com/formula-static/user-growth/public/4f_89d6a14b2f5f3f5c1ce3cac9fa6dab96.png","name":"困困薯"},"fstatus":"none","redOfficialVerifyIconType":0,"red_id":"509011835","officialVerifyIcon":"","officialVerifyName":"","isFollowed":false},"poi":{},"commentList":[{"content":"可以在网上买个灯盘很便宜安装也简单","user":{"id":"5af53b63e8ac2b55dbc4a72c","nickname":"12号下午"}}],"canShareMoment":false}}
System.out.println(new String(response.body().bytes()));

公钥hash值错误

// 7/7ZtGQbsxpnFWI8v2DmVSwEd6W6dmkHw4TjbuH1Ds0=

Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: Certificate pinning failure!
  Peer certificate chain:
    sha256/7/7ZtGQbsxpnFWI8v2DmVSwEd7W6dmkHw4TjbuH1Ds0=: CN=*.xiaohongshu.com, O=行吟信息科技(上海)有限公司, ST=上海市, C=CN
    sha256/TbrK7tI1CsyZLKNdMvoHsV863GbcuERLt4LWrjChCv0=: CN=DigiCert Secure Site CN CA G3, O=DigiCert Inc, C=US
    sha256/r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E=: CN=DigiCert Global Root CA, OU=www.digicert.com, O=DigiCert Inc, C=US
  Pinned certificates for www.xiaohongshu.com:
    sha256/7/7ZtGQbsxpnFWI8v2DmVSwEd6W6dmkHw4TjbuH1Ds0=
	at okhttp3.CertificatePinner.check$okhttp(CertificatePinner.kt:200)
	at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:410)
	at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
	at cn.baker.utils.OkHttpTest.main(OkHttpTest.java:37)

Process finished with exit code 1

https://github.com/skylot/jadx

  • https://www.jianshu.com/p/ad4c7ce94518/
  • https://www.jianshu.com/p/80282c1b3cff
  • https://www.jianshu.com/p/952254affbbf
OkHttp3 中,可以通过配置 OkHttpClient 实例的 SSLSocketFactory 和 TrustManager 来实现绕过 SSL 验证,具体如下: ```java // 创建一个信任所有证书的 TrustManager TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}; // 创建一个 SSLContext,并使用上面的 TrustManager 初始化 SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new SecureRandom()); // 创建一个 OkHttpClient 实例,并设置 SSLContext OkHttpClient client = new OkHttpClient.Builder() .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0]) .hostnameVerifier((hostname, session) -> true) .build(); // 发送请求 Request request = new Request.Builder() .url("https://example.com") .build(); Response response = client.newCall(request).execute(); ``` 在上面的代码中,我们创建了一个 TrustManager 实例,用于信任所有证书。然后使用这个 TrustManager 初始化一个 SSLContext,最后将这个 SSLContext 设置到 OkHttpClient 实例中。 需要注意的是,绕过 SSL 验证可能会带来安全风险,应该尽量避免在生产环境中使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值