当前位置:   article > 正文

unidbg 入门(一)_module.callstaticjnimethodobject

module.callstaticjnimethodobject

1 学习内容


1.1:调用native方法

        1.1 .1使用方法:callStaticJniMethodObject

        1.1.2 先new出对象再调用方法callJniMethodObject

1.2:传不同的参数:

        1.2.1  调用无参函数

       1.2.2  int类型参数

       1. 2.3  btye数组类型参数

        1.2.4  多个String类型的参数

       

2 代码如下


2.1:so文件在附件中,下载

2.2:安卓代码

  1. package com.example.jnitest4.jni;
  2. import android.content.Context;
  3. public class JniManager {
  4. // Used to load the 'native-lib' library on application startup.
  5. static {
  6. System.loadLibrary("native-lib");
  7. }
  8. public native String str2str(String org,String append);
  9. /**
  10. * 获取uuid字符串
  11. * @param type
  12. * @return
  13. */
  14. public native String uuid(int type);
  15. /**
  16. * 加密
  17. *
  18. * @param msg 原始字符串
  19. * @param type 加密的方式
  20. * @return 加密后的数据
  21. */
  22. public native String encode(String msg, int type);
  23. /**
  24. * 解密
  25. *
  26. * @param msg 解密前的 字符串
  27. * @param type 解密的方式
  28. * @return 解密后的数据
  29. */
  30. public native String decode(String msg, int type);
  31. /**
  32. * A native method that is implemented by the 'native-lib' native library,
  33. * which is packaged with this application.
  34. */
  35. public native String stringFromJNI();
  36. /**
  37. * SHA1签名 --失败
  38. *
  39. * @param src
  40. * @return
  41. */
  42. public native String encodeBySHA1(byte[] src);
  43. /**
  44. * SHA224签名
  45. *
  46. * @param src
  47. * @return
  48. */
  49. public native String encodeBySHA224(byte[] src);
  50. /**
  51. * SHA384签名
  52. *
  53. * @param src
  54. * @return
  55. */
  56. public native String encodeBySHA256(byte[] src);
  57. /**
  58. * SHA256签名
  59. *
  60. * @param src
  61. * @return
  62. */
  63. public native String encodeBySHA384(byte[] src);
  64. /**
  65. * SHA512签名
  66. *
  67. * @param src
  68. * @return
  69. */
  70. public native String encodeBySHA512(byte[] src);
  71. /**
  72. * AES加密
  73. *
  74. * @param keys
  75. * @param src
  76. * @return
  77. */
  78. public native byte[] encodeByAES(byte[] keys, byte[] src);
  79. /**
  80. * AES解密
  81. *
  82. * @param keys
  83. * @param src
  84. * @return
  85. */
  86. public native byte[] decodeByAES(byte[] keys, byte[] src);
  87. /**
  88. * RSA公钥加密
  89. *
  90. * @param keys
  91. * @param src
  92. * @return
  93. */
  94. public native byte[] encodeByRSAPubKey(byte[] keys, byte[] src);
  95. /**
  96. * RSA私钥解密
  97. *
  98. * @param keys
  99. * @param src
  100. * @return
  101. */
  102. public native byte[] decodeByRSAPrivateKey(byte[] keys, byte[] src);
  103. /**
  104. * RSA私钥加密
  105. *
  106. * @param keys
  107. * @param src
  108. * @return
  109. */
  110. public native byte[] encodeByRSAPrivateKey(byte[] keys, byte[] src);
  111. /**
  112. * RSA公钥解密
  113. *
  114. * @param keys
  115. * @param src
  116. * @return
  117. */
  118. public native byte[] decodeByRSAPubKey(byte[] keys, byte[] src);
  119. /**
  120. * RSA私钥签名
  121. *
  122. * @param keys
  123. * @param src
  124. * @return
  125. */
  126. public native byte[] signByRSAPrivateKey(byte[] keys, byte[] src);
  127. /**
  128. * RSA公钥验证签名 (未测试)
  129. *
  130. * @param keys
  131. * @param src
  132. * @param sign
  133. * @return 1:验证成功
  134. */
  135. public native int verifyByRSAPubKey(byte[] keys, byte[] src, byte[] sign);
  136. /**
  137. * 异或加解密
  138. *
  139. * @param src
  140. * @return
  141. */
  142. public native byte[] xOr(byte[] src);
  143. /**
  144. * MD5编码
  145. *
  146. * @param src
  147. * @return 默认小写
  148. */
  149. public native String md5(byte[] src);
  150. /**
  151. * HmacSHA1签名
  152. *
  153. * @param context
  154. * @param src
  155. * @return
  156. */
  157. public native byte[] encodeByHmacSHA1(Context context, byte[] src);
  158. /**
  159. * 获取apk-sha1
  160. *
  161. * @param context
  162. * @return
  163. */
  164. public native String sha1OfApk(Context context);
  165. /**
  166. * 校验apk签名是否有效(未验证)
  167. *
  168. * @param context
  169. * @return
  170. */
  171. public native boolean verifySha1OfApk(Context context);
  172. /**
  173. * 字节测试用例
  174. * @param bytes
  175. * @return
  176. */
  177. public native byte[] byteTestFn(byte[] bytes) ;
  178. }

