-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.py
executable file
·59 lines (46 loc) · 1.7 KB
/
cli.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
"""Module to handle commandline arguments for analyzing Android applications"""
import logging
import os
from pathlib import Path
import click
from pdroid import APK
from androguard.misc import AnalyzeAPK
CWD = os.getcwd()
logger = logging.getLogger(__name__)
@click.command()
@click.option(
"--apk", type=click.Path(exists=True), required=True, help="Path to the apk file"
)
def extract_prcs(apk):
"""Analyzes the apk and extracts permission-requiring code segments"""
a, _, dx = AnalyzeAPK(apk)
app = APK(a, dx)
prcs_list = app.prcs
path_to_apk = Path(apk)
dir_to_apk = path_to_apk.parent
app_name = app.get_app_name()
app_dir = dir_to_apk / app_name
os.makedirs(app_dir)
for i, el in enumerate(prcs_list):
filepath = app_dir / f"PRCS_{str(i)}.java"
with open(filepath, "w") as f:
header = f"/*Application Package Name: {a.get_package()}\nclass PRCS_{str(i)} {{\n"
footer = "\n}"
try:
f.write(header)
if type(el) == tuple:
for each_hop in el:
src_code = each_hop.get_source_code()
javadoc_comment = each_hop.get_id()
code = f"/**\n{javadoc_comment}\n*/\n{src_code}\n"
f.write(code)
else:
src_code = el.get_source_code()
javadoc_comment = el.get_id()
code = f"/**\n{javadoc_comment}\n*/\n{src_code}\n"
f.write(code)
f.write(footer)
except Exception as e:
print(f"Exception {e} occurred at index {i}")
if __name__ == "__main__":
extract_prcs()