-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (196 loc) · 5.23 KB
/
main.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"strconv"
)
const (
ORDERING_IDENTICAL = 0xa1b2c3d4
ORDERING_SWAPPED = 0xd4c3b2a1
)
type GlobalHeader struct {
magic_number uint32
version_major uint16
version_minor uint16
thiszone int32
sigfigs uint32
snaplen uint32
network uint32
}
type PacketHeader struct {
ts_sec uint32
ts_usec uint32
incl_len uint32
orig_len uint32
}
type Packet struct {
header PacketHeader
data []byte
}
func magic_number(chunk []byte) (magic_number uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &magic_number)
if err != nil {
panic("Reading magic number failed")
}
if magic_number == ORDERING_SWAPPED || magic_number == ORDERING_IDENTICAL {
return
}
panic("Bad magic number")
}
func version_major(chunk []byte) (version_major uint16) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &version_major)
if err != nil {
panic("Reading major version failed")
}
return
}
func version_minor(chunk []byte) (version_minor uint16) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &version_minor)
if err != nil {
panic("Reading minor version failed")
}
return
}
func thiszone(chunk []byte) (thiszone int32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &thiszone)
if err != nil {
panic("Reading time zone correction failed")
}
if thiszone != 0 {
fmt.Println("Note: thiszone (time zone correction) not 0")
}
return
}
func sigfigs(chunk []byte) (sigfigs uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &sigfigs)
if err != nil {
panic("Reading time zone correction failed")
}
if sigfigs != 0 {
fmt.Println("Note: sigfigs (time stamp accuracy) not 0")
}
return
}
func snaplen(chunk []byte) (snaplen uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &snaplen)
if err != nil {
panic("Reading snapshot length failed")
}
return
}
func network(chunk []byte) (network uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &network)
if err != nil {
panic("Reading snapshot length failed")
}
return
}
func ts_sec(chunk []byte) (ts_sec uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &ts_sec)
if err != nil {
panic("Reading packet timestamp failed")
}
return
}
func ts_usec(chunk []byte) (ts_usec uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &ts_usec)
if err != nil {
panic("Reading packet timestamp microseconds failed")
}
return
}
func incl_len(chunk []byte) (incl_len uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &incl_len)
if err != nil {
panic("Reading packet capture length failed")
}
return
}
func orig_len(chunk []byte) (orig_len uint32) {
buf := bytes.NewReader(chunk)
err := binary.Read(buf, binary.LittleEndian, &orig_len)
if err != nil {
panic("Reading packet capture original length failed")
}
return
}
func global_next() {
chunk_size := global_header_layout[global_index]
if global_index > 0 {
global_begin = global_end
}
global_end += chunk_size
global_index++
}
func packet_next() {
chunk_size := packet_header_layout[packet_index]
if packet_index > 0 {
packet_begin = packet_end
}
packet_end += chunk_size
packet_index++
}
var (
global_header_layout = []int{4, 2, 2, 4, 4, 4, 4}
packet_header_layout = []int{4, 4, 4, 4}
global_begin, global_end, global_index int = 0, 0, 0
packet_begin, packet_end, packet_index int = 0, 0, 0
)
func main() {
net, _ := ioutil.ReadFile("net.pcap")
header := GlobalHeader{}
global_next()
header.magic_number = magic_number(net[global_begin:global_end])
global_next()
header.version_major = version_major(net[global_begin:global_end])
global_next()
header.version_minor = version_minor(net[global_begin:global_end])
global_next()
header.thiszone = thiszone(net[global_begin:global_end])
global_next()
header.sigfigs = sigfigs(net[global_begin:global_end])
global_next()
header.snaplen = snaplen(net[global_begin:global_end])
global_next()
header.network = network(net[global_begin:global_end])
packet_begin = global_end
packet_end = global_end
var records []Packet
i := 0
for packet_end < len(net) {
packet := Packet{}
packet_next()
packet.header.ts_sec = ts_sec(net[packet_begin:packet_end])
packet_next()
packet.header.ts_usec = ts_usec(net[packet_begin:packet_end])
packet_next()
packet.header.incl_len = incl_len(net[packet_begin:packet_end])
packet_next()
packet.header.orig_len = orig_len(net[packet_begin:packet_end])
if packet.header.incl_len > header.snaplen {
panic("Encountered packet bigger than snapshot length specified")
}
if packet.header.incl_len > packet.header.orig_len {
panic("Encountered packet with larger included length than original packet")
}
data_end := packet_end + int(packet.header.incl_len)
packet.data = net[packet_end:data_end]
packet_end += int(packet.header.incl_len)
records = append(records, packet)
packet_index = 0
i++
}
fmt.Printf("Parsed %s records.\n\n", strconv.Itoa(i))
}