diff --git a/Makefile b/Makefile index 60ef00cf..0828bf38 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ dependencies: go get -v ./... install: - which wtf | xargs rm + which wtf | xargs rm || true go install -ldflags="-X main.version=$(shell git describe --always --abbrev=6)_$(BRANCH) -X main.date=$(shell date +%FT%T%z)" which wtf diff --git a/security/dns.go b/security/dns.go index c57a202b..343ddd94 100644 --- a/security/dns.go +++ b/security/dns.go @@ -2,13 +2,46 @@ package security import ( "os/exec" + "runtime" + "strings" "github.com/senorprogrammer/wtf/wtf" ) -const dnsCmd = "networksetup" - -func DnsServers() string { - cmd := exec.Command(dnsCmd, "-getdnsservers", "Wi-Fi") - return wtf.ExecuteCommand(cmd) +func dnsLinux() []string { + // This may be very Ubuntu specific + cmd := exec.Command("nmcli", "device", "show") + out := wtf.ExecuteCommand(cmd) + lines := strings.Split(out, "\n") + dns := []string{} + for _, l := range lines { + if strings.HasPrefix(l, "IP4.DNS") { + parts := strings.Split(l, ":") + dns = append(dns, strings.TrimSpace(parts[1])) + } + } + return dns +} + +func dnsMacOS() []string { + cmd := exec.Command("networksetup", "-getdnsservers", "Wi-Fi") + out := wtf.ExecuteCommand(cmd) + records := strings.Split(out, "\n") + + if len(records) > 0 { + return records + } else { + return []string{} + } +} + +func DnsServers() []string { + switch runtime.GOOS { + case "linux": + return dnsLinux() + case "darwin": + return dnsMacOS() + default: + return []string{runtime.GOOS} + } } diff --git a/security/firewall.go b/security/firewall.go index 5d5cb95a..507103cc 100644 --- a/security/firewall.go +++ b/security/firewall.go @@ -2,6 +2,7 @@ package security import ( "os/exec" + "runtime" "strings" "github.com/senorprogrammer/wtf/wtf" @@ -11,20 +12,50 @@ const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw" /* -------------------- Exported Functions -------------------- */ -func FirewallState() string { +func firewallStateLinux() string { + return "[red]NA[white]" +} + +func firewallStateMacOS() string { cmd := exec.Command(osxFirewallCmd, "--getglobalstate") str := wtf.ExecuteCommand(cmd) return status(str) } -func FirewallStealthState() string { +func FirewallState() string { + switch runtime.GOOS { + case "linux": + return firewallStateLinux() + case "darwin": + return firewallStateMacOS() + default: + return "" + } +} + +func firewallStealthStateLinux() string { + return "[red]NA[white]" +} + +func firewallStealthStateMacOS() string { cmd := exec.Command(osxFirewallCmd, "--getstealthmode") str := wtf.ExecuteCommand(cmd) return status(str) } +func FirewallStealthState() string { + switch runtime.GOOS { + case "linux": + return firewallStealthStateLinux() + case "darwin": + return firewallStealthStateMacOS() + default: + return "" + } +} + /* -------------------- Unexported Functions -------------------- */ func status(str string) string { diff --git a/security/security_data.go b/security/security_data.go index 1959b8f2..3a339340 100644 --- a/security/security_data.go +++ b/security/security_data.go @@ -1,11 +1,7 @@ package security -import ( - "strings" -) - type SecurityData struct { - Dns string + Dns []string FirewallEnabled string FirewallStealth string LoggedInUsers []string @@ -17,14 +13,11 @@ func NewSecurityData() *SecurityData { return &SecurityData{} } -func (data *SecurityData) DnsAt(idx int) string { - records := strings.Split(data.Dns, "\n") - - if len(records) > 0 && len(records) > idx { - return records[idx] - } else { - return "" +func (data SecurityData) DnsAt(idx int) string { + if len(data.Dns) > idx { + return data.Dns[idx] } + return "" } func (data *SecurityData) Fetch() { diff --git a/security/users.go b/security/users.go index 47e9f4c1..75b3cc2c 100644 --- a/security/users.go +++ b/security/users.go @@ -2,20 +2,56 @@ package security import ( "os/exec" + "runtime" "strings" "github.com/senorprogrammer/wtf/wtf" ) -// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/ +func loggedInUsersLinux() []string { + cmd := exec.Command("who", "-us") + users := wtf.ExecuteCommand(cmd) -func LoggedInUsers() []string { + cleaned := []string{} + for _, u := range strings.Split(users, "\n") { + clean := true + col := strings.Split(u, " ") + if len(col) > 0 { + for _, cleanedU := range cleaned { + if strings.Compare(cleanedU, col[0]) == 0 { + clean = false + } + } + if clean { + cleaned = append(cleaned, col[0]) + } + } + + } + + return cleaned +} + +func loggedInUsersMacOs() []string { cmd := exec.Command("dscl", []string{".", "-list", "/Users"}...) users := wtf.ExecuteCommand(cmd) return cleanUsers(strings.Split(users, "\n")) } +// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/ + +func LoggedInUsers() []string { + switch runtime.GOOS { + case "linux": + return loggedInUsersLinux() + case "darwin": + return loggedInUsersMacOs() + default: + return []string{} + } +} + func cleanUsers(users []string) []string { rejects := []string{"_", "root", "nobody", "daemon", "Guest"} cleaned := []string{} diff --git a/security/wifi.go b/security/wifi.go index d84b808d..47c31fae 100644 --- a/security/wifi.go +++ b/security/wifi.go @@ -2,6 +2,7 @@ package security import ( "os/exec" + "runtime" "github.com/senorprogrammer/wtf/wtf" ) @@ -12,16 +13,58 @@ const osxWifiArg = "-I" /* -------------------- Exported Functions -------------------- */ -func WifiEncryption() string { +func wifiEncryptionLinux() string { + cmd := exec.Command("nmcli", "-t", "-f", "active,security", "dev", "wifi") + out := wtf.ExecuteCommand(cmd) + name := wtf.FindMatch(`yes:(.+)`, out) + if len(name) > 0 { + return name[0][1] + } + return "" +} + +func wifkEncryptionMacOS() string { name := wtf.FindMatch(`s*auth: (.+)s*`, wifiInfo()) return matchStr(name) } -func WifiName() string { +func WifiEncryption() string { + switch runtime.GOOS { + case "linux": + return wifiEncryptionLinux() + case "darwin": + return wifkEncryptionMacOS() + default: + return "" + } +} + +func wifiNameMacOS() string { name := wtf.FindMatch(`s*SSID: (.+)s*`, wifiInfo()) return matchStr(name) } +func wifiNameLinux() string { + cmd := exec.Command("nmcli", "-t", "-f", "active,ssid", "dev", "wifi") + out := wtf.ExecuteCommand(cmd) + name := wtf.FindMatch(`yes:(.+)`, out) + if len(name) > 0 { + return name[0][1] + } + return "" +} + +func WifiName() string { + switch runtime.GOOS { + case "linux": + return wifiNameLinux() + case "darwin": + return wifiNameMacOS() + default: + return "" + } +} + /* -------------------- Unexported Functions -------------------- */ func wifiInfo() string {