forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
224 lines (180 loc) · 5.17 KB
/
tcp.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
package main
import (
"fmt"
"strings"
"time"
)
const TCP_STREAM_EXPIRY = 10 * 1e9
const TCP_STREAM_HASH_SIZE = 2 ^ 16
type TcpTuple struct {
Src_ip, Dst_ip uint32
Src_port, Dst_port uint16
stream_id uint32
}
func (t TcpTuple) String() string {
return fmt.Sprintf("TcpTuple src[%s:%d] dst[%s:%d] stream_id[%d]",
Ipv4_Ntoa(t.Src_ip),
t.Src_port,
Ipv4_Ntoa(t.Dst_ip),
t.Dst_port,
t.stream_id)
}
type TcpStream struct {
id uint32
tuple *IpPortTuple
timer *time.Timer
protocol protocolType
lastSeq [2]uint32
httpData [2]*HttpStream
mysqlData [2]*MysqlStream
redisData [2]*RedisStream
pgsqlData [2]*PgsqlStream
}
type Endpoint struct {
Ip string
Port uint16
Name string
Cmdline string
Proc string
}
var __id uint32 = 0
func GetId() uint32 {
__id += 1
return __id
}
const (
TCP_FLAG_FIN = 0x01
TCP_FLAG_SYN = 0x02
TCP_FLAG_RST = 0x04
TCP_FLAG_PSH = 0x08
TCP_FLAG_ACK = 0x10
TCP_FLAG_URG = 0x20
)
// Config
type tomlProtocol struct {
Ports []int
Send_request bool
Send_response bool
}
var tcpStreamsMap = make(map[IpPortTuple]*TcpStream, TCP_STREAM_HASH_SIZE)
var tcpPortMap map[uint16]protocolType
func decideProtocol(tuple *IpPortTuple) protocolType {
protocol, exists := tcpPortMap[tuple.Src_port]
if exists {
return protocol
}
protocol, exists = tcpPortMap[tuple.Dst_port]
if exists {
return protocol
}
return UnknownProtocol
}
func (stream *TcpStream) AddPacket(pkt *Packet, flags uint8, original_dir uint8) {
//DEBUG(" (tcp stream %d[%d])", stream.id, original_dir)
// create/reset timer
if stream.timer != nil {
stream.timer.Stop()
}
stream.timer = time.AfterFunc(TCP_STREAM_EXPIRY, func() { stream.Expire() })
// call upper layer
if len(pkt.payload) == 0 && stream.protocol == HttpProtocol {
if flags&TCP_FLAG_FIN != 0 {
HttpReceivedFin(stream, original_dir)
}
return
}
switch stream.protocol {
case HttpProtocol:
ParseHttp(pkt, stream, original_dir)
if flags&TCP_FLAG_FIN != 0 {
HttpReceivedFin(stream, original_dir)
}
break
case MysqlProtocol:
ParseMysql(pkt, stream, original_dir)
break
case RedisProtocol:
ParseRedis(pkt, stream, original_dir)
break
case PgsqlProtocol:
ParsePgsql(pkt, stream, original_dir)
break
}
}
func (stream *TcpStream) Expire() {
// de-register from dict
delete(tcpStreamsMap, *stream.tuple)
}
func TcpSeqBefore(seq1 uint32, seq2 uint32) bool {
return int32(seq1-seq2) < 0
}
func FollowTcp(tcphdr []byte, pkt *Packet) {
stream, exists := tcpStreamsMap[pkt.tuple]
var original_dir uint8 = 1
if !exists {
// search also the other direction
rev_tuple := IpPortTuple{Src_ip: pkt.tuple.Dst_ip, Dst_ip: pkt.tuple.Src_ip,
Src_port: pkt.tuple.Dst_port, Dst_port: pkt.tuple.Src_port}
stream, exists = tcpStreamsMap[rev_tuple]
if !exists {
protocol := decideProtocol(&pkt.tuple)
if protocol == UnknownProtocol {
// don't follow
return
}
// create
stream = &TcpStream{id: GetId(), tuple: &pkt.tuple, protocol: protocol}
tcpStreamsMap[pkt.tuple] = stream
} else {
original_dir = 0
}
}
flags := uint8(tcphdr[13])
tcp_seq := Bytes_Ntohl(tcphdr[4:8]) + uint32(len(pkt.payload))
DEBUG("tcp", "pkt.seq=%v len=%v stream.seq=%v",
Bytes_Ntohl(tcphdr[4:8]), len(pkt.payload), stream.lastSeq[original_dir])
if len(pkt.payload) > 0 &&
stream.lastSeq[original_dir] != 0 &&
!TcpSeqBefore(stream.lastSeq[original_dir], tcp_seq) {
DEBUG("tcp", "Ignoring what looks like a retrasmitted segment. pkt.seq=%v len=%v stream.seq=%v",
Bytes_Ntohl(tcphdr[4:8]), len(pkt.payload), stream.lastSeq[original_dir])
return
}
stream.lastSeq[original_dir] = tcp_seq
stream.AddPacket(pkt, flags, original_dir)
}
func PrintTcpMap() {
fmt.Printf("Streams in memory:")
for _, stream := range tcpStreamsMap {
fmt.Printf(" %d", stream.id)
}
fmt.Printf("\n")
}
func configToPortsMap(config *tomlConfig) map[uint16]protocolType {
var res = map[uint16]protocolType{}
var proto protocolType
for proto = UnknownProtocol + 1; int(proto) < len(protocolNames); proto++ {
protoConfig, exists := config.Protocols[protocolNames[proto]]
if !exists {
// skip
continue
}
for _, port := range protoConfig.Ports {
res[uint16(port)] = proto
}
}
return res
}
func configToFilter(config *tomlConfig) string {
res := []string{}
for _, protoConfig := range config.Protocols {
for _, port := range protoConfig.Ports {
res = append(res, fmt.Sprintf("port %d", port))
}
}
return strings.Join(res, " or ")
}
func TcpInit() error {
tcpPortMap = configToPortsMap(&_Config)
return nil
}