-
Notifications
You must be signed in to change notification settings - Fork 45
/
scrape.go
190 lines (160 loc) · 4.26 KB
/
scrape.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"encoding/json"
"flag"
"fmt"
"net"
"os"
"strings"
"time"
)
type Result struct {
IP string
Hit bool
Error error
Certificate *CertificateInfo
}
type Worker struct {
dialer *net.Dialer
input <-chan string
results chan<- Result
}
type WorkerPool struct {
workers []*Worker
input chan string
results chan Result
dialer *net.Dialer
}
type CertificateInfo struct {
IP string `json:"ip"`
Organization string `json:"organization"`
CommonName string `json:"commonName"`
SAN string `json:"san"`
}
type ScrapeArgs struct {
Concurrency int
Ports []string
Timeout int
PortList string
Help bool
Input string
AllOutput bool
JSONOutput bool
}
func NewWorker(dialer *net.Dialer, input <-chan string, results chan<- Result) *Worker {
return &Worker{
dialer: dialer,
input: input,
results: results,
}
}
func (w *Worker) run() {
for ip := range w.input {
cert, err := getSSLCert(ip, w.dialer)
if err != nil {
w.results <- Result{IP: ip, Error: err}
continue
}
names := extractNames(cert)
org := cert.Subject.Organization
certInfo := CertificateInfo{
IP: ip,
Organization: getOrganization(org),
CommonName: names[0],
SAN: joinNonEmpty(", ", names[1:]),
}
w.results <- Result{IP: ip, Hit: true, Certificate: &certInfo}
}
}
func NewWorkerPool(size int, dialer *net.Dialer, input chan string, results chan Result) *WorkerPool {
wp := &WorkerPool{
workers: make([]*Worker, size),
input: input,
results: results,
dialer: dialer,
}
for i := range wp.workers {
wp.workers[i] = NewWorker(wp.dialer, wp.input, wp.results)
}
return wp
}
func (wp *WorkerPool) Start() {
for _, worker := range wp.workers {
go worker.run()
}
}
func (wp *WorkerPool) Stop() {
close(wp.input)
}
func runCloudScrape(clArgs []string) {
args := parseScrapeCLI(clArgs)
dialer := &net.Dialer{
Timeout: time.Duration(args.Timeout) * time.Second,
}
inputChannel := make(chan string)
resultChannel := make(chan Result)
workerPool := NewWorkerPool(args.Concurrency, dialer, inputChannel, resultChannel)
workerPool.Start()
go intakeFunction(inputChannel, args.Ports, args.Input)
defer func() {
workerPool.Stop()
close(resultChannel)
}()
for result := range resultChannel {
if result.Error != nil {
fmt.Printf("Failed to get SSL certificate from %s: %v\n", result.IP, result.Error)
} else if result.Hit {
if args.JSONOutput {
outputJSON, _ := json.Marshal(result.Certificate)
fmt.Println(string(outputJSON))
} else {
fmt.Printf("Got SSL certificate from %s: [%s]\n", result.IP, result.Certificate.CommonName)
}
} else if args.AllOutput {
fmt.Printf("No SSL certificate found for %s\n", result.IP)
}
}
}
func getOrganization(org []string) string {
if len(org) > 0 {
return org[0]
}
return "NONE"
}
func joinNonEmpty(sep string, elements []string) string {
var result string
for _, element := range elements {
if element != "" {
if result != "" {
result += sep
}
result += element
}
}
return result
}
func parseScrapeCLI(clArgs []string) ScrapeArgs {
args := ScrapeArgs{}
scrapeUsage := "scrape [options] -i <IPs/CIDRs or File>"
scrapeCommand := flag.NewFlagSet("scrape", flag.ContinueOnError)
scrapeCommand.IntVar(&args.Concurrency, "c", 100, "How many goroutines running concurrently")
scrapeCommand.StringVar(&args.PortList, "p", "443", "TLS ports to check for certificates")
scrapeCommand.IntVar(&args.Timeout, "t", 4, "Timeout for TLS handshake")
scrapeCommand.BoolVar(&args.Help, "h", false, "print usage!")
scrapeCommand.StringVar(&args.Input, "i", "NONE", "Either IPs & CIDRs separated by commas, or a file with IPs/CIDRs on each line")
scrapeCommand.BoolVar(&args.AllOutput, "a", false, "Add this flag if you want to see all output including failures")
scrapeCommand.BoolVar(&args.JSONOutput, "j", false, "Generate JSON output")
scrapeCommand.Parse(clArgs)
if args.Input == "NONE" {
fmt.Print("No input detected, please use the -i flag to add input!\n\n")
fmt.Println(scrapeUsage)
scrapeCommand.PrintDefaults()
os.Exit(1)
}
if args.Help {
fmt.Println(scrapeUsage)
scrapeCommand.PrintDefaults()
}
args.Ports = strings.Split(args.PortList, ",")
return args
}