使用反编译后的so文件

来源:互联网 发布:有哪些社交软件 编辑:程序博客网 时间:2024/05/22 03:25

本文主要整理来之网络,用于记录学习的知识

1、编译别人的apk文件后,获取到so文件后

比如

  1. static {  
  2.     try {  
  3.         System.loadLibrary("NativeExampleActivity");  
  4.     } catch (Throwable t) {  
  5.     }  
  6. }  
  7. public native int addFunction(int a, int b);  
  8. public native String getString(String name);
2、由编译后文件知道so文件的名称为libNativeExampleActivity.so文件,两个native函数addFunction和getString

虽然知道了这两个native函数,但是我们还不能直接使用,因为这两个native函数在so库里面的真实函数名不是addFunction和getString,它在native函数名之前还有包名,所以必须使用nm命令,查看so库里面的函数名。

nm -A libNativeExampleActivity.so
或者
nm -D libNativeExampleActivity.so

这样我们看到so库里的主要信息:
Java_org_natives_example_NativeExampleActivity_addFunction
Java_org_natives_example_NativeExampleActivity_getString

3、新建一个android 项目

  1. package so.hello;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class SoHelloActivity extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  
  13.     static {  
  14.         try {  
  15.             System.loadLibrary("soHello");  
  16.         } catch (Throwable t) {  
  17.         }  
  18.     }  
  19.     public native int addFunction1(int a, int b);  
  20.     public native String getString1(String name);  

4、使用javah -jni so.hello.SoHelloActivity生成一个文件so_hello_SoHelloActivity.h

  1. /* DO NOT EDIT THIS FILE - it is machine generated */  
  2. #include <jni.h>  
  3. /* Header for class so_hello_SoHelloActivity */  
  4.   
  5. #ifndef _Included_so_hello_SoHelloActivity  
  6. #define _Included_so_hello_SoHelloActivity  
  7. #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif  
  10. /*  
  11.  * Class:     so_hello_SoHelloActivity  
  12.  * Method:    addFunction1  
  13.  * Signature: (II)I  
  14.  */  
  15. JNIEXPORT jint JNICALL Java_so_hello_SoHelloActivity_addFunction1  
  16.   (JNIEnv *, jobject, jint, jint);  
  17.   
  18. /*  
  19.  * Class:     so_hello_SoHelloActivity  
  20.  * Method:    getString1  
  21.  * Signature: (Ljava/lang/String;)Ljava/lang/String;  
  22.  */  
  23. JNIEXPORT jstring JNICALL Java_so_hello_SoHelloActivity_getString1  
  24.   (JNIEnv *, jobject, jstring);  
  25.   
  26. #ifdef __cplusplus  
  27. }  
  28. #endif  
  29. #endif 

6、写一个so_hello_SoHelloActivity.cpp文件

  1. #include "so_hello_SoHelloActivity.h"   
  2. #include <stdlib.h>  
  3. #include <fcntl.h>  
  4. #include <android/log.h>  
  5. #include <stdio.h>    
  6. #include <stdarg.h>    
  7. #include <dlfcn.h>   
  8.   
  9. void *filehandle = NULL;  
  10. jint (*addFunc)(JNIEnv *,jobject,jint,jint) = NULL;  
  11. jstring (*getStringFunc)(JNIEnv *, jobject, jstring) = NULL;  
  12.   
  13. JNIEXPORT jint JNICALL Java_so_hello_SoHelloActivity_addFunction1  
  14.   (JNIEnv *env, jobject obj, jint a, jint b);  
  15. {  
  16.     jint mun = 0;  
  17.     //事先把libNativeExampleActivity放到root/system/lib/目录下  
  18.     filehandle = dlopen("/system/lib/libNativeExampleActivity.so", RTLD_LAZY);  
  19.     if(filehandle)  
  20.     {  
  21.         addFunc = (jint (*)(JNIEnv *,jobject,jint,jint))dlsym(filehandle, "Java_org_natives_example_NativeExampleActivity_addFunction");  
  22.         if(addFunc)  
  23.             mun = addFunc(env, obj, a, b);  
  24.         dlclose(filehandle);   
  25.         filehandle = NULL;  
  26.     }  
  27.     return mun  
  28. }  
  29.   
  30. /*  
  31.  * Class:     so_hello_SoHelloActivity  
  32.  * Method:    getString1  
  33.  * Signature: (Ljava/lang/String;)Ljava/lang/String;  
  34.  */  
  35. JNIEXPORT jstring JNICALL Java_so_hello_SoHelloActivity_getString1  
  36.   (JNIEnv *, jobject, jstring name)  
  37. {  
  38.     jstring mun = 0;  
  39.     //事先把libNativeExampleActivity放到root/system/lib/目录下  
  40.     filehandle = dlopen("/system/lib/libNativeExampleActivity.so", RTLD_LAZY);  
  41.     if(filehandle)  
  42.     {  
  43.         getStringFunc = (jstring (*)(JNIEnv *,jobject,jstring))dlsym(filehandle, "Java_org_natives_example_NativeExampleActivity_getString");  
  44.         if(getStringFunc)  
  45.         {  
  46.             mun = getStringFunc(env, obj, name);  
  47.         }  
  48.         dlclose(filehandle);   
  49.         filehandle = NULL;  
  50.     }  
  51.     return mun  

7、编写make文件

  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_LDLIBS := -llog  
  6. LOCAL_C_INCLUDES += system/core/include/cutils  
  7. LOCAL_SHARED_LIBRARIES := \  
  8.     libdl \  
  9.     libcutils  
  10.   
  11. LOCAL_PRELINK_MODULE :false  
  12. LOCAL_MODULE      :libsoHello  
  13. LOCAL_MODULE_TAGS :optional  
  14. LOCAL_SRC_FILES   :so_hello_SoHelloActivity.cpp  
  15. LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog  
  16.   
  17. include $(BUILD_SHARED_LIBRARY) 

使用mm命令编译so_hello_SoHelloActivity.cpp,便可以生成libsoHello.so库。

综述:
这里主要使用了dlopen、dlsym、dlclose三个函数来加载so库:
void *filehandle = NULL;
jint (*addFunc)(JNIEnv *,jobject,jint,jint) = NULL;
jint mun = 0
//事先把libNativeExampleActivity放到root/system/lib/目录下
filehandle = dlopen("/system/lib/libNativeExampleActivity.so", RTLD_LAZY);
if(filehandle)
{
    addFunc = (jint (*)(JNIEnv *,jobject,jint,jint))dlsym(filehandle, "Java_org_natives_example_NativeExampleActivity_addFunction");
    if(addFunc)
        mun = addFunc(env, obj, a, b);
    dlclose(filehandle); 
    filehandle = NULL;
}

原创粉丝点击