forked from Ch0pin/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapkutils.py
executable file
·211 lines (161 loc) · 6.07 KB
/
apkutils.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
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
#!/usr/bin/env python3
import sys
import readline
import os
import shutil
import frida
import subprocess
import cmd
import pty
from libraries.xmlUtils import *
from libraries.libapkutils import *
from xml.dom import minidom
from xml.etree import ElementTree
from libraries.APKEnum import performRecon
import fileinput
RED = "\033[1;31m"
BLUE = "\033[1;34m"
CYAN = "\033[1;36m"
GREEN = "\033[0;32m"
RESET = "\033[0;0m"
BOLD = "\033[;1m"
REVERSE = "\033[;7m"
APK = False
INSTALL = False
#enumerate classes
# js = """Java.perform(function(){Java.enumerateLoadedClasses({"onMatch":function(c){send(c);}});});"""
apktool=os.getcwd()+"/dependencies/apktool.jar"
apksigner = os.getcwd()+"/dependencies/apksigner"
zipallign = os.getcwd()+ "/dependencies/zipalign"
debuggable_apk = os.getcwd()+"/debuggable.apk";
alligned_apk = os.getcwd()+"/debuggable_alligned_signed.apk"
tmp_folder = os.getcwd()+"/tmp"
classes = []
shell = os.environ.get('SHELL', 'sh')
def patch_debuggable(filename):
text_to_search = "<application"
replacement_text ='<application android:debuggable="true" '
with open(filename) as f:
if 'android:debuggable="true"' in f.read():
print(RED+"[!] Application is debuggable !"+RESET)
return False
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(text_to_search, replacement_text), end='')
return True
def print_usage():
print("\nInput file is missing ! \nUsage: \n\t{} path_to_AndroidManifest.xml OR ".format(sys.argv[0]))
print('\t{} path_to_apk (--patch to set android:debuggable to true) \n\n'.format(sys.argv[0]))
def extract_manifest(file):
print(GREEN+'[+] Unpacking apk....'+RESET)
subprocess.run('java -jar '+ apktool +' d {} -o {}'.format(file,tmp_folder), shell=True)
if len(sys.argv) > 2 and "--patch" in sys.argv[2]:
print(GREEN+'[+] Setting debuggable flag....'+RESET)
if patch_debuggable(tmp_folder+'/AndroidManifest.xml'):
subprocess.run('java -jar '+ apktool +' b tmp -o {}'.format(debuggable_apk), shell=True)
print(GREEN+'[+] Running Zipallign.....'+RESET)
subprocess.run(zipallign +' -f 4 {} {}'.format(debuggable_apk,alligned_apk),shell=True)
print(GREEN+'[+] Signing the apk.....'+RESET)
subprocess.run(apksigner +' sign --ks ./dependencies/common.jks -ks-key-alias common --ks-pass pass:password --key-pass pass:password {}'.format(alligned_apk),shell=True)
print(GREEN+'[+] Removing the unsigned apk.....'+RESET)
os.remove(debuggable_apk)
print(GREEN+'[+] Backing up the original...'+RESET)
shutil.move(file,file+'.back')
shutil.move(alligned_apk,file)
subprocess.run('cp tmp/AndroidManifest.xml ./manifest.xml', shell=True)
performRecon("./tmp")
shutil.rmtree('./tmp')
# def on_message(message, data):
# try:
# if message["type"] == "send":
# classes.append( message["payload"].split(":")[0].strip())
# except Exception as e:
# print('exception: ' + e)
try:
if len(sys.argv[1:]) < 1:
print_usage()
exit()
extension = sys.argv[1].split('.')[-1]
if extension == 'xml':
xmlDoc = minidom.parse(sys.argv[1])
filters = get_elements_sub(sys.argv[1])
elif extension == 'apk':
APK = True
print('APK file given as an input')
extract_manifest(sys.argv[1])
xmlDoc = minidom.parse('manifest.xml')
filters = get_elements_sub('manifest.xml')
else:
print_usage()
exit()
except Exception as e:
print(e)
print(GREEN+"""\n-----------Package Details-------------:
Name :{}
Version code :{}
Version Name :{}
Mimimum SDK :{}
Target SDK :{}
App Name :{}
Allow Backup :{}
---------------------------------------
Debuggable :{}
Exported Components:
""".format(get_elements(xmlDoc,'manifest','package')
,get_elements(xmlDoc,'manifest','android:versionCode')
,get_elements(xmlDoc,'manifest','android:versionName')
,get_elements(xmlDoc,'uses-sdk','android:minSdkVersion')
,get_elements(xmlDoc,'uses-sdk','android:targetSdkVersion')
,get_elements(xmlDoc,'application','android:name')
,get_elements(xmlDoc,'application','android:allowBackup')
,get_elements(xmlDoc,'application','android:debuggable')))
package = get_elements(xmlDoc,'manifest','package')
permissions = get_element_list(xmlDoc,'uses-permission','android:name')
activities = get_element_list(xmlDoc,'activity','android:name')
services = get_element_list(xmlDoc, 'service','android:name')
receivers = get_element_list(xmlDoc,'receiver','android:name')
providers = get_element_list(xmlDoc,'provider','android:name')
print(RESET)
pty.openpty()
try:
print('Availlable devices:\n')
devices = frida.enumerate_devices()
i = 0
for dv in devices:
print('{}) {}'.format(i,dv))
i += 1
j = input('\nEnter the index of device to use:')
device = devices[int(j)]
print('[+] Starting adb as root\n\n')
os.popen("""adb -s {} root""".format(device.id))
if APK == True:
install = input("Do you want to install the apk ? (yes/no)")
if 'yes' in install:
subprocess.run('adb -s {} install {}'.format(device.id,sys.argv[1]),shell=True)
INSTALL = True
except Exception as e:
print(e)
# try:
# if INSTALL==True:
# pid = device.spawn(package)
# print("[i] Starting process {} [pid:{}] to dump classes ".format(package,pid))
# session = device.attach(pid)
# device.resume(pid)
# script = session.create_script(js)
# script.on('message', on_message)
# script.load()
# except Exception as e:
# print(e)
p = parser()
p.package = package
p.permissions = permissions
p.activities = activities
p.services = services
p.receivers = receivers
p.providers = providers
p.device = device
p.INSTALL = INSTALL
p.filters = filters
# p.classes = classes
p.cmdloop()
print('\nBye !!')