forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
183 lines (148 loc) · 4.05 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
package main
import (
"fmt"
"strings"
"time"
)
const TCP_STREAM_EXPIRY = 10 * 1e9
const TCP_STREAM_HASH_SIZE = 2 ^ 16
type TcpStream struct {
id uint32
tuple *IpPortTuple
timer *time.Timer
protocol protocolType
httpData [2]*HttpStream
mysqlData [2]*MysqlStream
parserData [2]ParserStream
redisData [2]*RedisStream
}
type ParserStream interface {
Parse() (bool, bool)
AddData([]byte)
Message() interface{}
Reset()
}
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
}
}
func (stream *TcpStream) Expire() {
// de-register from dict
delete(tcpStreamsMap, *stream.tuple)
}
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])
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
}