forked from zmap/zgrab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
149 lines (129 loc) · 4.92 KB
/
config.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
package zgrab2
import (
"net"
"net/http"
"os"
"runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
// Config is the high level framework options that will be parsed
// from the command line
type Config struct {
OutputFileName string `short:"o" long:"output-file" default:"-" description:"Output filename, use - for stdout"`
InputFileName string `short:"f" long:"input-file" default:"-" description:"Input filename, use - for stdin"`
MetaFileName string `short:"m" long:"metadata-file" default:"-" description:"Metadata filename, use - for stderr"`
LogFileName string `short:"l" long:"log-file" default:"-" description:"Log filename, use - for stderr"`
Senders int `short:"s" long:"senders" default:"1000" description:"Number of send goroutines to use"`
Debug bool `long:"debug" description:"Include debug fields in the output."`
Flush bool `long:"flush" description:"Flush after each line of output."`
GOMAXPROCS int `long:"gomaxprocs" default:"0" description:"Set GOMAXPROCS"`
ConnectionsPerHost int `long:"connections-per-host" default:"1" description:"Number of times to connect to each host (results in more output)"`
ReadLimitPerHost int `long:"read-limit-per-host" default:"96" description:"Maximum total kilobytes to read for a single host (default 96kb)"`
Prometheus string `long:"prometheus" description:"Address to use for Prometheus server (e.g. localhost:8080). If empty, Prometheus is disabled."`
CustomDNS string `long:"dns" description:"Address of a custom DNS server for lookups. Default port is 53."`
Multiple MultipleCommand `command:"multiple" description:"Multiple module actions"`
inputFile *os.File
outputFile *os.File
metaFile *os.File
logFile *os.File
inputTargets InputTargetsFunc
outputResults OutputResultsFunc
localAddr *net.TCPAddr
}
// SetInputFunc sets the target input function to the provided function.
func SetInputFunc(f InputTargetsFunc) {
config.inputTargets = f
}
// SetOutputFunc sets the result output function to the provided function.
func SetOutputFunc(f OutputResultsFunc) {
config.outputResults = f
}
func init() {
config.Multiple.ContinueOnError = true // set default for multiple value
config.Multiple.BreakOnSuccess = false // set default for multiple value
}
var config Config
func validateFrameworkConfiguration() {
// validate files
if config.LogFileName == "-" {
config.logFile = os.Stderr
} else {
var err error
if config.logFile, err = os.Create(config.LogFileName); err != nil {
log.Fatal(err)
}
log.SetOutput(config.logFile)
}
SetInputFunc(InputTargetsCSV)
if config.InputFileName == "-" {
config.inputFile = os.Stdin
} else {
var err error
if config.inputFile, err = os.Open(config.InputFileName); err != nil {
log.Fatal(err)
}
}
if config.OutputFileName == "-" {
config.outputFile = os.Stdout
} else {
var err error
if config.outputFile, err = os.Create(config.OutputFileName); err != nil {
log.Fatal(err)
}
}
outputFunc := OutputResultsWriterFunc(config.outputFile)
SetOutputFunc(outputFunc)
if config.MetaFileName == "-" {
config.metaFile = os.Stderr
} else {
var err error
if config.metaFile, err = os.Create(config.MetaFileName); err != nil {
log.Fatal(err)
}
}
// Validate Go Runtime config
if config.GOMAXPROCS < 0 {
log.Fatalf("invalid GOMAXPROCS (must be positive, given %d)", config.GOMAXPROCS)
}
runtime.GOMAXPROCS(config.GOMAXPROCS)
//validate/start prometheus
if config.Prometheus != "" {
go func() {
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(config.Prometheus, nil); err != nil {
log.Fatalf("could not run prometheus server: %s", err.Error())
}
}()
}
//validate senders
if config.Senders <= 0 {
log.Fatalf("need at least one sender, given %d", config.Senders)
}
// validate connections per host
if config.ConnectionsPerHost <= 0 {
log.Fatalf("need at least one connection, given %d", config.ConnectionsPerHost)
}
// Stop the lowliest idiot from using this to DoS people
if config.ConnectionsPerHost > 50 {
log.Fatalf("connectionsPerHost must be in the range [0,50]")
}
// Stop even third-party libraries from performing unbounded reads on untrusted hosts
if config.ReadLimitPerHost > 0 {
DefaultBytesReadLimit = config.ReadLimitPerHost * 1024
}
// Validate custom DNS
if config.CustomDNS != "" {
var err error
if config.CustomDNS, err = addDefaultPortToDNSServerName(config.CustomDNS); err != nil {
log.Fatalf("invalid DNS server address: %s", err)
}
}
}
// GetMetaFile returns the file to which metadata should be output
func GetMetaFile() *os.File {
return config.metaFile
}
func includeDebugOutput() bool {
return config.Debug
}