-
Notifications
You must be signed in to change notification settings - Fork 277
/
device.go
230 lines (223 loc) · 5.89 KB
/
device.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
package core
import (
"context"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"ksubdomain/gologger"
"net"
"time"
)
func AutoGetDevices() EthTable {
domain := RandomStr(4) + "paper.seebug.org"
signal := make(chan EthTable)
devices, err := pcap.FindAllDevs()
if err != nil {
gologger.Fatalf("获取网络设备失败:%s\n", err.Error())
}
data := make(map[string]net.IP)
keys := []string{}
for _, d := range devices {
for _, address := range d.Addresses {
ip := address.IP
if ip.To4() != nil && !ip.IsLoopback() {
data[d.Name] = ip
keys = append(keys, d.Name)
}
}
}
ctx := context.Background()
// 在初始上下文的基础上创建一个有取消功能的上下文
ctx, cancel := context.WithCancel(ctx)
for _, drviceName := range keys {
go func(drviceName string, domain string, signal chan EthTable, ctx context.Context) {
var (
snapshot_len int32 = 1024
promiscuous bool = false
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
)
var err error
handle, err = pcap.OpenLive(
drviceName,
snapshot_len,
promiscuous,
timeout,
)
if err != nil {
gologger.Fatalf("pcap打开失败:%s\n", err.Error())
}
defer handle.Close()
// Use the handle as a packet source to process all packets
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
select {
case <-ctx.Done():
return
default:
packet, err := packetSource.NextPacket()
gologger.Printf(".")
if err != nil {
continue
}
if dnsLayer := packet.Layer(layers.LayerTypeDNS); dnsLayer != nil {
dns, _ := dnsLayer.(*layers.DNS)
if !dns.QR {
continue
}
for _, v := range dns.Questions {
if string(v.Name) == domain {
ethLayer := packet.Layer(layers.LayerTypeEthernet)
if ethLayer != nil {
eth := ethLayer.(*layers.Ethernet)
signal <- EthTable{SrcIp: data[drviceName], Device: drviceName, SrcMac: eth.DstMAC, DstMac: eth.SrcMAC}
// 网关mac 和 本地mac
return
}
}
}
}
}
}
}(drviceName, domain, signal, ctx)
}
var c EthTable
for {
select {
case c = <-signal:
cancel()
goto END
default:
_, _ = net.LookupHost(domain)
time.Sleep(time.Millisecond * 20)
}
}
END:
gologger.Printf("\n")
gologger.Infof("Use Device: %s\n", c.Device)
gologger.Infof("Use IP:%s\n", c.SrcIp.String())
gologger.Infof("Local Mac:%s\n", c.SrcMac.String())
gologger.Infof("GateWay Mac:%s\n", c.DstMac.String())
return c
}
func GetIpv4Devices() (keys []string, data map[string]net.IP) {
devices, err := pcap.FindAllDevs()
data = make(map[string]net.IP)
if err != nil {
gologger.Fatalf("获取网络设备失败:%s\n", err.Error())
}
for _, d := range devices {
for _, address := range d.Addresses {
ip := address.IP
if ip.To4() != nil && !ip.IsLoopback() {
gologger.Printf(" [%d] Name: %s\n", len(keys), d.Name)
gologger.Printf(" Description: %s\n", d.Description)
gologger.Printf(" Devices addresses: %s\n", d.Description)
gologger.Printf(" IP address: %s\n", ip)
gologger.Printf(" Subnet mask: %s\n\n", address.Netmask.String())
data[d.Name] = ip
keys = append(keys, d.Name)
}
}
}
return
}
func GetDevices(options *Options) EthTable {
// Find all devices
defaultSelect := options.NetworkId
keys, data := GetIpv4Devices()
if len(keys) == 0 {
gologger.Fatalf("获取不到可用的设备名称\n")
} else if len(keys) == 1 {
defaultSelect = 0
}
if defaultSelect == -1 {
if options.Silent || options.Stdin {
gologger.Fatalf("slient模式或Stdin模式下需要指定-e参数\n")
}
gologger.Infof("选择一个可用网卡ID:")
var i int
_, err2 := fmt.Scanln(&i)
if err2 != nil {
gologger.Fatalf("\n读入ID失败,确认输入的是数字?\n")
}
if i < 0 || i >= len(keys) {
gologger.Fatalf("ID超出了范围\n")
}
defaultSelect = i
}
deviceName := keys[defaultSelect]
ip := data[deviceName]
gologger.Infof("Use Device: %s\n", deviceName)
gologger.Infof("Use IP:%s\n", ip.String())
c := GetGateMacAddress(deviceName)
gologger.Infof("Local Mac:%s\n", c[1])
gologger.Infof("GateWay Mac:%s\n", c[0])
return EthTable{ip, deviceName, c[1], c[0]}
}
func GetGateMacAddress(dvice string) [2]net.HardwareAddr {
// 获取网关mac地址
domain := RandomStr(4) + "paper.seebug.org"
_signal := make(chan [2]net.HardwareAddr)
go func(device string, domain string, signal chan [2]net.HardwareAddr) {
var (
snapshot_len int32 = 1024
promiscuous bool = false
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
)
var err error
handle, err = pcap.OpenLive(
device,
snapshot_len,
promiscuous,
timeout,
)
if err != nil {
gologger.Fatalf("pcap打开失败:%s\n", err.Error())
}
defer handle.Close()
// Use the handle as a packet source to process all packets
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
packet, err := packetSource.NextPacket()
gologger.Printf(".")
if err != nil {
continue
}
if dnsLayer := packet.Layer(layers.LayerTypeDNS); dnsLayer != nil {
dns, _ := dnsLayer.(*layers.DNS)
if !dns.QR {
continue
}
for _, v := range dns.Questions {
if string(v.Name) == domain {
ethLayer := packet.Layer(layers.LayerTypeEthernet)
if ethLayer != nil {
eth := ethLayer.(*layers.Ethernet)
srcMac := eth.SrcMAC
dstMac := eth.DstMAC
signal <- [2]net.HardwareAddr{srcMac, dstMac}
// 网关mac 和 本地mac
return
}
}
}
}
}
}(dvice, domain, _signal)
var c [2]net.HardwareAddr
for {
select {
case c = <-_signal:
gologger.Printf("\n")
goto END
default:
_, _ = net.LookupHost(domain)
time.Sleep(time.Millisecond * 10)
}
}
END:
return c
}