Pārlūkot izejas kodu

defining some input and output behavior

AETH-erial 1 gadu atpakaļ
vecāks
revīzija
3faf34aff9
4 mainītis faili ar 107 papildinājumiem un 24 dzēšanām
  1. 27 0
      cmd/kyoketsu/kyoketsu.go
  2. 35 6
      pkg/scan/local.go
  3. 30 18
      pkg/scan/scanner.go
  4. 15 0
      pkg/storage/storage.go

+ 27 - 0
cmd/kyoketsu/kyoketsu.go

@@ -0,0 +1,27 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+
+	"git.aetherial.dev/aeth/kyoketsu/pkg/scan"
+)
+
+
+
+func main() {
+	if len(os.Args) == 1 {
+		log.Fatal("Please pass in the name of an interface that belongs to the network to scan.")
+	}
+	addr, err := scan.GetAllAddresses(os.Args[1])
+	if err != nil {
+		log.Fatal(err)
+	}
+	fmt.Printf("%+v\n", addr)
+
+
+}
+
+
+

+ 35 - 6
pkg/scan/local.go

@@ -1,10 +1,12 @@
 package scan
 
 import (
+	"fmt"
 	"log"
 	"net"
 	"net/netip"
 	"strings"
+
 )
 
 
@@ -12,6 +14,13 @@ type AllAddress struct {
 	Addr	[]net.IP `json:"addresses"`
 }
 
+type NetworkInterfaceNotFound struct {Passed string}
+
+// Implementing error interface
+func (n *NetworkInterfaceNotFound) Error() string {
+	return fmt.Sprintf("Interface: '%s' not found.", n.Passed)
+}
+
 /*
 Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
 	:param addr: the address to recursively find the next address for
@@ -32,16 +41,36 @@ func addressRecurse(addr netip.Addr, out *AllAddress) {
 
 }
 
+/*
+Retrieve the address of a specific interface
+	:param name: the name of the interface to get the address of
+*/
+func getAddressByInterface(name string) ([]net.Addr, error) {
+	interfaces, err := net.Interfaces()
+	if err != nil {
+		return nil, err
+	}
+	for idx := range interfaces {
+		if interfaces[idx].Name == name {
+			return interfaces[idx].Addrs()
+		}
+	}
+	return nil, &NetworkInterfaceNotFound{Passed: name}
+}
+
+
 /*
 Utilized a recursive function to find all addresses in the address space that the host belongs.
 Returns a pointer to an AllAddresses struct who has a list of net.IP structs inside
 */
-func GetAllAddresses() *AllAddress {
-	interfaces, _ := net.InterfaceAddrs()
+func GetAllAddresses(name string) (*AllAddress, error){
+	addresses, err := getAddressByInterface(name)
+	if err != nil {
+		return nil, err
+	}
 	out := &AllAddress{}
-	for idx := range interfaces {
-		ip := net.ParseIP(strings.Split(interfaces[idx].String(), "/")[0])
-
+	for idx := range addresses {
+		ip := net.ParseIP(strings.Split(addresses[idx].String(), "/")[0])
 		root, err := netip.ParseAddr(ip.Mask(ip.DefaultMask()).String())
 		if err != nil {
 			continue
@@ -51,6 +80,6 @@ func GetAllAddresses() *AllAddress {
 		}
 		addressRecurse(root, out)
 	}
-	return out
+	return out, nil
 }
 

+ 30 - 18
pkg/scan/scanner.go

@@ -10,20 +10,30 @@ import (
 
 )
 
-var PORT_LIST = []int{
-	22, 23, 53, 80, 125, 443, 8080, 8081, 8082, 8085, 8090, 8091, 9010, 9012, 10000,
+var PORT_MAP = map[int]string{
+	22: "ssh", 23: "telnet", 53: "dns", 80: "http", 25: "smtp", 443: "https", 8080: "unknown", 8081: "unknown",
+	8082: "unknown", 8085: "unknown", 8090: "unknown", 8091: "unknown", 9010: "unknown", 9012: "unknown", 10000: "unknown",
 }
 
 type TcpScanHost struct {
-	Host	string
-	Addr	netip.Addr
-	Ports	[]TcpScanResult
+	Host			string				`json:"host"`
+	Ipv4Address		netip.Addr			`json:"ipv4_address"`
+	PortsScanned	[]PortScanResult 	`json:"ports_scanned"`
 }
 
-type TcpScanResult struct {
-	PortNumber	int
-	Protocol	string
-	Listening	bool
+type PortScanResult struct {
+	PortNumber	int		`json:"port_number"`
+	Service		string	`json:"service"`
+	Protocol	string	`json:"protocol"`
+	Listening	bool	`json:"listening"`
+}
+
+type PortScanDirective struct {
+	Pairs	map[int]string
+}
+
+func RetrieveScanDirectives() PortScanDirective {
+	return PortScanDirective{Pairs: PORT_MAP}
 }
 
 
@@ -32,34 +42,36 @@ Scans a single host on a single port
 	:param addr: the address to dial
 	:param port: the port number to dial
 */
-func singlePortScan(addr string, port int) *TcpScanResult {
+func singlePortScan(addr string, port int, svcs string) *PortScanResult {
 	//defer wg.Done()
 	address := fmt.Sprintf("%v:%d", addr, port)
 	conn, err := net.DialTimeout("tcp", address, 1*time.Second)
 	if err != nil {
-		return &TcpScanResult{PortNumber: port, Protocol: "tcp", Listening: false}
+		return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: false}
 	}
 	conn.Close()
-	return &TcpScanResult{PortNumber: port, Protocol: "tcp", Listening: true}
+	return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: true}
 }
 
 /*
 Perform a TCP scan on the pointers host
 	:param posts: a list of ports to scan against a host
 */
-func (t *TcpScanHost) ScanPortRange(ports []int) []*TcpScanResult {
+func (t *TcpScanHost) ScanPortRange(ports []int) []*PortScanResult {
 	wg := &sync.WaitGroup{}
-	out := make(chan *TcpScanResult)
-	var res []*TcpScanResult
+	out := make(chan *PortScanResult)
+	var res []*PortScanResult
     wgOuter := &sync.WaitGroup{}
 	wgOuter.Add(1)
 	go func() {
 		defer wgOuter.Done()
-		for p := range ports {
+		ports := RetrieveScanDirectives()
+		for p, s := range ports.Pairs {
 			wg.Add(1)
-			port := ports[p]
+			port := p
+			svcs := s
 			go func() {
-				out <- singlePortScan(t.Addr.String(), port)
+				out <- singlePortScan(t.Ipv4Address.String(), port, svcs)
 				wg.Done()
 			}()
 		}

+ 15 - 0
pkg/storage/storage.go

@@ -0,0 +1,15 @@
+package storage
+
+import "git.aetherial.dev/aeth/kyoketsu/pkg/scan"
+
+
+
+type TopologyDatabaseIO interface {
+	AddHostToTable(*scan.TcpScanHost) error	// Add a host to the hosts table
+	UpdateHostEntry(string, *scan.TcpScanHost) error //Update a host entry, indexing by its ip address
+	RemoveHostEntry(string) error
+}
+
+
+
+