Browse Source

added some verbosity to the tests and moved dependency injection for port map

AETH-erial 10 months ago
parent
commit
e14687c9ee
5 changed files with 82 additions and 13 deletions
  1. 1 1
      Makefile
  2. 1 1
      cmd/kyoketsu/kyoketsu.go
  3. 4 1
      pkg/local_test.go
  4. 8 10
      pkg/scanner.go
  5. 68 0
      pkg/scanner_test.go

+ 1 - 1
Makefile

@@ -15,7 +15,7 @@ install:
 
 
 test:
-	go test ./...
+	go test -v ./...
 
 
 

+ 1 - 1
cmd/kyoketsu/kyoketsu.go

@@ -27,7 +27,7 @@ func main() {
 		wg.Add(1)
 		go func(target string, wg *sync.WaitGroup) {
 			defer wg.Done()
-			out := kyoketsu.PortWalk(target, kyoketsu.PORT_MAP)
+			out := kyoketsu.PortWalk(target, kyoketsu.RetrieveScanDirectives().Pairs)
 			if len(out.ListeningPorts) > 0 {
 				dns, _ := net.LookupAddr(out.IpAddress)
 				out.Fqdn = strings.Join(dns, ", ")

+ 4 - 1
pkg/local_test.go

@@ -75,7 +75,7 @@ func TestAddressRecurse(t *testing.T) {
 			}
 
 		}
-		log.Printf("Nice! Test: '%s' passed!\n", tc[i].Name)
+		t.Logf("Nice! Test: '%s' passed!\n", tc[i].Name)
 	}
 
 }
@@ -111,6 +111,7 @@ func TestGetNextAddr(t *testing.T) {
 			}
 
 		}
+		t.Logf("Test: '%s' passed!\n", tc[i].Name)
 	}
 
 }
@@ -148,6 +149,8 @@ func TestGetNetwork(t *testing.T) {
 
 		}
 
+		t.Logf("Test: '%s' passed!\n", tc[i].Name)
+
 	}
 
 }

+ 8 - 10
pkg/scanner.go

@@ -25,10 +25,10 @@ Need to work with with a database schema in mind, and revolve functionality arou
 */
 
 type Host struct {
-	Fqdn           string           // The FQDN of the address targeted as per the systems default resolver
-	IpAddress      string           // the IPv4 address (no ipv6 support yet)
-	PingResponse   bool             // boolean value representing if the host responded to ICMP
-	ListeningPorts []map[int]string // list of maps depicting a port number -> service name
+	Fqdn           string         // The FQDN of the address targeted as per the systems default resolver
+	IpAddress      string         // the IPv4 address (no ipv6 support yet)
+	PingResponse   bool           // boolean value representing if the host responded to ICMP
+	ListeningPorts map[int]string // list of maps depicting a port number -> service name
 }
 
 /*
@@ -41,8 +41,7 @@ func PortWalk(addr string, portmap map[int]string) *Host {
 	wg := &sync.WaitGroup{}
 	out := []*PortScanResult{}
 
-	ports := RetrieveScanDirectives()
-	for p, s := range ports.Pairs {
+	for p, s := range portmap {
 		wg.Add(1)
 		go func(target string, p int, s string) {
 			defer wg.Done()
@@ -50,13 +49,12 @@ func PortWalk(addr string, portmap map[int]string) *Host {
 		}(addr, p, s)
 	}
 	wg.Wait()
-	host := &Host{IpAddress: addr, ListeningPorts: []map[int]string{}}
+	host := &Host{IpAddress: addr, ListeningPorts: map[int]string{}}
 	for i := range out {
 		if out[i].Listening {
-			host.ListeningPorts = append(host.ListeningPorts, map[int]string{
-				out[i].PortNumber: out[i].Service,
-			})
+			host.ListeningPorts[out[i].PortNumber] = out[i].Service
 		}
+
 	}
 	return host
 

+ 68 - 0
pkg/scanner_test.go

@@ -1 +1,69 @@
 package kyoketsu
+
+import (
+	"fmt"
+	"log"
+	"net"
+	"sync"
+	"testing"
+)
+
+func startTestSever(port int, t *testing.T) {
+	listen, err := net.Listen("tcp", fmt.Sprintf("localhost:%v", port))
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer listen.Close()
+	for {
+		conn, err := listen.Accept()
+		if err != nil {
+			log.Fatal(err)
+		}
+		t.Logf("Recieved dial from PortWalk of port: %v from host: %s\n", port, conn.RemoteAddr().String())
+		conn.Close()
+	}
+}
+
+func TestPortWalk(t *testing.T) {
+	type TestCase struct {
+		Name       string
+		ScanPort   map[int]string
+		ListenPort map[int]string
+		ShouldFail bool
+	}
+	tc := []TestCase{
+		TestCase{
+			Name:       "Passing test, listening on 8080/27017 and scanned on 8080/27017.",
+			ScanPort:   map[int]string{8080: "unknown", 27017: "mongo"},
+			ListenPort: map[int]string{8080: "unknown", 27017: "mongo"},
+			ShouldFail: false,
+		},
+		TestCase{
+			Name:       "Failing test, listening on 8081 scanned on 6439",
+			ScanPort:   map[int]string{6439: "random_service"},
+			ListenPort: map[int]string{8081: "unknown"},
+			ShouldFail: true,
+		},
+	}
+	wg := &sync.WaitGroup{}
+	for i := range tc {
+		for port, _ := range tc[i].ListenPort {
+			wg.Add(1)
+			go startTestSever(port, t)
+		}
+		got := PortWalk("localhost", tc[i].ScanPort)
+		for k, _ := range tc[i].ListenPort {
+			wg.Done()
+
+			_, ok := got.ListeningPorts[k]
+			if !ok {
+				if !tc[i].ShouldFail {
+					t.Errorf("Test '%s' failed! PortWalk didnt detect the test server was listening on: %+v\n", tc[i].Name, tc[i].ListenPort)
+				}
+			}
+			t.Logf("Test '%s' passed! Scanned port: '%v'", tc[i].Name, k)
+
+		}
+
+	}
+}