forked from david415/HoneyBadger
/
pcap_logger.go
152 lines (133 loc) · 3.79 KB
/
pcap_logger.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
/*
* HoneyBadger core library for detecting TCP injection attacks
*
* Copyright (C) 2014, 2015 David Stainton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package logging
import (
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
"github.com/david415/HoneyBadger/types"
)
type TimedPacket struct {
RawPacket []byte
Timestamp time.Time
}
// PcapLogger struct is used to log packets to a pcap file
type PcapLogger struct {
packetChan chan TimedPacket
stopChan chan bool
LogDir string
ArchiveDir string
Flow *types.TcpIpFlow
writer *pcapgo.Writer
fileWriter io.WriteCloser
pcapLogNum int
pcapQuota int
basename string
}
func NewPcapLogger(logDir, archiveDir string, flow *types.TcpIpFlow, pcapLogNum int, pcapQuota int) types.PacketLogger {
p := PcapLogger{
packetChan: make(chan TimedPacket),
stopChan: make(chan bool),
Flow: flow,
LogDir: logDir,
ArchiveDir: archiveDir,
pcapLogNum: pcapLogNum,
pcapQuota: pcapQuota,
}
return types.PacketLogger(&p)
}
type PcapLoggerFactory struct {
LogDir string
ArchiveDir string
PcapLogNum int
PcapQuota int
}
func NewPcapLoggerFactory(logDir, archiveDir string, pcapLogNum, pcapQuota int) PcapLoggerFactory {
return PcapLoggerFactory{
LogDir: logDir,
ArchiveDir: archiveDir,
PcapLogNum: pcapLogNum,
PcapQuota: pcapQuota,
}
}
func (f PcapLoggerFactory) Build(flow *types.TcpIpFlow) types.PacketLogger {
return NewPcapLogger(f.LogDir, f.ArchiveDir, flow, f.PcapLogNum, f.PcapQuota)
}
func (p *PcapLogger) WriteHeader() {
err := p.writer.WriteFileHeader(65536, layers.LinkTypeEthernet)
if err != nil {
panic(err)
}
}
func (p *PcapLogger) Start() {
if p.fileWriter == nil {
p.basename = filepath.Join(p.LogDir, fmt.Sprintf("%s.pcap", p.Flow))
p.fileWriter = NewRotatingQuotaWriter(p.basename, p.pcapQuota, p.pcapLogNum, p.WriteHeader)
p.writer = pcapgo.NewWriter(p.fileWriter)
}
go p.logPackets()
}
func (p *PcapLogger) Stop() {
p.stopChan <- true
p.fileWriter.Close()
}
func (p *PcapLogger) Archive() {
newBasename := filepath.Join(p.ArchiveDir, filepath.Base(p.basename))
os.Rename(p.basename, newBasename)
for i := 1; i < p.pcapLogNum+1; i++ {
os.Rename(filepath.Join(p.LogDir, fmt.Sprintf("%s.pcap.%d", p.Flow.String(), i)), fmt.Sprintf("%s.%d", newBasename, i))
}
}
func (p *PcapLogger) logPackets() {
for {
select {
case <-p.stopChan:
return
case timedPacket := <-p.packetChan:
p.WritePacketToFile(timedPacket.RawPacket, timedPacket.Timestamp)
}
}
}
func (p *PcapLogger) Remove() {
os.Remove(p.basename)
for i := 1; i < p.pcapLogNum+1; i++ {
os.Remove(filepath.Join(p.LogDir, fmt.Sprintf("%s.pcap.%d", p.Flow, i)))
}
}
func (p *PcapLogger) WritePacket(rawPacket []byte, timestamp time.Time) {
p.packetChan <- TimedPacket{
RawPacket: rawPacket,
Timestamp: timestamp,
}
}
func (p *PcapLogger) WritePacketToFile(rawPacket []byte, timestamp time.Time) {
err := p.writer.WritePacket(gopacket.CaptureInfo{
Timestamp: timestamp,
CaptureLength: len(rawPacket),
Length: len(rawPacket),
}, rawPacket)
if err != nil {
panic(err)
}
}