forked from tailscale/wf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirewall_test.go
643 lines (575 loc) · 16 KB
/
firewall_test.go
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright (c) 2021 The Inet.Af AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wf
import (
"os"
"sort"
"syscall"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"golang.org/x/sys/windows"
)
func skipIfUnprivileged(t *testing.T) {
if !windows.GetCurrentProcessToken().IsElevated() {
if os.Getenv("CI") != "" {
t.Fatal("test requires admin privileges")
}
t.Skipf("skipping test that requires admin privileges")
}
}
func TestSession(t *testing.T) {
skipIfUnprivileged(t)
tests := []struct {
name string
opts *Options
}{
{
name: "nil",
opts: nil,
},
{
name: "name_only",
opts: &Options{
Name: "test",
},
},
{
name: "name_and_desc",
opts: &Options{
Name: "test2",
Description: "unit test session",
},
},
{
name: "dynamic",
opts: &Options{
Name: "test2",
Dynamic: true,
},
},
{
name: "tx_timeout",
opts: &Options{
Name: "test2",
TransactionStartTimeout: 5 * time.Minute,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
sess, err := New(test.opts)
if err != nil {
t.Fatalf("failed to open session: %v", err)
}
if err := sess.Close(); err != nil {
t.Errorf("closing session: %v", err)
}
})
}
}
func TestLayers(t *testing.T) {
skipIfUnprivileged(t)
s, err := New(nil)
if err != nil {
t.Fatal(err)
}
defer s.Close()
layers, err := s.Layers()
if err != nil {
t.Fatalf("getting layers: %v", err)
}
// Try to find a couple of the well-known layers that Windows
// should definitely have.
wantLayers := map[LayerID]*Layer{
LayerALEAuthRecvAcceptV4: {
ID: LayerALEAuthRecvAcceptV4,
KernelID: 44,
Name: "ALE Receive/Accept v4 Layer",
DefaultSublayer: guidSublayerUniversal,
Fields: []*Field{
{FieldALEAppID, typeString},
{FieldALENAPContext, typeUint32},
{FieldALEPackageID, typeSID},
{FieldALERemoteMachineID, typeSecurityDescriptor},
{FieldALERemoteUserID, typeSecurityDescriptor},
{FieldALESecurityAttributeFqbnValue, typeBytes},
{FieldALESioFirewallSystemPort, typeUint32},
{FieldALEUserID, typeSecurityDescriptor},
{FieldArrivalInterfaceIndex, typeUint32},
{FieldArrivalInterfaceType, typeUint32},
{FieldArrivalTunnelType, typeUint32},
{FieldCompartmentID, typeUint32},
{FieldCurrentProfileID, typeUint32},
{FieldFlags, typeUint32},
{FieldInterfaceIndex, typeUint32},
{FieldInterfaceQuarantineEpoch, typeUint64},
{FieldInterfaceType, typeUint32},
{FieldIPArrivalInterface, typeUint64},
{FieldIPLocalAddress, typeIP},
{FieldIPLocalAddressType, typeUint8},
{FieldIPLocalInterface, typeUint64},
{FieldIPLocalPort, typeUint16},
{FieldIPNexthopInterface, typeUint64},
{FieldIPProtocol, typeUint8},
{FieldIPRemoteAddress, typeIP},
{FieldIPRemotePort, typeUint16},
{FieldNexthopInterfaceIndex, typeUint32},
{FieldNexthopInterfaceType, typeUint32},
{FieldNexthopSubInterfaceIndex, typeUint32},
{FieldNexthopTunnelType, typeUint32},
{FieldOriginalICMPType, typeUint16},
{FieldOriginalProfileID, typeUint32},
{FieldReauthorizeReason, typeUint32},
{FieldSubInterfaceIndex, typeUint32},
{FieldTunnelType, typeUint32},
},
},
LayerStreamV4Discard: {
ID: LayerStreamV4Discard,
KernelID: 21,
Name: "Stream v4 Discard Layer",
DefaultSublayer: guidSublayerUniversal,
Fields: []*Field{
{FieldCompartmentID, typeUint32},
{FieldDirection, typeUint32},
{FieldFlags, typeUint32},
{FieldIPLocalAddress, typeIP},
{FieldIPLocalAddressType, typeUint8},
{FieldIPLocalPort, typeUint16},
{FieldIPRemoteAddress, typeIP},
{FieldIPRemotePort, typeUint16},
},
},
}
for guid, want := range wantLayers {
found := false
for _, got := range layers {
if got.ID != guid {
continue
}
found = true
sort.Slice(got.Fields, func(i, j int) bool {
return got.Fields[i].ID.String() < got.Fields[j].ID.String()
})
fieldCmp := func(a, b *Field) bool {
return a.ID == b.ID && a.Type == b.Type
}
if diff := cmp.Diff(got, want, cmp.Comparer(fieldCmp)); diff != "" {
t.Errorf("unexpected layer def (-got+want):\n%s", diff)
}
break
}
if !found {
t.Errorf("layer %s (%s) not found", guid, windows.GUID(guid))
}
}
}
func TestSublayers(t *testing.T) {
skipIfUnprivileged(t)
s, err := New(&Options{
Dynamic: true,
})
if err != nil {
t.Fatal(err)
}
defer s.Close()
guid, err := windows.GenerateGUID()
if err != nil {
t.Fatal(err)
}
sl := &Sublayer{
ID: SublayerID(guid),
Name: "test sublayer",
Description: "a test sublayer",
ProviderData: []byte("byte blob"),
Weight: 0x4242,
}
if err := s.AddSublayer(sl); err != nil {
t.Fatalf("add sublayer failed: %v", err)
}
sublayers, err := s.Sublayers(ProviderID{})
if err != nil {
t.Fatalf("get sublayers failed: %v", err)
}
found := false
for _, got := range sublayers {
if got.ID != sl.ID {
continue
}
found = true
if diff := cmp.Diff(got, sl); diff != "" {
t.Fatalf("sublayer is wrong (-got+want):\n%s", diff)
}
break
}
if !found {
t.Fatal("sublayer added but not found")
}
if err := s.DeleteSublayer(sl.ID); err != nil {
t.Fatalf("delete sublayer failed: %v", err)
}
sublayers, err = s.Sublayers(ProviderID{})
if err != nil {
t.Fatalf("get sublayers failed: %v", err)
}
for _, got := range sublayers {
if got.ID == sl.ID {
t.Fatalf("deleted sublayer but it's still there: %#v", got)
}
}
}
func TestProviders(t *testing.T) {
skipIfUnprivileged(t)
s, err := New(&Options{
Dynamic: true,
})
if err != nil {
t.Fatal(err)
}
defer s.Close()
guid, err := windows.GenerateGUID()
if err != nil {
t.Fatal(err)
}
p := &Provider{
ID: ProviderID(guid),
Name: "test provider",
Description: "a test provider",
Data: []byte("byte blob"),
}
if err := s.AddProvider(p); err != nil {
t.Fatalf("add provider failed: %v", err)
}
providers, err := s.Providers()
if err != nil {
t.Fatalf("get providers failed: %v", err)
}
found := false
for _, got := range providers {
if got.ID != p.ID {
continue
}
found = true
if diff := cmp.Diff(got, p); diff != "" {
t.Fatalf("provider is wrong (-got+want):\n%s", diff)
}
break
}
if !found {
t.Fatal("provider added but not found")
}
if err := s.DeleteProvider(p.ID); err != nil {
t.Fatalf("delete provider failed: %v", err)
}
providers, err = s.Providers()
if err != nil {
t.Fatalf("get providers failed: %v", err)
}
for _, got := range providers {
if got.ID == p.ID {
t.Fatalf("deleted provider but it's still there: %#v", got)
}
}
}
func TestFilter(t *testing.T) {
// Create a new dynamic session
s, err := New(&Options{
Dynamic: true,
})
if err != nil {
t.Fatal(err)
}
defer s.Close()
guid, err := windows.GenerateGUID()
if err != nil {
t.Fatal(err)
}
// Create a new provider
p := &Provider{
ID: ProviderID(guid),
Name: "test provider",
Description: "a test provider",
Data: []byte("byte blob"),
}
if err := s.AddProvider(p); err != nil {
t.Fatalf("add provider failed: %v", err)
}
rule_id, err := windows.GenerateGUID()
if err != nil {
t.Fatal(err)
}
// Create a rule so we can test enumeration
r := Rule{
ID: RuleID(rule_id),
Name: "inetaf-wf-test-rule",
Description: "A disabled rule for testing the inet.af/wf library",
Layer: LayerALEAuthConnectV4,
Weight: 0x4242,
Conditions: []*Match{
{
Field: FieldALEAppID,
Op: MatchTypeEqual,
Value: "test",
},
},
Action: ActionPermit,
Persistent: false,
BootTime: false,
Provider: ProviderID(guid),
Disabled: true,
}
if err := s.AddRule(&r); err != nil {
t.Fatal(err)
}
// Filter for rules from our provider
rules, err := s.EnumerateRules(FilterEnumTypeOverlapping, LayerALEAuthConnectV4).
WithProvider(ProviderID(guid)).
WithActionMask(ActionFlagIgnore).
WithFlags(FilterEnumFlagsSorted).
Execute()
if err != nil {
t.Fatal(err)
} else if len(rules) != 1 {
t.Fatalf("expected 1 rule in filter, but found %v", len(rules))
} else if rules[0].ID != RuleID(rule_id) {
t.Fatalf("wrong rule enumerated (expected %v; got %v)", rule_id, rules[0].ID)
}
}
func TestTransactionSessionClosed(t *testing.T) {
// Attempt to open a new WFP session
s, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: true,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
status := s.TransactionStatus()
if status.Err != nil {
t.Fatalf("transaction err: %v", status.Err)
}
if status.State != BeganTransaction {
t.Fatalf("transaction in wrong state: %v", status.State.String())
}
// This should abort the transaction
s.Close()
status = s.TransactionStatus()
if status.Err != nil {
t.Fatalf("unexpected transaction err: %v", status.Err)
}
if status.State != AbortedTransaction {
t.Fatalf("transaction in wrong state: %v", status.State.String())
}
// We should not be able to commit a transaction
s.CommitTransaction()
status = s.TransactionStatus()
if status.Err != syscall.Errno(NilPointer) {
t.Fatalf("unexpected transaction err: %v", status.Err)
}
if status.State != AbortedTransaction {
t.Fatalf("Transaction in wrong state: %v", status.State.String())
}
// We should not be able to abort a transaction
s.AbortTransaction()
status = s.TransactionStatus()
if status.Err != syscall.Errno(NilPointer) {
t.Fatalf("unexpected transaction err: %v", status.Err)
}
if status.State != AbortedTransaction {
t.Fatalf("Transaction in wrong state: %v", status.State.String())
}
}
func TestTransactionNeverStarted(t *testing.T) {
// Attempt to open a new WFP session
s, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: false,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
status := s.TransactionStatus()
if status.Err != nil {
t.Fatalf("transaction err: %v", status.Err)
}
if status.State != NoTransaction {
t.Fatalf("transaction in wrong state: %v", status.State.String())
}
// We should not be able to commit a transaction
s.CommitTransaction()
status = s.TransactionStatus()
if status.Err != syscall.Errno(NoTransactionInProgress) {
t.Fatalf("unexpected transaction err: %v", status.Err)
}
if status.State != NoTransaction {
t.Fatalf("Transaction in wrong state: %v", status.State.String())
}
// We should not be able to abort a transaction
s.AbortTransaction()
status = s.TransactionStatus()
if status.Err != syscall.Errno(NoTransactionInProgress) {
t.Fatalf("unexpected transaction err: %v", status.Err)
}
if status.State != NoTransaction {
t.Fatalf("Transaction in wrong state: %v", status.State.String())
}
}
func TestBlockedTransaction(t *testing.T) {
// Attempt to open a new WFP session
b, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: true,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
defer func() {
b.Close()
if b.TransactionStatus().State != AbortedTransaction {
t.Fatal("Transaction should now be in the aborted state")
}
}()
status := b.TransactionStatus()
if status.Err != nil {
t.Fatalf("transaction err: %v", status.Err)
}
if status.State != BeganTransaction {
t.Fatalf("transaction in wrong state: %v", status.State.String())
}
// We know we're going to timeout, so keep Timeout value 1 millisecond
// DON'T SET IT TO ZERO, THE DEFAULT TIMEOUT IS VERY LONG
_, err = New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: false,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: time.Millisecond * 1,
})
if err != syscall.Errno(Timeout) {
t.Fatalf("unexpected transaction err: %v", err)
}
}
func TestBeginTransactionAlreadyInProgress(t *testing.T) {
// Create a session that is started in a transaction
s, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: true,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
defer func() {
s.Close()
if s.TransactionStatus().State != AbortedTransaction {
t.Fatal("Transaction should now be in the aborted state")
}
}()
s.BeginTransaction(TransactionReadWrite)
if s.TransactionStatus().Err != syscall.Errno(TransactionInProgress) {
t.Fatal("Wrong error code.")
}
}
func TestTransactionCommitStarted(t *testing.T) {
// Create a session that is started in a transaction
s, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: true,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
defer func() {
s.Close()
if s.TransactionStatus().State != CommittedTransaction {
t.Fatal("Transaction should now be in the committed state")
}
}()
if s.TransactionStatus().State != BeganTransaction {
t.Fatal("Could not begin transaction")
}
s.CommitTransaction()
if s.TransactionStatus().State != CommittedTransaction {
t.Fatal("Could not commit transaction")
}
}
func TestTransactionBeginCommit(t *testing.T) {
// Create a session that is started in a transaction
s, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: false,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
defer func() {
s.Close()
if s.TransactionStatus().State != CommittedTransaction {
t.Fatal("Transaction should now be in the committed state")
}
}()
if s.TransactionStatus().State != NoTransaction {
t.Fatal("Should be in a non-transacted state")
}
s.BeginTransaction(TransactionReadWrite)
if s.TransactionStatus().State != BeganTransaction {
t.Fatal("Could not begin transaction")
}
s.CommitTransaction()
if s.TransactionStatus().State != CommittedTransaction {
t.Fatal("Could not commit transaction")
}
}
func TestTransactionAbort(t *testing.T) {
// Create a session that is started in a transaction
s, err := New(&Options{
Name: "huntress-isolation",
Description: "Huntress Isolation Context Session",
Dynamic: false,
StartTransaction: true,
TransactionFlags: TransactionReadWrite,
TransactionStartTimeout: 15 * time.Second,
})
if err != nil {
t.Fatal("unable to start a new session")
}
defer func() {
s.Close()
if s.TransactionStatus().State != AbortedTransaction {
t.Fatal("Transaction should now be in the aborted state")
}
}()
if s.TransactionStatus().State != BeganTransaction {
t.Fatal("Could not begin transaction")
}
s.AbortTransaction()
if s.TransactionStatus().State != AbortedTransaction {
t.Fatal("Could not abort transaction")
}
}