From c0e4eaa9e06dd9c9db29720a65dd9498ffab3069 Mon Sep 17 00:00:00 2001 From: Diretnan Domnan Date: Fri, 25 Mar 2022 09:54:33 +0100 Subject: [PATCH] Windows: Adding tcp inspector --- inspector/tcp.go | 65 +++++++++++++++++++++++++ integration/integration_windows_test.go | 13 +++++ 2 files changed, 78 insertions(+) diff --git a/inspector/tcp.go b/inspector/tcp.go index 5de57b9..33400e7 100644 --- a/inspector/tcp.go +++ b/inspector/tcp.go @@ -30,6 +30,12 @@ type TcpLinux struct { Values TcpMetrics } +type TcpWin struct { + Command string + Driver *driver.Driver + Values TcpMetrics +} + /* Parse : parsing the following kind of output Active Internet connections (including servers) Proto Recv-Q Send-Q Local Address Foreign Address (state) @@ -135,6 +141,61 @@ func (i *TcpLinux) Execute() { } } +/* Parse for output + +Active Connections + + Proto Local Address Foreign Address State + TCP 0.0.0.0:135 0.0.0.0:0 LISTENING + TCP 0.0.0.0:445 0.0.0.0:0 LISTENING + TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING + TCP 0.0.0.0:5700 0.0.0.0:0 LISTENING + TCP 0.0.0.0:6646 0.0.0.0:0 LISTENING + TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING +*/ +func (i *TcpWin) Parse(output string) { + ports := make(map[int]string) + lines := strings.Split(output, "\n") + for index, line := range lines { + // skip title lines + if index == 0 || index == 1 || index == 3 { + continue + } + columns := strings.Fields(line) + if len(columns) > 3 { + status := columns[3] + address := strings.Split(columns[1], ":") + portString := address[len(address)-1] + port, err := strconv.Atoi(portString) + if err != nil { + log.Fatal("Could not parse port number in TcpWin") + } + ports[port] = status + + } + } + i.Values.Ports = ports +} + +func (i *TcpWin) SetDriver(driver *driver.Driver) { + details := (*driver).GetDetails() + if !details.IsWindows { + panic("Cannot use TcpWin on drivers outside (windows)") + } + i.Driver = driver +} + +func (i TcpWin) driverExec() driver.Command { + return (*i.Driver).RunCommand +} + +func (i *TcpWin) Execute() { + output, err := i.driverExec()(i.Command) + if err == nil { + i.Parse(output) + } +} + // NewTcp: Initialize a new Tcp instance func NewTcp(driver *driver.Driver, _ ...string) (Inspector, error) { var tcp Inspector @@ -150,6 +211,10 @@ func NewTcp(driver *driver.Driver, _ ...string) (Inspector, error) { tcp = &TcpLinux{ Command: `ss -tan`, } + } else if details.IsWindows { + tcp = &TcpWin{ + Command: `netstat -anp tcp`, + } } tcp.SetDriver(driver) return tcp, nil diff --git a/integration/integration_windows_test.go b/integration/integration_windows_test.go index 5d8ca68..845f295 100644 --- a/integration/integration_windows_test.go +++ b/integration/integration_windows_test.go @@ -92,3 +92,16 @@ func TestDFonLocal(t *testing.T) { } } } + +func TestTcponLocal(t *testing.T) { + d := NewLocalForTest() + i, _ := inspector.Init(`tcp`, &d) + i.Execute() + iConcreteWindows, ok := i.(*inspector.TcpWin) + if ok { + if len(iConcreteWindows.Values.Ports) == 0 { + t.Errorf("%#v", iConcreteWindows.Values.Ports) + } + fmt.Printf("%#v", iConcreteWindows.Values.Ports) + } +}