/
disc.go
357 lines (326 loc) · 6.96 KB
/
disc.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
// Package disc is a LAN discovery library
package disc
import (
"bufio"
"encoding/csv"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"time"
"github.com/olekukonko/tablewriter"
"github.com/mehrdadrad/mylg/cli"
)
const (
// IEEEOUI holds ieee oui csv file url
IEEEOUI = "http://standards.ieee.org/develop/regauth/oui/oui.csv"
)
// ARP holds ARP information
type ARP struct {
IP string
MAC string
Interface string
Host string
}
// disc holds all discovery information
type disc struct {
Table []ARP
IPs []string
OUI map[string]string
IsMac bool
SKey string
}
// New creates new discovery object
func New(args string) *disc {
key, flag := cli.Flag(args)
// help
if _, ok := flag["help"]; ok {
help()
return nil
}
return &disc{
IsMac: IsMac(),
OUI: make(map[string]string, 25000),
SKey: key,
}
}
// WalkIP tries to salk through subnet as generator
func WalkIP(cidr string) chan string {
c := make(chan string, 1)
go func() {
defer close(c)
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
println(err.Error())
return
}
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); nextIP(ip) {
c <- ip.String()
}
}()
return c
}
// PingLan tries to send a tiny UDP packet to all LAN hosts
func (a *disc) PingLan() {
var isV4 bool
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
println(err.Error())
return
}
defer syscall.Close(fd)
// Set options
syscall.SetsockoptInt(fd, 0x0, syscall.IP_TTL, 10)
ifs, _ := net.Interfaces()
for _, i := range ifs {
addrs, _ := i.Addrs()
if i.Flags != 19 {
continue
}
// ip network(s) that assigned to interface(s)
for _, addr := range addrs {
if strings.IndexAny(addr.String(), "::") != -1 {
isV4 = false
} else {
isV4 = true
}
if isV4 {
a.IPs = append(a.IPs, addr.String())
for ipStr := range WalkIP(addr.String()) {
ip := net.ParseIP(ipStr).To4()
addr := syscall.SockaddrInet4{
Port: 33434,
Addr: [4]byte{ip[0], ip[1], ip[2], ip[3]},
}
syscall.Sendto(fd, []byte{}, 0, &addr)
}
} else {
//IPv6 doesn't support for the time being
// ToDo
}
}
}
}
func nextIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// StrToByte16 converts string to 16 bytes
func StrTobyte16(s string) [16]byte {
var r [16]byte
if len(s) > 16 {
copy(r[:], s)
} else {
copy(r[16-len(s):], s)
}
return r
}
// GetARPTable gets ARP table
func (a *disc) GetARPTable() error {
if a.IsMac {
return a.GetMACOSARPTable()
} else {
return a.GetLinuxARPTable()
}
return fmt.Errorf("")
}
// GetLinuxARPTable gets Linux ARP table
func (a *disc) GetLinuxARPTable() error {
var host string
file, err := os.Open("/proc/net/arp")
if err != nil {
return err
}
s := bufio.NewScanner(file)
s.Scan() // skip description
for s.Scan() {
fields := strings.Fields(s.Text())
hosts, _ := net.LookupAddr(fields[0])
if len(hosts) == 0 {
host = "NA"
} else {
host = hosts[0]
}
a.Table = append(a.Table, ARP{IP: fields[0], Host: host, MAC: fields[3], Interface: fields[5]})
}
return nil
}
// GetMACOSARPTable gets Mac OS X ARP table
func (a *disc) GetMACOSARPTable() error {
var host string
cmd := exec.Command("arp", "-an")
outBytes, err := cmd.Output()
if err != nil {
return err
}
out := strings.TrimSpace(string(outBytes))
for _, l := range strings.Split(out, "\n") {
fields := strings.Fields(l)
if len(fields) < 1 {
continue
}
if fields[3] != "(incomplete)" {
fields[1] = strings.Trim(fields[1], ")")
fields[1] = strings.Trim(fields[1], "(")
hosts, _ := net.LookupAddr(fields[1])
if len(hosts) == 0 {
host = "NA"
} else {
host = hosts[0]
}
a.Table = append(a.Table, ARP{IP: fields[1], Host: host, MAC: fields[3], Interface: fields[5]})
}
}
return nil
}
// LoadOUI
func (a *disc) LoadOUI() bool {
if _, ok := cache("validate", nil); ok {
if c, ok := cache("read", nil); ok {
r := csv.NewReader(strings.NewReader(c))
records, _ := r.ReadAll()
for _, record := range records {
if len(record) > 2 {
a.OUI[record[1]] = record[2]
}
}
return true
}
} else {
b, err := GetOUILive()
if err != nil {
println(err.Error())
return false
}
if c, ok := cache("write", b); ok {
r := csv.NewReader(strings.NewReader(c))
records, _ := r.ReadAll()
for _, record := range records {
if len(record) > 2 {
a.OUI[record[1]] = record[2]
}
}
return true
}
}
return false
}
// GETOUI gets oui info from iEEE
func GetOUILive() ([]byte, error) {
resp, err := http.Get(IEEEOUI)
if err != nil {
return []byte{}, fmt.Errorf("regauth.standards.ieee.org is unreachable (1)")
}
if resp.StatusCode != 200 {
return []byte{}, fmt.Errorf("regauth.standards.ieee.org returns none 200 HTTP code")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("peeringdb.com is unreachable (2) %s", err.Error())
}
return body, nil
}
func cache(r string, b []byte) (string, bool) {
var (
err error
res string
)
switch r {
case "write":
err = ioutil.WriteFile("/tmp/mylg.disc", b, 0644)
if err != nil {
return "", false
}
res = string(b)
return res, true
case "read":
b, err := ioutil.ReadFile("/tmp/mylg.disc")
if err != nil {
return "", false
}
res = string(b)
return res, true
case "validate":
f, err := os.Stat("/tmp/mylg.disc")
if err != nil {
return "", false
}
d := time.Since(f.ModTime())
if d.Hours() > 24*10 {
return "", false
}
}
return "", true
}
// PrintPretty prints ARP table
func (a *disc) PrintPretty() {
var (
orgName string
counter = 0
)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"IP", "MAC", "Host", "Interface", "Organization Name"})
for _, arp := range a.Table {
// OUI
if name, ok := a.OUI[strings.ToUpper(strings.Replace(arp.MAC, ":", "", -1))[:6]]; ok {
if len(name) > 25 {
orgName = name[:25] + "..."
} else {
orgName = name
}
} else {
orgName = "NA"
}
// Search
sHost := search(arp.Host, a.SKey)
sARP := search(arp.IP, a.SKey)
sMAC := search(arp.MAC, a.SKey)
sOrg := search(orgName, a.SKey)
if !sHost && !sARP && !sMAC && !sOrg {
continue
}
table.Append([]string{arp.IP, arp.MAC, arp.Host, arp.Interface, orgName})
counter++
}
table.Render()
println(counter, "host has been found")
}
// IsMac checks OS
func IsMac() bool {
if runtime.GOOS != "darwin" {
return false
}
return true
}
// search tries to find key at data
func search(data, key string) bool {
data = strings.ToLower(data)
key = strings.ToLower(key)
if strings.Contains(data, key) {
return true
}
return false
}
// help shows disc help
func help() {
fmt.Println(`
Network LAN Discovery
usage:
disc [search keyword]
Example:
disc
disc 5c:a:5b:aa:4a:99
disc apple
disc 192.168.0.10
`)
}