newhook.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Android SSL Re-pinning frida script v0.1 @masbog
  3. $ frida -U -f it.app.mobile -l frida-android-unpinning-ssl.js
  4. $ [Samsung GT-I9500::it.app.mobile]-> %resume
  5. or
  6. $ frida --codeshare masbog/frida-android-unpinning-ssl
  7. $ [Samsung GT-I9500::it.app.mobile]-> %resume
  8. TODO: bypass kony android application
  9. original source code from: https://github.com/sensepost/objection/blob/8974d37733d108762184bb41fe8d0a4f1fffb591/objection/hooks/android/pinning/disable.js
  10. */
  11. setTimeout(function() {
  12. Java.perform(function() {
  13. console.log("");
  14. console.log("[.] Android Cert Pinning Bypass");
  15. var CertificateFactory = Java.use("java.security.cert.CertificateFactory");
  16. var FileInputStream = Java.use("java.io.FileInputStream");
  17. var BufferedInputStream = Java.use("java.io.BufferedInputStream");
  18. var X509Certificate = Java.use("java.security.cert.X509Certificate");
  19. var KeyStore = Java.use("java.security.KeyStore");
  20. var TrustManagerFactory = Java.use("javax.net.ssl.TrustManagerFactory");
  21. var SSLContext = Java.use("javax.net.ssl.SSLContext");
  22. var X509TrustManager = Java.use('javax.net.ssl.X509TrustManager');
  23. //var is_android_n = 0;
  24. //--------
  25. console.log("[.] TrustManagerImpl Android 7+ detection...");
  26. // Android 7+ TrustManagerImpl
  27. // The work in the following NCC blogpost was a great help for this hook!
  28. // hattip @AdriVillaB :)
  29. // https://www.nccgroup.trust/uk/about-us/newsroom-and-events/blogs/2017/november/bypassing-androids-network-security-configuration/
  30. try {
  31. var TrustManagerImpl = Java.use('com.android.org.conscrypt.TrustManagerImpl');
  32. // https://github.com/google/conscrypt/blob/c88f9f55a523f128f0e4dace76a34724bfa1e88c/platform/src/main/java/org/conscrypt/TrustManagerImpl.java#L650
  33. TrustManagerImpl.verifyChain.implementation = function(untrustedChain, trustAnchorChain, host, clientAuth, ocspData, tlsSctData) {
  34. console.log("[+] (Android 7+) TrustManagerImpl verifyChain() called. Not throwing an exception.");
  35. // Skip all the logic and just return the chain again :P
  36. //is_android_n = 1;
  37. return untrustedChain;
  38. }
  39. PinningTrustManager.checkServerTrusted.implementation = function() {
  40. console.log("[+] Appcelerator checkServerTrusted() called. Not throwing an exception.");
  41. }
  42. } catch (err) {
  43. console.log("[-] TrustManagerImpl Not Found");
  44. }
  45. //if (is_android_n === 0) {
  46. //--------
  47. console.log("[.] TrustManager Android < 7 detection...");
  48. // Implement a new TrustManager
  49. // ref: https://gist.github.com/oleavr/3ca67a173ff7d207c6b8c3b0ca65a9d8
  50. var TrustManager = Java.registerClass({
  51. name: 'com.sensepost.test.TrustManager',
  52. implements: [X509TrustManager],
  53. methods: {
  54. checkClientTrusted: function(chain, authType) {},
  55. checkServerTrusted: function(chain, authType) {},
  56. getAcceptedIssuers: function() {
  57. return [];
  58. }
  59. }
  60. });
  61. // Prepare the TrustManagers array to pass to SSLContext.init()
  62. var TrustManagers = [TrustManager.$new()];
  63. // Get a handle on the init() on the SSLContext class
  64. var SSLContext_init = SSLContext.init.overload(
  65. '[Ljavax.net.ssl.KeyManager;', '[Ljavax.net.ssl.TrustManager;', 'java.security.SecureRandom');
  66. try {
  67. // Override the init method, specifying our new TrustManager
  68. SSLContext_init.implementation = function(keyManager, trustManager, secureRandom) {
  69. console.log("[+] Overriding SSLContext.init() with the custom TrustManager android < 7");
  70. SSLContext_init.call(this, keyManager, TrustManagers, secureRandom);
  71. };
  72. } catch (err) {
  73. console.log("[-] TrustManager Not Found");
  74. }
  75. //}
  76. //-------
  77. console.log("[.] OkHTTP 3.x detection...");
  78. // OkHTTP v3.x
  79. // Wrap the logic in a try/catch as not all applications will have
  80. // okhttp as part of the app.
  81. try {
  82. var CertificatePinner = Java.use('okhttp3.CertificatePinner');
  83. console.log("[+] OkHTTP 3.x Found");
  84. CertificatePinner.check.overload('java.lang.String', 'java.util.List').implementation = function() {
  85. console.log("[+] OkHTTP 3.x check() called. Not throwing an exception.");
  86. };
  87. } catch (err) {
  88. // If we dont have a ClassNotFoundException exception, raise the
  89. // problem encountered.
  90. console.log("[-] OkHTTP 3.x Not Found")
  91. }
  92. //--------
  93. console.log("[.] Appcelerator Titanium detection...");
  94. // Appcelerator Titanium PinningTrustManager
  95. // Wrap the logic in a try/catch as not all applications will have
  96. // appcelerator as part of the app.
  97. try {
  98. var PinningTrustManager = Java.use('appcelerator.https.PinningTrustManager');
  99. console.log("[+] Appcelerator Titanium Found");
  100. PinningTrustManager.checkServerTrusted.implementation = function() {
  101. console.log("[+] Appcelerator checkServerTrusted() called. Not throwing an exception.");
  102. }
  103. } catch (err) {
  104. // If we dont have a ClassNotFoundException exception, raise the
  105. // problem encountered.
  106. console.log("[-] Appcelerator Titanium Not Found");
  107. }
  108. });
  109. }, 0);