forked from Kisesy/gscan_quic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan.go
280 lines (246 loc) · 6.54 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
276
277
278
279
280
package gscan
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
quic "github.com/phuslu/quic-go"
"github.com/phuslu/quic-go/h2quic"
)
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, Found %d records\n", options.scanCounter, options.RecordSize())
}
}
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 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
}
var (
tlsCfg = &tls.Config{
InsecureSkipVerify: true,
}
g2pkp, _ = base64.StdEncoding.DecodeString("7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=")
g3pkp, _ = base64.StdEncoding.DecodeString("f8NnEFZxQ4ExFOhSN7EiFWtiudZQVD2oY60uauV/n78=")
g3ecc, _ = base64.StdEncoding.DecodeString("ekG8/PoSqjfKunOaS9iIzR3hZAJptWJKV7CgyriO+MA=")
)
func testip_once(ip string, options *ScanOptions, record *ScanRecord) bool {
start := time.Now()
pingRTT := (options.Config.ScanMinPingRTT + options.Config.ScanMaxPingRTT) / 2
if options.Config.VerifyPing {
err := Ping(ip, options.Config.ScanMaxPingRTT)
if err != nil {
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")
start = time.Now()
success := make(chan bool, 5)
go func() {
<-time.After(options.Config.ScanMaxSSLRTT + 500*time.Millisecond)
success <- false
}()
var quicSessn quic.Session
defer func() {
if quicSessn != nil {
quicSessn.Close(nil)
}
}()
tr := &h2quic.RoundTripper{DisableCompression: true}
defer tr.Close()
quicCfg := &quic.Config{
HandshakeTimeout: options.Config.ScanMaxSSLRTT - 500*time.Millisecond,
// IdleTimeout: options.Config.ScanMaxSSLRTT,
KeepAlive: false,
}
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return false
}
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return false
}
udpConn.SetDeadline(time.Now().Add(options.Config.ScanMaxSSLRTT))
defer udpConn.Close()
go func(chan bool) {
var err error
// quicSessn, err = quic.DialAddr(addr, tlsCfg, quicCfg)
quicSessn, err = quic.Dial(udpConn, udpAddr, addr, tlsCfg, quicCfg)
if err != nil {
success <- false
return
}
// 证书验证
cs := quicSessn.ConnectionState()
if cs == nil {
success <- false
return
}
pcs := cs.PeerCertificates
if len(pcs) < 2 {
success <- false
return
}
pkp := sha256.Sum256(pcs[1].RawSubjectPublicKeyInfo)
if !bytes.Equal(g2pkp, pkp[:]) && !bytes.Equal(g3pkp, pkp[:]) && !bytes.Equal(g3ecc, pkp[:]) {
success <- false
return
}
tr.DialAddr = func(hostname string, tlsConfig *tls.Config, config *quic.Config) (quic.Session, error) {
return quicSessn, err
}
hclient := &http.Client{
Transport: tr,
}
for _, verifyHost := range options.Config.ScanGoogleIP.HTTPVerifyHosts {
req, _ := http.NewRequest(http.MethodHead, "https://"+verifyHost, nil)
req.Close = true
resp, err := hclient.Do(req)
if resp != nil && resp.Body != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
if nil != err || resp.StatusCode >= 400 {
success <- false
return
}
}
success <- true
}(success)
if <-success == false {
return false
}
sslRTT := time.Since(start)
record.SSLRTT = record.SSLRTT + sslRTT
return true
}
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, wg *sync.WaitGroup) {
for ip := range ch {
record := testip(ip, options)
if nil != record {
if !options.Config.scanIP {
options.RemoveFromInputHosts(record.MatchHosts)
}
options.AddRecord(record)
}
options.IncScanCounter()
}
wg.Done()
}