forked from yinqiwen/gscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
executable file
·275 lines (250 loc) · 6.7 KB
/
scan.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"crypto/tls"
"log"
"math/rand"
"net"
"net/http"
"net/http/httputil"
"strings"
"sync"
"sync/atomic"
"time"
)
type ScanRecord struct {
IP string
MatchHosts []string
PingRTT time.Duration
SSLRTT time.Duration
httpVerifyTimeout time.Duration
}
type ScanRecordArray []*ScanRecord
type ScanOptions struct {
Config *GScanConfig
recordMutex sync.Mutex
hostsMutex sync.Mutex
inputHosts HostIPTable
records ScanRecordArray
scanCounter int32
}
func (options *ScanOptions) AddRecord(rec *ScanRecord) {
options.recordMutex.Lock()
if nil == options.records {
options.records = make(ScanRecordArray, 0)
}
options.records = append(options.records, rec)
options.recordMutex.Unlock()
log.Printf("Found a record: IP=%s, SSLRTT=%fs\n", rec.IP, rec.SSLRTT.Seconds())
}
func (options *ScanOptions) IncScanCounter() {
atomic.AddInt32(&(options.scanCounter), 1)
if options.scanCounter%1000 == 0 {
log.Printf("Scanned %d IPs\n", options.scanCounter)
}
}
func (options *ScanOptions) RecordSize() int {
options.recordMutex.Lock()
defer options.recordMutex.Unlock()
return len(options.records)
}
func (options *ScanOptions) SSLMatchHosts(conn *tls.Conn) []string {
hosts := make([]string, 0)
options.hostsMutex.Lock()
for _, host := range options.inputHosts {
testhost := host.Host
if strings.Contains(testhost, ".appspot.com") {
testhost = "appengine.google.com"
} else if strings.Contains(testhost, "ggpht.com") {
testhost = "googleusercontent.com"
} else if strings.Contains(testhost, ".books.google.com") {
testhost = "books.google.com"
} else if strings.Contains(testhost, ".googleusercontent.com") {
testhost = "googleusercontent.com"
}
if conn.VerifyHostname(testhost) == nil {
hosts = append(hosts, host.Host)
}
}
options.hostsMutex.Unlock()
dest := make([]string, len(hosts))
perm := rand.Perm(len(hosts))
for i, v := range perm {
dest[v] = hosts[i]
}
hosts = dest
return hosts
}
func (options *ScanOptions) HaveHostInRecords(host string) bool {
options.hostsMutex.Lock()
defer options.hostsMutex.Unlock()
_, exists := options.inputHosts[host]
return !exists
}
func (options *ScanOptions) RemoveFromInputHosts(hosts []string) {
options.hostsMutex.Lock()
for _, host := range hosts {
delete(options.inputHosts, host)
}
options.hostsMutex.Unlock()
}
func (records *ScanRecordArray) Len() int {
return len(*records)
}
func (records *ScanRecordArray) Less(i, j int) bool {
return (*records)[i].SSLRTT < (*records)[j].SSLRTT
}
func (records *ScanRecordArray) Swap(i, j int) {
tmp := (*records)[i]
(*records)[i] = (*records)[j]
(*records)[j] = tmp
}
func matchHostnames(pattern, host string) bool {
if len(pattern) == 0 || len(host) == 0 {
return false
}
patternParts := strings.Split(pattern, ".")
hostParts := strings.Split(host, ".")
if len(patternParts) != len(hostParts) {
return false
}
for i, patternPart := range patternParts {
if patternPart == "*" {
continue
}
if patternPart != hostParts[i] {
return false
}
}
return true
}
func find_match_hosts(conn *tls.Conn, options *ScanOptions, record *ScanRecord) bool {
if nil == record.MatchHosts || len(record.MatchHosts) == 0 {
record.MatchHosts = options.SSLMatchHosts(conn)
}
newhosts := make([]string, 0)
for _, host := range record.MatchHosts {
if options.HaveHostInRecords(host) {
continue
}
newhosts = append(newhosts, host)
valid := true
for _, pattern := range options.Config.ScanGoogleHosts.HTTPVerifyHosts {
if matchHostnames(pattern, host) {
conn.SetReadDeadline(time.Now().Add(record.httpVerifyTimeout))
req, _ := http.NewRequest("HEAD", "https://"+host, nil)
res, err := httputil.NewClientConn(conn, nil).Do(req)
if nil != err || res.StatusCode >= 400 {
valid = false
}
break
}
}
if valid {
newhosts = append(newhosts, host)
}
}
record.MatchHosts = newhosts
return len(record.MatchHosts) > 0
}
func test_conn(conn *tls.Conn, options *ScanOptions, record *ScanRecord) bool {
//check SSL certificate
success := false
for _, cert := range conn.ConnectionState().PeerCertificates {
for _, verifyHost := range options.Config.ScanGoogleIP.SSLCertVerifyHosts {
if cert.VerifyHostname(verifyHost) != nil {
return false
} else {
success = true
}
}
if success {
break
}
}
for _, verifyHost := range options.Config.ScanGoogleIP.HTTPVerifyHosts {
conn.SetReadDeadline(time.Now().Add(record.httpVerifyTimeout))
req, _ := http.NewRequest("HEAD", "https://"+verifyHost, nil)
res, err := httputil.NewClientConn(conn, nil).Do(req)
if nil != err || res.StatusCode >= 400 {
return false
}
}
return true
}
func testip_once(ip string, options *ScanOptions, record *ScanRecord) bool {
start := time.Now()
var end time.Time
pingRTT := (options.Config.ScanMinPingRTT + options.Config.ScanMaxPingRTT) / 2
if options.Config.VerifyPing {
err := Ping(ip, options.Config.ScanMaxPingRTT)
if err != nil {
log.Printf("####error ip:%s for %v", ip, err)
return false
}
end = time.Now()
if nil == err {
if options.Config.ScanMinPingRTT > 0 && end.Sub(start) < options.Config.ScanMinPingRTT {
return false
}
pingRTT = end.Sub(start)
}
}
record.PingRTT = record.PingRTT + pingRTT
addr := net.JoinHostPort(ip, "443")
var config tls.Config
config.InsecureSkipVerify = true
dialer := new(net.Dialer)
dialer.Timeout = options.Config.ScanMaxSSLRTT
conn, err := tls.DialWithDialer(dialer, "tcp", addr, &config)
if err != nil {
//log.Printf("####ssl dial:%v", err)
return false
}
end = time.Now()
sslRTT := end.Sub(start)
if options.Config.ScanMinSSLRTT > 0 && sslRTT < options.Config.ScanMinSSLRTT {
conn.Close()
return false
}
success := true
record.httpVerifyTimeout = options.Config.ScanMaxSSLRTT - sslRTT
if options.Config.scanIP {
success = test_conn(conn, options, record)
} else {
success = find_match_hosts(conn, options, record)
}
//end = time.Now()
conn.Close()
if success {
record.SSLRTT = record.SSLRTT + sslRTT
}
return success
}
func testip(ip string, options *ScanOptions) *ScanRecord {
record := new(ScanRecord)
record.IP = ip
for i := 0; i < options.Config.ScanCountPerIP; i++ {
if !testip_once(ip, options, record) {
return nil
}
}
record.PingRTT = record.PingRTT / time.Duration(options.Config.ScanCountPerIP)
record.SSLRTT = record.SSLRTT / time.Duration(options.Config.ScanCountPerIP)
return record
}
func testip_worker(ch chan string, options *ScanOptions, w *sync.WaitGroup) {
for ip := range ch {
if len(ip) == 0 {
w.Done()
break
}
record := testip(ip, options)
if nil != record {
if !options.Config.scanIP {
options.RemoveFromInputHosts(record.MatchHosts)
}
options.AddRecord(record)
}
options.IncScanCounter()
}
}