2.3:unidbg实现代码

  1. package com.jni4;
  2. import com.github.unidbg.AndroidEmulator;
  3. import com.github.unidbg.linux.android.AndroidEmulatorBuilder;
  4. import com.github.unidbg.linux.android.AndroidResolver;
  5. import com.github.unidbg.linux.android.dvm.DalvikModule;
  6. import com.github.unidbg.linux.android.dvm.DvmClass;
  7. import com.github.unidbg.linux.android.dvm.DvmObject;
  8. import com.github.unidbg.linux.android.dvm.VM;
  9. import com.github.unidbg.linux.android.dvm.jni.ProxyClassFactory;
  10. import com.github.unidbg.memory.Memory;
  11. import java.io.File;
  12. import java.io.IOException;
  13. public class JniManagerUtil {
  14. private final AndroidEmulator emulator;
  15. private final DvmClass jniManagerUtil;
  16. private final VM vm;
  17. public JniManagerUtil() {
  18. emulator = AndroidEmulatorBuilder.for64Bit()
  19. .setProcessName("com.example.jnitest4")
  20. .build();
  21. Memory memory = emulator.getMemory();
  22. memory.setLibraryResolver(new AndroidResolver(23));
  23. vm = emulator.createDalvikVM();
  24. vm.setDvmClassFactory(new ProxyClassFactory());
  25. vm.setVerbose(false);
  26. DalvikModule dm = vm.loadLibrary(new File("unidbg-android/src/test/resources/jnitest4/libnative-lib.so"), false);
  27. jniManagerUtil = vm.resolveClass("com/example/jnitest4/jni/JniManager");
  28. dm.callJNI_OnLoad(emulator);
  29. }
  30. public void destroy() throws IOException {
  31. emulator.close();
  32. }
  33. /**
  34. * 这里有个问题是使用callStaticJniMethodObject还是callJniMethodObject
  35. */
  36. public void stringFromJNI(){
  37. String methodStringFromJNI = "stringFromJNI()Ljava/lang/String;";
  38. DvmObject<?> strRc = jniManagerUtil.callStaticJniMethodObject(emulator, methodStringFromJNI);
  39. System.out.println("stringFromJNI返回值:"+strRc.getValue());
  40. }
  41. /**
  42. * 这个例子的重点是参数是int类型,对应的参数类型标识为I
  43. */
  44. public void UUIDTest(){
  45. String methodStringFromJNI = "uuid(I)Ljava/lang/String;";
  46. int paramInt = 15;
  47. DvmObject<?> strRc = jniManagerUtil.callStaticJniMethodObject(emulator, methodStringFromJNI,paramInt);
  48. System.out.println("UUIDTest返回值:"+strRc.getValue());
  49. }
  50. // public native String str2str(String org,String append);
  51. public void str2strTest(){
  52. String methodStringFromJNI = "str2str(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
  53. String paramString0 = "hello ";
  54. String paramString1 = "mr wang";
  55. DvmObject<?> strRc = jniManagerUtil.callStaticJniMethodObject(emulator, methodStringFromJNI,paramString0,paramString1);
  56. System.out.println("str2strTest返回值:"+strRc.getValue());
  57. }
  58. public void encode(){
  59. String methodStringFromJNI = "encode(Ljava/lang/String;I;)Ljava/lang/String;";
  60. String paramString0 = "hello ";
  61. int paramString1 = 20;
  62. DvmObject<?> strRc = jniManagerUtil.callStaticJniMethodObject(emulator, methodStringFromJNI,paramString0,paramString1);
  63. System.out.println("encode返回值:"+strRc.getValue());
  64. }
  65. /**
  66. * 学习重点:
  67. * 1:这里重点是参数是byte数组,学习byte数组如何传参。
  68. * 2:调用方法采用先new一个对象,然后再调用非静态方法来调用。
  69. */
  70. public void encodeBySHA1(){
  71. String methodStringFromJNI = "encodeBySHA1(B[;)Ljava/lang/String;";
  72. String paramString0 = "99999 ";
  73. // 方法1
  74. // DvmObject<?> strRc = jniManagerUtil.callStaticJniMethodObject(emulator, methodStringFromJNI,paramString0.getBytes());
  75. // 方法2:
  76. DvmObject<?> strRc = jniManagerUtil.newObject(null).callJniMethodObject(emulator, methodStringFromJNI,paramString0.getBytes());
  77. System.out.println("encodeBySHA1返回值:"+strRc.getValue());
  78. }
  79. public static void main(String[] args) throws Exception {
  80. JniManagerUtil jniManagerUtil = new JniManagerUtil();
  81. jniManagerUtil.stringFromJNI();
  82. jniManagerUtil.UUIDTest();
  83. jniManagerUtil.str2strTest();
  84. jniManagerUtil.encode();
  85. jniManagerUtil.encodeBySHA1();
  86. jniManagerUtil.destroy();
  87. }
  88. }

 3 其他


   3.1:使用unidbg时的参数类型如何定义的,可以参考文章


本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/158473
推荐阅读
相关标签
  

闽ICP备14008679号