-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (66 loc) · 1.87 KB
/
main.go
File metadata and controls
82 lines (66 loc) · 1.87 KB
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
package main
import (
"context"
"log"
"github.com/Ullaakut/nmap/v4"
)
func main() {
ctx := context.Background()
scanner, err := nmap.NewScanner()
if err != nil {
log.Fatalf("creating nmap scanner: %v", err)
}
interfaceList, err := scanner.InterfaceList(ctx)
if err != nil {
log.Fatalf("getting 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 \
// scanme.nmap.org`.
scanner, err = nmap.NewScanner(
nmap.WithInterface(interfaceToScan),
nmap.WithTargets("scanme.nmap.org"),
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("creating nmap scanner: %v", err)
}
log.Println("Running the following nmap command:", scanner.Args())
result, err := scanner.Run(ctx)
if err != nil {
log.Fatalf("running network scan: %v", err)
}
warnings := result.Warnings()
if len(warnings) > 0 {
log.Printf("warning: %v\n", warnings) // Warnings are non-critical errors from nmap.
}
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
}
log.Printf("Host %q:\n", host.Addresses[0])
for _, port := range host.Ports {
log.Printf("\tPort %d/%s %s %s\n", port.ID, port.Protocol, port.State, port.Service.Name)
}
}
log.Printf("Nmap done: %d hosts up scanned in %.2f seconds\n", len(result.Hosts), result.Stats.Finished.Elapsed)
}