如何使用Retrofit 2 + OkHttp 3加密/隐藏HTTPS呼叫的正文?

Sil*_*los 8 encryption obfuscation android proguard minify

我目前正在开发一个项目,我通过https调用将数据发送到我们的服务器api.项目的基本URL支持ssl(我们的url api端点以https:// api ....开头).我正在使用Retrofit 2和OkHttp3,我正在设置这样的客户端:

    public static void buildClient(){
        //Misc code here.... not showing for security reasons.
        OkHttpClient client = RetrofitClient.configureClient(new OkHttpClient());
        //I make calls here to update interceptors, timeouts, etc.
        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(new Gson()))
            .client(client)
            .build();
    }

    //Setup the ssl stuff here
    public static OkHttpClient configureClient(final OkHttpClient client) {
        final TrustManager[] certs = new TrustManager[]{new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            @Override
            public void checkServerTrusted(final X509Certificate[] chain,
                                           final String authType)
                    throws CertificateException {
            }

            @Override
            public void checkClientTrusted(final X509Certificate[] chain,
                                           final String authType)
                    throws CertificateException {
            }
        }};

        SSLContext ssl = null;
        try {
            ssl = SSLContext.getInstance("TLS");
            ssl.init(null, certs, new SecureRandom());
        } catch (final java.security.GeneralSecurityException ex) {
        }

        try {
            final HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(final String hostname,
                                      final SSLSession session) {
                    return true;
                }
            };
            client.setHostnameVerifier(hostnameVerifier);
            client.setSslSocketFactory(ssl.getSocketFactory());
        } catch (final Exception e) {
        }

        return client;
    }
Run Code Online (Sandbox Code Playgroud)

所以在此之后,我们都准备好了.

现在,这就是我所知道的:

1)我通过HTTPS发送,因为如果我不是,服务器会抛出错误,但事实并非如此.

2)我的代码工作得很好,因为它正在与服务器通信,应用程序将工作.

这里的问题是实际的Body数据没有被加密.这是2个照片示例,以显示我的意思.

1) 在此输入图像描述

2) 在此输入图像描述

第一个图像显示实际正文数据的正确混淆,因为数据正在转换为加密的"东西",而第二个图像显示纯文本.第二个是我用一个对象向服务器发送POST调用.

我的问题是,我如何复制这个,以便我的身体文本像其他人一样隐藏/加密?

笔记:

1)我正在使用Proguard进行混淆

2)我将minifyEnabled设置为true

3)我是如何通过数据包嗅探器发现的

任何人有任何想法如何实现这一目标?或者,任何人都可以指出我的具体方向是什么?

谢谢.

编辑:

所以,看起来我不是在理解这里的关键组件.

简短的回答是,呼叫已加密并正在发送Https.

答案很长,我一直在将我的数据调用与以下数据进行比较:

1) 在此输入图像描述

2) 在此输入图像描述

我只是假设这些是加密的,而我的则没有.事实证明,我发送的呼叫加密得很好,就像这些一样,但是这些数据是压缩/压缩的,这让人无法理解,这让我觉得这是加密数据的样子.包嗅探器.

dra*_*eet 10

您的问题是:为什么我使用HTTPS但Packet CaptureCharles可以查看客户端和Internet之间的所有SSL/HTTPS流量?

因为数据包捕获(VPN代理)或查尔斯欺骗您的客户作为中间人:

您的客户端<-->Packet Capture/Charles <-->您的目标服务器.

因此代理工具可以查看您的所有HTTPS内容(实际上它们确实是加密的).

方案:

你可以参考OkHttp wiki:https://github.com/square/okhttp/wiki/HTTPS

并为您的HTTPS检查设置证书固定.例如:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) findViewById(R.id.text);

        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
            .tlsVersions(TlsVersion.TLS_1_2)
            .cipherSuites(
                CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
            .build();

        OkHttpClient client = new OkHttpClient.Builder()
            .connectionSpecs(Collections.singletonList(spec))
            .certificatePinner(new CertificatePinner.Builder()
                .add("drakeet.me", "sha256/gGOcYKAwzEaUfun6YdxZvFSQq/x2lF/R8UizDFofveY=")
                .build())
            .build();

        Request request = new Request.Builder()
            .url("https://drakeet.me?s=type")
            .post(RequestBody.create(MediaType.parse("text"), "xxx...xxx"))
            .addHeader("token", "xxx")
            .build();

        final Handler handler = new Handler();
        client.newCall(request).enqueue(new Callback() {
            @Override public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }


            @Override public void onResponse(Call call, final Response response)
                throws IOException {
                final String t = response.body().string();
                handler.post(new Runnable() {
                    @Override public void run() {
                        textView.setText(t);
                    }
                });
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

正如我上面的代码,我预设了一个certificatePinner与我的目标服务器的真实certificatePinner的关联,所以如果我现在使用Packet Capture/Charles,他们将通过他们创建一个错误的certificatePinner,OkHttp将比较两个固定,如果不相等,抛出一个javax.net.ssl.SSLPeerUnverifiedException: Certificate pinning failure!

如果您关闭Packet Capture/Charles,则异常将成功解除并发送HTTPS内容.