1. function test(){
    2. Java.perform(function () {
    3. function showStacks() {console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Throwable").$new()));}
    4. var ByteString = Java.use("com.android.okhttp.okio.ByteString");
    5. function toBase64(tag, data) {
    6. console.log(tag + " Base64: ", ByteString.of(data).base64());
    7. }
    8. function toHex(tag, data) {
    9. console.log(tag + " Hex: ", ByteString.of(data).hex());
    10. }
    11. function toUtf8(tag, data) {
    12. console.log(tag + " Utf8: ", ByteString.of(data).utf8());
    13. }
    14. var messageDigest = Java.use("java.security.MessageDigest");
    15. messageDigest.update.overload('byte').implementation = function (data) {
    16. console.log("MessageDigest.update('byte') is called!");
    17. return this.update(data);
    18. }
    19. messageDigest.update.overload('java.nio.ByteBuffer').implementation = function (data) {
    20. console.log("MessageDigest.update('java.nio.ByteBuffer') is called!");
    21. return this.update(data);
    22. }
    23. messageDigest.update.overload('[B').implementation = function (data) {
    24. console.log("MessageDigest.update('[B') is called!");
    25. var algorithm = this.getAlgorithm();
    26. var tag = algorithm + " update data";
    27. toUtf8(tag, data);
    28. toHex(tag, data);
    29. toBase64(tag, data);
    30. console.log("=======================================================");
    31. return this.update(data);
    32. }
    33. messageDigest.update.overload('[B', 'int', 'int').implementation = function (data, start, length) {
    34. console.log("MessageDigest.update('[B', 'int', 'int') is called!");
    35. var algorithm = this.getAlgorithm();
    36. var tag = algorithm + " update data";
    37. toUtf8(tag, data);
    38. toHex(tag, data);
    39. toBase64(tag, data);
    40. console.log("=======================================================", start, length);
    41. return this.update(data, start, length);
    42. }
    43. messageDigest.digest.overload().implementation = function () {
    44. console.log("MessageDigest.digest() is called!");
    45. var result = this.digest();
    46. var algorithm = this.getAlgorithm();
    47. var tag = algorithm + " digest result";
    48. toHex(tag, result);
    49. toBase64(tag, result);
    50. console.log("=======================================================");
    51. return result;
    52. }
    53. messageDigest.digest.overload('[B').implementation = function (data) {
    54. console.log("MessageDigest.digest('[B') is called!");
    55. var algorithm = this.getAlgorithm();
    56. var tag = algorithm + " digest data";
    57. toUtf8(tag, data);
    58. toHex(tag, data);
    59. toBase64(tag, data);
    60. var result = this.digest(data);
    61. var tags = algorithm + " digest result";
    62. toHex(tags, result);
    63. toBase64(tags, result);
    64. console.log("=======================================================");
    65. return result;
    66. }
    67. messageDigest.digest.overload('[B', 'int', 'int').implementation = function (data, start, length) {
    68. console.log("MessageDigest.digest('[B', 'int', 'int') is called!");
    69. var algorithm = this.getAlgorithm();
    70. var tag = algorithm + " digest data";
    71. toUtf8(tag, data);
    72. toHex(tag, data);
    73. toBase64(tag, data);
    74. var result = this.digest(data, start, length);
    75. var tags = algorithm + " digest result";
    76. toHex(tags, result);
    77. toBase64(tags, result);
    78. console.log("=======================================================", start, length);
    79. return result;
    80. }
    81. var mac = Java.use("javax.crypto.Mac");
    82. mac.init.overload('java.security.Key', 'java.security.spec.AlgorithmParameterSpec').implementation = function (key, AlgorithmParameterSpec) {
    83. console.log("Mac.init('java.security.Key', 'java.security.spec.AlgorithmParameterSpec') is called!");
    84. return this.init(key, AlgorithmParameterSpec);
    85. }
    86. mac.init.overload('java.security.Key').implementation = function (key) {
    87. console.log("Mac.init('java.security.Key') is called!");
    88. var algorithm = this.getAlgorithm();
    89. var tag = algorithm + " init Key";
    90. var keyBytes = key.getEncoded();
    91. toUtf8(tag, keyBytes);
    92. toHex(tag, keyBytes);
    93. toBase64(tag, keyBytes);
    94. console.log("=======================================================");
    95. return this.init(key);
    96. }
    97. mac.update.overload('byte').implementation = function (data) {
    98. console.log("Mac.update('byte') is called!");
    99. return this.update(data);
    100. }
    101. mac.update.overload('java.nio.ByteBuffer').implementation = function (data) {
    102. console.log("Mac.update('java.nio.ByteBuffer') is called!");
    103. return this.update(data);
    104. }
    105. mac.update.overload('[B').implementation = function (data) {
    106. console.log("Mac.update('[B') is called!");
    107. var algorithm = this.getAlgorithm();
    108. var tag = algorithm + " update data";
    109. toUtf8(tag, data);
    110. toHex(tag, data);
    111. toBase64(tag, data);
    112. console.log("=======================================================");
    113. return this.update(data);
    114. }
    115. mac.update.overload('[B', 'int', 'int').implementation = function (data, start, length) {
    116. console.log("Mac.update('[B', 'int', 'int') is called!");
    117. var algorithm = this.getAlgorithm();
    118. var tag = algorithm + " update data";
    119. toUtf8(tag, data);
    120. toHex(tag, data);
    121. toBase64(tag, data);
    122. console.log("=======================================================", start, length);
    123. return this.update(data, start, length);
    124. }
    125. mac.doFinal.overload().implementation = function () {
    126. console.log("Mac.doFinal() is called!");
    127. var result = this.doFinal();
    128. var algorithm = this.getAlgorithm();
    129. var tag = algorithm + " doFinal result";
    130. toHex(tag, result);
    131. toBase64(tag, result);
    132. console.log("=======================================================");
    133. return result;
    134. }
    135. var cipher = Java.use("javax.crypto.Cipher");
    136. cipher.init.overload('int', 'java.security.cert.Certificate').implementation = function () {
    137. console.log("Cipher.init('int', 'java.security.cert.Certificate') is called!");
    138. return this.init.apply(this, arguments);
    139. }
    140. cipher.init.overload('int', 'java.security.Key', 'java.security.SecureRandom').implementation = function () {
    141. console.log("Cipher.init('int', 'java.security.Key', 'java.security.SecureRandom') is called!");
    142. return this.init.apply(this, arguments);
    143. }
    144. cipher.init.overload('int', 'java.security.cert.Certificate', 'java.security.SecureRandom').implementation = function () {
    145. console.log("Cipher.init('int', 'java.security.cert.Certificate', 'java.security.SecureRandom') is called!");
    146. return this.init.apply(this, arguments);
    147. }
    148. cipher.init.overload('int', 'java.security.Key', 'java.security.AlgorithmParameters', 'java.security.SecureRandom').implementation = function () {
    149. console.log("Cipher.init('int', 'java.security.Key', 'java.security.AlgorithmParameters', 'java.security.SecureRandom') is called!");
    150. return this.init.apply(this, arguments);
    151. }
    152. cipher.init.overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec', 'java.security.SecureRandom').implementation = function () {
    153. console.log("Cipher.init('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec', 'java.security.SecureRandom') is called!");
    154. return this.init.apply(this, arguments);
    155. }
    156. cipher.init.overload('int', 'java.security.Key', 'java.security.AlgorithmParameters').implementation = function () {
    157. console.log("Cipher.init('int', 'java.security.Key', 'java.security.AlgorithmParameters') is called!");
    158. return this.init.apply(this, arguments);
    159. }
    160. cipher.init.overload('int', 'java.security.Key').implementation = function () {
    161. console.log("Cipher.init('int', 'java.security.Key') is called!");
    162. var algorithm = this.getAlgorithm();
    163. var tag = algorithm + " init Key";
    164. var className = JSON.stringify(arguments[1]);
    165. if(className.indexOf("OpenSSLRSAPrivateKey") === -1){
    166. var keyBytes = arguments[1].getEncoded();
    167. toUtf8(tag, keyBytes);
    168. toHex(tag, keyBytes);
    169. toBase64(tag, keyBytes);
    170. }
    171. console.log("=======================================================");
    172. return this.init.apply(this, arguments);
    173. }
    174. cipher.init.overload('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec').implementation = function () {
    175. console.log("Cipher.init('int', 'java.security.Key', 'java.security.spec.AlgorithmParameterSpec') is called!");
    176. var algorithm = this.getAlgorithm();
    177. var tag = algorithm + " init Key";
    178. var keyBytes = arguments[1].getEncoded();
    179. toUtf8(tag, keyBytes);
    180. toHex(tag, keyBytes);
    181. toBase64(tag, keyBytes);
    182. var tags = algorithm + " init iv";
    183. var iv = Java.cast(arguments[2], Java.use("javax.crypto.spec.IvParameterSpec"));
    184. var ivBytes = iv.getIV();
    185. toUtf8(tags, ivBytes);
    186. toHex(tags, ivBytes);
    187. toBase64(tags, ivBytes);
    188. console.log("=======================================================");
    189. return this.init.apply(this, arguments);
    190. }
    191. cipher.doFinal.overload('java.nio.ByteBuffer', 'java.nio.ByteBuffer').implementation = function () {
    192. console.log("Cipher.doFinal('java.nio.ByteBuffer', 'java.nio.ByteBuffer') is called!");
    193. return this.doFinal.apply(this, arguments);
    194. }
    195. cipher.doFinal.overload('[B', 'int').implementation = function () {
    196. console.log("Cipher.doFinal('[B', 'int') is called!");
    197. return this.doFinal.apply(this, arguments);
    198. }
    199. cipher.doFinal.overload('[B', 'int', 'int', '[B').implementation = function () {
    200. console.log("Cipher.doFinal('[B', 'int', 'int', '[B') is called!");
    201. return this.doFinal.apply(this, arguments);
    202. }
    203. cipher.doFinal.overload('[B', 'int', 'int', '[B', 'int').implementation = function () {
    204. console.log("Cipher.doFinal('[B', 'int', 'int', '[B', 'int') is called!");
    205. return this.doFinal.apply(this, arguments);
    206. }
    207. cipher.doFinal.overload().implementation = function () {
    208. console.log("Cipher.doFinal() is called!");
    209. return this.doFinal.apply(this, arguments);
    210. }
    211. cipher.doFinal.overload('[B').implementation = function () {
    212. console.log("Cipher.doFinal('[B') is called!");
    213. var algorithm = this.getAlgorithm();
    214. var tag = algorithm + " doFinal data";
    215. var data = arguments[0];
    216. toUtf8(tag, data);
    217. toHex(tag, data);
    218. toBase64(tag, data);
    219. var result = this.doFinal.apply(this, arguments);
    220. var tags = algorithm + " doFinal result";
    221. toHex(tags, result);
    222. toBase64(tags, result);
    223. console.log("=======================================================");
    224. return result;
    225. }
    226. cipher.doFinal.overload('[B', 'int', 'int').implementation = function () {
    227. console.log("Cipher.doFinal('[B', 'int', 'int') is called!");
    228. var algorithm = this.getAlgorithm();
    229. var tag = algorithm + " doFinal data";
    230. var data = arguments[0];
    231. toUtf8(tag, data);
    232. toHex(tag, data);
    233. toBase64(tag, data);
    234. var result = this.doFinal.apply(this, arguments);
    235. var tags = algorithm + " doFinal result";
    236. toHex(tags, result);
    237. toBase64(tags, result);
    238. console.log("=======================================================", arguments[1], arguments[2]);
    239. return result;
    240. }
    241. var signature = Java.use("java.security.Signature");
    242. signature.update.overload('byte').implementation = function (data) {
    243. console.log("Signature.update('byte') is called!");
    244. return this.update(data);
    245. }
    246. signature.update.overload('java.nio.ByteBuffer').implementation = function (data) {
    247. console.log("Signature.update('java.nio.ByteBuffer') is called!");
    248. return this.update(data);
    249. }
    250. signature.update.overload('[B', 'int', 'int').implementation = function (data, start, length) {
    251. console.log("Signature.update('[B', 'int', 'int') is called!");
    252. var algorithm = this.getAlgorithm();
    253. var tag = algorithm + " update data";
    254. toUtf8(tag, data);
    255. toHex(tag, data);
    256. toBase64(tag, data);
    257. console.log("=======================================================", start, length);
    258. return this.update(data, start, length);
    259. }
    260. signature.sign.overload('[B', 'int', 'int').implementation = function () {
    261. console.log("Signature.sign('[B', 'int', 'int') is called!");
    262. return this.sign.apply(this, arguments);
    263. }
    264. signature.sign.overload().implementation = function () {
    265. console.log("Signature.sign() is called!");
    266. var result = this.sign();
    267. var algorithm = this.getAlgorithm();
    268. var tag = algorithm + " sign result";
    269. toHex(tag, result);
    270. toBase64(tag, result);
    271. console.log("=======================================================");
    272. return result;
    273. }
    274. })};
    275. setImmediate(function(){
    276. setTimeout(test(), 5000);
    277. });

    上方为hook代码