GoSecure / frida-xamarin-unpin

A Frida script to bypass Xamarin certificate pinning implementations
https://www.gosecure.net/blog/2020/04/06/bypassing-xamarin-certificate-pinning-on-android/
66 stars 27 forks source link

Xamarin Certificate Pinning Bypass

This is a small Frida script to bypass Mono-based certificate pinning.

The repository includes a sample application with sources and a prebuilt APK for quick testing.

The fully functional script is also available in dist/ for quick usage

NOTE: Frida requires a rooted Android device.

Using Pre-built Frida Script

Simply run frida -U -l dist/xamarin-unpin.js com.your.app after your application has launched.

Technical Details

On recent versions (Xamarin >= 10.x, Mono >= 6.0) of the Mono runtime, the script works by creating a default HttpClientHandler and hooking the HttpClient base class' SendAsync method, which is the underlying method for all HTTP requests. When the method is called, the HttpClientHandler is checked and if it isn't the script-created handler, it replaces it by the default handler before proceeding with the SendAsync logic.

Using this method, it is possible to hook any HTTP request as long as it is performed with the Xamarin System.Net.Http stack. Furthermore, it can be performed at any point during the program's lifecycle and does not require heap scanning.

On older versions (Xamarin < 10.x, Mono < 6.0) of the Mono runtime, it is even simpler because the certificate validation callback is a static property of the class System.Net.ServicePointManager. This property's get and set methods are hooked to always return null and always set null, respectively. Additionally, the setter is called with null explicitly by the script to remove any handler that may already be present.

In both cases, the hooking process works by first forcing a JIT of the target method by the mono runtime using mono_compile_method() followed by a hooking of the native method code.

The script has not yet been tested with tiered compilation, AOT compilation or full AOT compilation for iOS. We are interested in any feedback or sample applications to help us implement and debug these particular scenarios.

Limitations

Developing

If you want to build the APK or Frida script to play around, follow the instructions below.

Building and Deploying the APK

Building and Running the Frida Script

To test the behavior, navigate in the application to the About page and hit the "Make HTTP Request" Button with/without the script.

Contributing

If you run into issues while trying to bypass pinning in real applications, feel free to open an issue with the Frida output and as much detail as possible regarding the application you tried to instrument.

Pull requests, improvements, bug fixes and additional features are more than welcome!