-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
278 lines (237 loc) · 7.09 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
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/tumi8/goscanner/scanner"
"github.com/tumi8/goscanner/scanner/cmd"
"github.com/tumi8/goscanner/scanner/misc"
"github.com/tumi8/goscanner/scanner/results"
log2 "log"
"net"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"syscall"
"time"
)
var GitBranch, GitHash string
// opts specifies the command line arguments
var opts misc.Options
func init() {
// Set the number of Go processes to the number of CPUs
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
log.Logger = log.Output(zerolog.NewConsoleWriter())
// Option parser
parser := flags.NewParser(&opts, flags.Default)
var subCommand cmd.ScannerCommand
parser.CommandHandler = func(command flags.Commander, args []string) error {
if command, ok := command.(cmd.ScannerCommand); ok {
subCommand = command
} else if command != nil {
log.Fatal().Msg("Command was not a ScannerCommand")
}
return nil
}
parser.SubcommandsOptional = true
_, err := parser.Parse()
// Parse command line arguments
if err != nil {
if err.(*flags.Error).Type == flags.ErrHelp {
return
} else if err.(*flags.Error).Type != flags.ErrRequired {
log.Fatal().Err(err).Msg("Error parsing command line")
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) // syscall.SIGHUP and syscall.SIGTERM maybe as well
cSigpipe := make(chan os.Signal, 1)
signal.Notify(cSigpipe, syscall.SIGPIPE)
go sigPipeListener(cSigpipe)
// Parse config file
if opts.Config != "" {
iniParser := flags.NewIniParser(parser)
iniParser.ParseAsDefaults = true
err := iniParser.ParseFile(opts.Config)
if err != nil {
log.Fatal().Err(err).Str("file", opts.Config).Msg("Error parsing config file")
}
}
if opts.Version {
fmt.Printf("Git version hash %v\n", GitHash)
os.Exit(1)
}
if opts.LogFile != "" {
fh, err := os.Create(opts.LogFile)
if err != nil {
log.Fatal().Err(err).Str("file", opts.LogFile).Msg("Error creating log file")
}
log.Logger = log.Output(fh)
}
log2.SetOutput(misc.LogWriter{})
if len(opts.Verbose) >= 2 {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else if len(opts.Verbose) == 1 {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
log.Info().Str("branch", GitBranch).Str("hash", GitHash).Msg("")
// By default the SynTimeout is the same as Timeout
if opts.SynTimeout == 0 {
opts.SynTimeout = opts.Timeout
}
// By default Concurrency is qps * (max(timeout,syn-timeout) in seconds)
if opts.Concurrency == 0 {
opts.Concurrency = int(float64(opts.QPS) * (float64(misc.MaxInt64(opts.Timeout, opts.SynTimeout)) * float64(time.Millisecond) / float64(time.Second)))
}
if subCommand != nil {
err := subCommand.Run()
if err == cmd.ErrPrintHelp {
parser.WriteHelp(os.Stderr)
} else if err != nil {
log.Fatal().Err(err).Msg("Error running command")
}
os.Exit(0)
}
// Check if all necessary options are given
contextLoggerOpts := log.Fatal().Interface("options", opts)
if opts.Input == "" {
contextLoggerOpts.Msg("Input has to be given")
} else if opts.Input == "-" && !misc.IsStdInPresent() {
contextLoggerOpts.Msg("Stdin was defined as input but is not present.")
}
if err := os.Mkdir(opts.OutputDir, 0755); err != nil {
log.Fatal().Str("directory", opts.OutputDir).Err(err).Msg("Output directory already exists")
}
// Check if CPU profiling should be done
if opts.Profile != "" {
cpuFile := filepath.Join(opts.OutputDir, opts.Profile)
fh, err := os.Create(cpuFile)
if err != nil {
log.Fatal().Str("file", opts.Profile).Msg("Could not create profile file")
}
err = pprof.StartCPUProfile(fh)
if err != nil {
log.Err(err).Msg("Could not start CPU Profiling")
}
defer pprof.StopCPUProfile()
}
var addr *net.TCPAddr
if opts.SourceIP != "" {
addr = getAddr(opts.SourceIP)
}
misc.LoadClientHellos(&opts)
if len(opts.Scans) == 0 {
// Set the default behaviour
if opts.SSH {
opts.Scans = []string{"ssh"}
} else {
// If nothing is configured do TLS scan
opts.Scans = []string{"tls"}
// If HTTP Headers are given do http
if opts.HTTPHeaders != "" || len(opts.HTTPRequests) != 0 {
opts.Scans = append(opts.Scans, "http")
}
if opts.SCSV {
opts.Scans = append(opts.Scans, "scvs")
}
}
}
var interruptTarget *scanner.Scanner
go func() {
<-c
log.Log().Msg("INTERRUPTED - Gracefully shutting down. Press again to force exit")
misc.PrintStacktrace(true)
if interruptTarget != nil {
interruptTarget.Interrupt()
} else {
log.Warn().Msg("Could not interrupt scanner")
}
if opts.Profile != "" {
pprof.StopCPUProfile()
}
<-c
log.Log().Msg("INTERRUPTED - Forcing exit")
os.Exit(1)
}()
// Create scanner and start scanning
var s *scanner.Scanner
if opts.SSH {
s = startScanner(addr, []string{"ssh"})
} else {
s = startScanner(addr, opts.Scans)
}
interruptTarget = s
// Generate input targets
go func() {
scanner.ReadTargetsToChannel(opts, s.Input)
}()
// Process results
var proc scanner.ResultProcessor
if opts.SSH {
fileHostKeys := filepath.Join(opts.OutputDir, results.FileHostKeys)
fileHosts := filepath.Join(opts.OutputDir, results.FileHosts)
fileRelations := filepath.Join(opts.OutputDir, results.FileRelations)
proc = *scanner.NewSSHHostKeyHostProcessor(fileHostKeys, fileHosts, fileRelations, opts.SkipErrors)
} else {
cacheFunc := misc.GetSHA256
if opts.HashCache == "sha1" {
cacheFunc = misc.GetSHA1
} else if opts.HashCache == "none" {
cacheFunc = nil
}
proc = scanner.NewCsvProcessor(opts.OutputDir, opts.SkipErrors, cacheFunc)
}
// Process results
scanner.Processor{ResultProcessor: proc, OutputChan: s.Output}.Process()
if opts.MemProfile {
misc.DumpMemProfile(opts.OutputDir, "final")
}
}
func startScanner(addr *net.TCPAddr, activatesScans []string) *scanner.Scanner {
// Create scanner and start scanning
s := scanner.NewScanner(&opts, addr, activatesScans)
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(err).Msg("")
}
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.Fatal().Str("ip", sourceIP).Interface("interface", ifaces).Msg("Source IP could not be matched to assigned interface addresses")
return nil
}
func sigPipeListener(c chan os.Signal) {
msg := <-c
event := log.Debug()
var rlim syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
if err != nil {
event.AnErr("RLimit fetching error", err)
} else {
event.Uint64("rlim.Max", rlim.Max)
event.Uint64("rlim.Cur", rlim.Cur)
}
if runtime.GOOS == "linux" {
misc.RunCommandToLog(exec.Command("bash", "-c", fmt.Sprintf("ls /proc/%v/fd | wc -l", os.Getpid())), event)
}
event.
Str("Signal", msg.String()).
Msg("SIGPIPE signal received, is there a problem with too many sockets?")
}