-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstumble.py
83 lines (67 loc) · 2.9 KB
/
stumble.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
"""Stumble.
Usage:
stumble.py APK_PATHS ... [-s <SEARCH_STRING>] ... [-f FILE_TYPES ...] [options]
stumble.py (-h | --help)
stumble.py --version
Arguments:
APK_PATHS the path of the APK you want to search through
SEARCH_STRING the string(s) you want to search for in a space seperated format
FILE_TYPES The types of file extensions you want to search (.xml, .txt, etc...)
FILE_NAME Result file to write JSON summary to
OUT_DIR Save file(s) matched, if no FILE_TYPES specified, then save ALL files
Options:
-s SEARCH_STRING The search string(s) you want to look for
-f FILE_TYPES Specify the types of file extensions you want to search (.xml, .txt, etc...)
-i --insensitive Make search string case insensitive
-o FILE_NAME Write a result summary JSON file to FILE_NAME
-w OUT_DIR Save matched files to OUT_DIR directory
"""
import json
import os
from pprint import pprint
import sys
from docopt import docopt
from ApkFileSearch import ApkFileSearch
# stumble.py APK_PATHS ... -f FILE_TYPES ... [-w OUT_DIR] [-o FILE_NAME]
if __name__ == '__main__':
arguments = docopt(__doc__, version='0.0.1rc')
print(arguments)
apk_paths = arguments['APK_PATHS']
search_strings = arguments['-s']
file_types = arguments['-f']
is_case_insensitive = arguments['--insensitive']
output_file_name = arguments['-o']
save_matched_files_dir = arguments['-w']
file_types = [file_type.replace('.','') for file_type in file_types]#remove . from extensions
print file_types
#file_types = arguments[]
#save_files #save files that contain a match
#out_file #optionally, save summary results to JSON
#
for path in apk_paths:
if not os.path.isfile(path):
print '{path} is not a valid file'.format(path=path)
sys.exit()
#initialize a file with an empty JSON array
if output_file_name:
with open(output_file_name, 'a') as f:
json.dump([], f)
for apk_path in apk_paths:
afs = ApkFileSearch(apk_path,
search_strings,
file_types,
case_insensitive=is_case_insensitive,
save_matched_files_dir=save_matched_files_dir)
results = afs.search()
pprint(results)
#load existing JSON data, append, and re-dump it
if output_file_name and os.path.exists(output_file_name):
with open(output_file_name,'r') as f:
data = json.load(f)
#key as apk name for identifier
results = {apk_path:results}
data.append(results)
with open(output_file_name,'w') as f:
json.dump(data, f, sort_keys=True,
indent=4,
separators=(',',': '))