From ac49d5aee3d8bb443dffa28b798efbd0e7690f1f Mon Sep 17 00:00:00 2001 From: Azrotronik <60074481+Azrotronik@users.noreply.github.com> Date: Sun, 2 Oct 2022 23:17:34 +0000 Subject: [PATCH] Added JSON output --- cmd/root.go | 16 ++++++++++++---- scan/scan-connect.go | 6 ++++++ scan/scan-device.go | 6 ++++++ scan/scan-syn.go | 6 ++++++ scan/scanner.go | 1 + 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 0ca03c1..7d04a88 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,6 +16,7 @@ import ( ) var debug bool +var jsonOutput bool var timeoutMS int = 2000 var parallelism int = 500 var portSelection string @@ -28,6 +29,7 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&versionRequested, "version", "", versionRequested, "Output version information and exit") rootCmd.PersistentFlags().StringVarP(&scanType, "scan-type", "s", scanType, "Scan type. Must be one of stealth, connect") rootCmd.PersistentFlags().BoolVarP(&debug, "verbose", "v", debug, "Enable verbose logging") + rootCmd.PersistentFlags().BoolVarP(&jsonOutput, "json", "j", jsonOutput, "Enable JSON output") rootCmd.PersistentFlags().IntVarP(&timeoutMS, "timeout-ms", "t", timeoutMS, "Scan timeout in MS") rootCmd.PersistentFlags().IntVarP(¶llelism, "workers", "w", parallelism, "Parallel routines to scan on") rootCmd.PersistentFlags().StringVarP(&portSelection, "ports", "p", portSelection, "Port to scan. Comma separated, can sue hyphens e.g. 22,80,443,8080-8090") @@ -90,8 +92,9 @@ var rootCmd = &cobra.Command{ }() startTime := time.Now() - fmt.Printf("\nStarting scan at %s\n\n", startTime.String()) - + if !jsonOutput { + fmt.Printf("\nStarting scan at %s\n\n", startTime.String()) + } for _, target := range args { targetIterator := scan.NewTargetIterator(target) @@ -118,6 +121,10 @@ var rootCmd = &cobra.Command{ } for _, result := range results { + if jsonOutput { + scanner.OutputResultJSON(result) + continue + } if !hideUnavailableHosts || result.IsHostUp() { scanner.OutputResult(result) } @@ -125,8 +132,9 @@ var rootCmd = &cobra.Command{ } - fmt.Printf("Scan complete in %s.\n", time.Since(startTime).String()) - + if !jsonOutput { + fmt.Printf("Scan complete in %s.\n", time.Since(startTime).String()) + } }, } diff --git a/scan/scan-connect.go b/scan/scan-connect.go index ba4ddbb..954daaa 100644 --- a/scan/scan-connect.go +++ b/scan/scan-connect.go @@ -2,6 +2,7 @@ package scan import ( "context" + "encoding/json" "fmt" "io" "net" @@ -197,3 +198,8 @@ func (s *ConnectScanner) scanPort(target net.IP, port int) (PortState, error) { func (s *ConnectScanner) OutputResult(result Result) { fmt.Println(result.String()) } + +func (s *ConnectScanner) OutputResultJSON(result Result) { + resultJSON, _ := json.Marshal(&result) + fmt.Println(string(resultJSON)) +} diff --git a/scan/scan-device.go b/scan/scan-device.go index 1e6df7a..45b7a5e 100644 --- a/scan/scan-device.go +++ b/scan/scan-device.go @@ -2,6 +2,7 @@ package scan import ( "context" + "encoding/json" "fmt" "io" "net" @@ -181,3 +182,8 @@ func (s *DeviceScanner) OutputResult(result Result) { fmt.Println("") } + +func (s *DeviceScanner) OutputResultJSON(result Result) { + resultJSON, _ := json.Marshal(&result) + fmt.Println(string(resultJSON)) +} diff --git a/scan/scan-syn.go b/scan/scan-syn.go index 96a18c3..450a962 100644 --- a/scan/scan-syn.go +++ b/scan/scan-syn.go @@ -2,6 +2,7 @@ package scan import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -411,3 +412,8 @@ func (s *SynScanner) scanHost(job hostJob) (Result, error) { func (s *SynScanner) OutputResult(result Result) { fmt.Println(result.String()) } + +func (s *SynScanner) OutputResultJSON(result Result) { + resultJSON, _ := json.Marshal(&result) + fmt.Println(string(resultJSON)) +} diff --git a/scan/scanner.go b/scan/scanner.go index 7193e16..002774c 100644 --- a/scan/scanner.go +++ b/scan/scanner.go @@ -7,4 +7,5 @@ type Scanner interface { Start() error Scan(ctx context.Context, ports []int) ([]Result, error) OutputResult(result Result) + OutputResultJSON(result Result) }