JNI_Onload—c++

要逆向,必须会正向编写。so文件混淆——jni_load()——c++

JNI_Onload编写

利用JNI_Onload()函数

check.java

1
2
3
4
5
6
7
8
9
package com.example.ese.zjgsuctf;
public class check {
static {
System.loadLibrary("check");
}
public native static String check(String sstr);
}

check.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include<iostream>
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <assert.h>
#include <android/log.h>
#include "com_example_ese_zjgsuctf1_check.h"
#define JNIREG_CLASS "com/example/ese/zjgsuctf1/check"//指定要注册的类
struct Node{
int data;
Node *Pnext;
};
int enc_flag[]={52,67,41,1,31,85,44,87,46,89,19,111,55,0,46,91,31,69,112,73,20};
Node *ceateList(char *str){
int len=0;
Node *pHead = (Node *)malloc(sizeof(Node));
Node *pTail = pHead;
pTail->Pnext = NULL;
if(pHead == NULL)
{
exit(-1);
}
while(str[len] != '\0')
{
len++ ;
}
pTail->data = len;
for(int i=0;i<len;i++)
{
Node *pNew = (Node *)malloc(sizeof(Node));
if(pNew == NULL)
{
exit(-1);
}
if(i%2==0)
{
pNew->data = str[i]^0x30;
}else
{
pNew->data = str[i]^0x40;
}
pNew->Pnext = NULL;
pTail->Pnext = pNew;
pTail = pNew;
}
return pHead;
}
Node *insert(Node *lst,int pos,int data)
{
int i=0;
Node *pTmp = lst;
while(i<pos&&lst!=NULL)
{
lst=lst->Pnext;
i++;
}
Node *pNew = (Node *)malloc(sizeof(Node));
if(pNew == NULL)
{
exit(-1);
}
pNew->data = data;
pNew->Pnext = lst->Pnext;
lst->Pnext = pNew;
return pTmp;
}
Node *deletet(Node *lst,int pos,int data)
{
int i=0;
Node *pTmp = lst;
if(lst==NULL)
{
return lst;
}
while(i<pos&&lst!=NULL)
{
lst=lst->Pnext;
i++;
}
Node *pNew = (Node *)malloc(sizeof(Node));
if(pNew == NULL)
{
exit(-1);
}
pNew = lst->Pnext->Pnext;
lst->Pnext = pNew;
return pTmp;
}
int select(Node *lst,int pos)
{
int i=0;
if(lst==NULL)
{
return 0;
}
while(i<pos&&lst!=NULL)
{
lst=lst->Pnext;
i++;
}
return lst->data;
}
Node *reverseList(Node *lst){
if(lst==NULL || lst->Pnext == NULL)
{
return lst;
}
Node *pTmp = reverseList(lst->Pnext);
lst->Pnext->Pnext=lst;
lst->Pnext = NULL;
return pTmp;
}
int displayList(Node *lst)
{
int i=0;
Node *tmp = lst;
while(tmp != NULL)
{
if(tmp->data!=enc_flag[i])
{
return 0;
}
i++;
tmp = tmp->Pnext;
}
return 1;
}
int crypt(char *str)
{
//string str = "y0u_kn0w_Single_1ist";
Node *list1 = ceateList(str);
list1 = reverseList(list1);
if(displayList(list1))
{
return 1;
}
return 0;
}
//JNI
__attribute__((section (".mytext")))jstring esecheck( JNIEnv* env,jobject thiz,jstring str1)
{
//LOGV("This is esecheck()");
char* str = (char*)env->GetStringUTFChars(str1, NULL);
if(str == NULL) {
return NULL;
}
int key = crypt(str);
char* tmpstr = "succeeded";
if(key)
{
jstring rtstr = env->NewStringUTF(tmpstr);
return rtstr;
}else{
tmpstr = "failed!!";
jstring rtstr = env->NewStringUTF(tmpstr);
return rtstr;
}
}
/**
* Table of methods associated with a single class.
*/
static JNINativeMethod gMethods[] = {//绑定,注意,V,Z签名的返回值不能有分号“;”
{ "check", "(Ljava/lang/String;)Ljava/lang/String;", (void*)esecheck},
};
/*
* Register several native methods for one class.
*/
static int registerNativeMethods(JNIEnv* env, const char* className,
JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = env->FindClass( className);
if (clazz == NULL) {
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* Register native methods for all classes we know about.
*/
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env,JNIREG_CLASS, gMethods,
sizeof(gMethods) / sizeof(gMethods[0])))
return JNI_FALSE;
return JNI_TRUE;
}
/*
* Set some test stuff up.
*
* Returns the JNI version on success, -1 on failure.
*/
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return result;
}
assert(env != NULL);
if (!registerNatives(env)) {//注册
return -1;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
return result;
}

Application.mk

1
2
3
APP_ABI := armeabi-v7a
APP_STL:=stlport_static

Android

1
2
3
4
5
6
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := check
LOCAL_SRC_FILES := check.cpp
include $(BUILD_SHARED_LIBRARY)
LOCAL_CFLAGS := -fvisibility=hidden #隐藏符号表

调用

在app目录下的build.gradle中添加

1
2
3
4
5
6
7
8
9
android {
......
sourceSets {
main() {
jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs = [] //屏蔽掉默认的jni编译生成过程
}
}
}

这时一个简单的输入结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
activity_main.xml
{
.....
<EditText
android:id="@+id/input_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="100dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="input your flag"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="UnknownId" />
<Button
android:id="@+id/input_ok"
android:layout_width="102dp"
android:layout_height="45dp"
android:layout_marginBottom="276dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:text="确认"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.458"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
}

MainActivity结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText sstr = (EditText) findViewById(R.id.input_flag);
final Button ok = (Button) findViewById(R.id.input_ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String st = sstr.getText().toString().trim();
String res = check.Check(st);
Toast.makeText(MainActivity.this, res, Toast.LENGTH_SHORT).show();
}
});
}
}

运行就可以得到调用native方法的apk了。

优点

1.源码改动少,只需要添加JNI_Onload函数
2.无需加解密so,就可以实现混淆so中的JNI函数
3.后续可以添加so加解密,使破解难度更大

Donate
-------------本文结束感谢您的阅读-------------