Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
166 lines (133 loc) · 5.92 KB
/
Copy pathmain.py
File metadata and controls
166 lines (133 loc) · 5.92 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
import importlib
import time
import tabulate
import logging
logging.getLogger("scapy").setLevel(logging.ERROR)
from lib.readconfig import *
from lib.interfaces import *
from lib.connectivity import *
import scapy.all as scapy
import scapy.config
import ipaddress
from pyroute2 import netns as pyrouteNetns, IPRoute
import netns
class L2CheckSuite:
def initialize(self):
ipr = IPRoute()
interfaces = self.config.get("interfaces")
for index, (ifname, ip) in enumerate(zip(interfaces.keys(), self.ip4network.hosts())):
namespace = f'host{index}'
pyrouteNetns.create(namespace)
ifindex = ipr.link_lookup(ifname=ifname)[0]
ipr.link('set', index=ifindex, net_ns_fd=namespace)
ifup(ifname, namespace)
if self.config.get('dhcp'):
start_dhclient(ifname, namespace, type(self).__name__)
else:
add_ip(ifname, ip, self.ip4network.prefixlen, namespace)
with netns.NetNS(nsname=namespace):
while True:
scapy.config.conf.ifaces.reload()
scapy.config.conf.route.resync()
intf = scapy.interfaces.dev_from_networkname(ifname)
if intf.ip is None:
self.print_debug(f'DHCP Server does not answer on interface {ifname}. Configuring static IP.')
add_ip(ifname, ip, self.ip4network.prefixlen, namespace)
elif intf.ips[6]:
break
self.Interfaces.append(intf)
ipr.close()
self.print_debug('Initialization successful.')
def check_base_connectivity(self):
retry_count = 0
retrys = 50
sniffer = []
for i in range(1,4):
filterstr = f'arp and ether dst {self.Interfaces[0].mac} and ether src {self.Interfaces[i].mac}'
sniffer.append(get_async_sniffer(self.Interfaces, i, filterstr))
while retry_count < retrys:
error = None
for s in sniffer:
s.start()
ans = arping_neighbors(self.Interfaces[0], 'host0', self.ip4network)
for s in sniffer:
s.stop()
ans_macs = []
for res in ans.res:
ans_macs.append(res.answer.src)
if (not ans_macs.__contains__(self.Interfaces[1].mac)) or \
(not ans_macs.__contains__(self.Interfaces[2].mac)):
self.print_debug(ans_macs)
error = RuntimeError('Physical setup not valid. No ARP response.')
if (ans_macs.__contains__(self.Interfaces[3].mac)) and (len(sniffer[2].results.res) != 0):
self.print_debug(ans_macs)
error = RuntimeError('Physical setup not valid. ARP response from isolated interface.')
if error is None:
self.print_debug('Initial setup successful.')
return True
retry_count += 1
time.sleep(1)
raise error
def renew_bindings(self):
for index, intf in enumerate(self.Interfaces):
namespace = f'host{index}'
if self.config.get('dhcp'):
renew_dhclient(intf.name, namespace, type(self).__name__)
def run(self):
for attack_config in self.config.get("attacks"):
start_time = time.time()
attack_name = attack_config.get("name")
attack_module = attack_config.get("module")
attack_class = getattr(importlib.import_module('attacks.'+attack_module), attack_module)
attack_instance = attack_class(attack_config, self.Interfaces, self.ip4network)
try:
result = attack_instance.run()
end_time = time.time()
self.Results.append({'name': attack_name, 'validation': 'success', 'vulnerable': result, 'duration': end_time-start_time})
self.print_debug(f'{attack_name}: {result}')
except RuntimeError as err:
end_time = time.time()
self.Results.append({'name': attack_name, 'validation': f'error: {err}', 'vulnerable': 'unknown', 'duration': end_time-start_time})
self.print_debug(f'{attack_name}: {err}')
self.renew_bindings()
def print_debug(self, text:str):
if self.Verbosity != 0:
print(text)
def print_results(self):
print(tabulate.tabulate(self.Results, headers='keys'))
def teardown(self):
for index, (interface, ip) in enumerate(zip(self.Interfaces, self.ip4network.hosts())):
namespace = f'host{index}'
if self.config.get('dhcp'):
stop_dhclient(interface.name, namespace, type(self).__name__)
pyrouteNetns.remove(namespace)
def check_config(self):
if self.config.get('attacks') is None \
or self.config.get('interfaces') is None:
raise SystemExit('Invalid Config: Mandatory section "attacks" or "interfaces" does not exist.')
def __init__(self, configfile: str = 'config.yaml'):
self.Interfaces = []
self.Results = []
self.config = read_yaml_file(configfile)
if self.config.get('verbosity') is None:
self.Verbosity = 0
scapy.config.conf.verb = 0
else:
self.Verbosity = self.config.get('verbosity')
scapy.config.conf.verb = self.Verbosity
if self.config.get('ip4network') is None:
self.ip4network = ipaddress.ip_network('192.168.3.0/24')
else:
self.ip4network = ipaddress.ip_network(self.config.get('ip4network'))
if self.config.get('dhcp') is None:
self.config['dhcp'] = False
self.initialize()
self.check_base_connectivity()
def __del__(self):
self.teardown()
def main():
l2check = L2CheckSuite('config.yaml')
l2check.run()
l2check.print_results()
if __name__ == '__main__':
main()