-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from vdesouza/main
Adds `wifi_network` table to capture wifi network name.
- Loading branch information
Showing
6 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "wifi_network", | ||
srcs = ["wifi_network.go"], | ||
importpath = "github.com/macadmins/osquery-extension/tables/wifi_network", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_github_osquery_osquery_go//:osquery-go", | ||
"@com_github_osquery_osquery_go//plugin/table", | ||
"@com_github_pkg_errors//:errors", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "wifi_network_test", | ||
srcs = ["wifi_network_test.go"], | ||
embed = [":wifi_network"], | ||
deps = [ | ||
"@com_github_osquery_osquery_go//plugin/table", | ||
"@com_github_stretchr_testify//assert", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package wifi_network | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
|
||
"github.com/osquery/osquery-go" | ||
"github.com/osquery/osquery-go/plugin/table" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type CommandExecutor interface { | ||
ExecCommand(command string, args ...string) ([]byte, error) | ||
} | ||
|
||
type CmdExecutor struct{} | ||
|
||
func (r CmdExecutor) ExecCommand(name string, args ...string) ([]byte, error) { | ||
cmd := exec.Command(name, args...) | ||
cmd.Stderr = os.Stderr | ||
return cmd.Output() | ||
} | ||
|
||
type OsqueryClient interface { | ||
QueryRow(query string) (map[string]string, error) | ||
Close() | ||
} | ||
|
||
func WifiNetworkColumns() []table.ColumnDefinition { | ||
return []table.ColumnDefinition{ | ||
table.TextColumn("ssid"), | ||
table.TextColumn("interface"), | ||
table.TextColumn("rssi"), | ||
table.TextColumn("noise"), | ||
table.TextColumn("channel"), | ||
table.TextColumn("channel_width"), | ||
table.TextColumn("channel_band"), | ||
table.TextColumn("transmit_rate"), | ||
table.TextColumn("security_type"), | ||
table.TextColumn("mode"), | ||
} | ||
} | ||
|
||
type WifiNetwork struct { | ||
SSID string `json:"ssid"` | ||
Interface string `json:"interface"` | ||
RSSI string `json:"rssi"` | ||
Noise string `json:"noise"` | ||
Channel string `json:"channel"` | ||
ChannelWidth string `json:"channel_width"` | ||
ChannelBand string `json:"channel_band"` | ||
TransmitRate string `json:"transmit_rate"` | ||
SecurityType string `json:"security_type"` | ||
Mode string `json:"mode"` | ||
} | ||
|
||
func WifiNetworkGenerate( | ||
ctx context.Context, | ||
queryContext table.QueryContext, | ||
socketPath string, | ||
) ([]map[string]string, error) { | ||
// get the wifi interface from osquery | ||
osqueryClient, err := osquery.NewClient(socketPath, 10*time.Second) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer osqueryClient.Close() | ||
|
||
wifiStatus, err := getWifiStatus(osqueryClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cmdExecutor := CmdExecutor{} | ||
wifiNetwork, err := buildWifiNetworkFromResponse(cmdExecutor, wifiStatus) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return buildWifiNetworkResults(wifiNetwork), nil | ||
} | ||
|
||
// getWifiInterface checks the wifi_status table to determine the wifi interface | ||
func getWifiStatus(client OsqueryClient) (map[string]string, error) { | ||
wifiStatusQuery := "SELECT * FROM wifi_status;" | ||
|
||
resp, err := client.QueryRow(wifiStatusQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
// getValueFromResponse extracts the value from the response | ||
func getValueFromResponse(resp map[string]string, key string) (string, error) { | ||
if value, ok := resp[key]; ok { | ||
return value, nil | ||
} | ||
return "", errors.New("wifi_status table missing '" + key + "' column") | ||
} | ||
|
||
// getWifiNetworkName shells out to 'networksetup -getairportnetwork ${wifiInterface}' | ||
func getWifiNetworkName(cmdExecutor CommandExecutor, wifiInterface string) (string, error) { | ||
out, err := cmdExecutor.ExecCommand("/usr/sbin/networksetup", "-getairportnetwork", wifiInterface) | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to run networksetup") | ||
} | ||
outStr := string(out) | ||
splitOut := strings.Split(outStr, ": ") | ||
if len(splitOut) != 2 { | ||
// Wifi may not be on or connected | ||
return "", nil | ||
} | ||
return strings.TrimSpace(splitOut[1]), nil | ||
} | ||
|
||
// buildWifiNetwork | ||
func buildWifiNetworkFromResponse(cmdExecutor CommandExecutor, wifiStatus map[string]string) (*WifiNetwork, error) { | ||
wifiInterface, err := getValueFromResponse(wifiStatus, "interface") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rssi, err := getValueFromResponse(wifiStatus, "rssi") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
noise, err := getValueFromResponse(wifiStatus, "noise") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
channel, err := getValueFromResponse(wifiStatus, "channel") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
channelWidth, err := getValueFromResponse(wifiStatus, "channel_width") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
channelBand, err := getValueFromResponse(wifiStatus, "channel_band") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
transmitRate, err := getValueFromResponse(wifiStatus, "transmit_rate") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
securityType, err := getValueFromResponse(wifiStatus, "security_type") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mode, err := getValueFromResponse(wifiStatus, "mode") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// get the wifi network name | ||
wifiNetworkName, err := getWifiNetworkName(cmdExecutor, wifiInterface) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &WifiNetwork{ | ||
SSID: wifiNetworkName, | ||
Interface: wifiInterface, | ||
RSSI: rssi, | ||
Noise: noise, | ||
Channel: channel, | ||
ChannelWidth: channelWidth, | ||
ChannelBand: channelBand, | ||
TransmitRate: transmitRate, | ||
SecurityType: securityType, | ||
Mode: mode, | ||
}, nil | ||
} | ||
|
||
// buildWifiNetworkResults creates a map of the results | ||
func buildWifiNetworkResults(info *WifiNetwork) []map[string]string { | ||
var results []map[string]string | ||
results = append(results, map[string]string{ | ||
"ssid": info.SSID, | ||
"interface": info.Interface, | ||
"rssi": info.RSSI, | ||
"noise": info.Noise, | ||
"channel": info.Channel, | ||
"channel_width": info.ChannelWidth, | ||
"channel_band": info.ChannelBand, | ||
"transmit_rate": info.TransmitRate, | ||
"security_type": info.SecurityType, | ||
"mode": info.Mode, | ||
}) | ||
return results | ||
} |
Oops, something went wrong.