-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdev_commands.py
More file actions
316 lines (259 loc) · 12.1 KB
/
dev_commands.py
File metadata and controls
316 lines (259 loc) · 12.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
'''Commands used for development.'''
import fcntl
import make
import os
import shutil
import signal
import socket
import struct
import subprocess
import sys
from functools import wraps
from pathlib import Path
from contextlib import contextmanager
import build_variant
@wraps(subprocess.run)
def sh(*args, **kwargs):
return subprocess.run(*args, shell=True, **kwargs)
def run(args: str):
return make.Popen(args.split(),
env={
'PORTABLE': '1',
'LAN': 'enxe8802ee74415'
})
gatekeeper_bin = build_variant.BASE / 'gatekeeper'
def debug():
return run(str(gatekeeper_bin))
def gdb():
return run(f'gdb {gatekeeper_bin} -q -ex run')
def net_reset():
sh('sudo nft delete table gatekeeper')
sh('sudo ip addr flush dev veth0a')
sh('sudo ip netns exec ns0 ip addr flush dev veth0b')
sh('sudo dhclient -x')
sh('sudo rm -f /etc/dhcp/dhclient-enter-hooks.d/test-dhclient-hook')
def dogfood():
'''Copy the binary to maf's router and run it.'''
sh(f'scp {gatekeeper_bin} root@protectli:/opt/gatekeeper/gatekeeper.new',
check=True)
try:
sh('ssh root@protectli "mv /opt/gatekeeper/gatekeeper{,.old} && mv /opt/gatekeeper/gatekeeper{.new,} && systemctl restart gatekeeper"',
check=True)
except subprocess.CalledProcessError:
print('Failed to restart gatekeeper. Reverting changes.')
sh('ssh root@protectli "mv /opt/gatekeeper/gatekeeper{.old,} && systemctl restart gatekeeper"',
check=True)
@contextmanager
def dns_blocker():
'''
Start a "blocker" of port 53.
Gatekeeper should kill it during startup.
'''
blocker = subprocess.Popen(['nc', '-lup', '53'])
try:
yield blocker
finally:
blocker.send_signal(signal.SIGTERM)
@contextmanager
def run_systemd(env):
'''
Run Gatekeeper as a systemd service.
'''
# This seems to mess with the DHCP server used by Ubuntu's NetworkManager
# TODO: figure out why this happens (Wireshark) & fix it
subprocess.check_call(['systemctl', 'reset-failed'])
args = ['systemd-run', '--service-type=notify', '--same-dir', '--unit=gatekeeper-e2e', '--quiet']
for k, v in env.items():
args.append(f'--setenv={k}={v}')
args += [str(gatekeeper_bin)]
p = subprocess.run(args)
p.invocation_id = subprocess.check_output(['systemctl', 'show', '--value', '-p', 'InvocationID', 'gatekeeper-e2e']).decode().strip()
if p.returncode != 0:
print('Gatekeeper failed to start. Status code: ', p.returncode)
print('Gatekeeper log:')
subprocess.run(['journalctl', '_SYSTEMD_INVOCATION_ID=' + p.invocation_id])
sys.exit(1)
try:
print('Use "journalctl _SYSTEMD_INVOCATION_ID=' + p.invocation_id + ' -o cat" to see Gatekeeper logs')
yield p
finally:
subprocess.run(['systemctl', 'stop', 'gatekeeper-e2e'])
# If Gatekeeper crashes it may leave its nft table active.
# This breaks the internet on the test machine.
# So we delete it here.
subprocess.run(['nft', 'delete', 'table', 'gatekeeper'], stderr=subprocess.DEVNULL)
subprocess.run(['ip', 'link', 'set', 'lan', 'down'], stderr=subprocess.DEVNULL)
subprocess.run(['brctl', 'delbr', 'lan'], stderr=subprocess.DEVNULL)
@contextmanager
def run_dhclient(namespace, interface):
if not os.path.exists('./tests/dhclient'):
shutil.copyfile('/sbin/dhclient', './tests/dhclient')
shutil.copymode('/sbin/dhclient', './tests/dhclient')
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', namespace, './tests/dhclient', '-1', '-cf', './tests/dhclient.conf', '-sf', './tests/dhclient-script', '-pf', './tests/dhclient.pid', interface])
try:
yield
finally:
subprocess.call(['sudo', 'ip', 'netns', 'exec', namespace, './tests/dhclient', '-cf', './tests/dhclient.conf', '-sf', './tests/dhclient-script', '-pf', './tests/dhclient.pid', '-x', interface], stderr=subprocess.DEVNULL)
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname.encode()[:15])
)[20:24])
except OSError:
return None
def setup_veth_namespace(i):
NS = f'ns{i}'
A = f'veth{i}a'
B = f'veth{i}b'
if not os.path.exists(f'/run/netns/{NS}'):
subprocess.check_call(['sudo', 'ip', 'netns', 'add', NS])
if not os.path.exists(f'/sys/class/net/{A}'):
subprocess.check_call(['sudo', 'ip', 'link', 'add', A, 'type', 'veth', 'peer', 'name', B, 'netns', NS])
subprocess.check_call(['sudo', 'ip', 'addr', 'flush', 'dev', A])
subprocess.check_call(['sudo', 'ip', 'link', 'set', A, 'down'])
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, 'ip', 'addr', 'flush', 'dev', B])
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, 'ip', 'link', 'set', B, 'down'])
# Setup fake resolv.conf for our network namespaces.
# See man ip-netns for reference.
# TL;DR is that empty file is enough for `ip netns` to bind mount it over /etc/resolv.conf
# This file will be filled by `test-dhclient-hook`
subprocess.check_call(['sudo', 'mkdir', '-p', f'/etc/netns/{NS}'])
subprocess.check_call(['sudo', 'touch', f'/etc/netns/{NS}/resolv.conf'])
return NS, A, B
def test_env():
if os.geteuid() != 0:
raise Exception('This script must be run as root')
for i in range(4):
setup_veth_namespace(i)
# Start Gatekeeper
env = {'LAN': ' '.join([f'veth{i}a' for i in range(4)])}
with run_systemd(env), run_dhclient('ns0', 'veth0b'):
input('Press Enter to continue...')
def test_e2e():
if os.geteuid() != 0:
raise Exception('This script must be run as root')
for i in range(4):
setup_veth_namespace(i)
# Start Gatekeeper
env = {'LAN': ' '.join([f'veth{i}a' for i in range(4)])}
with dns_blocker() as blocker, run_systemd(env), run_dhclient('ns0', 'veth0b'):
# Collect test results
GATEKEEPER_IP = get_ip_address('lan')
if GATEKEEPER_IP is None:
print('DHCP issue. Gatekeeper IP is None')
sys.exit(1)
CLIENT_IP = subprocess.check_output("ip netns exec ns0 ip route get 1 | awk '{print $(NF-2);exit}'", shell=True).decode().strip()
TEST_DOMAIN = 'www.google.com'
CURL_EXAMPLE_STATUS = subprocess.call(['ip', 'netns', 'exec', 'ns0', 'curl', '-v', '--no-progress-meter', '--connect-timeout', '5', '--max-time', '10', TEST_DOMAIN], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
CURL_1337 = subprocess.check_output(['ip', 'netns', 'exec', 'ns0', 'curl', '-s', 'http://' + GATEKEEPER_IP + ':1337']).decode().strip()
NFT_RULES = subprocess.check_output(['nft', 'list', 'ruleset']).decode().strip()
if blocker.poll() == None: # DNS blocker is still running
print('Startup issue. Gatekeeper failed to kill another process listening on DNS port')
sys.exit(1)
# replace the last segment of $GATEKEEPER_IP with '2'
EXPECTED_CLIENT_IP = '.'.join(GATEKEEPER_IP.split('.')[:-1]) + '.2'
if CLIENT_IP != EXPECTED_CLIENT_IP:
print('DHCP issue. Client IP is [{}] but expected [{}].'.format(CLIENT_IP, EXPECTED_CLIENT_IP))
sys.exit(1)
if 'Gatekeeper' not in CURL_1337:
print('Web UI issue. http://{}:1337 should contain [Gatekeeper]. Got [{}].'.format(GATEKEEPER_IP, CURL_1337))
sys.exit(1)
if CURL_EXAMPLE_STATUS != 0:
print('DNS / NAT issue. Curl {} should return status code 0 but returned {}.'.format(TEST_DOMAIN, CURL_EXAMPLE_STATUS))
print('Netfilter rules (`nft list ruleset`):')
print(NFT_RULES)
sys.exit(1)
def test_outgoing():
'''Verify that FullConeNAT entries don't hijack gateway's own connections.
Reproduces the bug where:
1. LAN client connects to the internet, creating a FullConeNAT entry for port P
2. Gateway itself connects to the internet using port P
3. The response is hijacked by gatekeeper and sent to the LAN client
'''
if os.geteuid() != 0:
raise Exception('This script must be run as root')
COLLISION_PORT = 55555
for i in range(4):
setup_veth_namespace(i)
env = {'LAN': ' '.join([f'veth{i}a' for i in range(4)])}
with run_systemd(env), run_dhclient('ns0', 'veth0b'):
GATEKEEPER_IP = get_ip_address('lan')
if GATEKEEPER_IP is None:
print('DHCP issue. Gatekeeper IP is None')
sys.exit(1)
# Step 1: LAN client makes a connection from COLLISION_PORT.
# This populates FullConeNAT[TCP][COLLISION_PORT] = <ns0 IP>.
# We use curl with --local-port to control the source port.
rc = subprocess.call(
['ip', 'netns', 'exec', 'ns0', 'curl', '-s', '-o', '/dev/null',
'--local-port', str(COLLISION_PORT),
'--connect-timeout', '5', '--max-time', '10',
'http://www.google.com/'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if rc != 0:
print(f'Setup issue. LAN client curl failed with status {rc}')
sys.exit(1)
# Step 2: Gateway itself connects from the same port.
# The outgoing SYN bypasses gatekeeper (source IP is wan_ip, not 10.x/16).
# But the SYN-ACK response will be hijacked by the stale FullConeNAT entry.
rc = subprocess.call(
['curl', '-s', '-o', '/dev/null',
'--local-port', str(COLLISION_PORT),
'--connect-timeout', '5', '--max-time', '10',
'http://www.google.com/'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if rc != 0:
print(f'FAIL: Gateway outgoing connection failed (status {rc}) — response was likely hijacked by FullConeNAT')
sys.exit(1)
def test_dhcp():
NS, A, B = setup_veth_namespace(0)
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, 'ip', 'link', 'set', B, 'up'])
with run_systemd({'LAN': A}):
# Start dhammer
# 200 requests / second over 10 seconds
# Linux has limits for its ARP table size.
# See /proc/sys/net/ipv4/neigh/default/gc_thresh{1,2,3}.
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, './tests/dhammer.v2.0.0.linux.amd64', 'dhcpv4', '--interface', B, '--mac-count', '1000', '--rps', '200', '--maxlife', '10'])
def test_dns():
NS, A, B = setup_veth_namespace(0)
with run_systemd({'LAN': A}), run_dhclient(NS, B):
ip = get_ip_address(A)
# Start dnsblast
# 1000 queries at a rate of 100 QPS.
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, './tests/dnsblast.linux.amd64', ip, '1000', '100'])
def test_tcp():
NS, A, B = setup_veth_namespace(0)
with run_systemd({'LAN': A}), run_dhclient(NS, B):
ip = get_ip_address(A)
# Start iperf server
iperf = subprocess.Popen(['iperf3', '-s', '-B', f'{ip}%{A}'])
# Start iperf client
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, 'iperf3', '-c', ip, '-t', '10'])
# Stop iperf server
iperf.kill()
def test_udp():
NS, A, B = setup_veth_namespace(0)
with run_systemd({'LAN': A}), run_dhclient(NS, B):
ip = get_ip_address(A)
# Start iperf server
iperf = subprocess.Popen(['iperf3', '-s', '-B', f'{ip}%{A}'])
# Start iperf client
subprocess.check_call(['sudo', 'ip', 'netns', 'exec', NS, 'iperf3', '-c', ip, '-u', '-b', '1000M', '-t', '10'])
# Stop iperf server
iperf.kill()
def hook_final(srcs, objs, bins, recipe: make.Recipe):
deps = [str(gatekeeper_bin)]
recipe.add_step(debug, [], deps)
recipe.add_step(gdb, [], deps)
recipe.add_step(net_reset, [], deps)
recipe.add_step(test_env, [], deps)
recipe.add_step(test_e2e, [], deps)
recipe.add_step(test_outgoing, [], deps)
recipe.add_step(test_dhcp, [], deps)
recipe.add_step(test_dns, [], deps)
recipe.add_step(test_tcp, [], deps)
recipe.add_step(test_udp, [], deps)
recipe.add_step(dogfood, [], deps)