/
net_packet_http.go
260 lines (209 loc) · 6 KB
/
net_packet_http.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package derive
import (
"bufio"
"bytes"
"net/http"
"github.com/nextlinux/tracee/pkg/errfmt"
"github.com/nextlinux/tracee/pkg/events"
"github.com/nextlinux/tracee/types/trace"
)
const httpMinLen int = 7 // longest http command is "DELETE "
//
// NetPacketHTTP
//
func NetPacketHTTP() DeriveFunction {
return deriveSingleEvent(events.NetPacketHTTP, deriveHTTP())
}
func deriveHTTP() deriveArgsFunction {
return deriveHTTPEvents
}
func deriveHTTPEvents(event trace.Event) ([]interface{}, error) {
net, h, err := eventToProtoHTTP(&event)
if err != nil {
return nil, errfmt.WrapError(err)
}
if h == nil {
return nil, nil // connection related packets
}
if net == nil {
return nil, parsePacketError()
}
return []interface{}{
net.srcIP,
net.dstIP,
net.srcPort,
net.dstPort,
h,
}, nil
}
//
// NetPacketHTTPRequest
//
func NetPacketHTTPRequest() DeriveFunction {
return deriveSingleEvent(events.NetPacketHTTPRequest, deriveHTTPRequest())
}
func deriveHTTPRequest() deriveArgsFunction {
return deriveHTTPRequestEvents
}
func deriveHTTPRequestEvents(event trace.Event) ([]interface{}, error) {
net, h, err := eventToProtoHTTPRequest(&event)
if err != nil {
return nil, errfmt.WrapError(err)
}
if h == nil {
return nil, nil // not a request
}
if net == nil {
return nil, errfmt.WrapError(err)
}
meta := convertNetPairToPktMeta(net)
return []interface{}{
*meta,
h,
}, nil
}
//
// NetPacketHTTPResponse
//
func NetPacketHTTPResponse() DeriveFunction {
return deriveSingleEvent(events.NetPacketHTTPResponse, deriveHTTPResponse())
}
func deriveHTTPResponse() deriveArgsFunction {
return deriveHTTPResponseEvents
}
func deriveHTTPResponseEvents(event trace.Event) ([]interface{}, error) {
net, h, err := eventToProtoHTTPResponse(&event)
if err != nil {
return nil, errfmt.WrapError(err)
}
if h == nil {
return nil, nil // // not a response
}
if net == nil {
return nil, errfmt.WrapError(err)
}
meta := convertNetPairToPktMeta(net)
return []interface{}{
*meta,
h,
}, nil
}
func eventToProtoHTTP(event *trace.Event) (*netPair, *trace.ProtoHTTP, error) {
var httpNetPair netPair
layer7, err := parseUntilLayer7(event, &httpNetPair)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
if layer7 == nil {
return nil, nil, nil
}
layer7Payload := layer7.Payload()
if len(layer7Payload) > httpMinLen {
var httpReq *http.Request
var httpRes *http.Response
var protoHttp trace.ProtoHTTP
reader := bufio.NewReader(bytes.NewReader(layer7Payload))
// event retval encodes HTTP direction
if event.ReturnValue&protoHttpRequest == protoHttpRequest {
httpReq, err = http.ReadRequest(reader)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
copyHTTPReqToProtoHTTP(httpReq, &protoHttp)
} else if event.ReturnValue&protoHttpResponse == protoHttpResponse {
httpRes, err = http.ReadResponse(reader, nil)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
copyHTTPResToProtoHTTP(httpRes, &protoHttp)
} else {
return &httpNetPair, nil, errfmt.Errorf("unspecified direction for HTTP packet")
}
return &httpNetPair, &protoHttp, nil
}
return &httpNetPair, nil, notProtoPacketError("HTTP")
}
func eventToProtoHTTPRequest(event *trace.Event) (*netPair, *trace.ProtoHTTPRequest, error) {
var httpNetPair netPair
layer7, err := parseUntilLayer7(event, &httpNetPair)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
if layer7 == nil {
return nil, nil, nil
}
layer7Payload := layer7.Payload()
if len(layer7Payload) > httpMinLen {
// event retval encodes HTTP direction
if event.ReturnValue&protoHttpRequest != protoHttpRequest {
return nil, nil, nil
}
reader := bufio.NewReader(bytes.NewReader(layer7Payload))
httpReq, err := http.ReadRequest(reader)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
var httpRequest trace.ProtoHTTPRequest
copyHTTPReqToProtoHTTPRequest(httpReq, &httpRequest)
return &httpNetPair, &httpRequest, nil
}
return &httpNetPair, nil, notProtoPacketError("HTTP")
}
func eventToProtoHTTPResponse(event *trace.Event) (*netPair, *trace.ProtoHTTPResponse, error) {
var httpNetPair netPair
layer7, err := parseUntilLayer7(event, &httpNetPair)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
if layer7 == nil {
return nil, nil, nil
}
layer7Payload := layer7.Payload()
if len(layer7Payload) > httpMinLen {
// event retval encodes HTTP direction
if event.ReturnValue&protoHttpResponse != protoHttpResponse {
return nil, nil, nil
}
reader := bufio.NewReader(bytes.NewReader(layer7Payload))
httpRes, err := http.ReadResponse(reader, nil)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
var httpResponse trace.ProtoHTTPResponse
copyHTTPResToProtoHTTPResponse(httpRes, &httpResponse)
return &httpNetPair, &httpResponse, nil
}
return &httpNetPair, nil, notProtoPacketError("HTTP")
}
func copyHTTPReqToProtoHTTP(httpReq *http.Request, proto *trace.ProtoHTTP) {
proto.Direction = "request"
proto.Method = httpReq.Method
proto.Protocol = httpReq.Proto
proto.Host = httpReq.Host
proto.URIPath = httpReq.URL.Path
proto.Headers = httpReq.Header
proto.ContentLength = httpReq.ContentLength
}
func copyHTTPResToProtoHTTP(httpRes *http.Response, proto *trace.ProtoHTTP) {
proto.Direction = "response"
proto.Status = httpRes.Status
proto.StatusCode = httpRes.StatusCode
proto.Protocol = httpRes.Proto
proto.Headers = httpRes.Header
proto.ContentLength = httpRes.ContentLength
}
func copyHTTPReqToProtoHTTPRequest(httpReq *http.Request, proto *trace.ProtoHTTPRequest) {
proto.Method = httpReq.Method
proto.Protocol = httpReq.Proto
proto.Host = httpReq.Host
proto.URIPath = httpReq.URL.Path
proto.Headers = httpReq.Header
proto.ContentLength = httpReq.ContentLength
}
func copyHTTPResToProtoHTTPResponse(httpRes *http.Response, proto *trace.ProtoHTTPResponse) {
proto.Status = httpRes.Status
proto.StatusCode = httpRes.StatusCode
proto.Protocol = httpRes.Proto
proto.Headers = httpRes.Header
proto.ContentLength = httpRes.ContentLength
}