-
Notifications
You must be signed in to change notification settings - Fork 32
/
testMonitor.py
103 lines (80 loc) · 2.96 KB
/
testMonitor.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Landroid/widget/TextView;->setText(Ljava/lang/CharSequence;)V
# Lorg/honeynet/apimonitor/APIMonitor;->android_widget_TextView__setText(Landroid/widget/TextView;Ljava/lang/CharSequence;)V
# example java code:
# package org.honeynet.apimonitor;
# import android.util.Log;
# import android.widget.TextView;
# public class APIMonitor {
# private static final String TAG = "APIMonitor";
# public static void android_widget_TextView__setText(TextView textView, CharSequence text) {
# textView.setText(text);
# Log.v(TAG, text.toString());
# }
# }
import sys
import time
import shutil
import os
from androguard.core.bytecodes import apk
from apkil import smali, monitor, logger
from subprocess import call
#j = monitor.JavaType("[[Landroid/adf/arae;")
#print j.get_java_desc()
#sys.exit(0)
APK = "examples/DroidBoxTests.apk"
DEX = "examples/DroidBoxTests.dex"
SMALI_DIR = "examples/DroidBoxTests_smali"
a = apk.APK(APK)
dex_file = open(DEX, 'w')
dex_file.write(a.get_dex())
dex_file.close()
call(args=['baksmali', '-b', '-o', SMALI_DIR, DEX])
s = smali.SmaliTree(SMALI_DIR)
NEW_OUT = "examples/new"
NEW_DEX = "examples/classes.dex"
NEW_APK = "examples/new.apk"
# sys.exit(0)
API_LIST = [ "Landroid/widget/TextView;->setText(Ljava/lang/CharSequence;)V", \
"Landroid/content/ContentValues;->put(Ljava/lang/String;Ljava/lang/Integer;)V", \
"Landroid/content/ContentValues;->put(Ljava/lang/String;Ljava/lang/String;)V", \
"Ldroidbox/tests/DroidBoxTests;->openFileOutput(Ljava/lang/String;I)Ljava/io/FileOutputStream;",
\
"Ljava/io/OutputStreamWriter;->write(Ljava/lang/String;)V", \
#"Ldroidbox/tests/DroidBoxTests;->openFileInput(Ljava/lang/String;)Ljava/io/FileInputStream;",
\
# hard to read
#"Ljava/io/BufferedReader;->readLine()Ljava/lang/String;", \
# new intent
]
TEMPLATE = "template/APIMonitor"
EXPORT_FOLDER = "examples/APIMonitor/java"
if os.path.exists(EXPORT_FOLDER):
shutil.rmtree(EXPORT_FOLDER)
shutil.copytree(TEMPLATE, EXPORT_FOLDER)
m = monitor.APIMonitor(API_LIST)
m.export(os.path.join(EXPORT_FOLDER, "src"))
# sys.exit(0)
call(args=["android", "update", "project", "--path", EXPORT_FOLDER])
call(args=["ant", "debug", "-buildfile", \
os.path.join(EXPORT_FOLDER, "build.xml")])
sys.exit(0)
dex_file_path = os.path.join(EXPORT_FOLDER, "bin", "classes.dex")
MONITOR_SMALI = "examples/APIMonitor/smali"
call(args=['baksmali', '-b', '-o', MONITOR_SMALI, dex_file_path])
m_s = smali.SmaliTree(MONITOR_SMALI)
for api in API_LIST:
insns = s.get_insn35c("invoke-virtual", api)
for i in insns:
i.obj.replace("invoke-static", m.method_map[api])
for c in m.get_class_descs():
s.add_class(m_s.get_class(c))
s.save(NEW_OUT)
call(args=['smali', '-a', '6', '-o', NEW_DEX, NEW_OUT])
new_dex = open(NEW_DEX).read();
a.new_zip(filename=NEW_APK,
deleted_files="(META-INF/.)", new_files = {
"classes.dex" : new_dex } )
apk.sign_apk( NEW_APK, \
"/Users/kelwin/Dropbox/Backup/androguard", "apkil", "apkilapkil" )