forked from tumi8/goscanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
336 lines (275 loc) · 10.7 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"fmt"
"io"
"math"
"net"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/jessevdk/go-flags"
"github.com/tumi8/goscanner/scanner"
)
// opts specifies the command line arguments
var opts struct {
Config string `short:"C" long:"config" description:"Config file with options" no-ini:"true" value-name:"CONFIG-FILE"`
Input string `short:"i" long:"input-file" description:"Input file/named pipe with IPs or input directory" value-name:"INPUT"`
OutputDir string `short:"o" long:"output" description:"Output directory for scan results. If it already exists, the program will exit" value-name:"OUTPUT-DIR"`
DumpDir string `short:"s" long:"dump" description:"Output directory for certificate dump" value-name:"DUMP-DIR"`
DatabaseTable string `short:"d" long:"db-table" description:"Table name for PostgreSQL database" default:"simplehashes"`
LogFile string `short:"l" long:"log-file" description:"Log to file LOG-FILE (JSON formatted) instead of stderr" value-name:"LOG-FILE"`
Concurrency int `short:"c" long:"concurrency" description:"Number of concurrent scanning goroutines. By default it is (qps/1000)*(timeout + syn-timeout)" default:"0"`
QPS int `short:"q" long:"qps" description:"Number of queries per second" default:"100"`
Timeout int64 `short:"t" long:"timeout" description:"Timeout for connections in milliseconds" default:"10000"`
SynTimeout int64 `long:"syn-timeout" description:"Timeout for the TCP connection setup in milliseconds. If not specified the same as Timeout" default:"0"`
Seed string `short:"S" long:"seed" description:"Seed for IP Generator" value-name:"SEED" default:""`
Increment int64 `long:"increment" description:"How many IPs should be given out" value-name:"INCREMENT" default:"1"`
Offset int64 `long:"offset" description:"How many IPs should be skipped before start" value-name:"OFFSET" default:"0"`
SourceIP string `long:"source" description:"Source IP address if host has multiple addresses or interfaces" value-name:"SOURCE-IP" default:""`
Profile string `short:"p" long:"profile" description:"Output file for profiling"`
SkipErrors bool `long:"skip-errors" description:"Skip TCP errors like connection timeout and don't write them to output file"`
Verbose []bool `short:"v" long:"verbose" description:"Increase verbosity from warning to info or even debug"`
Version bool `short:"V" long:"version" description:"Show version information"`
HTTPHeaders string `long:"http-headers" description:"Establish HTTP connection and store headers specified as comma-separated list. No HTTP connection if omitted"`
SCSV bool `long:"scsv" description:"Send SCSV pseudo cipher suite"`
SSH bool `long:"ssh" description:"Scan SSH instead of TLS"`
}
// Output files
const (
fileHosts = "hosts.csv"
// files to save SSH scan results
fileHostKeys = "host_keys.csv"
fileRelations = "relations.csv"
// files to save TLS scan results
fileCerts = "certs.csv"
fileCertHostRel = "cert_host_rel.csv"
fileScsv = "scsv.csv"
fileHttp = "http.csv"
)
func init() {
// Set the number of Go processes to the number of CPUs
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
// Option parser
parser := flags.NewParser(&opts, flags.Default)
// Parse command line arguments
if _, err := parser.Parse(); err != nil {
if err.(*flags.Error).Type == flags.ErrHelp {
return
} else if err.(*flags.Error).Type != flags.ErrRequired {
log.Fatal(err)
}
}
// Parse config file
if opts.Config != "" {
iniParser := flags.NewIniParser(parser)
iniParser.ParseAsDefaults = true
err := iniParser.ParseFile(opts.Config)
if err != nil {
log.WithFields(log.Fields{
"file": opts.Config,
}).Fatal(err)
}
}
if opts.Version {
fmt.Println("Git version hash", scanner.ReadGitVersionFromAsset())
os.Exit(0)
}
if opts.LogFile != "" {
fh, err := os.Create(opts.LogFile)
if err != nil {
log.WithFields(log.Fields{
"file": opts.LogFile,
}).Fatal("Could not create log file")
}
log.SetOutput(fh)
log.SetFormatter(&log.JSONFormatter{})
}
if len(opts.Verbose) >= 2 {
log.SetLevel(log.DebugLevel)
} else if len(opts.Verbose) == 1 {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.WarnLevel)
}
// Check if CPU profiling should be done
if opts.Profile != "" {
fh, err := os.Create(opts.Profile)
if err != nil {
log.WithFields(log.Fields{
"file": opts.Profile,
}).Fatal("Could not create profile file")
}
pprof.StartCPUProfile(fh)
defer pprof.StopCPUProfile()
}
// By default the SynTimeout is the same as Timeout
if opts.SynTimeout == 0 {
opts.SynTimeout = opts.Timeout
}
// By default Concurrency is (qps/1000)*(timeout + syn-timeout)
if opts.Concurrency == 0 {
opts.Concurrency = int(math.Max(1, (float64(opts.QPS)/1000.0)*float64(opts.Timeout+opts.SynTimeout)))
}
// Check if all necessary options are given
contextLoggerOpts := log.WithFields(log.Fields{
"options": opts,
})
if (opts.Input == "" && opts.Seed == "") || (opts.Input != "" && opts.Seed != "") {
contextLoggerOpts.Fatal("Either input or seed must be specified")
}
if err := os.Mkdir(opts.OutputDir, 0755); err != nil {
contextLoggerOpts.Fatal("Output directory already exists")
}
var addr *net.TCPAddr
if opts.SourceIP != "" {
addr = getAddr(opts.SourceIP)
}
// Live scanning and database writing
if opts.DumpDir != "" {
// Get input files, output files and certificate dump directory
inFiles, outFiles, dumpDirs := getFiles(opts.Input, opts.OutputDir, opts.DumpDir)
// Do scanning for all valid input files
for i, inFile := range inFiles {
outFile := outFiles[i]
dumpDir := dumpDirs[i]
// Create scanner and start scanning
s := startTLSScanner(addr)
go func() {
scanner.ReadTargetsFromJSON(inFile, s.InputChannel(), scanner.NewMultiTLSTarget)
}()
proc, err := scanner.NewTLSLiveProcessor(outFile, dumpDir, opts.DatabaseTable)
if err != nil {
log.WithFields(log.Fields{
"outFile": outFile,
"dumpDir": dumpDir,
"databaseTable": opts.DatabaseTable,
}).Fatal(err)
}
scanner.Processor{proc, s.OutputChannel()}.Process()
}
} else {
// Create scanner and start scanning
var s scanner.Scanner
var target func(string) scanner.Target
if opts.SSH {
s = startSSHScanner(addr)
target = scanner.NewSSHTarget
} else {
s = startTLSScanner(addr)
target = scanner.NewCertHostTLSTarget
}
// Generate input targets
go func() {
if opts.Input != "" {
scanner.ReadIPsFromTxt(opts.Input, s.InputChannel(), target)
} else {
scanner.GenerateTargetsFromSeed(opts.Seed, opts.Increment, opts.Offset, s.InputChannel(), target)
}
}()
// Process results
var proc scanner.ResultProcessor
if opts.SSH {
fileHostKeys := filepath.Join(opts.OutputDir, fileHostKeys)
fileHosts := filepath.Join(opts.OutputDir, fileHosts)
fileRelations := filepath.Join(opts.OutputDir, fileRelations)
proc = scanner.NewSSHHostKeyHostProcessor(fileHostKeys, fileHosts, fileRelations, opts.SkipErrors)
} else {
fileCerts := filepath.Join(opts.OutputDir, fileCerts)
fileHosts := filepath.Join(opts.OutputDir, fileHosts)
fileCertHostRel := filepath.Join(opts.OutputDir, fileCertHostRel)
fileScsv := filepath.Join(opts.OutputDir, fileScsv)
fileHttp := filepath.Join(opts.OutputDir, fileHttp)
if !opts.SCSV {
fileScsv = ""
}
if opts.HTTPHeaders == "" {
fileHttp = ""
}
proc = scanner.NewTLSCertHostProcessor(fileCerts, fileHosts, fileCertHostRel, fileScsv, fileHttp, opts.SkipErrors)
}
// Process results
scanner.Processor{proc, s.OutputChannel()}.Process()
}
}
// startSSHScanner creates a SSHScanner, starts the scanning routine and returns the scanner
func startSSHScanner(addr *net.TCPAddr) scanner.Scanner {
// Create scanner and start scanning
s := scanner.Scanner{scanner.NewSSHScanner(), opts.Concurrency, opts.QPS, time.Duration(opts.Timeout) * time.Millisecond, time.Duration(opts.SynTimeout) * time.Millisecond, addr, opts.Input}
s.Scan()
return s
}
// startTLSScanner creates a TLSScanner, starts the scanning routine and returns the scanner
func startTLSScanner(addr *net.TCPAddr) scanner.Scanner {
// Create scanner and start scanning
s := scanner.Scanner{scanner.NewTLSScanner(opts.HTTPHeaders, opts.SCSV), opts.Concurrency, opts.QPS, time.Duration(opts.Timeout) * time.Millisecond, time.Duration(opts.SynTimeout) * time.Millisecond, addr, opts.Input}
s.Scan()
return s
}
// getAddr returns the correct *net.TCPAddr interface for an IP address in string format
func getAddr(sourceIP string) *net.TCPAddr {
ifaces, err := net.InterfaceAddrs()
if err != nil {
log.Fatal(err)
}
for _, iface := range ifaces {
// Only use IP address if specified as CIDR
if strings.Split(iface.String(), "/")[0] == sourceIP {
return &net.TCPAddr{IP: iface.(*net.IPNet).IP}
}
}
log.WithFields(log.Fields{
"ip": sourceIP,
"interfaces": ifaces,
}).Fatal("Source IP could not be matched to assigned interface addresses")
return nil
}
// getFiles returns slices of valid input files, output files and dump directories
func getFiles(inputDir, outputDir, scannedDir string) ([]string, []string, []string) {
var inRet, outRet, dumpRet []string
// Get input files from input directory
fh, err := os.OpenFile(inputDir, os.O_RDONLY, os.ModeDir)
if err != nil {
log.WithFields(log.Fields{
"directory": inputDir,
}).Fatal(err)
}
inFiles, err := fh.Readdirnames(0)
if err == io.EOF {
log.WithFields(log.Fields{
"directory": inputDir,
}).Fatal("No input file found")
} else if err != nil {
log.WithFields(log.Fields{
"directory": inputDir,
}).Fatal(err)
}
// Iterate through all potential input files
for _, inFile := range inFiles {
inFile = filepath.Join(inputDir, inFile)
// Extract timestamp from input file name
_, file := filepath.Split(inFile)
timestamp := file[:strings.Index(file, "_")]
// Get output file
outFile := filepath.Join(outputDir, timestamp+"_submoas.results")
// Get cert dump directory
dumpDir := filepath.Join(scannedDir, timestamp, "certs")
// Only scan the files if the dump directory does NOT yet exist
if _, err := os.Stat(dumpDir); os.IsNotExist(err) {
inRet = append(inRet, inFile)
outRet = append(outRet, outFile)
dumpRet = append(dumpRet, dumpDir)
}
}
return inRet, outRet, dumpRet
}
// Download and update the tls-parameters-4.csv asset file
//go:generate wget -q -O tls-parameters-4.csv http://www.iana.org/assignments/tls-parameters/tls-parameters-4.csv
//go:generate go get -u github.com/jteeuwen/go-bindata/...
//go:generate go-bindata -pkg asset -o scanner/asset/assets.go tls-parameters-4.csv git-version
//go:generate rm tls-parameters-4.csv