-
Notifications
You must be signed in to change notification settings - Fork 17
/
module_actions.py
148 lines (98 loc) · 6.09 KB
/
module_actions.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
import importlib, traceback
import time
from UtilityController import UtilityTools
def interact_with_module(session, module_path,module_args, project_ids = None, zones_choices = None):
try:
# Check if creds are none and print error message before entering function.
if "Unauthenticated" not in module_path and session.credentials is None:
print(f"{UtilityTools.RED}{UtilityTools.BOLD}[X] Cannot run module as credentials are 'None'. Please load in credentials or run an unauthenticated module.{UtilityTools.RESET}")
return -1
module_import_path = module_path.replace("/",".")
module = importlib.import_module(module_import_path)
if "-h" in module_args:
module.run_module(module_args, session)
output_format =[]
# Handle output format selection; cmdline takes precedence
if "enum_" in module_import_path:
if "--txt" in module_args or "--csv" in module_args or "--table" in module_args:
if "--txt" in module_args:
output_format.append("txt")
if "--csv" in module_args:
output_format.append("csv")
if "--table" in module_args or ("--txt" not in module_args and "--csv" not in module_args):
output_format.append("table")
elif session.workspace_config.preferred_output_formats:
output_format = [f"{fmt}" for fmt in session.workspace_config.preferred_output_formats]
flags_to_remove = ["--csv", "--txt", "--table"]
for flag in flags_to_remove:
if flag in module_args:
module_args.remove(flag)
one_project_only = False
project_list = []
module_indicators_of_no_project_prompt = [
"enum_policy_bindings",
"ResourceManager",
"Exploit",
"Process",
"Unauthenticated"
]
# Check if user supplied project IDs at cmdline
if project_ids:
project_list = project_ids
# Next check if user has global setting set; cmdline takes precedence
elif session.workspace_config.preferred_project_ids and not project_ids:
print("[*] Proceeding with worskpace configuration for project IDs")
for project_id in session.workspace_config.preferred_project_ids:
print(f"[-] {project_id}")
time.sleep(1)
project_list = session.workspace_config.preferred_project_ids
# Depending on some items set proejct ID to just current project iD
elif any(module_indicator in module_import_path for module_indicator in module_indicators_of_no_project_prompt) and session.project_id != None:
project_list = [session.project_id]
elif not project_ids and session.project_id != None:
project_list = [session.project_id]
if len(session.global_project_list) > 1:
all_projects_choice = session.choice_selector(
["All Projects","Current/Single"],
f"Do you want to scan all projects or current single project? If not specify a project-id(s) with '--project-ids project1,project2,project3'"
)
if all_projects_choice == "All Projects":
# A set of unique project IDs for all creds
project_list = session.global_project_list
one_project_only = False
elif all_projects_choice == "Current/Single":
print("[*] Proceeding with just the current project ID")
one_project_only = True
else:
print("[*] Exiting...")
return -1
else:
project_list = None
if "Unauthenticated" not in module_path and project_list == None:
print(f"{UtilityTools.RED}{UtilityTools.BOLD}[X] Cannot run module without default project_id specified. Either specify one with '--project-ids <project_id1>,<project_id2>' or set it via 'projects set <project_id>.{UtilityTools.RESET}")
return -1
original_project_id = session.project_id
current_project_length = len(project_list)
for index, project_id in enumerate(project_list):
UtilityTools.log_action(session.workspace_directory_name , f"[START_MODULE] Entering {module_path.split('/')[-1]} module for {project_id}...")
session.project_id = project_id
first_run = (index == 0)
last_run = (index == len(project_list) - 1)
callback = module.run_module(module_args, session, first_run = first_run, last_run = last_run, output_format = output_format)
# If callback in enum_all and user didnt specify project dis
if callback == 2 and "enum_all" in module_import_path and not project_ids and not one_project_only:
new_project_length = len(session.global_project_list)
if new_project_length != current_project_length:
# Find the difference between new and old project lists
diff_projects = list(set(session.global_project_list) - set(project_list))
# Append the difference to project_list
project_list.extend(diff_projects)
current_project_length = new_project_length
UtilityTools.log_action(session.workspace_directory_name, f"[END_MODULE] Exiting {module_path.split('/')[-1]} module for {project_id}...")
# Reset session at end to default project
session.project_id = original_project_id
except KeyboardInterrupt:
pass # Handle Ctrl+C to exit gracefully
except Exception as e:
print(f"{UtilityTools.RED}{UtilityTools.BOLD}[X] A generic occured while executing the module. See details below:{UtilityTools.RESET}")
print(traceback.format_exc())