-
Notifications
You must be signed in to change notification settings - Fork 158
/
pcap.go
162 lines (134 loc) · 3.76 KB
/
pcap.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
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package collector
import (
"fmt"
"io"
"log"
"os"
"time"
"github.com/dreadl0ck/netcap/encoder"
humanize "github.com/dustin/go-humanize"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)
// openPcap opens pcap files
func openPcap(file string) (*pcapgo.Reader, *os.File, error) {
// get file handle
f, err := os.Open(file)
if err != nil {
return nil, nil, err
}
// try to create pcap reader
r, err := pcapgo.NewReader(f)
if err != nil {
return nil, nil, err
}
return r, f, nil
}
// IsPcap checks wheter a file is a PCAP file
func IsPcap(file string) (bool, error) {
// get file handle
f, err := os.Open(file)
if err != nil {
return false, err
}
defer f.Close()
// try to create pcap reader
_, err = pcapgo.NewReader(f)
if err != nil {
// file exists but is not a pcap
// dont return error in this case
return false, nil
}
return true, nil
}
// countPackets returns the number of packets in a PCAP file
func countPackets(path string) (count int64) {
// get reader and file handle
r, f, err := openPcap(path)
if err != nil {
panic(err)
}
defer f.Close()
for {
// loop over packets and discard all data
_, _, err := r.ReadPacketData()
if err != nil {
if err == io.EOF {
break
}
log.Fatal("Error reading packet data: ", err)
}
// increment counter
count++
}
return count
}
// CollectPcap implements parallel decoding of incoming packets
func (c *Collector) CollectPcap(path string) {
// stat input file
stat, errStat := os.Stat(path)
if errStat != nil {
log.Fatal("failed to open file", errStat)
}
// file exists.
clearLine()
println("opening", path+" | size:", humanize.Bytes(uint64(stat.Size())))
// set input filesize on collector
c.inputSize = stat.Size()
// display total packet count
print("counting packets...")
start := time.Now()
c.numPackets = countPackets(path)
clearLine()
fmt.Println("counting packets... done.", c.numPackets, "packets found in", time.Since(start))
r, f, err := openPcap(path)
if err != nil {
panic(err)
}
defer f.Close()
// initialize collector
c.Init()
print("decoding packets... ")
for {
// fetch the next packetdata and packetheader
// for pcap, currently ZeroCopyReadPacketData() is not supported
data, ci, err := r.ReadPacketData()
if err != nil {
if err == io.EOF {
break
}
log.Fatal("Error reading packet data: ", err)
}
// show progress
c.printProgress()
// create a new gopacket with lazy decoding
// base layer is currently Ethernet
// TODO make base layer configurable
p := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Lazy)
p.Metadata().Timestamp = ci.Timestamp
p.Metadata().CaptureInfo = ci
// if HTTP capture is desired, tcp stream reassembly needs to be performed.
// the gopacket/reassembly implementation does not allow packets to arrive out of order
// therefore the http decoding must not happen in a worker thread
// and instead be performed here to guarantee packets are being processed sequentially
if encoder.HTTPActive {
encoder.DecodeHTTP(p)
}
// pass packet to a worker routine
c.handlePacket(p)
}
c.cleanup()
}