Search code examples
frida

Frida, hook OKHttp Library does not work with examples


I'm trying to hook functions of the OKHttp library by using then following Frida script:


setTimeout(function(){
    Java.perform(function (){
        console.log("-- Hook OKHttp library --");

        try {
            var Builder = Java.use('okhttp3.CertificatePinner$Builder');
            var Pin = Java.use('okhttp3.CertificatePinner$Pin');
            var OkHttpClient = Java.use('okhttp3.OkHttpClient$Builder');
            console.log("OkHTTP classes found");


            Builder.add.overload.implementation = function(a, b) {
                console.log("TEST ADD");
            }

            Pin.matches.overload.implementation = function (a) {
                console.log("TEST matches")
                return true;
            }

            OkHttpClient.certificatePinner.overload.implementation = function (a) {
                console.log("TEST certificatePinner");
            }

            console.log("OkHTTP functions found");            
        } catch (err) {
            // If we dont have a ClassNotFoundException exception, raise the
            // problem encountered.
            console.log("OkHTTP 3.x classes/functions not Found");
        }

    });
},0);

And I am executing the following code in my Android application:


            CertificatePinner certificatePinner = new CertificatePinner.Builder()
                    .add(certificateDNWildcard, certificateHash)
                    .build();
            //Create http client with pinned certificate
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .certificatePinner(certificatePinner)
                    .build();

As you can see I am calling the function .add(certificateDNWildcard, certificateHash) which I am trying to hook. However, nothing is printed in the terminal when I execute this function. Furthermore, the output of my terminal is:

-- Hook OKHttp library --
OkHTTP classes found
OkHTTP functions found

Hence, it does find the classes and functions; however, the hook itself does not work effectively. Could someone help me?

I am using:

* Frida 12.8.11
* Android 10
* ARM64

Solution

  • You should either:

    // works if add method has a single implementation
    Builder.add.implementation = function(a, b) {
        console.log("TEST ADD");
    }
    

    or

    // always works
    Builder.add.overload('java.lang.String', 'java.lang.String').implementation = function(a, b) {
        console.log("TEST ADD");
    }