2023-08-01 22:35:47 +00:00
|
|
|
package shodan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ns3777k/go-shodan/v4/shodan"
|
|
|
|
|
|
|
|
"git.tcp.direct/perp/shogo/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Return shodan search results
|
2023-08-11 23:25:58 +00:00
|
|
|
func (s *Shodan) Search() {
|
2023-08-01 22:35:47 +00:00
|
|
|
// Setup options
|
|
|
|
options := &shodan.HostQueryOptions{
|
2023-08-11 23:25:58 +00:00
|
|
|
Query: s.Flags.Query,
|
|
|
|
Page: s.Flags.Page,
|
2023-08-01 22:35:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
// Store lines
|
|
|
|
var lines string
|
|
|
|
|
2023-08-01 22:35:47 +00:00
|
|
|
// More than 2 matches
|
|
|
|
if len(results.Matches) > 2 {
|
|
|
|
// Go through matches
|
|
|
|
for match := range results.Matches {
|
2023-08-11 23:25:58 +00:00
|
|
|
if len(s.Flags.Fields) != 0 {
|
|
|
|
lines += utils.Title.Sprintf("Filtering has not been implemented yet\n")
|
|
|
|
break
|
2023-08-01 22:35:47 +00:00
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
/*
|
|
|
|
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
|
2023-08-01 22:35:47 +00:00
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
// 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
|
|
|
|
}
|
2023-08-01 22:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
data := strings.Split(content, "\n")
|
|
|
|
for line := range data {
|
|
|
|
result += strings.TrimRight(data[line], ":")
|
|
|
|
}
|
2023-08-01 22:35:47 +00:00
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
// Print results
|
|
|
|
fmt.Println(result)
|
|
|
|
*/
|
2023-08-01 22:35:47 +00:00
|
|
|
} else {
|
|
|
|
// Send results
|
2023-08-11 23:25:58 +00:00
|
|
|
lines += fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n",
|
2023-08-01 22:35:47 +00:00
|
|
|
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, ",")),
|
|
|
|
)
|
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
data := strings.Split(results.Matches[match].Data, "\n")
|
2023-08-01 22:35:47 +00:00
|
|
|
green := true
|
2023-08-11 23:25:58 +00:00
|
|
|
for line := range data {
|
|
|
|
values := strings.Split(data[line], ":")
|
2023-08-01 22:35:47 +00:00
|
|
|
if green {
|
2023-08-11 23:25:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-01 22:35:47 +00:00
|
|
|
} else {
|
2023-08-11 23:25:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-01 22:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-11 23:25:58 +00:00
|
|
|
lines += "\n"
|
2023-08-01 22:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 23:25:58 +00:00
|
|
|
|
|
|
|
// Remove empty newline
|
|
|
|
// Send results
|
|
|
|
lines = strings.TrimRight(lines, "\n")
|
|
|
|
s.Results <- lines
|
2023-08-01 22:35:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 23:25:58 +00:00
|
|
|
// Todo: Redo filter sorting, pages
|