From 0f5ca03bd6d669a7c379ec2f0e325d8a012d1532 Mon Sep 17 00:00:00 2001 From: perp Date: Sat, 12 Aug 2023 00:25:58 +0100 Subject: [PATCH] :art: Updated code Redid printing and commented out filtering. --- internal/shodan/search.go | 131 +++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 58 deletions(-) diff --git a/internal/shodan/search.go b/internal/shodan/search.go index 3644fa2..ae76b26 100644 --- a/internal/shodan/search.go +++ b/internal/shodan/search.go @@ -3,7 +3,6 @@ package shodan import ( "context" "fmt" - "strconv" "strings" "github.com/ns3777k/go-shodan/v4/shodan" @@ -11,21 +10,12 @@ import ( "git.tcp.direct/perp/shogo/internal/utils" ) -// Search query -type Search struct { - Query string // Search query - Page int // Current page - Fields []string // Filter fields - Separator string // Filter separator - Results chan string // Results channel -} - // Return shodan search results -func (s *Search) Search() { +func (s *Shodan) Search() { // Setup options options := &shodan.HostQueryOptions{ - Query: s.Query, - Page: s.Page, + Query: s.Flags.Query, + Page: s.Flags.Page, Minify: false, } @@ -37,76 +27,101 @@ func (s *Search) Search() { return } + // Store lines + var lines string + // More than 2 matches if len(results.Matches) > 2 { // Go through matches for match := range results.Matches { - if len(s.Fields) != 0 { - fields := map[string]string{ - "product": results.Matches[match].Product, - "ip_str": results.Matches[match].IP.String(), - "org": results.Matches[match].Organization, - "isp": results.Matches[match].ISP, - "transport": results.Matches[match].Transport, - "data": results.Matches[match].Data, - "asn": results.Matches[match].ASN, - "port": strconv.Itoa(results.Matches[match].Port), - "timestamp": results.Matches[match].Timestamp, - "os": results.Matches[match].OS, - } + if len(s.Flags.Fields) != 0 { + lines += utils.Title.Sprintf("Filtering has not been implemented yet\n") + break - var result string - var content string + /* + fields := map[string]string{ + "product": results.Matches[match].Product, + "ip_str": results.Matches[match].IP.String(), + "org": results.Matches[match].Organization, + "isp": results.Matches[match].ISP, + "transport": results.Matches[match].Transport, + "data": results.Matches[match].Data, + "asn": results.Matches[match].ASN, + "port": strconv.Itoa(results.Matches[match].Port), + "timestamp": results.Matches[match].Timestamp, + "os": results.Matches[match].OS, + } - // Go through custom fields - for key, value := range fields { - // Go through real fields - for index := range s.Fields { - // Real field matched custom field - if s.Fields[index] == key { - // Port may go on the wrong side - if key == "port" { - content += value - } else { - content += value + s.Separator + var result string + var content string + + // Go through custom fields + for key, value := range fields { + // Go through real fields + for index := range s.Fields { + // Real field matched custom field + if s.Fields[index] == key { + // Port may go on the wrong side + if key == "port" { + content += value + } else { + content += value + s.Separator + } } } } - } - lines := strings.Split(content, "\n") - for line := range lines { - result += strings.TrimRight(lines[line], ":") - } - - // Print results - fmt.Println(result) + data := strings.Split(content, "\n") + for line := range data { + result += strings.TrimRight(data[line], ":") + } + // Print results + fmt.Println(result) + */ } else { // Send results - content := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n", + lines += fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n", utils.Green("IP"), utils.Blue(results.Matches[match].IP.String()), utils.Blue("Port"), utils.Green(results.Matches[match].Port), utils.Green("Hostname"), utils.Blue(strings.Join(results.Matches[0].Hostnames, ",")), ) - lines := strings.Split(results.Matches[match].Data, "\n") + data := strings.Split(results.Matches[match].Data, "\n") green := true - for line := range lines { - values := strings.Split(lines[line], ":") + for line := range data { + values := strings.Split(data[line], ":") if green { - content += fmt.Sprintf("%s:%s\n", utils.Green(values[0]), utils.Blue(strings.Join(values[1:], ""))) - green = false + if values[0] != ":" { + for value := range values[1:] { + if values[value] != "" { + lines += fmt.Sprintf("%s:%s\n", utils.Green(values[0]), utils.Blue(strings.Join(values[1:], ""))) + green = false + break + } + } + } } else { - content += fmt.Sprintf("%s:%s\n", utils.Blue(values[0]), utils.Green(strings.Join(values[1:], ""))) - green = true + if values[0] != ":" { + for value := range values[1:] { + if values[value] != "" { + lines += fmt.Sprintf("%s:%s\n", utils.Blue(values[0]), utils.Green(strings.Join(values[1:], ""))) + green = true + break + } + } + } } } - - s.Results <- content + lines += "\n" } } } + + // Remove empty newline + // Send results + lines = strings.TrimRight(lines, "\n") + s.Results <- lines } -// Todo: Redo filter sorting +// Todo: Redo filter sorting, pages