Debian Codegraph @

wfp/wfp_windows.go

raw
1 //go:build (amd64 || arm64) && !e2e_testing
2 // +build amd64 arm64
3 // +build !e2e_testing
4
5 // Package wfp installs Windows Filtering Platform (WFP) PERMIT filters in a dynamic, session-scoped sublayer.
6 // Because WFP sits below Windows Defender Firewall, a high-weight permit at FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4/V6 lets
7 // the matching inbound traffic through regardless of WDF rules.
8 //
9 // Each Session owns its own engine handle. When the handle closes, every dynamic object added during the session
10 // is auto-deleted by Windows, so there are no orphaned filters.
11 //
12 // Type definitions and constants are derived from the wireguard-windows firewall package (MIT).
13 // Only the subset we exercise is reproduced.
14 package wfp
15
16 import (
17 "fmt"
18 "unsafe"
19
20 "golang.org/x/sys/windows"
21 )
22
23 // FWPM layer GUIDs (fwpmu.h).
24 //
25 // FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4 = e1cd9fe7-f4b5-4273-96c0-592e487b8650
26 // FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V6 = a3b42c97-9f04-4672-b87e-cee9c483257f
27 var (
28 fwpmLayerAleAuthRecvAcceptV4 = windows.GUID{
29 Data1: 0xe1cd9fe7, Data2: 0xf4b5, Data3: 0x4273,
30 Data4: [8]byte{0x96, 0xc0, 0x59, 0x2e, 0x48, 0x7b, 0x86, 0x50},
31 }
32 fwpmLayerAleAuthRecvAcceptV6 = windows.GUID{
33 Data1: 0xa3b42c97, Data2: 0x9f04, Data3: 0x4672,
34 Data4: [8]byte{0xb8, 0x7e, 0xce, 0xe9, 0xc4, 0x83, 0x25, 0x7f},
35 }
36 )
37
38 // FWPM_CONDITION_IP_LOCAL_INTERFACE = 4cd62a49-59c3-4969-b7f3-bda5d32890a4
39 var fwpmConditionIPLocalInterface = windows.GUID{
40 Data1: 0x4cd62a49, Data2: 0x59c3, Data3: 0x4969,
41 Data4: [8]byte{0xb7, 0xf3, 0xbd, 0xa5, 0xd3, 0x28, 0x90, 0xa4},
42 }
43
44 // FWPM_CONDITION_IP_PROTOCOL = 3971ef2b-623e-4f9a-8cb1-6e79b806b9a7
45 var fwpmConditionIPProtocol = windows.GUID{
46 Data1: 0x3971ef2b, Data2: 0x623e, Data3: 0x4f9a,
47 Data4: [8]byte{0x8c, 0xb1, 0x6e, 0x79, 0xb8, 0x06, 0xb9, 0xa7},
48 }
49
50 // FWPM_CONDITION_IP_LOCAL_PORT = 0c1ba1af-5765-453f-af22-a8f791ac775b
51 var fwpmConditionIPLocalPort = windows.GUID{
52 Data1: 0x0c1ba1af, Data2: 0x5765, Data3: 0x453f,
53 Data4: [8]byte{0xaf, 0x22, 0xa8, 0xf7, 0x91, 0xac, 0x77, 0x5b},
54 }
55
56 // IPPROTO_UDP from in.h.
57 const ipprotoUDP uint8 = 17
58
59 // FWP_ACTION_TYPE values (fwptypes.h). PERMIT is terminating.
60 const fwpActionPermit uint32 = 0x00001002 // 0x2 | FWP_ACTION_FLAG_TERMINATING(0x1000)
61
62 // FWP_DATA_TYPE values we use.
63 const (
64 fwpEmpty uint32 = 0
65 fwpUint8 uint32 = 1
66 fwpUint16 uint32 = 2
67 fwpUint64 uint32 = 4
68 )
69
70 // FWP_MATCH_TYPE values.
71 const fwpMatchEqual uint32 = 0
72
73 // FWPM_SESSION flags.
74 const fwpmSessionFlagDynamic uint32 = 0x1
75
76 // FWPM_FILTER_FLAG_CLEAR_ACTION_RIGHT prevents lower-priority filters in other sublayers,
77 // notably Windows Defender Firewall's MPSSVC_WF sublayer, which shares our 0xFFFF weight from overriding this PERMIT.
78 // Without it, a default WDF block at the same sublayer weight can still win arbitration.
79 const fwpmFilterFlagClearActionRight uint32 = 0x8
80
81 // RPC authentication.
82 // RPC_C_AUTHN_WINNT works on workgroup machines with no domain context
83 // RPC_C_AUTHN_DEFAULT falls back through a chain that can land on something WFP doesn't accept on a fresh box.
84 const rpcCAuthnWinNT uint32 = 10
85
86 // fwpByteBlob (FWP_BYTE_BLOB). 16 bytes on 64-bit.
87 type fwpByteBlob struct {
88 size uint32
89 _ uint32 // padding
90 data *uint8
91 }
92
93 // fwpValue0 / FWP_CONDITION_VALUE0 layout. 16 bytes on 64-bit.
94 // The union is pointer-sized; types <= 32 bits (UINT8/16/32, INT8/16/32, float) live inline in the low bytes
95 // of `value`, while UINT64/INT64/double and aggregate types are stored *by pointer*, even on 64-bit, where the
96 // union member is declared as UINT64*. So when populating an FWP_UINT64 condition, pass
97 // uintptr(unsafe.Pointer(&luidVar)) instead of the LUID inline.
98 type fwpValue0 struct {
99 type_ uint32
100 _ uint32 // padding before union to 8-byte alignment
101 value uintptr
102 }
103
104 // fwpmDisplayData0 / FWPM_DISPLAY_DATA0. 16 bytes on 64-bit.
105 type fwpmDisplayData0 struct {
106 name *uint16
107 description *uint16
108 }
109
110 // fwpmAction0 / FWPM_ACTION0. 20 bytes; no leading padding because actionType
111 // is uint32 and GUID's first field is uint32.
112 type fwpmAction0 struct {
113 actionType uint32
114 filterType windows.GUID
115 }
116
117 // fwpmFilterCondition0. 40 bytes on 64-bit.
118 type fwpmFilterCondition0 struct {
119 fieldKey windows.GUID // 16
120 matchType uint32 // 4
121 _ uint32 // 4 padding
122 conditionValue fwpValue0 // 16
123 }
124
125 // fwpmFilter0. 200 bytes on 64-bit.
126 type fwpmFilter0 struct {
127 filterKey windows.GUID
128 displayData fwpmDisplayData0
129 flags uint32
130 _ uint32 // padding before *GUID
131 providerKey *windows.GUID
132 providerData fwpByteBlob
133 layerKey windows.GUID
134 subLayerKey windows.GUID
135 weight fwpValue0
136 numFilterConditions uint32
137 _ uint32 // padding before pointer
138 filterCondition *fwpmFilterCondition0
139 action fwpmAction0
140 _ [4]byte // layout correction
141 providerContextKey windows.GUID
142 reserved *windows.GUID
143 filterID uint64
144 effectiveWeight fwpValue0
145 }
146
147 // fwpmSublayer0. 72 bytes on 64-bit.
148 type fwpmSublayer0 struct {
149 subLayerKey windows.GUID
150 displayData fwpmDisplayData0
151 flags uint32
152 _ uint32 // padding before *GUID
153 providerKey *windows.GUID
154 providerData fwpByteBlob
155 weight uint16
156 _ [6]byte // padding to 72 bytes
157 }
158
159 // fwpmSession0. 72 bytes on 64-bit.
160 type fwpmSession0 struct {
161 sessionKey windows.GUID
162 displayData fwpmDisplayData0
163 flags uint32
164 txnWaitTimeoutInMSec uint32
165 processId uint32
166 _ uint32 // padding before *SID
167 sid *windows.SID
168 username *uint16
169 kernelMode uint8
170 _ [7]byte // tail padding
171 }
172
173 // fwpuclnt.dll bindings. Only the calls we use.
174 var (
175 modFwpuclnt = windows.NewLazySystemDLL("fwpuclnt.dll")
176 procFwpmEngineOpen0 = modFwpuclnt.NewProc("FwpmEngineOpen0")
177 procFwpmEngineClose0 = modFwpuclnt.NewProc("FwpmEngineClose0")
178 procFwpmSubLayerAdd0 = modFwpuclnt.NewProc("FwpmSubLayerAdd0")
179 procFwpmFilterAdd0 = modFwpuclnt.NewProc("FwpmFilterAdd0")
180 )
181
182 // Session holds the WFP engine handle for a single bypass operation. The handle owns a dynamic session:
183 // when it is closed, every WFP object added during the session (sublayer + filters) is automatically deleted by
184 // Windows. That gives us correct cleanup even if the host process is killed hard between Permit* and Close.
185 type Session struct {
186 engine uintptr
187 }
188
189 // Close releases the engine handle. Windows deletes every dynamic object (sublayer + filters) the session installed.
190 // Safe to call on a nil receiver.
191 func (s *Session) Close() {
192 if s == nil || s.engine == 0 {
193 return
194 }
195 procFwpmEngineClose0.Call(s.engine)
196 s.engine = 0
197 }
198
199 // PermitInterface installs PERMIT filters at FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4 and _V6 scoped to the given network
200 // interface LUID. Inbound traffic on that interface bypasses Windows Defender Firewall.
201 func PermitInterface(luid uint64) (*Session, error) {
202 s, sublayerKey, err := newSession()
203 if err != nil {
204 return nil, err
205 }
206
207 if err := addInterfaceFilter(s.engine, sublayerKey, fwpmLayerAleAuthRecvAcceptV4, luid); err != nil {
208 s.Close()
209 return nil, fmt.Errorf("add v4 filter: %w", err)
210 }
211 if err := addInterfaceFilter(s.engine, sublayerKey, fwpmLayerAleAuthRecvAcceptV6, luid); err != nil {
212 s.Close()
213 return nil, fmt.Errorf("add v6 filter: %w", err)
214 }
215 return s, nil
216 }
217
218 // PermitUDPPort installs PERMIT filters at FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4 and _V6 scoped to UDP traffic with the
219 // given local port. Inbound UDP to that port on any interface bypasses Windows Defender Firewall.
220 func PermitUDPPort(port uint16) (*Session, error) {
221 s, sublayerKey, err := newSession()
222 if err != nil {
223 return nil, err
224 }
225
226 if err := addUDPPortFilter(s.engine, sublayerKey, fwpmLayerAleAuthRecvAcceptV4, port); err != nil {
227 s.Close()
228 return nil, fmt.Errorf("add v4 filter: %w", err)
229 }
230 if err := addUDPPortFilter(s.engine, sublayerKey, fwpmLayerAleAuthRecvAcceptV6, port); err != nil {
231 s.Close()
232 return nil, fmt.Errorf("add v6 filter: %w", err)
233 }
234 return s, nil
235 }
236
237 func newSession() (*Session, windows.GUID, error) {
238 engine, err := openDynamicEngine()
239 if err != nil {
240 return nil, windows.GUID{}, err
241 }
242 sublayerKey, err := registerSublayer(engine)
243 if err != nil {
244 procFwpmEngineClose0.Call(engine)
245 return nil, windows.GUID{}, err
246 }
247 return &Session{engine: engine}, sublayerKey, nil
248 }
249
250 func openDynamicEngine() (uintptr, error) {
251 session := fwpmSession0{flags: fwpmSessionFlagDynamic}
252 var engine uintptr
253 r1, _, _ := procFwpmEngineOpen0.Call(
254 0, // serverName == NULL (local)
255 uintptr(rpcCAuthnWinNT),
256 0, // authIdentity == NULL
257 uintptr(unsafe.Pointer(&session)),
258 uintptr(unsafe.Pointer(&engine)),
259 )
260 if r1 != 0 {
261 return 0, fmt.Errorf("FwpmEngineOpen0: 0x%x", r1)
262 }
263 return engine, nil
264 }
265
266 // registerSublayer adds a session-scoped sublayer with a freshly generated GUID, weight 0xFFFF so its filters arbitrate
267 // above WDF's default sublayer. The sublayer is dynamic (no PERSISTENT flag) and goes away when the engine handle closes.
268 func registerSublayer(engine uintptr) (windows.GUID, error) {
269 key, err := windows.GenerateGUID()
270 if err != nil {
271 return windows.GUID{}, fmt.Errorf("GenerateGUID for sublayer: %w", err)
272 }
273
274 name, _ := windows.UTF16PtrFromString("Nebula WDF bypass sublayer")
275 desc, _ := windows.UTF16PtrFromString("Permit filters bypassing Windows Defender Firewall")
276 sl := fwpmSublayer0{
277 subLayerKey: key,
278 displayData: fwpmDisplayData0{name: name, description: desc},
279 weight: 0xFFFF,
280 }
281 r1, _, _ := procFwpmSubLayerAdd0.Call(
282 engine,
283 uintptr(unsafe.Pointer(&sl)),
284 0, // sd == NULL
285 )
286 if r1 != 0 {
287 return windows.GUID{}, fmt.Errorf("FwpmSubLayerAdd0: 0x%x", r1)
288 }
289 return key, nil
290 }
291
292 func addInterfaceFilter(engine uintptr, sublayerKey, layer windows.GUID, luid uint64) error {
293 name, _ := windows.UTF16PtrFromString("Nebula allow interface inbound")
294 desc, _ := windows.UTF16PtrFromString("Permits inbound traffic on a nebula interface")
295
296 // luid must remain addressable through the syscall -- FWP_UINT64 is stored
297 // by pointer in the FWP_VALUE0 union.
298 cond := fwpmFilterCondition0{
299 fieldKey: fwpmConditionIPLocalInterface,
300 matchType: fwpMatchEqual,
301 conditionValue: fwpValue0{
302 type_: fwpUint64,
303 value: uintptr(unsafe.Pointer(&luid)),
304 },
305 }
306
307 filter := fwpmFilter0{
308 // filterKey left zero: WFP assigns one when the filter is added.
309 displayData: fwpmDisplayData0{name: name, description: desc},
310 flags: fwpmFilterFlagClearActionRight,
311 layerKey: layer,
312 subLayerKey: sublayerKey,
313 weight: fwpValue0{type_: fwpUint8, value: uintptr(15)},
314 numFilterConditions: 1,
315 filterCondition: &cond,
316 action: fwpmAction0{actionType: fwpActionPermit},
317 }
318
319 r1, _, _ := procFwpmFilterAdd0.Call(
320 engine,
321 uintptr(unsafe.Pointer(&filter)),
322 0, // sd == NULL
323 0, // id == NULL
324 )
325 if r1 != 0 {
326 return fmt.Errorf("FwpmFilterAdd0: 0x%x", r1)
327 }
328 return nil
329 }
330
331 // addUDPPortFilter installs a PERMIT filter that matches (IP_PROTOCOL == UDP) AND (IP_LOCAL_PORT == port).
332 // FWP_UINT8 and FWP_UINT16 are <= 32 bits so they live inline in the FWP_VALUE0 union.
333 func addUDPPortFilter(engine uintptr, sublayerKey, layer windows.GUID, port uint16) error {
334 name, _ := windows.UTF16PtrFromString("Nebula allow UDP port inbound")
335 desc, _ := windows.UTF16PtrFromString("Permits inbound UDP to a nebula listener port")
336
337 conds := [2]fwpmFilterCondition0{
338 {
339 fieldKey: fwpmConditionIPProtocol,
340 matchType: fwpMatchEqual,
341 conditionValue: fwpValue0{
342 type_: fwpUint8,
343 value: uintptr(ipprotoUDP),
344 },
345 },
346 {
347 fieldKey: fwpmConditionIPLocalPort,
348 matchType: fwpMatchEqual,
349 conditionValue: fwpValue0{
350 type_: fwpUint16,
351 value: uintptr(port),
352 },
353 },
354 }
355
356 filter := fwpmFilter0{
357 displayData: fwpmDisplayData0{name: name, description: desc},
358 flags: fwpmFilterFlagClearActionRight,
359 layerKey: layer,
360 subLayerKey: sublayerKey,
361 weight: fwpValue0{type_: fwpUint8, value: uintptr(15)},
362 numFilterConditions: 2,
363 filterCondition: &conds[0],
364 action: fwpmAction0{actionType: fwpActionPermit},
365 }
366
367 r1, _, _ := procFwpmFilterAdd0.Call(
368 engine,
369 uintptr(unsafe.Pointer(&filter)),
370 0,
371 0,
372 )
373 if r1 != 0 {
374 return fmt.Errorf("FwpmFilterAdd0: 0x%x", r1)
375 }
376 return nil
377 }