Skip to content

Commit d1a308d

Browse files
authored
Code block
1 parent bdacfc3 commit d1a308d

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

analyze_apk.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import os
2+
import re
3+
import argparse
4+
import subprocess
5+
import json
6+
7+
def process_smali_files(folder_path):
8+
http_annotations = []
9+
okhttp3_variables = []
10+
11+
annotation_pattern = re.compile(r'\.annotation runtime Lretrofit2/http/([A-Z]+);([\s\S]*?)\.end annotation')
12+
okhttp3_pattern = re.compile(r'\.field .*? Lokhttp3/')
13+
14+
for root, _, files in os.walk(folder_path):
15+
for file in files:
16+
if file.endswith('.smali'):
17+
file_path = os.path.join(root, file)
18+
with open(file_path, 'r') as smali_file:
19+
content = smali_file.read()
20+
http_annotations.extend(annotation_pattern.findall(content))
21+
okhttp3_variables.extend(okhttp3_pattern.findall(content))
22+
23+
return http_annotations, okhttp3_variables
24+
25+
def parse_apk(apk_path):
26+
try:
27+
subprocess.run(["apktool", "d", apk_path])
28+
return True
29+
except Exception as e:
30+
print("APK parsing failed:", e)
31+
return False
32+
33+
def generate_txt_output(http_annotations, okhttp3_variables, output_file):
34+
result = ""
35+
if http_annotations:
36+
result += "HTTP Method Annotations:\n"
37+
for annotation in http_annotations:
38+
http_method = annotation[0].strip()
39+
value = annotation[1].strip()
40+
result += f"HTTP Method: {http_method}\n"
41+
result += f"Value: {value}\n\n"
42+
43+
if okhttp3_variables:
44+
result += "OkHttp3 Variables:\n"
45+
for variable in okhttp3_variables:
46+
variable_name = variable.strip()
47+
result += f"OkHttp3 Variable: {variable_name}\n\n"
48+
49+
with open(output_file, 'w') as file:
50+
file.write(result)
51+
print(f"Output file created: {output_file}")
52+
53+
def generate_postman_output(http_annotations, output_file):
54+
postman_collection = {
55+
"info": {
56+
"name": "Retrofit Analysis",
57+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
58+
},
59+
"item": []
60+
}
61+
62+
if http_annotations:
63+
http_item = {
64+
"name": "HTTP Method Annotations",
65+
"item": []
66+
}
67+
68+
for annotation in http_annotations:
69+
http_method = annotation[0].strip()
70+
value = annotation[1].strip()
71+
72+
request_item = {
73+
"name": f"HTTP Method: {http_method}",
74+
"request": {
75+
"method": http_method,
76+
"url": "{{base_url}}" + value
77+
}
78+
}
79+
80+
http_item["item"].append(request_item)
81+
82+
postman_collection["item"].append(http_item)
83+
84+
with open(output_file, 'w') as file:
85+
json.dump(postman_collection, file, indent=2)
86+
print(f"Output collection JSON file created: {output_file}")
87+
88+
def main():
89+
parser = argparse.ArgumentParser(description='Retrofit and OkHttp3 Annotation Analyzer')
90+
parser.add_argument('-apk', '--apk-path', help='Path to the APK file')
91+
parser.add_argument('-s', '--smali-folder', help='Path to the smali folder')
92+
parser.add_argument('-o', '--output-file', help='Path to the output file')
93+
parser.add_argument('-f', '--output-format', choices=['txt', 'postman'], default='txt', help='Output format (txt or postman)')
94+
args = parser.parse_args()
95+
96+
apk_path = args.apk_path
97+
smali_folder_path = args.smali_folder
98+
output_file = args.output_file
99+
output_format = args.output_format
100+
101+
if apk_path and not smali_folder_path:
102+
apk_name = os.path.splitext(os.path.basename(apk_path))[0]
103+
if parse_apk(apk_path):
104+
smali_folder_path = os.path.join(os.getcwd(), apk_name)
105+
106+
http_annotations = []
107+
okhttp3_variables = []
108+
109+
if smali_folder_path:
110+
http_annotations, okhttp3_variables = process_smali_files(smali_folder_path)
111+
112+
if output_format == 'postman':
113+
generate_postman_output(http_annotations, output_file)
114+
else:
115+
generate_txt_output(http_annotations, okhttp3_variables, output_file)
116+
117+
if __name__ == "__main__":
118+
main()

0 commit comments

Comments
 (0)