forked from yinqiwen/gscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gscan.go
executable file
·169 lines (157 loc) · 4.26 KB
/
gscan.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
)
type ScanGoogleIPConfig struct {
HTTPVerifyHosts []string
SSLCertVerifyHosts []string
RecordLimit int
OutputFile string
OutputSeparator string
}
type ScanGoogleHostsConfig struct {
InputHosts string
OutputHosts string
HTTPVerifyHosts []string
}
type GScanConfig struct {
VerifyPing bool
ScanMinPingRTT time.Duration
ScanMaxPingRTT time.Duration
ScanMinSSLRTT time.Duration
ScanMaxSSLRTT time.Duration
ScanWorker int
ScanCountPerIP int
Operation string
scanIP bool
ScanGoogleIP ScanGoogleIPConfig
ScanGoogleHosts ScanGoogleHostsConfig
}
func main() {
iprange_file := flag.String("iprange", "./iprange.conf", "IP Range file")
conf_file := flag.String("conf", "./gscan.conf", "Config file, json format")
flag.Parse()
conf_content, err := ioutil.ReadFile(*conf_file)
if nil != err {
fmt.Printf("%v\n", err)
return
}
var cfg GScanConfig
err = json.Unmarshal(conf_content, &cfg)
if nil != err {
fmt.Printf("%v\n", err)
return
}
cfg.scanIP = strings.EqualFold(cfg.Operation, "ScanGoogleIP")
cfg.ScanMaxSSLRTT = cfg.ScanMaxSSLRTT * time.Millisecond
cfg.ScanMinSSLRTT = cfg.ScanMinSSLRTT * time.Millisecond
cfg.ScanMaxPingRTT = cfg.ScanMaxPingRTT * time.Millisecond
cfg.ScanMinPingRTT = cfg.ScanMinPingRTT * time.Millisecond
var outputfile *os.File
var outputfile_path string
if cfg.scanIP {
outputfile_path = cfg.ScanGoogleIP.OutputFile
} else {
outputfile_path = cfg.ScanGoogleHosts.OutputHosts
}
outputfile_path, _ = filepath.Abs(outputfile_path)
outputfile, err = os.OpenFile(outputfile_path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
if nil != err {
fmt.Printf("%v\n", err)
return
}
options := ScanOptions{
Config: &cfg,
}
if !cfg.scanIP {
options.inputHosts, err = parseHostsFile(cfg.ScanGoogleHosts.InputHosts)
if nil != err {
fmt.Printf("%v\n", err)
return
}
}
log.Printf("Start loading IP Range file:%s\n", *iprange_file)
ipranges, err := parseIPRangeFile(*iprange_file)
if nil != err {
fmt.Printf("%v\n", err)
return
}
worker_count := cfg.ScanWorker
ch := make(chan string)
var w sync.WaitGroup
w.Add(worker_count)
for i := 0; i < worker_count; i++ {
go testip_worker(ch, &options, &w)
}
log.Printf("Start scanning available IP\n")
start_time := time.Now()
eval_count := 0
for _, iprange := range ipranges {
var i int64
for i = iprange.StartIP; i <= iprange.EndIP; i++ {
ch <- inet_ntoa(i).String()
eval_count = eval_count + 1
if cfg.scanIP {
if options.RecordSize() >= cfg.ScanGoogleIP.RecordLimit {
goto _end
}
} else {
if len(options.inputHosts) == 0 {
goto _end
}
}
}
}
_end:
for i := 0; i < worker_count; i++ {
ch <- ""
}
w.Wait()
close(ch)
log.Printf("Scanned %d IP in %fs, found %d records\n", eval_count, time.Now().Sub(start_time).Seconds(), len(options.records))
sort.Sort(&(options.records))
if cfg.scanIP {
ss := make([]string, 0)
for _, rec := range options.records {
ss = append(ss, rec.IP)
}
_, err = outputfile.WriteString(strings.Join(ss, cfg.ScanGoogleIP.OutputSeparator))
if nil != err {
log.Printf("Failed to write output file:%s for reason:%v\n", outputfile_path, err)
}
} else {
outputfile.WriteString(fmt.Sprintf("###############Update %s###############\n", time.Now().Format("2006-01-02 15:04:05")))
outputfile.WriteString("###############GScan Hosts Begin#################\n")
domains_set := make(map[string]int)
for _, rec := range options.records {
for _, domain := range rec.MatchHosts {
if _, exists := domains_set[domain]; !exists {
outputfile.WriteString(fmt.Sprintf("%s\t%s\n", rec.IP, domain))
domains_set[domain] = 1
}
}
}
outputfile.WriteString("###############GScan Hosts End##################\n")
outputfile.WriteString("\n")
outputfile.WriteString("#No available IP found,inherit from input\n")
for _, h := range options.inputHosts {
outputfile.WriteString(fmt.Sprintf("%s\t%s\n", h.IP, h.Host))
}
}
err = outputfile.Close()
if nil != err {
log.Printf("Failed to close output file:%s for reason:%v\n", outputfile_path, err)
} else {
log.Printf("All results writed to %s\n", outputfile_path)
}
}