Android SO逆向-全局变量和静态局部变量

    0x00

    这一节比较简单,主要分析全局变量,全局静态变量,静态局部变量的实现。

  

    0x01

    我们直接看代码。

#include "com_example_ndkreverse2_Lesson2.h"
#include <android/log.h>
#define LOG_TAG "lesson2"
#define ALOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
static int a = 15;

JNIEXPORT void JNICALL Java_com_example_ndkreverse2_Lesson2_main
  (JNIEnv * env, jobject jobject) {
	static int b = 16;
	a = 2;
	b = 1;
	ALOGD("a=%d\n, b=%d\n", a, b);
}

   这个函数的汇编实现,我们打开ida打开对应的so查看:

.text:00000D2C                 EXPORT Java_com_example_ndkreverse2_Lesson2_main
.text:00000D2C Java_com_example_ndkreverse2_Lesson2_main
.text:00000D2C
.text:00000D2C var_10          = -0x10
.text:00000D2C
.text:00000D2C                 PUSH    {R0-R2,LR}
.text:00000D2E                 MOVS    R3, #2
.text:00000D30                 MOVS    R1, #1
.text:00000D32                 LDR     R2, =(dword_4004 - 0xD3A)
.text:00000D34                 MOVS    R0, #3
.text:00000D36                 ADD     R2, PC ; dword_4004
.text:00000D38                 STR     R1, [R2,#(dword_4008 - 0x4004)] ; 把1赋值给静态局部变量b
.text:00000D3A                 STR     R3, [R2] ;把2赋值给全局静态变量a
.text:00000D3C                 STR     R1, [SP,#0x10+var_10] ;第5个参数放在堆栈中
.text:00000D3E                 LDR     R2, =(aADBD - 0xD48)
.text:00000D40                 LDR     R1, =(aLesson2 - 0xD46)
.text:00000D42                 ADD     R1, PC          ; "lesson2"
.text:00000D44                 ADD     R2, PC          ; "a=%d\n, b=%d\n"
.text:00000D46                 BL      j_j___android_log_print
.text:00000D4A                 POP     {R0-R2,PC}

.text:00000D4C off_D4C         DCD dword_4004 - 0xD3A  ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+6r
.text:00000D50 off_D50         DCD aADBD - 0xD48       ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+12r
.text:00000D50                                         ; "a=%d\n, b=%d\n"
.text:00000D54 off_D54         DCD aLesson2 - 0xD46    ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+14r
.text:00000D54                                         ; "lesson2"

    其中dword_4004:

.data:00004004 dword_4004      DCD 0xF                 ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+Ao
.data:00004004                                         ; Java_com_example_ndkreverse2_Lesson2_main+Ew ...
.data:00004008 dword_4008      DCD 0x10                ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+Cw

    我们看到全局静态变量a和静态局部变量b都存放的位置是.data段,具体的详见代码注释。


    0x02

    我们接着看全局变量的实现。

#include "com_example_ndkreverse2_Lesson2.h"
#include <android/log.h>
#define LOG_TAG "lesson2"
#define ALOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
int a = 15;

JNIEXPORT void JNICALL Java_com_example_ndkreverse2_Lesson2_main
  (JNIEnv * env, jobject jobject) {
	static int b = 16;
	a = 2;
	b = 1;
	ALOGD("a=%d, b=%d\n", a, b);
}
   此时a变为了全局变量,我们看一下全局变量的汇编实现:

.text:00000D48                 EXPORT Java_com_example_ndkreverse2_Lesson2_main
.text:00000D48 Java_com_example_ndkreverse2_Lesson2_main
.text:00000D48
.text:00000D48 var_10          = -0x10
.text:00000D48
.text:00000D48                 MOVS    R3, #2
.text:00000D4A                 PUSH    {R0-R2,LR}
.text:00000D4C                 LDR     R2, =(a_ptr - 0xD56)
.text:00000D4E                 LDR     R1, =(dword_4004 - 0xD5A)
.text:00000D50                 MOVS    R0, #3
.text:00000D52                 ADD     R2, PC ; a_ptr
.text:00000D54                 LDR     R2, [R2] ; a 此时R2地址位于.got段,取出地址中的内容,就是真实a变量存储的地址,位于.data段
.text:00000D56                 ADD     R1, PC ; dword_4004
.text:00000D58                 STR     R3, [R2] 把2赋值给全局变量a,位于.data段中
.text:00000D5A                 MOVS    R2, #1
.text:00000D5C                 STR     R2, [R1]
.text:00000D5E                 STR     R2, [SP,#0x10+var_10]
.text:00000D60                 LDR     R1, =(aLesson2 - 0xD68)
.text:00000D62                 LDR     R2, =(aADBD - 0xD6A)
.text:00000D64                 ADD     R1, PC          ; "lesson2"
.text:00000D66                 ADD     R2, PC          ; "a=%d\n, b=%d\n"
.text:00000D68                 BL      j_j___android_log_print
.text:00000D6C                 POP     {R0-R2,PC}
.text:00000D6E                 ALIGN 0x10
.text:00000D70 off_D70         DCD a_ptr - 0xD56       ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+4r
.text:00000D74 off_D74         DCD dword_4004 - 0xD5A  ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+6r
.text:00000D78 off_D78         DCD aLesson2 - 0xD68    ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+18r
.text:00000D78                                         ; "lesson2"
.text:00000D7C off_D7C         DCD aADBD - 0xD6A       ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+1Ar
.text:00000D7C                                         ; "a=%d\n, b=%d\n"
    .got段,a代表.data段中全局变量a的地址。
.got:00003FB4 a_ptr           DCD a                   ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+Ao

    .data段,真正存储全局变量a的地方。

.data:00004004 dword_4004      DCD 0x10                ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+Eo
.data:00004004                                         ; Java_com_example_ndkreverse2_Lesson2_main+14w ...
.data:00004008                 EXPORT a
.data:00004008 a               DCB  0xF                ; DATA XREF: Java_com_example_ndkreverse2_Lesson2_main+Co
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值