-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathiflist.go
More file actions
154 lines (129 loc) · 3.29 KB
/
iflist.go
File metadata and controls
154 lines (129 loc) · 3.29 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
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
package nmap
import (
"bytes"
"context"
"net"
"os/exec"
"regexp"
"strconv"
"strings"
)
// InterfaceList contains interfaces and routes.
type InterfaceList struct {
Interfaces []*Interface `json:"interfaces"`
Routes []*Route `json:"routes"`
}
// Interface is an interface object.
type Interface struct {
Device string `json:"device"`
Short string `json:"short"`
IP net.IP `json:"ip"`
IPMask net.IP `json:"ip_mask"`
Type string `json:"type"`
Up bool `json:"up"`
MTU int `json:"mtu"`
Mac net.HardwareAddr `json:"mac"`
}
// Route is a route object.
type Route struct {
DestinationIP net.IP `json:"destination_ip"`
DestinationIPMask net.IP `json:"destination_ip_mask"`
Device string `json:"device"`
Metric int `json:"metric"`
Gateway net.IP `json:"gateway"`
}
// InterfaceList runs nmap with the --iflist option.
// The return value is a struct containing all host interfaces and routes.
func (s *Scanner) InterfaceList(ctx context.Context) (*InterfaceList, error) {
args := append([]string{}, s.args...)
args = append(args, "--iflist")
// Prepare nmap process
//nolint:gosec // Arguments are passed directly to nmap; users intentionally control args.
cmd := exec.CommandContext(ctx, s.binaryPath, args...)
// Bind stdout and stderr.
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Run nmap process.
err := cmd.Run()
if err != nil {
return nil, err
}
return parseInterfaces(stdout.String()), nil
}
func parseInterfaces(content string) *InterfaceList {
list := InterfaceList{
Interfaces: make([]*Interface, 0),
Routes: make([]*Route, 0),
}
lines := strings.Split(content, "\n")
interfaceRegex := regexp.MustCompile(`\*INTERFACES\*`)
routesRegex := regexp.MustCompile(`\*ROUTES\*`)
for i, line := range lines {
if interfaceRegex.MatchString(line) {
for _, l := range lines[i+2:] {
if iface := convertInterface(l); iface != nil {
list.Interfaces = append(list.Interfaces, iface)
}
}
}
if routesRegex.MatchString(line) {
for _, l := range lines[i+2:] {
if route := convertRoute(l); route != nil {
list.Routes = append(list.Routes, route)
}
}
}
}
return &list
}
func convertInterface(line string) *Interface {
fields := strings.Fields(line)
if len(fields) < 6 {
return nil
}
iface := &Interface{
Device: fields[0],
Short: fields[1],
Type: fields[3],
}
ip, ipnet, err := net.ParseCIDR(fields[2])
if err == nil {
iface.IP = ip
iface.IPMask = net.IP(ipnet.Mask)
}
iface.Up = strings.ToLower(fields[4]) == "up"
mtu, err := strconv.Atoi(fields[5])
if err == nil {
iface.MTU = mtu
}
if len(fields) > 6 {
mac, err := net.ParseMAC(fields[6])
if err == nil {
iface.Mac = mac
}
}
return iface
}
func convertRoute(line string) *Route {
fields := strings.Fields(line)
if len(fields) < 3 {
return nil
}
route := &Route{
Device: fields[1],
}
ip, ipnet, err := net.ParseCIDR(fields[0])
if err == nil {
route.DestinationIP = ip
route.DestinationIPMask = net.IP(ipnet.Mask)
}
metric, err := strconv.Atoi(fields[2])
if err == nil {
route.Metric = metric
}
if len(fields) > 3 {
route.Gateway = net.ParseIP(fields[3])
}
return route
}