Projects STRLCPY shodanidb Commits 511f274b
🤬
  • initial release

  • Loading...
  • s4hm4d committed 2 years ago
    511f274b
  • ■ ■ ■ ■ ■ ■
    README.md
     1 +# shodanIDB
     2 + 
     3 +A command-line tool to fetch data (open ports, CVEs, CPEs, ...) from [Shodan internetDB API](https://internetdb.shodan.io/). Free to use and no API key required.
     4 + 
     5 + 
     6 +## Installation
     7 + 
     8 +```
     9 +go install -v github.com/s4hm4d/shodanidb
     10 +```
     11 + 
     12 + 
     13 +## Usage
     14 + 
     15 +```
     16 +echo [ip] | shodanidb [options]
     17 + 
     18 + 
     19 +Options:
     20 + -nc Hide CPEs
     21 + -nh Hide hostnames
     22 + -nt Hide tags
     23 + -nv Hide vulnerabilities
     24 + -nocolor Disable color in output
     25 + 
     26 + 
     27 +Examples:
     28 + echo [ip] | shodanidb -nt
     29 + cat ips.txt | shodanidb -nh
     30 +```
     31 + 
     32 + 
     33 +## Credit
     34 + 
     35 +The original tool is [nrich](https://gitlab.com/shodan-public/nrich). I wanted to learn Go and write this tool with Go for practise.
     36 + 
  • ■ ■ ■ ■ ■ ■
    go.mod
     1 +module github.com/s4hm4d/shodanidb
     2 + 
     3 +go 1.17
     4 + 
     5 +require (
     6 + github.com/logrusorgru/aurora v2.0.3+incompatible
     7 +)
     8 + 
  • ■ ■ ■ ■ ■ ■
    go.sum
     1 +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
     2 +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
     3 + 
  • ■ ■ ■ ■ ■ ■
    shodanidb.go
     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 +}
Please wait...
Page is in error, reload to recover