-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscan.go
314 lines (248 loc) · 6.18 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
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
package gonmap
import (
"errors"
"fmt"
"reflect"
)
// Scan contains basic scan information like hosts, ports, performance of scan
// and a flag for service version scan.
// Is the base for every scan type
type Scan struct {
hosts []string
ports []int
scripts []string
runScripts bool
performance int
versionScan bool
osDetection bool
}
// NewScan returns a new Scan with default values
func NewScan() Scan {
return Scan{
hosts: []string{},
ports: []int{},
scripts: []string{},
runScripts: false,
performance: 5,
versionScan: false,
osDetection: false,
}
}
// HasPort checks if scan has given port
func (s Scan) HasPort(p int) bool {
for _, v := range s.ports {
if p == v {
return true
}
}
return false
}
// HasHost checks if scan has given host
func (s Scan) HasHost(h string) bool {
if !isValidHost(h) {
return false
}
for _, v := range s.hosts {
if h == v {
return true
}
}
return false
}
// HasScript checks if scan has given script
func (s Scan) HasScript(script string) bool {
if script == "" {
return false
}
for _, scpt := range s.scripts {
if scpt == script {
return true
}
}
return false
}
// AddHost adds given host to scan if it's a valid host
// and returns error otherwise
func (s *Scan) AddHost(h string) error {
if h == "" {
return errors.New("Parameter must be a created Host")
}
if !isValidHost(h) {
return errors.New("Parameter must be a valid Host")
}
if !s.HasHost(h) {
s.hosts = append(s.hosts, h)
}
return nil
}
// AddHosts adds all given hosts to scan if all hosts are valid
// and returns error otherwise
func (s *Scan) AddHosts(hosts []string) error {
if hosts == nil {
return errors.New("Parameter must be a initialized slice")
}
for _, h := range hosts {
if !isValidHost(h) {
return errors.New("Hosts mus be all valid")
}
}
for _, h := range hosts {
if !s.HasHost(h) {
s.hosts = append(s.hosts, h)
}
}
return nil
}
// AddPort adds given port to scan if is a valid port
// and returns error otherwise
func (s *Scan) AddPort(p int) error {
if p < 0 || p > 65536 {
return errors.New("Port parameter must be an integer between 0 and 65536")
}
if !s.HasPort(p) {
s.ports = append(s.ports, p)
}
return nil
}
// AddTopPorts adds top n ports to scan
func (s *Scan) AddTopPorts(n int) error {
if n <= 0 {
return errors.New("Parameter should be an integer 1 or greater")
}
if n > 65536 {
return errors.New("Parameter is too large")
}
ports, err := getTopPorts(n)
if err != nil {
return err
}
s.ports = nil
s.AddPorts(ports)
return nil
}
// AddPortRange adds ports from min to max if min and max are valid bounds
// and returns error otherwise
func (s *Scan) AddPortRange(min int, max int) error {
if min < 0 || min > 65536 {
return errors.New("Min parameter must be an integer between 0 and 65536")
}
if max < 0 || max > 65536 {
return errors.New("Max parameter must be an integer between 0 and 65536")
}
if min > max {
return errors.New("Max parameter must be bigger or equal to min parameter")
}
for p := min; p <= max; p++ {
if !s.HasPort(p) {
s.ports = append(s.ports, p)
}
}
return nil
}
// AddPorts adds all given ports to scan if all ports are valid
// and returns error otherwise
func (s *Scan) AddPorts(ports []int) error {
if ports == nil {
return errors.New("Ports slice must be with almost an element")
}
for _, p := range ports {
if p < 0 || p > 65536 {
return errors.New("All ports must be an integer between 0 and 65536")
}
}
for _, p := range ports {
if !s.HasPort(p) {
s.ports = append(s.ports, p)
}
}
return nil
}
// SetPerformance sets scan performance if
// performance parameter is an integer between 0 and 5
func (s *Scan) SetPerformance(performance int) error {
if performance < 0 || performance > 5 {
return errors.New("Performance must be between 0 and 5")
}
s.performance = performance
return nil
}
// SetVersionScan sets service version scan. Nmap flag: -sV
func (s *Scan) SetVersionScan(choise bool) {
s.versionScan = choise
}
// SetOsDetection sets operative system detection flag if choise is true. Nmap flag: -O
func (s *Scan) SetOsDetection(choise bool) {
s.osDetection = choise
}
// AddScript adds given script to scan
func (s *Scan) AddScript(script string) error {
if script == "" {
return errors.New("Script parameter must be a valid script")
}
s.runScripts = true
s.scripts = append(s.scripts, script)
return nil
}
// AddScripts adds all given scripts to scan
func (s *Scan) AddScripts(scripts []string) error {
for _, scpt := range scripts {
if scpt == "" {
return errors.New("Scripts parameter must contain all valid script")
}
}
s.runScripts = true
for _, scpt := range scripts {
if !s.HasScript(scpt) {
s.scripts = append(s.scripts, scpt)
}
}
return nil
}
// RunScripts sets scripts running if choise is true.
// Setting this true without adding scripts it's equivalent to a scan with default scripts. Flag: -sC
func (s *Scan) RunScripts(choise bool) {
s.runScripts = choise
}
// GetHosts returns hosts set
func (s Scan) GetHosts() []string {
ret := make([]string, len(s.hosts))
copy(ret, s.hosts)
return ret
}
// GetPorts returns ports set
func (s Scan) GetPorts() []int {
ret := make([]int, len(s.ports))
copy(ret, s.ports)
return ret
} // TODO test
// GetScripts returns scripts set
func (s Scan) GetScripts() []string {
ret := make([]string, len(s.scripts))
copy(ret, s.scripts)
return ret
}
// GetPerformance returns performance
func (s Scan) GetPerformance() int {
return s.performance
}
// GetOsDetection returns os detection choise
func (s Scan) GetOsDetection() bool {
return s.osDetection
}
// GetRunScript returns script running choise
func (s Scan) GetRunScript() bool {
return s.runScripts
}
func (s Scan) String() string {
return fmt.Sprintf("Scan{%v, %v}", s.hosts, s.ports)
}
// IsEqual cheks if two scans are equal
func (s Scan) IsEqual(s1 Scan) bool {
return reflect.DeepEqual(s.hosts, s1.hosts) &&
reflect.DeepEqual(s.ports, s1.ports) &&
reflect.DeepEqual(s.scripts, s1.scripts) &&
s.runScripts == s1.runScripts &&
s.performance == s1.performance &&
s.osDetection == s1.osDetection &&
s.versionScan == s1.versionScan
}