| 1 | + | package main |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "fmt" |
| 5 | + | "net/http" |
| 6 | + | "io/ioutil" |
| 7 | + | "encoding/json" |
| 8 | + | "flag" |
| 9 | + | "bufio" |
| 10 | + | "os" |
| 11 | + | "strings" |
| 12 | + | "github.com/logrusorgru/aurora" |
| 13 | + | ) |
| 14 | + | |
| 15 | + | type Response struct { |
| 16 | + | CPES []string |
| 17 | + | Hostnames []string |
| 18 | + | IP string |
| 19 | + | Ports []int32 |
| 20 | + | Tags []string |
| 21 | + | Vulns []string |
| 22 | + | } |
| 23 | + | |
| 24 | + | func main() { |
| 25 | + | |
| 26 | + | var noCPEs bool |
| 27 | + | flag.BoolVar(&noCPEs, "nc", false, "hide cpes") |
| 28 | + | |
| 29 | + | var noHostnames bool |
| 30 | + | flag.BoolVar(&noHostnames, "nh", false, "hide hostnames") |
| 31 | + | |
| 32 | + | var noTags bool |
| 33 | + | flag.BoolVar(&noTags, "nt", false, "hide tags") |
| 34 | + | |
| 35 | + | var noVulns bool |
| 36 | + | flag.BoolVar(&noVulns, "nv", false, "hide vulnerabilities") |
| 37 | + | |
| 38 | + | var noColor bool |
| 39 | + | flag.BoolVar(&noColor, "nocolor", false, "disable color in output") |
| 40 | + | |
| 41 | + | flag.Parse() |
| 42 | + | |
| 43 | + | |
| 44 | + | var ips []string |
| 45 | + | |
| 46 | + | if flag.NArg() > 0 { |
| 47 | + | ips = []string{flag.Arg(0)} |
| 48 | + | } else { |
| 49 | + | sc := bufio.NewScanner(os.Stdin) |
| 50 | + | for sc.Scan() { |
| 51 | + | ips = append(ips, sc.Text()) |
| 52 | + | } |
| 53 | + | if err := sc.Err(); err != nil { |
| 54 | + | fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err) |
| 55 | + | } |
| 56 | + | } |
| 57 | + | |
| 58 | + | channel := make(chan Response) |
| 59 | + | for _, ip := range ips { |
| 60 | + | go getData(ip, channel) |
| 61 | + | } |
| 62 | + | |
| 63 | + | for i:=0; i<len(ips); i++ { |
| 64 | + | printResult(<-channel, noCPEs, noHostnames, noTags, noVulns, noColor) |
| 65 | + | } |
| 66 | + | } |
| 67 | + | |
| 68 | + | |
| 69 | + | func getData(ip string, channel chan Response) { |
| 70 | + | |
| 71 | + | res, err := http.Get( |
| 72 | + | fmt.Sprintf("https://internetdb.shodan.io/%s", ip), |
| 73 | + | ) |
| 74 | + | |
| 75 | + | if err != nil { |
| 76 | + | return |
| 77 | + | } |
| 78 | + | |
| 79 | + | raw, err := ioutil.ReadAll(res.Body) |
| 80 | + | |
| 81 | + | if err != nil { |
| 82 | + | return |
| 83 | + | } |
| 84 | + | |
| 85 | + | res.Body.Close() |
| 86 | + | |
| 87 | + | var wrapper Response |
| 88 | + | err = json.Unmarshal(raw, &wrapper) |
| 89 | + | |
| 90 | + | if err != nil { |
| 91 | + | return |
| 92 | + | } |
| 93 | + | |
| 94 | + | channel <- wrapper |
| 95 | + | } |
| 96 | + | |
| 97 | + | func printResult(output Response, noCPEs bool, noHostnames bool, noTags bool, noVulns bool, noColor bool) { |
| 98 | + | |
| 99 | + | builder := &strings.Builder{} |
| 100 | + | |
| 101 | + | fmt.Println(output.IP) |
| 102 | + | |
| 103 | + | ports := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(output.Ports)), ", "), "[]") |
| 104 | + | |
| 105 | + | if !noColor { |
| 106 | + | builder.WriteString("Ports: " + aurora.Green(ports).String() + "\n") |
| 107 | + | } else { |
| 108 | + | builder.WriteString("Ports: " + ports + "\n") |
| 109 | + | } |
| 110 | + | |
| 111 | + | if (!noCPEs && len(output.CPES) > 0) { |
| 112 | + | cpes := strings.Join(output.CPES, ", ") |
| 113 | + | if !noColor { |
| 114 | + | builder.WriteString("CPEs: " + aurora.Yellow(cpes).String() + "\n") |
| 115 | + | } else { |
| 116 | + | builder.WriteString("CPEs: " + cpes + "\n") |
| 117 | + | } |
| 118 | + | } |
| 119 | + | |
| 120 | + | if (!noVulns && len(output.Vulns) > 0) { |
| 121 | + | vulns := strings.Join(output.Vulns, ", ") |
| 122 | + | if !noColor { |
| 123 | + | builder.WriteString("Vulnerabilities: " + aurora.Red(vulns).String() + "\n") |
| 124 | + | } else { |
| 125 | + | builder.WriteString("Vulnerabilities: " + vulns + "\n") |
| 126 | + | } |
| 127 | + | } |
| 128 | + | |
| 129 | + | if (!noHostnames && len(output.Hostnames) > 0) { |
| 130 | + | hostnames := strings.Join(output.Hostnames, ", ") |
| 131 | + | if !noColor { |
| 132 | + | builder.WriteString("Hostnames: " + aurora.Blue(hostnames).String() + "\n") |
| 133 | + | } else { |
| 134 | + | builder.WriteString("Hostnames: " + hostnames + "\n") |
| 135 | + | } |
| 136 | + | } |
| 137 | + | |
| 138 | + | if (!noTags && len(output.Tags) > 0) { |
| 139 | + | tags := strings.Join(output.Tags, ", ") |
| 140 | + | if !noColor { |
| 141 | + | builder.WriteString("Tags: " + aurora.Magenta(tags).String() + "\n") |
| 142 | + | } else { |
| 143 | + | builder.WriteString("Tags: " + tags + "\n") |
| 144 | + | } |
| 145 | + | } |
| 146 | + | |
| 147 | + | fmt.Println(builder.String()) |
| 148 | + | } |