forked from OWASP/Nettacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.py
More file actions
199 lines (179 loc) · 8.04 KB
/
module.py
File metadata and controls
199 lines (179 loc) · 8.04 KB
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
189
190
191
192
193
194
195
196
197
198
199
import copy
import importlib
import json
import os
import time
from threading import Thread
from nettacker import logger
from nettacker.config import Config
from nettacker.core.messages import messages as _
from nettacker.core.template import TemplateLoader
from nettacker.core.utils.common import expand_module_steps, wait_for_threads_to_finish
from nettacker.database.db import find_events
log = logger.get_logger()
class Module:
def __init__(
self,
module_name,
options,
target,
scan_id,
process_number,
thread_number,
total_number_threads,
):
self.module_name = module_name
self.process_number = process_number
self.module_thread_number = thread_number
self.total_module_thread_number = total_number_threads
self.module_inputs = options.__dict__
self.module_inputs["target"] = target
if options.modules_extra_args:
for module_extra_args in self.module_inputs["modules_extra_args"]:
self.module_inputs[module_extra_args] = self.module_inputs["modules_extra_args"][
module_extra_args
]
self.target = target
self.scan_id = scan_id
self.skip_service_discovery = options.skip_service_discovery
self.discovered_services = None
self.ignored_core_modules = [
"subdomain_scan",
"icmp_scan",
"port_scan",
"ssl_weak_version_vuln",
"ssl_weak_cipher_vuln",
"ssl_certificate_weak_signature_vuln",
"ssl_self_signed_certificate_vuln",
"ssl_expired_certificate_vuln",
"ssl_expiring_certificate_scan",
]
contents = TemplateLoader("port_scan", {"target": ""}).load()
self.service_discovery_signatures = list(
set(
contents["payloads"][0]["steps"][0]["response"]["conditions"]
.get("service", set(contents["payloads"][0]["steps"][0]["response"]["conditions"]))
.keys()
)
)
self.libraries = [
module_protocol.split(".py")[0]
for module_protocol in os.listdir(Config.path.module_protocols_dir)
if module_protocol.endswith(".py")
and module_protocol not in {"__init__.py", "base.py"}
]
def load(self):
self.module_content = TemplateLoader(self.module_name, self.module_inputs).load()
if not self.skip_service_discovery and self.module_name not in self.ignored_core_modules:
services = {}
for service in find_events(self.target, "port_scan", self.scan_id):
service_event = json.loads(service)
port = service_event["port"]
protocols = service_event["response"]["conditions_results"].keys()
for protocol in protocols:
if protocol and protocol in self.libraries:
if protocol in services:
services[protocol].append(port)
else:
services[protocol] = [port]
self.discovered_services = copy.deepcopy(services)
index_payload = 0
for payload in copy.deepcopy(self.module_content["payloads"]):
if (
payload["library"] not in self.discovered_services
and payload["library"] in self.service_discovery_signatures
):
del self.module_content["payloads"][index_payload]
index_payload -= 1
else:
index_step = 0
for step in copy.deepcopy(
self.module_content["payloads"][index_payload]["steps"]
):
step = TemplateLoader.parse(
step, {"port": self.discovered_services[payload["library"]]}
)
self.module_content["payloads"][index_payload]["steps"][index_step] = step
index_step += 1
index_payload += 1
def generate_loops(self):
if self.module_inputs["excluded_ports"]:
excluded_port_set = set(self.module_inputs["excluded_ports"])
if self.module_content and "ports" in self.module_content["payloads"][0]["steps"][0]:
all_ports = self.module_content["payloads"][0]["steps"][0]["ports"]
all_ports[:] = [port for port in all_ports if port not in excluded_port_set]
self.module_content["payloads"] = expand_module_steps(self.module_content["payloads"])
def sort_loops(self):
for index in range(len(self.module_content["payloads"])):
steps_without_dependencies = []
steps_with_temp_dependencies = []
steps_with_normal_dependencies = []
for step in copy.deepcopy(self.module_content["payloads"][index]["steps"]):
resp = step[0]["response"]
if "dependent_on_temp_event" not in resp:
steps_without_dependencies.append(step)
elif "save_to_temp_events_only" in resp:
steps_with_temp_dependencies.append(step)
else:
steps_with_normal_dependencies.append(step)
self.module_content["payloads"][index]["steps"] = (
steps_without_dependencies
+ steps_with_temp_dependencies
+ steps_with_normal_dependencies
)
def start(self):
active_threads = []
# counting total number of requests
total_number_of_requests = 0
for payload in self.module_content["payloads"]:
if payload["library"] not in self.libraries:
log.warn(_("library_not_supported").format(payload["library"]))
return None
for step in payload["steps"]:
total_number_of_requests += len(step)
request_number_counter = 0
for payload in self.module_content["payloads"]:
library = payload["library"]
engine = getattr(
importlib.import_module(f"nettacker.core.lib.{library.lower()}"),
f"{library.capitalize()}Engine",
)()
for step in payload["steps"]:
for sub_step in step:
thread = Thread(
target=engine.run,
args=(
sub_step,
self.module_name,
self.target,
self.scan_id,
self.module_inputs,
self.process_number,
self.module_thread_number,
self.total_module_thread_number,
request_number_counter,
total_number_of_requests,
),
)
thread.name = f"{self.target} -> {self.module_name} -> {sub_step}"
request_number_counter += 1
log.verbose_event_info(
_("sending_module_request").format(
self.process_number,
self.module_name,
self.target,
self.module_thread_number,
self.total_module_thread_number,
request_number_counter,
total_number_of_requests,
)
)
thread.start()
time.sleep(self.module_inputs["time_sleep_between_requests"])
active_threads.append(thread)
wait_for_threads_to_finish(
active_threads,
maximum=self.module_inputs["thread_per_host"],
terminable=True,
)
wait_for_threads_to_finish(active_threads, maximum=None, terminable=True)