/
helper.go
272 lines (240 loc) · 5.99 KB
/
helper.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
package pcaphelper
import (
"crypto/md5"
"crypto/sha1"
"encoding/binary"
"encoding/hex"
"errors"
"io"
"os"
"time"
)
// IsPcap returns the PcapType based on the magic code of the file and the endianness of the pcap
func IsPcap(filepath string) (PcapType, Endianness, error) {
data, err := readHeaders(filepath)
if err != nil {
return INVALID, LITTLE, err
}
return isPcap(data)
}
// GetDataLink returns the datalink of the pcap
func GetDataLink(filepath string) (DataLink, error) {
data, err := readHeaders(filepath)
if err != nil {
return LINKTYPE_NULL, err
}
typ, e, err := isPcap(data)
if err != nil {
return LINKTYPE_NULL, err
} else if typ == INVALID || typ == PCAP_NG {
return LINKTYPE_NULL, errors.New("invalid pcap")
}
var dt uint32
if e == LITTLE {
dt = binary.LittleEndian.Uint32(data[20 : 20+4])
} else {
dt = binary.BigEndian.Uint32(data[20 : 20+4])
}
return (DataLink)(dt), nil
}
// GetVersion return the major and minor version of the pcap file
func GetVersion(filepath string) (int, int, error) {
data, err := readHeaders(filepath)
if err != nil {
return -1, -1, err
}
typ, e, err := isPcap(data)
if err != nil {
return -1, -1, err
} else if typ == INVALID || typ == PCAP_NG {
return -1, -1, errors.New("not a valid pcap")
}
if e == LITTLE {
major := binary.LittleEndian.Uint16(data[4 : 4+2])
minor := binary.LittleEndian.Uint16(data[6 : 6+2])
return (int)(major), (int)(minor), nil
} else {
major := binary.BigEndian.Uint16(data[4 : 4+2])
minor := binary.BigEndian.Uint16(data[6 : 6+2])
return (int)(major), (int)(minor), nil
}
}
// GetFirstTimestamp returns the timestamp of the first packet
func GetFirstTimestamp(filepath string) (*time.Time, error) {
data, err := readHeaders(filepath)
if err != nil {
return nil, err
}
typ, e, err := isPcap(data)
if err != nil {
return nil, err
} else if typ == INVALID || typ == PCAP_NG {
return nil, errors.New("not a valid pcap")
}
if len(data) < 24+4 {
return nil, errors.New("no packet the in pcap")
}
if e == LITTLE {
i := binary.LittleEndian.Uint32(data[24 : 24+4]) // access to ts_sec; in the first packet's heade
t := time.Unix((int64)(i), 0)
return &t, nil
} else {
i := binary.BigEndian.Uint32(data[24 : 24+4]) // access to ts_sec; in the first packet's heade
t := time.Unix((int64)(i), 0)
return &t, nil
}
}
// GetMD5 returns the md5 hash of the file
func GetMD5(filename string) (string, error) {
var result []byte
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(result)), nil
}
// GetSHA1 returns the sha1 hash of the file
func GetSHA1(filename string) (string, error) {
var result []byte
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
hash := sha1.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(result)), nil
}
// NumberOfPacket returns the number of packet in the pcap
func NumberOfPacket(filename string) (int, error) {
counter := 0
f, err := os.Open(filename)
if err != nil {
return 0, err
}
defer f.Close()
data := make([]byte, 24) //pcap header
_, err = f.Read(data)
if err != nil {
return 0, err
}
typ, e, err := isPcap(data)
if err != nil {
return 0, err
} else if typ == INVALID || typ == PCAP_NG {
return 0, errors.New("invalid pcap")
}
for {
// iterate over packet header, seek over packet
data = make([]byte, 16)
_, err = f.Read(data)
if err != nil {
if err == io.EOF {
return counter, nil
}
return 0, err
}
var i uint32
if e == LITTLE {
i = binary.LittleEndian.Uint32(data[8 : 8+4]) // access incl_len
} else {
i = binary.BigEndian.Uint32(data[8 : 8+4]) // access incl_len
}
f.Seek((int64)(i), os.SEEK_CUR)
counter++
}
return 0, nil
}
// GetLastTimestamp returns the timestamp of the last packet
func GetLastTimestamp(filename string) (*time.Time, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
data := make([]byte, 24) //pcap header
_, err = f.Read(data)
if err != nil {
return nil, err
}
typ, e, err := isPcap(data)
if err != nil {
return nil, err
} else if typ == INVALID || typ == PCAP_NG {
return nil, errors.New("invalid pcap")
}
var t time.Time
for {
// iterate over packet header, seek over packet
data = make([]byte, 16)
_, err = f.Read(data)
if err != nil {
if err == io.EOF {
return &t, nil
}
return nil, err
}
var i uint32
if e == LITTLE {
i = binary.LittleEndian.Uint32(data[0:4]) // access to ts_sec
t = time.Unix((int64)(i), 0)
i = binary.LittleEndian.Uint32(data[8 : 8+4]) // access incl_len
} else {
i = binary.BigEndian.Uint32(data[0:4]) // access to ts_sec
t = time.Unix((int64)(i), 0)
i = binary.BigEndian.Uint32(data[8 : 8+4]) // access incl_len
}
f.Seek((int64)(i), os.SEEK_CUR)
}
return nil, nil
}
func GetDuration(filename string) (*time.Duration, error) {
start, err := GetFirstTimestamp(filename)
if err != nil {
return nil, err
}
end, err := GetLastTimestamp(filename)
if err != nil {
return nil, err
}
delta := end.Sub(*start)
return &delta, nil
}
func isPcap(data []byte) (PcapType, Endianness, error) {
if len(data) < 24 {
return INVALID, LITTLE, errors.New("invalid pcap header")
}
i := binary.LittleEndian.Uint32(data[0:4])
if i == PCAP {
return PCAP, LITTLE, nil
} else if i == PCAP_SWAPPED {
return PCAP, BIG, nil
} else if i == PCAP_NS {
return PCAP_NS, LITTLE, nil
} else if i == PCAP_NS_SWAPPED {
return PCAP_NS, BIG, nil
} else if i == PCAP_NG {
return PCAP_NG, LITTLE, nil
}
return INVALID, LITTLE, nil
}
func readHeaders(filepath string) ([]byte, error) {
f, err := os.Open(filepath)
if err != nil {
return []byte{}, err
}
defer f.Close()
data := make([]byte, 24+16) //pcap header + first packet header
_, err = f.Read(data)
if err != nil {
return []byte{}, err
}
return data, nil
}