-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_finder.py
188 lines (143 loc) · 6.68 KB
/
secret_finder.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
import os
import re
import subprocess
import time
from tqdm import tqdm
from colorama import init, Fore
# Initialize colorama
init(autoreset=True)
APKTOOL_PATH = 'apktool_2.7.0.jar'
# Additional keywords to find sensitive hardcoded values
SENSITIVE_KEYWORDS = [' token ', 'TOKEN', 'TOKENS', 'tokens', ' key ',
'password', 'secret', 'SECRET', 'confidential', '_key', '_token', 'api_key']
# Decorator to print tool name with ASCII art
def print_tool_name(func):
def wrapper(*args, **kwargs):
print(r'''
/$$$$$$ /$$ /$$$$$$$$ /$$ /$$
/$$__ $$ | $$ | $$_____/|__/ | $$
| $$ \__/ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ | $$ /$$ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$
| $$$$$$ /$$__ $$ /$$_____/ /$$__ $$ /$$__ $$|_ $$_/ | $$$$$ | $$| $$__ $$ /$$__ $$ /$$__ $$ /$$__ $$
\____ $$| $$$$$$$$| $$ | $$ \__/| $$$$$$$$ | $$ | $$__/ | $$| $$ \ $$| $$ | $$| $$$$$$$$| $$ \__/
/$$ \ $$| $$_____/| $$ | $$ | $$_____/ | $$ /$$ | $$ | $$| $$ | $$| $$ | $$| $$_____/| $$
| $$$$$$/| $$$$$$$| $$$$$$$| $$ | $$$$$$$ | $$$$/ | $$ | $$| $$ | $$| $$$$$$$| $$$$$$$| $$
\______/ \_______/ \_______/|__/ \_______/ \___/ |__/ |__/|__/ |__/ \_______/ \_______/|__/
github.com/viralvaghela
''')
print("Welcome to the Secret Finder!")
return func(*args, **kwargs)
return wrapper
# Decompiles the specified APK file
# Decompiles the specified APK file
def decompile_apk(apk_path):
# Remove double quotes from the APK path
apk_path = apk_path.strip('"')
# Extract the APK file name without extension
apk_name = os.path.splitext(os.path.basename(apk_path))[0]
# Replace spaces with underscores in the APK name
apk_name = apk_name.replace(' ', '_')
# Create a new directory in the current working directory
decompiled_path = os.path.join(os.getcwd(), apk_name + '_decompiled')
print(
f"Decompiling APK: {apk_path} It may take some time, please wait...\n ")
# Start the decompilation process
process = subprocess.Popen(['java', '-jar', APKTOOL_PATH, 'd', apk_path, '-o', decompiled_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
pbar = None
while True:
output = process.stdout.readline().strip()
if output:
match = re.search(r'(\d+)%', output)
if match:
progress = int(match.group(1))
if not pbar:
pbar = tqdm(total=100, desc="Decompiling",
unit="%", leave=True)
pbar.n = progress
pbar.refresh()
else:
break
process.wait()
if pbar:
pbar.close()
if process.returncode == 0:
print("APK decompiled successfully!")
print(f"Decompiled files saved in: {decompiled_path}")
else:
print("Failed to decompile APK.")
print(process.stderr.read())
return decompiled_path
# Checks a file for matching sensitive keywords
def check_file_for_sensitive_keywords(file_path):
matches = []
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line_number, line in enumerate(file, start=1):
line = line.strip()
for keyword in SENSITIVE_KEYWORDS:
if keyword in line.lower():
matches.append((file_path, line_number, line))
break
except UnicodeDecodeError:
print(f"Failed to decode file: {file_path}")
return matches
# Checks the APK for matching sensitive keywords in specified file(s) or all files
def check_apk_for_sensitive_keywords(apk_path, check_all_files=False):
decompiled_path = decompile_apk(apk_path)
print("Searching for sensitive strings in all files...\n")
matches = []
total_files = 0
if check_all_files:
for root, _, files in os.walk(decompiled_path):
for file in files:
if file.endswith(".smali"): # Only process *.smali files
total_files += 1
with tqdm(total=total_files, desc="Progress", unit="file") as pbar:
for root, _, files in os.walk(decompiled_path):
for file in files:
if file.endswith(".smali"): # Only process *.smali files
file_path = os.path.join(root, file)
file_matches = check_file_for_sensitive_keywords(
file_path)
if file_matches:
matches.extend(file_matches)
pbar.update(1)
# Optional delay for smoother progress display
time.sleep(0.01)
else:
strings_xml_path = os.path.join(
decompiled_path, 'res', 'values', 'strings.xml')
manifest_xml_path = os.path.join(
decompiled_path, 'AndroidManifest.xml')
total_files = 2
pbar = tqdm(total=total_files, desc="Progress", unit="file")
strings_matches = check_file_for_sensitive_keywords(strings_xml_path)
manifest_matches = check_file_for_sensitive_keywords(manifest_xml_path)
matches.extend(strings_matches)
matches.extend(manifest_matches)
pbar.update(total_files)
return matches
# Main function
@print_tool_name
def main():
apk_path = input("Enter the path to the APK file: ")
file_check_option = int(input(
"Select file check option \n1. Basic Scan(Fast - Check for only important)\n2. Advance Scan(Slow - Check for All files): "))
if file_check_option == 1:
sensitive_matches = check_apk_for_sensitive_keywords(apk_path)
elif file_check_option == 2:
sensitive_matches = check_apk_for_sensitive_keywords(
apk_path, check_all_files=True)
else:
print("Invalid option selected. Please try again.")
return
if not sensitive_matches:
print(Fore.RED + "No sensitive strings found.")
else:
print(Fore.RED + "\nSensitive strings found:\n")
for match in sensitive_matches:
print(Fore.GREEN + f"File: {match[0]}")
print(Fore.GREEN + f"Line Number: {match[1]}")
print(Fore.GREEN + f"Line: {match[2]}\n")
if __name__ == '__main__':
main()