-
Notifications
You must be signed in to change notification settings - Fork 105
/
iflist.go
143 lines (122 loc) · 3.16 KB
/
iflist.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
package nmap
import (
"bytes"
"net"
"os/exec"
"regexp"
"strconv"
"strings"
)
// InterfaceList contains interfaces and routes.
type InterfaceList struct {
Interfaces []*Interface `json:"interfaces"`
Routes []*Route `json:"routes"`
}
// Interface is a 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"`
}
// GetInterfaceList runs nmap with the --iflist option. The output will be parsed.
// The return value is a struct containing all host interfaces and routes.
func (s *Scanner) GetInterfaceList() (result *InterfaceList, err error) {
var stdout, stderr bytes.Buffer
args := append(s.args, "--iflist")
// Prepare nmap process
cmd := exec.Command(s.binaryPath, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Run nmap process
err = cmd.Run()
if err != nil {
return nil, err
}
result = parseInterfaces(stdout.Bytes())
return result, nil
}
func parseInterfaces(content []byte) *InterfaceList {
list := InterfaceList{
Interfaces: make([]*Interface, 0),
Routes: make([]*Route, 0),
}
output := string(content)
lines := strings.Split(output, "\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],
}
if ip, val, err := net.ParseCIDR(fields[2]); err == nil {
iface.IP = ip
iface.IPMask = net.IP(val.Mask)
}
iface.Up = strings.ToLower(fields[4]) == "up"
if val, err := strconv.Atoi(fields[5]); err == nil {
iface.MTU = val
}
if len(fields) > 6 {
if val, err := net.ParseMAC(fields[6]); err == nil {
iface.Mac = val
}
}
return iface
}
func convertRoute(line string) *Route {
fields := strings.Fields(line)
if len(fields) < 3 {
return nil
}
route := &Route{
Device: fields[1],
}
if ip, val, err := net.ParseCIDR(fields[0]); err == nil {
route.DestinationIP = ip
route.DestinationIPMask = net.IP(val.Mask)
}
if val, err := strconv.Atoi(fields[2]); err == nil {
route.Metric = val
}
if len(fields) > 3 {
route.Gateway = net.ParseIP(fields[3])
}
return route
}