package shodan import ( "context" "fmt" "strings" "github.com/ns3777k/go-shodan/v4/shodan" "git.supernets.org/perp/shogo/internal/utils" ) // Return shodan search results func (s *Shodan) Search() { // Setup options options := &shodan.HostQueryOptions{ Query: s.Flags.Query, Page: s.Flags.Page, Minify: false, } // Get results results, err := utils.Client.GetHostsForQuery(context.Background(), options) if err != nil { fmt.Printf("%s: %s\n", utils.Red("Error"), err.Error()) s.Results <- "" 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.Flags.Fields) != 0 { lines += utils.Title.Sprintf("Filtering has not been implemented yet\n") break /* 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, } 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 } } } } data := strings.Split(content, "\n") for line := range data { result += strings.TrimRight(data[line], ":") } // Print results fmt.Println(result) */ } else { // Send results 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, ",")), ) data := strings.Split(results.Matches[match].Data, "\n") green := true for line := range data { values := strings.Split(data[line], ":") if green { 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 { 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 } } } } } lines += "\n" } } } // Remove empty newline // Send results lines = strings.TrimRight(lines, "\n") s.Results <- lines } // Todo: Redo filter sorting, pages