Skip to content

Commit 3fd4836

Browse files
committed
Get the security level too
1 parent ba24b0d commit 3fd4836

File tree

4 files changed

+171
-6
lines changed

4 files changed

+171
-6
lines changed

tables/wifi_network/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_test(
1616
name = "wifi_network_test",
1717
srcs = ["wifi_network_test.go"],
1818
embed = [":wifi_network"],
19+
embedsrcs = ["wdutil_out.txt"],
1920
deps = [
2021
"@com_github_osquery_osquery_go//plugin/table",
2122
"@com_github_stretchr_testify//assert",

tables/wifi_network/wdutil_out.txt

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
————————————————————————————————————————————————————————————————————
2+
NETWORK
3+
————————————————————————————————————————————————————————————————————
4+
Primary IPv4 : en0 ((null) / gpd.pan)
5+
: 192.168.1.1
6+
Primary IPv6 : en0 (Wi-Fi / 8FE3682E-51CE-450A-9A7D-B2A02AA407F2)
7+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
8+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
9+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
10+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
11+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
12+
DNS Addresses : 8.8.8.8
13+
Apple : Reachable
14+
————————————————————————————————————————————————————————————————————
15+
WIFI
16+
————————————————————————————————————————————————————————————————————
17+
MAC Address : <redacted> (hw=<redacted>)
18+
Interface Name : en0
19+
Power : On [On]
20+
Op Mode : STA
21+
SSID : <redacted>
22+
BSSID : <redacted>
23+
RSSI : -34 dBm
24+
Noise : -92 dBm
25+
Tx Rate : 2401.0 Mbps
26+
Security : WPA3 Personal
27+
PHY Mode : 11ax
28+
MCS Index : 11
29+
Guard Interval : 800
30+
NSS : 2
31+
Channel : 6g101 (160 MHz, Active)
32+
Country Code : US
33+
Scan Cache Count : 31
34+
NetworkServiceID : 9A0B692D-C462-4771-982E-65EC1C2B28F8
35+
IPv4 Config Method : DHCP
36+
IPv4 Address : 192.168.1.34
37+
IPv4 Router : 192.168.1.1
38+
IPv6 Config Method : Automatic
39+
IPv6 Address : 2001:0db8:85a3:0000:0000:8a2e:0370:7334
40+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
41+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
42+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
43+
: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
44+
IPv6 Router : fd12:3456:789a:1::1
45+
DNS : 192.168.1.1
46+
BTC Mode : Off
47+
Desense :
48+
Chain Ack : []
49+
BTC Profile 2.4GHz : Disabled
50+
BTC Profile 5GHz : Disabled
51+
————————————————————————————————————————————————————————————————————
52+
BLUETOOTH
53+
————————————————————————————————————————————————————————————————————
54+
Power : On
55+
Address : 00:16:3E:28:52:1A
56+
Discoverable : No
57+
Connectable : No
58+
Scanning : No
59+
Devices : 10 (paired=10 cloud=0 connected=0)
60+
61+
Office
62+
Address : 40-ab-cd-12-e3-4f
63+
Paired : Yes
64+
CloudPaired : No
65+
Connected : No
66+
67+
————————————————————————————————————————————————————————————————————
68+
AWDL
69+
————————————————————————————————————————————————————————————————————
70+
AirDrop Disc Mode : Everyone
71+
AWDL Enabled : No
72+
Interface Name : awdl0
73+
Power : On
74+
IPv6 Address : fd12:3456:789a:1::1
75+
Schedule State : n/a
76+
Channel Sequence : n/a
77+
Op Mode : n/a
78+
Real Time Mode : No
79+
Sync State : n/a
80+
Sync Params : n/a
81+
Master Channel : n/a
82+
Election Params : n/a
83+
————————————————————————————————————————————————————————————————————
84+
POWER
85+
————————————————————————————————————————————————————————————————————
86+
Power Source : AC
87+
Battery Warning Level: None
88+
System Caps : FullWake:cpu disk net aud vid

tables/wifi_network/wifi_network.go

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package wifi_network
22

33
import (
4+
"bufio"
45
"context"
56
"os"
67
"os/exec"
@@ -117,6 +118,45 @@ func getWifiNetworkName(cmdExecutor CommandExecutor, wifiInterface string) (stri
117118
return strings.TrimSpace(splitOut[1]), nil
118119
}
119120

121+
func getSecurityLevel(cmdExecutor CommandExecutor, interfaceName string) (string, error) {
122+
out, err := getWdutilOutput(cmdExecutor)
123+
if err != nil {
124+
return "", err
125+
}
126+
127+
return extractSecurityValue(out, interfaceName), nil
128+
}
129+
130+
func getWdutilOutput(cmdExecutor CommandExecutor) (string, error) {
131+
out, err := cmdExecutor.ExecCommand("/usr/bin/wdutil", "info", "-q")
132+
if err != nil {
133+
return "", errors.Wrap(err, "failed to run wdutil")
134+
}
135+
return string(out), nil
136+
}
137+
138+
func extractSecurityValue(input string, desiredInterfaceName string) string {
139+
scanner := bufio.NewScanner(strings.NewReader(input))
140+
interfaceName := ""
141+
for scanner.Scan() {
142+
line := scanner.Text()
143+
if strings.Contains(line, "Interface Name") {
144+
parts := strings.Split(line, ":")
145+
if len(parts) > 1 {
146+
interfaceName = strings.TrimSpace(parts[1])
147+
}
148+
}
149+
if strings.Contains(line, "Security") && interfaceName == desiredInterfaceName {
150+
parts := strings.Split(line, ":")
151+
if len(parts) > 1 {
152+
return strings.TrimSpace(parts[1])
153+
}
154+
}
155+
}
156+
157+
return ""
158+
}
159+
120160
// buildWifiNetwork
121161
func buildWifiNetworkFromResponse(cmdExecutor CommandExecutor, wifiStatus map[string]string) (*WifiNetwork, error) {
122162
wifiInterface, err := getValueFromResponse(wifiStatus, "interface")
@@ -154,18 +194,19 @@ func buildWifiNetworkFromResponse(cmdExecutor CommandExecutor, wifiStatus map[st
154194
return nil, err
155195
}
156196

157-
securityType, err := getValueFromResponse(wifiStatus, "security_type")
197+
mode, err := getValueFromResponse(wifiStatus, "mode")
158198
if err != nil {
159199
return nil, err
160200
}
161201

162-
mode, err := getValueFromResponse(wifiStatus, "mode")
202+
// get the wifi network name
203+
wifiNetworkName, err := getWifiNetworkName(cmdExecutor, wifiInterface)
163204
if err != nil {
164205
return nil, err
165206
}
166207

167-
// get the wifi network name
168-
wifiNetworkName, err := getWifiNetworkName(cmdExecutor, wifiInterface)
208+
// get the security level
209+
securityType, err := getSecurityLevel(cmdExecutor, wifiInterface)
169210
if err != nil {
170211
return nil, err
171212
}

tables/wifi_network/wifi_network_test.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package wifi_network
22

33
import (
44
"errors"
5+
"fmt"
56
"testing"
67

78
"github.com/osquery/osquery-go/plugin/table"
89
"github.com/stretchr/testify/assert"
10+
11+
_ "embed"
912
)
1013

14+
//go:embed wdutil_out.txt
15+
var wdutilOut []byte
16+
1117
type MockOsqueryClient struct{}
1218

1319
func (m MockOsqueryClient) QueryRow(query string) (map[string]string, error) {
@@ -19,9 +25,14 @@ func (m MockOsqueryClient) Close() {}
1925
type MockCommandExecutor struct{}
2026

2127
func (m MockCommandExecutor) ExecCommand(name string, args ...string) ([]byte, error) {
28+
fmt.Println(args)
2229
if args[1] == "en0" {
2330
return []byte("Current Wi-Fi Network: MyNetwork"), nil
2431
}
32+
// /usr/bin/wdutil info -q
33+
if args[0] == "info" {
34+
return wdutilOut, nil
35+
}
2536
return nil, errors.New("commad failed")
2637
}
2738

@@ -130,7 +141,7 @@ func TestBuildWifiNetworkFromResponse(t *testing.T) {
130141
"channel_width": "20",
131142
"channel_band": "2.4 GHz",
132143
"transmit_rate": "300 Mbps",
133-
"security_type": "WPA2 Personal",
144+
"security_type": "",
134145
"mode": "Station",
135146
},
136147
&WifiNetwork{
@@ -142,7 +153,7 @@ func TestBuildWifiNetworkFromResponse(t *testing.T) {
142153
ChannelWidth: "20",
143154
ChannelBand: "2.4 GHz",
144155
TransmitRate: "300 Mbps",
145-
SecurityType: "WPA2 Personal",
156+
SecurityType: "WPA3 Personal",
146157
Mode: "Station",
147158
},
148159
false,
@@ -222,3 +233,27 @@ func TestBuildWifiNetworkResults(t *testing.T) {
222233
})
223234
}
224235
}
236+
237+
func TestGetWdutilOutput(t *testing.T) {
238+
t.Log(wdutilOut)
239+
mockCmdExecutor := MockCommandExecutor{}
240+
result, err := getWdutilOutput(mockCmdExecutor)
241+
assert.NoError(t, err)
242+
t.Logf("Result: %s", result)
243+
assert.NotEmpty(t, result)
244+
}
245+
246+
func TestExtractSecurityValue(t *testing.T) {
247+
expectedOutput := "WPA3 Personal"
248+
249+
output := extractSecurityValue(string(wdutilOut), "en0")
250+
251+
assert.Equal(t, expectedOutput, output)
252+
}
253+
254+
func TestGetSecurityLevel(t *testing.T) {
255+
mockCmdExecutor := MockCommandExecutor{}
256+
result, err := getSecurityLevel(mockCmdExecutor, "en0")
257+
assert.NoError(t, err)
258+
assert.Equal(t, "WPA3 Personal", result)
259+
}

0 commit comments

Comments
 (0)