-
Notifications
You must be signed in to change notification settings - Fork 1
/
gorich.go
110 lines (95 loc) · 2.42 KB
/
gorich.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/domainr/whois"
"github.com/likexian/doh-go"
"github.com/likexian/simplejson-go"
"github.com/projectdiscovery/shodan/v2/pkg/shodan"
"github.com/tidwall/gjson"
)
var (
resolver = "1.1.1.1"
ipinfoToken string
hostFile string
shodanAPIKey string
shodanClient *shodan.Client
shodanCache = make(map[string]shodanResponse)
httpClient = &http.Client{Timeout: 10 * time.Second}
)
type shodanResponse struct {
OpenPorts []int
Vulns []string
}
type ipInfoResponse struct {
ASN string `json:"asn"`
Organization string `json:"org"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
}
func main() {
flag.StringVar(&hostFile, "hostfile", "", "A txt file with hosts per newline")
flag.StringVar(&ipinfoToken, "ipinfo-token", "", "API token for ipinfo.com")
flag.StringVar(&shodanAPIKey, "shodan-apikey", "", "API key for Shodan")
flag.Parse()
if hostFile == "" {
fmt.Println("[ERROR] missing required flag: --hostfile")
os.Exit(1)
}
if ipinfoToken == "" {
fmt.Println("[ERROR] missing required flag: --ipinfo-token")
os.Exit(1)
}
if shodanAPIKey == "" {
fmt.Println("[ERROR] missing required flag: --shodan-apikey")
os.Exit(1)
}
shodanClient = shodan.NewClient(nil, shodanAPIKey)
hosts, err := readHostFile(hostFile)
if err != nil {
fmt.Printf("[ERROR] failed to read host file: %v\n", err)
os.Exit(1)
}
ips, err := resolveHosts(hosts)
if err != nil {
fmt.Printf("[ERROR] failed to resolve hosts: %v\n", err)
os.Exit(1)
}
results := make([][]string, len(hosts))
for i, host := range hosts {
ip := ips[i]
openPorts, vulns, err := getShodanInfo(ip)
if err != nil {
fmt.Printf("[WARN] failed to get Shodan info for %s: %v\n", host, err)
}
ipInfo, err := getIpInfo(ip)
if err != nil {
fmt.Printf("[WARN] failed to get IP info for %s: %v\n", host, err)
}
results[i] = []string{
fmt.Sprintf("HOST-%d", i),
getRootDomain(host),
host,
ip,
intSliceToString(openPorts),
strings.Join(vulns, ","),
ipInfo.ASN,
ipInfo.Organization,
ipInfo.City,
ipInfo.Region,
ipInfo.Country,
}
}
fmt.Println("ID;ROOT;DOMAIN;IP;PORTS;CVE;ASN;ORG;CITY;REGION;COUNTRY")
for _, result := range results {
fmt.Println(strings.Join(result, ";"))
}
}
func readHostFile(filename string)