summaryrefslogtreecommitdiff
path: root/lectures/07-os-network-security.org
blob: 281cc62bc07b5b80c66281313b1d57b5c1dee76c (about) (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#+title: OS Network Security
#+date: 2026-05-04 Mon
#+author: W. Kosior
#+email: wkosior@agh.edu.pl

* Linux Kernel Network Interfaces
- physical interfaces
  - ethernet
  - wlan
- virtual interfaces
  - bridges
  - virtual ethernet
- tun/tap
- other (e.g., VPN)
  - wireguard
  - ppp

* netfilter (iptables)
- 1999 (kernel 2.3.15)
  - superseded ipchains
    - that superseded ipfwadm (ipfirewall port)
- https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg

* *tables
- iptables
- ip6tables
- arptables
- ebtables

* =iptables= Command Example
#+begin_example
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
#+end_example

* =iptables= Command Example, Cont.
#+begin_example
# iptables -A OUTPUT --proto udp --dport 53 --destination 192.168.1.1 -j ACCEPT
# iptables -t filter -A OUTPUT --proto tcp --dport 443 -j ACCEPT
# iptables -t filter -A OUTPUT --proto tcp --dport 587 -j ACCEPT
# iptables -t filter -A OUTPUT --proto tcp --dport 995 -j ACCEPT
# iptables -t filter -A OUTPUT --proto tcp --dport 993 -j ACCEPT
# iptables -t filter -P OUTPUT DROP
#+end_example

* =iptables= Command Example, Cont…
#+begin_example
# iptables  -L --numeric
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     udp  --  0.0.0.0/0            192.168.1.1          udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:587
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:995
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:993
#+end_example

* iptables Structure
- rules
  - targets
- chains
  - default policies
- *tables*

* =filter= Table
- =INPUT=
- =OUTPUT=
- =FORWARD=
- "traditional" firewall operations

#+begin_example
                              ‍+---------+
in ---> routing decision ---> | FORWARD | -------------------‍+---> out
                |             ‍+---------+                    |
                |                                            |
                V                                            |
            ‍+-------+                          ‍+--------+    |
            | INPUT | ------> application ---> | OUTPUT | ---‍+
            ‍+-------+                          ‍+--------+
#+end_example

* Linux Kernel Packet Forwarding
#+begin_example
# echo 1 > /proc/sys/net/ipv4/ip_forward
#+end_example

#+begin_example
# sysctl -w net.ipv4.ip_forward=1
#+end_example

* Targets & Custom Chains
- standard targets
  - =ACCEPT=
  - =DROP=
  - =QUEUE=
  - user-defined chain
    - =--jump=
    - =--goto=
  - =RETURN= (for user-defined chains)
- target modules targets
  - =LOG=
  - =REJECT=
    - default: =icmp-port-unreachable=
    - =tcp-reset= (=RST/ACK=)

* =REJECT= & Custom Chain Example
#+begin_example
# iptables -N HTTPS-TRAFFIC
# iptables -A HTTPS-TRAFFIC --destination 1.2.3.4 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --destination 5.6.7.8 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --destination 9.10.11.12 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --destination 13.14.15.16 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --destination 17.18.19.20 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --destination 21.22.23.24 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --destination 25.26.27.28 -j ACCEPT
# iptables -A HTTPS-TRAFFIC --proto tcp -j REJECT --reject-with tcp-reset
# iptables -A OUTPUT --proto tcp --destination-port 443 -j HTTPS-TRAFFIC
# iptables -P OUTPUT DROP
#+end_example

* =REJECT= & Custom Chain Example, Cont.
#+begin_example
# iptables -L OUTPUT
Chain OUTPUT (policy DROP)
target     prot opt source               destination
HTTPS-TRAFFIC  tcp  --  anywhere             anywhere             tcp dpt:https
# iptables -L HTTPS-TRAFFIC
Chain HTTPS-TRAFFIC (1 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             1.2.3.4
ACCEPT     all  --  anywhere             5.6.7.8
ACCEPT     all  --  anywhere             9.10.11.12
ACCEPT     all  --  anywhere             13.14.15.16
ACCEPT     all  --  anywhere             17.18.19.20
ACCEPT     all  --  anywhere             21.22.23.24
ACCEPT     all  --  anywhere             25.26.27.28
ACCEPT     all  --  anywhere             29.30.31.32
REJECT     tcp  --  anywhere             anywhere             reject-with tcp-reset
#+end_example

* =nat= Table
- =PREROUTING=
- =OUTPUT=
- =POSTROUTING=
- *only first connection packet*

* Address Translation Targets
- =SNAT=
  - =OUTPUT= & =POSTROUTING=
- =MASQUERADE=
  - =OUTPUT= & =POSTROUTING=
- =DNAT=
  - =PREROUTING=

* Ordinary NAT Example (=MASQUERADE=), Cont.
#+begin_example
# iptables -t nat -A POSTROUTING --out-interface eth0 -j MASQUERADE
# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  anywhere             anywhere
#+end_example

* Ordinary NAT Example (=MASQUERADE=), Cont.
#+begin_example
# iptables -t nat -S
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-A POSTROUTING -o eth0 -j MASQUERADE
#+end_example

* Connection Tracking
#+begin_example
# iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
#+end_example

- protocol + source addr + source port + destination addr + destination port
- =nat= table → first connection packet
- other tables → all packets

* =mangle= Table
- =INPUT=
- =OUTPUT=
- =FORWARD=
- =PREROUTING=
- =POSTROUTING=
- packet header changes
- marks
  - =MARK=
  - =CONNMARK=
  - =SEC*MARK=

* NAT with Exposed Service Example
#+begin_example
# iptables -t nat -A POSTROUTING --out-interface eth0 -j MASQUERADE
# iptables -t nat -A PREROUTING --proto tcp --destination 12.34.56.78 \
>     --dport 587 -j DNAT --to-destination 192.168.7.205:25
# iptables -t mangle -A PREROUTING --in-interface \!eth0  --proto tcp \
>     --destination 12.34.56.78 --dport 587 -j MARK --set-mark 0x400
# iptables -t nat -A POSTROUTING --proto tcp -m mark --mark 0x400 -j MASQUERADE
#+end_example

* NAT with Exposed Service Example, Cont.
#+begin_example
# iptables -t mangle -L | sed 's/ \{8\}/  /g;s/ \{5\}/  /g'
Chain PREROUTING (policy ACCEPT)
target  prot opt source      destination
MARK    tcp  --  anywhere    12.34.56.78    tcp dpt:submission MARK set 0x400

Chain INPUT (policy ACCEPT)
target  prot opt source      destination

Chain FORWARD (policy ACCEPT)
target  prot opt source      destination

Chain OUTPUT (policy ACCEPT)
target  prot opt source      destination

Chain POSTROUTING (policy ACCEPT)
target  prot opt source      destination
#+end_example

* NAT with Exposed Service Example, Cont…
#+begin_example
# iptables -t nat -L | sed 's/\t/        /g;s/  \+/  /g;'
Chain PREROUTING (policy ACCEPT)
target  prot opt source  destination
DNAT  tcp  --  anywhere  12.34.56.78  tcp dpt:submission to:192.168.7.205:25

Chain INPUT (policy ACCEPT)
target  prot opt source  destination

Chain OUTPUT (policy ACCEPT)
target  prot opt source  destination

Chain POSTROUTING (policy ACCEPT)
target  prot opt source  destination
MASQUERADE  all  --  anywhere  anywhere
MASQUERADE  tcp  --  anywhere  anywhere  mark match 0x400
#+end_example

* =raw= Table
- =OUTPUT=
- =REROUTING=
- disabling conntrack

* =security= Table
- =INPUT=
- =OUTPUT=
- =FORWARD=
- specicific marks
  - =SECMARK= & =CONNSECMARK=
  - SELinux
  - initially through =mangle= only

* Packet Couting
#+begin_example
sudo iptables -vL
Chain INPUT (policy ACCEPT 7025 packets, 4955K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 4625 packets, 648K bytes)
 pkts bytes target     prot opt in     out     source               destination
#+end_example

* iptables Rate Limiting Example
#+begin_example
# iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
# iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update \
>     --seconds 120 --hitcount 10 -j DROP
#+end_example

/from: https://www.baeldung.com/linux/iptables-packet-rate-limit/

* iptables Persistence
- iptables-save
- iptables-restore
  - guaranteed atomic

* nftables
- 2014 (kernel TODO)
- VM (like BPF)
- hooks
  - https://people.netfilter.org/pablo/nf-hooks.png

* nftables differences
- no builtin tables
- no builtin chains
- user-created chains attached to *hooks*
- =iptables-legacy=, =iptables-nft=

* Firewalls & NAT in Other OSes
- Packet Filter (BSDs & macOS)
  - originated: OpenBSD
  - superseded ipfw
- IP Filter (Solaris family)
- Windows Firewall

/note: FreeBSD TCP/IP stack as base for macOS & Windows NT/

* DNS
- DHCP/static
- =/etc/resolv.conf=
- resolvers
- DoT, DoH
  - move power from ISP to Google/Cloudflare 🤔
- DNSSEC
  - typical setup: verification by resolver only
  - DANE (with SMTP)
- mDNS

* dnsmasq
- DHCP+TFTP+DNS
- caching
- *filtering/redirections*

* Proxy Setup Approaches
- application-specific (e.g., GUI)
- environment variables
- =LD_PRELOAD=
  - e.g. proxychains tool
  - applications w/out proxy support
  - *NOT* a security measure (bypassable)
- firewall redirection
- eBPF

* TLS & Operating System
- certs supplied by
  - OS
  - adminitrator
  - application (e.g., Mozilla certs)
- kTLS
  - not really relevant here but let's know this exists

* HTTPS proxies
- implementations in dedicated firewall/router hw
  - e.g., FortiGate
- ordinary software
  - e.g., mitmproxy
- policy: all traffic goes through proxy
  - enforcement: block of other traffic (firewall)

* Selected VPN technologies
- IPSec
- PPTP
- *OpenVPN*
- *WireGuard*

* OpenVPN
- TLS
- authentication
  - x.509
  - shared secret
  - login/password
- =tun=​/​=tap=
- TCP/UDP

* WireGuard
- 2020 (kernel 5.6)
  - 2015 (patched kernel)
- in-kernel
  - later: cross-platform clients
- custom protocol
  - public keys (aka SSH)
  - PFS
  - optional additional shared secret
- =wg= interface
- UDP