/
viewcap.go
225 lines (188 loc) · 7.25 KB
/
viewcap.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
// Copyright 2014-2015 Bret Jordan, All rights reserved.
//
// Use of this source code is governed by an Apache 2.0 license
// that can be found in the LICENSE file in the root of the source
// tree.
package main
import (
"code.google.com/p/getopt"
"encoding/hex"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"net"
"os"
"strings"
)
var sOptPcapSrcFilename = getopt.StringLong("file", 'f', "", "Filename of the source PCAP file", "string")
var sOptMacAddress = getopt.StringLong("mac", 0, "", "The MAC Address to search for in AA:BB:CC:DD:EE:FF format", "string")
var sOptIPv4Address = getopt.StringLong("ip4", 0, "", "The IPv4 Address to change", "string")
var iOptHead = getopt.IntLong("head", 'h', 0, "Number of packets to show", "int")
var bOptHelp = getopt.BoolLong("help", 0, "Help")
var bOptVer = getopt.BoolLong("version", 0, "Version")
var iDebug = 0
var sVersion = "0.2"
//
//
//
// --------------------------------------------------------------------------------
// Function Main
// --------------------------------------------------------------------------------
func main() {
getopt.HelpColumn = 26
getopt.SetParameters("")
getopt.Parse()
checkCommandLineOptions()
iHeadCount := *iOptHead
// // Figure out if there is a change needed for the date of each packet. We will
// // compute the difference between what is in the first packet and what was passed
// // in via the command line arguments.
// pcapStartTimestamp := getFirstPacketTimestamp(*sOptPcapSrcFilename)
// // Parse layer 2 addresses
// userSuppliedMacAddress := parseSuppliedLayer2Address(*sOptMacAddress)
// // Parse layer 3 IPv4 address
// userSuppliedIPv4Address := parseSuppliedLayer3IPv4Address(*sOptIPv4Address)
//
// Get a handle to the PCAP source file so we can loop through each packet and make
// changes as needed.
handle, err1 := pcap.OpenOffline(*sOptPcapSrcFilename)
if err1 != nil {
fmt.Println(err1)
os.Exit(0)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
// -------------------------------------------------------------------------
// Define counters for status
// -------------------------------------------------------------------------
iTotalPacketCounter := 0
i802dot1QCounter := 0
i802dot1QinQCounter := 0
// -------------------------------------------------------------------------
// Loop through every packet and update them as needed writing the changes
// out to a new file
// -------------------------------------------------------------------------
for packet := range packetSource.Packets() {
if *iOptHead != 0 && iHeadCount == 0 {
return
}
ts := packet.Metadata().CaptureInfo.Timestamp
dstMacAddressFromPacket := packet.LinkLayer().LayerContents()[0:6]
srcMacAddressFromPacket := packet.LinkLayer().LayerContents()[6:12]
sDstMacAddress := makePrettyMacAddress(dstMacAddressFromPacket)
sSrcMacAddress := makePrettyMacAddress(srcMacAddressFromPacket)
if *sOptMacAddress != "" {
if sDstMacAddress != *sOptMacAddress && sSrcMacAddress != *sOptMacAddress {
continue
}
}
i802dot1QOffset := 0
// ---------------------------------------------------------------------
// Look for an 802.1Q frames
// ---------------------------------------------------------------------
if packet.LinkLayer().LayerContents()[12] == 81 && packet.LinkLayer().LayerContents()[13] == 0 {
if iDebug == 1 {
fmt.Println("DEBUG: Found an 802.1Q packet")
}
i802dot1QOffset = 4
i802dot1QCounter++
}
// ---------------------------------------------------------------------
// Look for an 802.1QinQ frame
// ---------------------------------------------------------------------
if packet.LinkLayer().LayerContents()[12] == 88 && packet.LinkLayer().LayerContents()[13] == 168 {
if iDebug == 1 {
fmt.Println("DEBUG: Found an 802.1QinQ packet")
}
i802dot1QOffset = 8
i802dot1QinQCounter++
}
iEthType1 := 12 + i802dot1QOffset
iEthType2 := 13 + i802dot1QOffset
if packet.LinkLayer().LayerContents()[iEthType1] == 8 && packet.LinkLayer().LayerContents()[iEthType2] == 0 && packet.NetworkLayer().LayerContents()[0] == 69 {
// Define the byte offsets for the data we are looking for
iLayer3SrcIPStart := 12 + i802dot1QOffset
iLayer3SrcIPEnd := iLayer3SrcIPStart + 4
iLayer3DstIPStart := 16 + i802dot1QOffset
iLayer3DstIPEnd := iLayer3DstIPStart + 4
srcIPv4AddressFromPacket := packet.NetworkLayer().LayerContents()[iLayer3SrcIPStart:iLayer3SrcIPEnd]
dstIPv4AddressFromPacket := packet.NetworkLayer().LayerContents()[iLayer3DstIPStart:iLayer3DstIPEnd]
fmt.Print(ts, " - ", sSrcMacAddress)
fmt.Printf(" - %-15s", makePrettyIPAddress(srcIPv4AddressFromPacket))
fmt.Print(" > ", sDstMacAddress)
fmt.Printf(" - %-15s\n", makePrettyIPAddress(dstIPv4AddressFromPacket))
}
// Write some output to the screen so users know we are doing something
iTotalPacketCounter++
if *iOptHead != 0 {
iHeadCount--
}
} // End loop through every packet
fmt.Println("\nTotal number of packets processed:", iTotalPacketCounter)
fmt.Println("Total number of 802.1Q packets processed:", i802dot1QCounter)
fmt.Println("Total number of 802.1QinQ packets processed:", i802dot1QinQCounter)
} // main()
//
// --------------------------------------------------------------------------------
// checkCommandLineOptions()
// --------------------------------------------------------------------------------
// Verify that all of the command line options meet the required dependencies
func checkCommandLineOptions() {
if *bOptVer {
fmt.Println("viewcap, copyright Bret Jordan, 2015")
fmt.Println("Version:", sVersion)
fmt.Println("")
os.Exit(0)
}
if *bOptHelp || *sOptPcapSrcFilename == "" {
fmt.Println("viewcap, copyright Bret Jordan, 2015")
fmt.Println("Version:", sVersion)
fmt.Println("")
getopt.Usage()
os.Exit(0)
}
} //checkCommandLineOptions()
//
// -----------------------------------------------------------------------------
// areByteSlicesEqual
// -----------------------------------------------------------------------------
// Compare two byte slices to see if they are the same
func areByteSlicesEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
} // areByteSlicesEqual
//
// -----------------------------------------------------------------------------
// makePrettyMacAddress()
// -----------------------------------------------------------------------------
// This function will create a human readable MAC address in upper case using
// the : notation between octets
func makePrettyMacAddress(mac []byte) string {
sMAC := strings.ToUpper(hex.EncodeToString(mac))
var sNewMAC string
// This will add a ":" after ever ODD index value but not on the last one
for i, value := range sMAC {
sNewMAC += string(value)
if i%2 != 0 && i%11 != 0 {
sNewMAC += ":"
}
}
if iDebug == 1 {
fmt.Println("DEBUG: MAC Address", sNewMAC)
}
return sNewMAC
} // makePrettyMacAddress()
//
// -----------------------------------------------------------------------------
// makePrettyIPAddress()
// -----------------------------------------------------------------------------
// This function will create a human readable IP address
func makePrettyIPAddress(ip net.IP) string {
return ip.String()
}