RE env for inspecting APKs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.8 KiB

  1. /*
  2. Android SSL Re-pinning frida script v0.2 030417-pier
  3. $ adb push burpca-cert-der.crt /data/local/tmp/cert-der.crt
  4. $ frida -U -f it.app.mobile -l frida-android-repinning.js --no-pause
  5. https://techblog.mediaservice.net/2017/07/universal-android-ssl-pinning-bypass-with-frida/
  6. UPDATE 20191605: Fixed undeclared var. Thanks to @oleavr and @ehsanpc9999 !
  7. */
  8. setTimeout(function(){
  9. Java.perform(function (){
  10. console.log("");
  11. console.log("[.] Cert Pinning Bypass/Re-Pinning");
  12. var CertificateFactory = Java.use("java.security.cert.CertificateFactory");
  13. var FileInputStream = Java.use("java.io.FileInputStream");
  14. var BufferedInputStream = Java.use("java.io.BufferedInputStream");
  15. var X509Certificate = Java.use("java.security.cert.X509Certificate");
  16. var KeyStore = Java.use("java.security.KeyStore");
  17. var TrustManagerFactory = Java.use("javax.net.ssl.TrustManagerFactory");
  18. var SSLContext = Java.use("javax.net.ssl.SSLContext");
  19. // Load CAs from an InputStream
  20. console.log("[+] Loading our CA...")
  21. var cf = CertificateFactory.getInstance("X.509");
  22. try {
  23. var fileInputStream = FileInputStream.$new("/data/local/tmp/cert-der.crt");
  24. }
  25. catch(err) {
  26. console.log("[o] " + err);
  27. }
  28. var bufferedInputStream = BufferedInputStream.$new(fileInputStream);
  29. var ca = cf.generateCertificate(bufferedInputStream);
  30. bufferedInputStream.close();
  31. var certInfo = Java.cast(ca, X509Certificate);
  32. console.log("[o] Our CA Info: " + certInfo.getSubjectDN());
  33. // Create a KeyStore containing our trusted CAs
  34. console.log("[+] Creating a KeyStore for our CA...");
  35. var keyStoreType = KeyStore.getDefaultType();
  36. var keyStore = KeyStore.getInstance(keyStoreType);
  37. keyStore.load(null, null);
  38. keyStore.setCertificateEntry("ca", ca);
  39. // Create a TrustManager that trusts the CAs in our KeyStore
  40. console.log("[+] Creating a TrustManager that trusts the CA in our KeyStore...");
  41. var tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  42. var tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
  43. tmf.init(keyStore);
  44. console.log("[+] Our TrustManager is ready...");
  45. console.log("[+] Hijacking SSLContext methods now...")
  46. console.log("[-] Waiting for the app to invoke SSLContext.init()...")
  47. SSLContext.init.overload("[Ljavax.net.ssl.KeyManager;", "[Ljavax.net.ssl.TrustManager;", "java.security.SecureRandom").implementation = function(a,b,c) {
  48. console.log("[o] App invoked javax.net.ssl.SSLContext.init...");
  49. SSLContext.init.overload("[Ljavax.net.ssl.KeyManager;", "[Ljavax.net.ssl.TrustManager;", "java.security.SecureRandom").call(this, a, tmf.getTrustManagers(), c);
  50. console.log("[+] SSLContext initialized with our custom TrustManager!");
  51. }
  52. });
  53. },0);