-
Notifications
You must be signed in to change notification settings - Fork 106
/
main.go
80 lines (66 loc) · 1.9 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
package main
import (
"context"
"fmt"
"log"
"github.com/Ullaakut/nmap/v3"
)
func main() {
ifaceScanner, err := nmap.NewScanner(context.Background())
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
interfaceList, err := ifaceScanner.GetInterfaceList()
if err != nil {
log.Fatalf("could not get interface list: %v", err)
}
if len(interfaceList.Interfaces) == 0 {
log.Fatal("no interface to scan with")
}
lastInterfaceIndex := len(interfaceList.Interfaces) - 1
interfaceToScan := interfaceList.Interfaces[lastInterfaceIndex].Device
// Equivalent to
// nmap -S 192.168.0.10 \
// -D 192.168.0.2,192.168.0.3,192.168.0.4,192.168.0.5,192.168.0.6,ME,192.168.0.8 \
// 192.168.0.72`.
scanner, err := nmap.NewScanner(
context.Background(),
nmap.WithInterface(interfaceToScan),
nmap.WithTargets("192.168.0.72"),
nmap.WithSpoofIPAddress("192.168.0.10"),
nmap.WithDecoys(
"192.168.0.2",
"192.168.0.3",
"192.168.0.4",
"192.168.0.5",
"192.168.0.6",
"ME",
"192.168.0.8",
),
)
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
fmt.Println("Running the following nmap command:", scanner.Args())
result, warnings, err := scanner.Run()
if len(*warnings) > 0 {
log.Printf("run finished with warnings: %s\n", *warnings) // Warnings are non-critical errors from nmap.
}
if err != nil {
log.Fatalf("nmap scan failed: %v", err)
}
printResults(result)
}
func printResults(result *nmap.Run) {
// Use the results to print an example output
for _, host := range result.Hosts {
if len(host.Ports) == 0 || len(host.Addresses) == 0 {
continue
}
fmt.Printf("Host %q:\n", host.Addresses[0])
for _, port := range host.Ports {
fmt.Printf("\tPort %d/%s %s %s\n", port.ID, port.Protocol, port.State, port.Service.Name)
}
}
fmt.Printf("Nmap done: %d hosts up scanned in %.2f seconds\n", len(result.Hosts), result.Stats.Finished.Elapsed)
}