From 3e6efdb18153d2dcf0e221013bc36096a8cb5742 Mon Sep 17 00:00:00 2001 From: perp Date: Thu, 3 Aug 2023 23:49:57 +0100 Subject: [PATCH] :sparkles: Added commands Added new commands. --- internal/shodan/alert/alert.go | 9 +++ internal/shodan/pool.go | 112 +++++++++++++++++++++++++++++++++ internal/shodan/scan/scan.go | 9 +++ internal/shodan/shodan.go | 29 +++++++++ 4 files changed, 159 insertions(+) create mode 100644 internal/shodan/alert/alert.go create mode 100644 internal/shodan/pool.go create mode 100644 internal/shodan/scan/scan.go create mode 100644 internal/shodan/shodan.go diff --git a/internal/shodan/alert/alert.go b/internal/shodan/alert/alert.go new file mode 100644 index 0000000..d1280ac --- /dev/null +++ b/internal/shodan/alert/alert.go @@ -0,0 +1,9 @@ +package alert + +// Alert manager +type Alert struct { + Name string // Alert name + ID string // Alert ID + IP string // IP address + Results chan string // Results channel +} diff --git a/internal/shodan/pool.go b/internal/shodan/pool.go new file mode 100644 index 0000000..0d5f8d8 --- /dev/null +++ b/internal/shodan/pool.go @@ -0,0 +1,112 @@ +package shodan + +import ( + "fmt" + "os" + + "github.com/panjf2000/ants/v2" + + "git.tcp.direct/perp/shogo/internal/shodan/alert" + "git.tcp.direct/perp/shogo/internal/shodan/scan" + "git.tcp.direct/perp/shogo/internal/utils" +) + +// Manage a goroutine pool +func Pool(function string, flags *Flags, alert *alert.Alert, scan *scan.Scan) { + // Results channel + results := make(chan string, len(flags.Args)-1) + + // Thread count + var threads int + + // Get thread count + if flags.Threads == 0 { + threads = 1 + } else { + threads = flags.Threads + } + + // Goroutine pool + pool, err := ants.NewPool(threads) + if err != nil { + fmt.Printf("%s: %s\n", utils.Red("Error"), err.Error()) + os.Exit(1) + } + defer pool.Release() + + // Go through flags + for a := 0; a <= len(flags.Args)-1; a++ { + // Shodan manager + shodan := &Shodan{ + Flags: flags, + Results: results, + } + shodan.Flags.Query = flags.Args[a] // Bug + + // Scan manager + if scan != nil { + shodan.Scan = scan + shodan.Scan.Results = results + } + + // Alert manager + if alert != nil { + shodan.Alert = alert + shodan.Alert.ID = flags.Args[a] // Bug + shodan.Alert.Results = results + } + + // Submit job + pool.Submit(func() { + switch function { + // Alert commands + case "alert clear": + go shodan.Alert.Clear() + case "alert create": + go shodan.Alert.Create() + case "alert delete": + go shodan.Alert.Delete() + case "alert info": + go shodan.Alert.Info() + case "alert list": + go shodan.Alert.List() + + // Scan commands + case "scan internet": + go shodan.Scan.Internet() + case "scan protocols": + go shodan.Scan.Protocols() + case "scan submit": + go shodan.Scan.Submit() + + // General commands + case "count": + go shodan.Count() + // case "domain": + // go shodan.Domain() + case "host": + go shodan.Host() + case "info": + go shodan.Info() + case "myip": + go shodan.MyIp() + // case "search": + // go shodan.Search() + case "stats": + go shodan.Stats() + } + }) + } + + // Get result + for j := 0; j <= len(flags.Args)-1; j++ { + result := <-results + // Multiple results needs newline + if j != len(flags.Args)-1 { + fmt.Println(result + "\n") + } else { + fmt.Println(result) + } + } + defer close(results) +} diff --git a/internal/shodan/scan/scan.go b/internal/shodan/scan/scan.go new file mode 100644 index 0000000..ddc4196 --- /dev/null +++ b/internal/shodan/scan/scan.go @@ -0,0 +1,9 @@ +package scan + +// Scan manager +type Scan struct { + IPs []string // IP addresses + Port int // Port location + Protocol string // Protocol type + Results chan string // Results channel +} diff --git a/internal/shodan/shodan.go b/internal/shodan/shodan.go new file mode 100644 index 0000000..979e264 --- /dev/null +++ b/internal/shodan/shodan.go @@ -0,0 +1,29 @@ +package shodan + +import ( + "git.tcp.direct/perp/shogo/internal/shodan/alert" + "git.tcp.direct/perp/shogo/internal/shodan/scan" +) + +// Shodan manager +type Shodan struct { + Scan *scan.Scan // Scan commands + Alert *alert.Alert // Alert commands + Flags *Flags // Command flags + Results chan string // Results channel +} + +type Flags struct { + // General flags + Args []string // Command arguments + Query string // Search query + Facets string // Facet flters + Threads int // Thread amount + + // Info flags + Profile bool // Profile info + + // Search flags + Fields []string // Filter fields + Separator string // Filter separator +}