-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydump.go
250 lines (226 loc) · 6.35 KB
/
mydump.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
package main
import (
"encoding/hex"
"flag"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"log"
"regexp"
"strconv"
"strings"
"time"
)
var (
snapshotLen int32 = 65535
err error
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
tcpSrcPort layers.TCPPort
tcpDestPort layers.TCPPort
udpSrcPort layers.UDPPort
udpDestPort layers.UDPPort
payloadString string = ""
bpfFilter string = ""
)
func main() {
// -i flag is used to specify the network device interface
var iFlag = flag.String("i", "", "Pass the network device interface for live capture")
// -r flag is used to read packets from the file
var rFlag = flag.String("r", "", "Pass the file path to read packets from")
// -s flag is used to search for a particular string in a payload
var sFlag = flag.String("s", "", "Search for string in packet payload")
flag.Parse()
// Getting the string to search from the -s flag
payloadString = *sFlag
// Storing the BPF string to be applied
bpfFilter = strings.Join(flag.Args(), " ")
/*
If both -i and -r flags are set then packets are read from file else according to flag set
Else packets are captured live from first available interface
*/
if *iFlag!="" || *rFlag!=""{
if *rFlag!="" {
readFromFile(*rFlag)
} else {
captureLive(*iFlag)
}
} else {
captureLive("")
}
}
// Check for a particular device interface or from available interfaces to capture packets
func captureLive(device string) {
if checkForDevice(device) {
readLivePackets(device)
} else {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Panic(err)
}
if len(devices)>0 {
readLivePackets(devices[0].Name)
} else {
fmt.Println("No devices available for capturing live packets")
}
}
}
// Checks for given device interface name is set of all available network devices
func checkForDevice(name string) bool {
devices, err := pcap.FindAllDevs()
if err != nil {
log.Panic(err)
}
for _, device := range devices {
if device.Name == name {
return true
}
}
return false
}
// Capturing the packets live on a particular device by setting BPF filter if passed
func readLivePackets(device string) {
handle, error := pcap.OpenLive(device, snapshotLen, true, timeout)
if error != nil {
panic(error)
}else {
if bpfFilter != "" {
err := handle.SetBPFFilter(bpfFilter)
if err != nil {
panic(err)
}
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
printDetails(packet)
}
}
}
// Packets are read from an existing file with a BPF filter if passed
func readFromFile(filePath string) {
if handle, err := pcap.OpenOffline(filePath); err != nil {
panic(err)
} else {
if bpfFilter != "" {
err := handle.SetBPFFilter(bpfFilter)
if err != nil {
panic(err)
}
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
printDetails(packet)
}
}
}
// Printing the details of a packet
func printDetails(packet gopacket.Packet) {
ethernetLayer := packet.Layer(layers.LayerTypeEthernet)
ipv4Layer := packet.Layer(layers.LayerTypeIPv4)
tcpLayer := packet.Layer(layers.LayerTypeTCP)
udpLayer := packet.Layer(layers.LayerTypeUDP)
icmpLayer := packet.Layer(layers.LayerTypeICMPv4)
applicationLayer := packet.ApplicationLayer()
// Check if expression to be searched in payload is provided
if payloadString!="" {
if ethernetLayer != nil {
if !strings.Contains(string(ethernetLayer.LayerPayload()), payloadString) {
return
}
} else if ipv4Layer != nil {
if !strings.Contains(string(ipv4Layer.LayerPayload()), payloadString) {
return
}
} else if applicationLayer!= nil {
if !strings.Contains(string(applicationLayer.LayerPayload()), payloadString) {
return
}
}
}
// Get the timestamp of the packet
var timeStamp = packet.Metadata().Timestamp.String()
var timeStampList = strings.Split(timeStamp, " ")
var timeStampRequired = timeStampList[0] + " " + timeStampList[1]
// Printing the time
fmt.Print(timeStampRequired + " ")
// Get the ethernet frame
if ethernetLayer != nil {
ethernetFrame := ethernetLayer.(*layers.Ethernet)
// Printing the source and destination MAC, ethernet type, packet length
fmt.Print(ethernetFrame.SrcMAC, " -> ", ethernetFrame.DstMAC, " ")
// Printing the ethernet type
fmt.Print("type 0x", strconv.FormatInt(int64(ethernetFrame.EthernetType), 16), " ")
}
// Printing the packet length
fmt.Printf("len %s ", strconv.Itoa(packet.Metadata().Length))
// Checking for tcp, udp and icmp layer data
if tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
tcpSrcPort = tcp.SrcPort
tcpDestPort = tcp.DstPort
fmt.Print(ipv4Layer.(*layers.IPv4).SrcIP.String() + ":")
re := regexp.MustCompile("^[0-9]+")
temp := re.FindString(tcpSrcPort.String())
fmt.Print(temp + " ")
fmt.Print(" -> ")
fmt.Print(ipv4Layer.(*layers.IPv4).DstIP.String() + ":")
temp = re.FindString(tcpDestPort.String())
fmt.Print(temp + " ")
fmt.Print(ipv4Layer.(*layers.IPv4).Protocol.String() + " ")
if tcp.FIN {
fmt.Print("FIN" + " ")
}
if tcp.SYN {
fmt.Print("SYN" + " ")
}
if tcp.RST {
fmt.Print("RST" + " ")
}
if tcp.PSH {
fmt.Print("PSH" + " ")
}
if tcp.ACK {
fmt.Print("ACK" + " ")
}
if tcp.URG {
fmt.Print("URG" + " ")
}
if tcp.ECE {
fmt.Print("ECE" + " ")
}
if tcp.CWR {
fmt.Print("CWR" + " ")
}
if tcp.NS {
fmt.Print("NS" + " ")
}
} else if udpLayer != nil {
udp, _ := udpLayer.(*layers.UDP)
udpSrcPort = udp.SrcPort
udpDestPort = udp.DstPort
fmt.Print(ipv4Layer.(*layers.IPv4).SrcIP.String() + ":")
re := regexp.MustCompile("^[0-9]+")
temp := re.FindString(udpSrcPort.String())
fmt.Println(temp + " ")
fmt.Print(" -> ")
fmt.Print(ipv4Layer.(*layers.IPv4).DstIP.String() + ":")
temp = re.FindString(udpDestPort.String())
fmt.Print(temp + " ")
fmt.Print(ipv4Layer.(*layers.IPv4).Protocol.String())
} else if icmpLayer != nil {
fmt.Print(" ICMP ")
} else {
fmt.Print("OTHER")
}
fmt.Println()
// Printing the payload
if ethernetLayer!=nil {
fmt.Println(hex.Dump(ethernetLayer.LayerPayload()))
} else if ipv4Layer!=nil {
fmt.Println(hex.Dump(ipv4Layer.LayerPayload()))
} else if applicationLayer!=nil {
fmt.Printf(hex.Dump(applicationLayer.LayerPayload()))
}
fmt.Println()
}