-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacketparser.go
231 lines (212 loc) · 5.62 KB
/
packetparser.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
package edrint
import (
"errors"
"fmt"
"net"
"time"
"github.com/sharat910/edrint/common"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/rs/zerolog/log"
"github.com/sharat910/edrint/events"
)
type CaptureMode int
const (
UNDEFINEDCM CaptureMode = iota
PCAPFILE
INTERFACE
)
type DirectionMode int
const (
UNDEFINEDDM DirectionMode = iota
CLIENT_MAC
CLIENT_IP
)
type ParserConfig struct {
CapMode CaptureMode
CapSource string
DirMode DirectionMode
DirMatches []string
BPF string
MaxPackets int
}
func PacketParser(c ParserConfig, pf events.PubFunc) error {
if err := SanityCheck(c); err != nil {
return err
}
handle, err := GetHandle(c)
if err != nil {
return err
}
var (
// Will reuse these for each packet
ethLayer layers.Ethernet
ip4Layer layers.IPv4
ip6Layer layers.IPv6
icmp4Layer layers.ICMPv4
tcpLayer layers.TCP
udpLayer layers.UDP
)
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
ðLayer,
&ip4Layer,
&ip6Layer,
&icmp4Layer,
&tcpLayer,
&udpLayer,
)
// Uni IPs
var clientSubnets []*net.IPNet
if c.DirMode == CLIENT_IP {
clientSubnets, err = GetClientSubnets(c)
if err != nil {
return err
}
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
packetSource.DecodeOptions.Lazy = true
packetSource.DecodeOptions.NoCopy = true
upPktCount := 0
pktCount := 0
log.Info().Msg("packet processor started")
var firstPacketTS, lastPacketTS time.Time
for packet := range packetSource.Packets() {
pktCount++
var p common.Packet
p.Timestamp = packet.Metadata().Timestamp
p.TotalLen = uint(packet.Metadata().Length)
appLayer := packet.ApplicationLayer()
if appLayer != nil {
p.Payload = appLayer.LayerContents()
//p.Payload = append(p.Payload, appLayer.Payload()...)
if len(appLayer.Payload()) != 0 && len(appLayer.Payload()) != len(appLayer.LayerContents()) {
log.Warn().
Int("layer_content_len", len(appLayer.LayerContents())).
Int("layer_payload_len", len(appLayer.Payload())).
Msg("app layer len difference!")
}
}
// book keeping
if pktCount == 1 {
firstPacketTS = p.Timestamp
}
lastPacketTS = p.Timestamp
var foundLayerTypes []gopacket.LayerType
_ = parser.DecodeLayers(packet.Data(), &foundLayerTypes)
for _, layerType := range foundLayerTypes {
switch layerType {
case layers.LayerTypeEthernet:
if c.DirMode == CLIENT_MAC {
for _, clientMac := range c.DirMatches {
if ethLayer.SrcMAC.String() == clientMac {
p.IsOutbound = true
upPktCount++
break
}
}
}
case layers.LayerTypeIPv4:
p.Header.SrcIP = ip4Layer.SrcIP.String()
p.Header.DstIP = ip4Layer.DstIP.String()
p.Header.Protocol = uint8(ip4Layer.Protocol)
if c.DirMode == CLIENT_IP {
for _, s := range clientSubnets {
if s.Contains(ip4Layer.SrcIP) {
p.IsOutbound = true
upPktCount++
break
}
}
}
case layers.LayerTypeIPv6:
p.Header.SrcIP = ip6Layer.SrcIP.String()
p.Header.DstIP = ip6Layer.DstIP.String()
p.Header.Protocol = uint8(ip6Layer.NextHeader)
if c.DirMode == CLIENT_IP {
for _, s := range clientSubnets {
if s.Contains(ip6Layer.SrcIP) {
p.IsOutbound = true
break
}
}
}
case layers.LayerTypeICMPv4:
pf(events.PACKET, p)
case layers.LayerTypeUDP:
p.Header.SrcPort = uint16(udpLayer.SrcPort)
p.Header.DstPort = uint16(udpLayer.DstPort)
pf(events.PACKET, p)
case layers.LayerTypeTCP:
p.Header.SrcPort = uint16(tcpLayer.SrcPort)
p.Header.DstPort = uint16(tcpLayer.DstPort)
p.TCPLayer = tcpLayer
pf(events.PACKET, p)
}
}
if c.MaxPackets != 0 && pktCount >= c.MaxPackets {
break
}
}
log.Info().Int("packet_count", pktCount).Msg("packet processing completed")
if upPktCount == 0 {
log.Warn().Msg("No upload packet! Maybe check config.")
}
pf("packet_parser.metadata", struct {
NPackets int `json:"n_packets"`
UpPackets int `json:"up_packets"`
FirstPacketTS time.Time `json:"first_packet_ts"`
LastPacketTS time.Time `json:"last_packet_ts"`
Source string
}{pktCount, upPktCount, firstPacketTS, lastPacketTS, c.CapSource})
return nil
}
func SanityCheck(c ParserConfig) error {
if c.CapMode == UNDEFINEDCM {
return errors.New("capture mode undefined")
}
if c.DirMode == UNDEFINEDDM {
return errors.New("direction inference mode undefined")
}
return nil
}
func GetClientSubnets(c ParserConfig) ([]*net.IPNet, error) {
var clientSubnets []*net.IPNet
for _, s := range c.DirMatches {
_, subnet, err := net.ParseCIDR(s)
if err != nil {
return clientSubnets, fmt.Errorf("unable to parse subnet: %s", s)
}
clientSubnets = append(clientSubnets, subnet)
}
return clientSubnets, nil
}
func GetHandle(c ParserConfig) (*pcap.Handle, error) {
var err error
var source string
var handle *pcap.Handle
switch c.CapMode {
case PCAPFILE:
handle, err = pcap.OpenOffline(c.CapSource)
if err != nil {
log.Fatal().Err(err).Msg("unable to open pcap")
}
if c.BPF != "" {
err = handle.SetBPFFilter(c.BPF)
if err != nil {
log.Fatal().Err(err).Msg("unable to set bpf filter")
}
}
log.Info().Str("packet_source", source).Str("pcap_path", c.CapSource).Msg("handle created")
case INTERFACE:
handle, err = pcap.OpenLive(c.CapSource, 9600, true, time.Minute)
if err != nil {
log.Fatal().Err(err).Msg("unable to open pcap")
}
log.Info().Str("packet_source", source).Str("interface", c.CapSource).Msg("handle created")
default:
return nil, errors.New("unknown capture mode")
}
return handle, nil
}