-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.go
744 lines (559 loc) · 13.8 KB
/
functions.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
package gonmap
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os/exec"
"strconv"
"strings"
"sync"
)
var openedPipes = 0
var maxOpenedPipes = sync.NewCond(new(sync.Mutex))
// ScanTecnique contains all methods that implement a scan tecnique
type ScanTecnique interface {
TCPScan()
UDPScan()
SYNScan()
ACKScan()
FINScan()
NULLScan()
XmasScan()
WindowScan()
MaimonScan()
IDLEScan()
}
// HostDiscovery contains all methods that implement an host discovery scan
type HostDiscovery interface {
SYNDiscovery()
ACKDiscovery()
UDPDiscovery()
SCTPDiscovery()
}
type settings struct {
performance string
versionScan string
osDetection string
zombie string // IDLE scan argument only
scripts string
}
func arrayToString(a []int, delim string) string {
return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]")
}
func getSettings(s Scan) settings {
configuration := settings{}
// adds performance flag to configuration
switch s.performance {
case 0:
configuration.performance = "-T0"
case 1:
configuration.performance = "-T1"
case 2:
configuration.performance = "-T2"
case 3:
configuration.performance = "-T3"
case 4:
configuration.performance = "-T4"
case 5:
configuration.performance = "-T5"
}
// adds version scan flag to configuration
if s.versionScan {
configuration.versionScan = "-sV"
}
// adds os detection to configuration
if s.osDetection {
configuration.osDetection = ""
}
if s.runScripts {
if len(s.scripts) == 0 {
configuration.scripts = "-sC"
} else {
configuration.scripts = "--script=" + strings.Join(s.scripts, ",")
}
}
return configuration
}
func getTopPorts(n int) ([]int, error) {
res := []int{}
// Finds nmap binary path
nmap, err := exec.LookPath("nmap")
if err != nil {
return nil, err
}
args := []string{
"-v",
"-oX",
"-",
"--top-ports",
fmt.Sprintf("%d", n),
}
cmd := exec.Cmd{
Path: nmap,
Args: args,
}
// Configure output pipes
errPipe, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
outPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
// Start command
if err := cmd.Start(); err != nil {
return nil, err
}
stdout, err := ioutil.ReadAll(outPipe)
if err != nil {
return nil, err
}
stderr, err := ioutil.ReadAll(errPipe)
if err != nil {
return nil, err
}
// Waits nmap command
if err := cmd.Wait(); err != nil {
return nil, errors.New(err.Error() + "\n" + string(stderr))
}
data := nmapXMLParse(stdout).Info.Services
splitData := strings.Split(data, ",")
for _, v := range splitData {
if strings.Contains(v, "-") {
bounds := strings.Split(v, "-")
min, err := strconv.Atoi(bounds[0])
if err != nil {
return nil, err
}
max, err := strconv.Atoi(bounds[1])
if err != nil {
return nil, err
}
for j := min; j <= max; j++ {
res = append(res, j)
}
} else {
intVal, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
res = append(res, intVal)
}
}
return res, nil
}
// makes a simple port scan with given configuration
func portScan(config settings, scanFlag, host string, ports []int, dataChan chan NmapRun) {
// Finds nmap binary path
nmap, err := exec.LookPath("nmap")
if err != nil {
log.Fatal(err)
}
args := []string{
config.performance,
config.versionScan,
config.osDetection,
scanFlag,
config.zombie,
fmt.Sprintf("-p%s", arrayToString(ports, ",")),
config.scripts,
"-oX",
"-",
"-vvvvv",
host,
}
cmd := exec.Cmd{
Path: nmap,
Args: args,
}
// checks if there are already 400 opened pipes
maxOpenedPipes.L.Lock()
// waits for at least 2 close on pipes
for openedPipes > 400 {
maxOpenedPipes.Wait()
}
openedPipes += 2
maxOpenedPipes.L.Unlock()
// Configure output pipes
errPipe, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
outPipe, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
// Start command
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
stdout, err := ioutil.ReadAll(outPipe)
if err != nil {
log.Fatal(err)
}
stderr, err := ioutil.ReadAll(errPipe)
if err != nil {
log.Fatal(err)
}
// Waits nmap command
if err := cmd.Wait(); err != nil {
log.Fatal(errors.New(err.Error() + "\n" + string(stderr)))
}
// signals waiting goroutines
maxOpenedPipes.L.Lock()
openedPipes -= 2
maxOpenedPipes.Signal()
maxOpenedPipes.L.Unlock()
dataChan <- nmapXMLParse(stdout)
}
func osDetection(ports []int, host string, dataChan chan Host) {
// Finds nmap binary path
nmap, err := exec.LookPath("nmap")
if err != nil {
log.Fatal(err)
}
args := []string{
fmt.Sprintf("-p%s", arrayToString(ports, ",")),
"-O",
"-oX",
"-",
host,
}
cmd := exec.Cmd{
Path: nmap,
Args: args,
}
// checks if there are already 400 opened pipes
maxOpenedPipes.L.Lock()
// waits for at least 2 close on pipes
for openedPipes > 400 {
maxOpenedPipes.Wait()
}
openedPipes += 2
maxOpenedPipes.L.Unlock()
// Configure output pipes
errPipe, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
outPipe, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
// Start command
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
stdout, err := ioutil.ReadAll(outPipe)
if err != nil {
log.Fatal(err)
}
stderr, err := ioutil.ReadAll(errPipe)
if err != nil {
log.Fatal(err)
}
// Waits nmap command
if err := cmd.Wait(); err != nil {
log.Fatal(errors.New(err.Error() + "\n" + string(stderr)))
}
// signals waiting goroutines
maxOpenedPipes.L.Lock()
openedPipes -= 2
maxOpenedPipes.Signal()
maxOpenedPipes.L.Unlock()
data := nmapXMLParse(stdout)
dataChan <- data.Hosts[0]
}
// merge run NmapRun with new NmapRun
func mergeRuns(run, new NmapRun) NmapRun {
if run.Hosts == nil {
return new
}
res := NmapRun{}
// ScanInfo merge
res.Info.Type = new.Info.Type
res.Info.Protocol = new.Info.Protocol
if !strings.Contains(run.Info.Services, new.Info.Services) {
res.Info.NumServices = run.Info.NumServices + new.Info.NumServices
res.Info.Services = run.Info.Services + "," + new.Info.Services
} else {
res.Info.NumServices = run.Info.NumServices
res.Info.Services = run.Info.Services
}
// Hosts merge
res.Hosts = run.Hosts
if run.Hosts[len(run.Hosts)-1].Address.Value == new.Hosts[0].Address.Value {
res.Hosts[len(res.Hosts)-1].PortList.Port = append(res.Hosts[len(res.Hosts)-1].PortList.Port, new.Hosts[0].PortList.Port...)
} else {
res.Hosts = append(res.Hosts, new.Hosts[0])
}
return res
}
// runs scan with given configuration
func runScan(config settings, ports []int, hosts []string, osDet bool, scanFlag string) NmapRun {
counter := 0
chunkSize := 0
nChunks := 0
res := NmapRun{}
osData := map[string]OsInfo{}
// calculates number and size of chunks
if len(ports)/10 < 1 {
chunkSize = 1
nChunks = len(ports)
} else {
chunkSize = len(ports) / 10
tmp := float64(len(ports)) / float64(chunkSize)
if tmp-float64(int(tmp)) > 0 {
nChunks = int(tmp) + 1
} else {
nChunks = int(tmp)
}
}
runResults := make([]chan NmapRun, nChunks*len(hosts))
osResults := make([]chan Host, len(hosts))
// initializing runResult
for i := range runResults {
runResults[i] = make(chan NmapRun)
}
// initializing osResult
for i := range osResults {
osResults[i] = make(chan Host)
}
// launching goroutines:
// one go routines every chunkSize port for every host
for i, host := range hosts {
if osDet {
go osDetection(ports, host, osResults[i])
}
for _, group := range chunkBy(ports, chunkSize) {
go portScan(config, scanFlag, host, group, runResults[counter])
counter++
}
}
// waiting for os discovery routines end
if osDet {
for _, c := range osResults {
data := <-c
osData[data.Address.Value] = data.OsInfo
}
}
// waiting for scan routines end
for _, c := range runResults {
data := <-c
res = mergeRuns(res, data)
}
// adding os discovery info to res
for i, h := range res.Hosts {
res.Hosts[i].OsInfo = osData[h.Address.Value]
}
return res
}
// TCPScan is generally used to check and complete a three-way handshake
// between you and a chosen target system. Flag: -sT
func (s Scan) TCPScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sT")
return data, nil
}
// UDPScan are used to check whether there is any UDP port up and
// listening for incoming requests on the target machine. Flag: -sU
func (s Scan) UDPScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sU")
return data, nil
}
// SYNScan is another form of TCP scan. The difference is unlike a normal TCP scan, nmap itself crafts a syn packet,
// which is the first packet that is sent to establish a TCP connection. Flag: -sS
func (s Scan) SYNScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sS")
return data, nil
}
// ACKScan are used to determine whether a particular port is filtered or not. Flag: -sA
func (s Scan) ACKScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sA")
return data, nil
}
// FINScan is like SYN scan, but sends a TCP FIN packet instead. Flag: -sF
func (s Scan) FINScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sF")
return data, nil
}
// NULLScan are extremely stealthy scan and what they do
// is as the name suggests — they set all the header fields to null. Flag: -sN
func (s Scan) NULLScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sN")
return data, nil
}
// XmasScan is just like null scans, these are also stealthy in nature. Flag -sX
func (s Scan) XmasScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sX")
return data, nil
}
// WindowScan is exactly the same as ACK scan except that it exploits
// an implementation detail of certain systems to differentiate open ports
// from closed ones, rather than always printing unfiltered when a RST is returned. Flag: -sW
func (s Scan) WindowScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sW")
return data, nil
}
// MaimonScan is exactly the same as NULL, FIN, and Xmas scan, except that the probe is FIN/ACK.
// Flag: -sM
func (s Scan) MaimonScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sM")
return data, nil
}
// IDLEScan is the stealthiest of all scans as the packets are bounced off an external host.
// Flag: -sI
func (s Scan) IDLEScan(zombie string) (NmapRun, error) {
if !isValidHost(zombie) {
return NmapRun{}, errors.New("Zombie target must be a valid host")
}
config := getSettings(s)
config.zombie = zombie
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-sI")
return data, nil
}
// AggressiveScan makes a scan with version scan (-sV), os detection (-O), script scanning (-sC) and traceroute
func (s Scan) AggressiveScan() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, true, "-A")
return data, nil
}
// SYNDiscovery makes a TCP SYN discovery
func (s Scan) SYNDiscovery() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-PS")
return data, nil
}
// ACKDiscovery makes a TCP ACK discovery
func (s Scan) ACKDiscovery() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-PA")
return data, nil
}
// UDPDiscovery makes an UDP discovery
func (s Scan) UDPDiscovery() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-PU")
return data, nil
}
// SCTPDiscovery makes an SCTP discovery
func (s Scan) SCTPDiscovery() (NmapRun, error) {
config := getSettings(s)
if (len(s.ports)) == 0 {
ports, err := getTopPorts(1000)
if err != nil {
return NmapRun{}, err
}
s.ports = ports
}
data := runScan(config, s.ports, s.hosts, s.osDetection, "-PY")
return data, nil
}