-
Notifications
You must be signed in to change notification settings - Fork 0
/
apk_manager.py
168 lines (130 loc) · 5.42 KB
/
apk_manager.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
import os
import subprocess
from pathlib import Path
from gui import elements_layout as el
from utils import ECoreElements
from threading import Thread
from tkinter import filedialog
from apk_parser import ApkParser
g_apk_parser = None
g_current_file = None
class ApkManager:
def __init__(self, gui) -> None:
self.__gui = gui
self.__logger = gui.get_logger()
self.__app_root = gui.get_root()
self.__ui_root = gui.get_general_tab()
self.__construct()
def __construct(self) -> None:
self.__gui.get_root_tab_bar().bind(
'<<NotebookTabChanged>>',
self.__on_tab_changed
)
self.__gui.create_button(
self.__ui_root, 20, 20, "Decompile APK",
self.__decompile_apk_callback,
el["button"]["width"]
)
self.__gui.create_button(
self.__ui_root, 20 + el["button"]["width"] * 1, 20, "Re-build APK",
self.__recompile_apk_callback, el["button"]["width"]
)
self.__apk_info_label = self.__gui.create_label(
self.__ui_root, 20, 20 + 50, 'APK info:'
)
self.__apk_info_label.focus_set()
self.__apk_sub_info_label = self.__gui.create_label(
self.__ui_root, 20, 20 + 70, '< Load APK >',
ECoreElements.APK_INFO_LABEL
)
self.__apk_icon_view = self.__gui.create_image(
self.__ui_root, 20, 20 + 150, 0, 32, 32,
ECoreElements.APK_ICON_VIEW
)
self.__progressbar = self.__gui.create_progressbar(
self.__ui_root, -6, -7, 0, 0,
ECoreElements.PROGRESS_BAR
)
def __on_tab_changed(self, event):
if event.widget.index("current") == 0: # general tab
self.__apk_info_label.focus_set()
def __decompile_apk_callback(self, __none__=None, threaded=False) -> None:
if threaded == False: # hack, i'm too lazy to asyncify tkinter
thread = Thread(
target=self.__decompile_apk_callback,
args=(self, True,)
)
thread.start()
return
global g_current_file, g_apk_parser
file_path = filedialog.askopenfilename()
if file_path == '': return
_, file_name = os.path.split(file_path)
g_current_file = file_path
self.__progressbar.start()
g_apk_parser = ApkParser(
g_current_file, self.__apk_sub_info_label,
self.__apk_icon_view
)
if g_apk_parser.parse() != True:
self.__progressbar.stop()
self.__logger.error(f"[-] Failed to decompile: {g_current_file}\n")
return
self.__app_root.title(f'A-RE: Android ({g_current_file})')
decompile_cmd = f'java -Xmx1024m -jar {str(Path.cwd())}\\vendor\\apktool\\apktool.jar' \
f" d {g_current_file} -o {str(Path.cwd())}\\decompiled\\{file_name}_decompiled -f"
# decompiling file
with subprocess.Popen(
decompile_cmd, shell=False, stdout=subprocess.PIPE,
bufsize=1, universal_newlines=True
) as p:
out, err = p.communicate()
self.__logger.external(out)
self.__progressbar
# logging
self.__logger.info(f"[+] Decompiled apk: {g_current_file}\n")
def __recompile_apk_callback(self, __none__=None,threaded=False) -> None:
if threaded == False: # hack, i'm too lazy to asyncify tkinter
thread = Thread(
target=self.__recompile_apk_callback,
args=(self, True,)
)
thread.start()
return
global g_current_file, g_apk_parser
if g_current_file == None:
self.__logger.error(f'[ !] Nothing to re-compile\n')
return
_, file_name = os.path.split(g_current_file)
recompile_cmd = f'java -Xmx1024m -jar {str(Path.cwd())}\\vendor\\apktool\\apktool.jar' \
f" b {str(Path.cwd())}\\decompiled\\{file_name}_decompiled -o {str(Path.cwd())}\\build\\{file_name}_recompiled.apk"
self.__progressbar.start()
# re-packing apk
with subprocess.Popen(
recompile_cmd, shell=False, stdout=subprocess.PIPE,
bufsize=1, universal_newlines=True
) as p:
out, err = p.communicate()
self.__logger.external(out)
sign_cmd = f'java -jar {str(Path.cwd())}\\vendor\\apksigner\\apksigner.jar' \
f' sign --key {str(Path.cwd())}\\vendor\\apksigner\\testkey.pk8' \
f' --cert {str(Path.cwd())}\\vendor\\apksigner\\testkey.x509.pem' \
f' {str(Path.cwd())}\\build\\{file_name}_recompiled.apk'
# signing packed apk
with subprocess.Popen(
sign_cmd, shell=False, stdout=subprocess.PIPE,
bufsize=1, universal_newlines=True
) as p:
out, err = p.communicate()
self.__logger.external(out)
# check if signed successfully
g_apk_parser.set_apk(f'{str(Path.cwd())}\\build\\{file_name}_recompiled.apk')
if g_apk_parser.is_signed():
self.__logger.info(f"[+] Signed apk: {g_current_file}_recompiled\n")
else:
self.__logger.error(f'[ !] Failed to sign apk: {g_current_file}_recompiled\n')
self.__progressbar.stop()
return
self.__progressbar.stop()
# logging
self.__logger.info(f"[+] Re-compiled apk: {g_current_file}_recompiled\n")