-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
381 lines (336 loc) · 7.52 KB
/
process.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/robertkrimen/otto"
)
func process(scriptFile, pcapFile, outdir string) (err error) {
var handle *pcap.Handle
var ctx *JSCtx
ctx, err = NewJSCtx(scriptFile, outdir)
if err != nil {
return
}
handle, err = pcap.OpenOffline(pcapFile)
if err != nil {
return
}
defer handle.Close()
_, err = ctx.vm.Call("BEGIN", nil, version+"; "+pcap.Version(), scriptFile, pcapFile)
if err != nil && err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
var packet gopacket.Packet
packet, err = packetSource.NextPacket()
if err != nil {
break
}
debug("[packet]", packet.String())
err = ctx.processPacket(packet)
if err != nil {
break
}
ctx.Count = ctx.Count + 1
}
if err == io.EOF {
err = nil
}
if err != nil {
return
}
_, err = ctx.vm.Call("END", nil, ctx.Count)
if err != nil && err.Error()[0:15] == "ReferenceError:" {
err = nil
}
return
}
// JSCtx JavaScript実行コンテクスト
type JSCtx struct {
//
Count int
filePath string
vm *otto.Otto
script *otto.Script
}
// NewJSCtx 新規JavaSript実行コンテクストの生成
func NewJSCtx(filePath, outdir string) (r *JSCtx, err error) {
vm := otto.New()
var script *otto.Script
script, err = vm.Compile(filePath, nil)
if err != nil {
return
}
/*
err = vm.Set("hex", hex.Dump)
if err != nil {
return
}
err = vm.Set("hwaddr", func(bb []byte) string {
return net.HardwareAddr(bb).String()
})
if err != nil {
return
}
err = vm.Set("ipaddr", func(bb []byte) string {
return net.IP(bb).String()
})
if err != nil {
return
}
err = vm.Set("str", func(bb []byte) string {
return string(bb)
})
if err != nil {
return
}
*/
// バイト列をファイルに保存する組み込み関数
addBuiltIn("save", func(filePath string, bb []byte) {
saveFile(outdir, filePath, bb)
})
// 組み込み関数をJavaSript実行コンテクストに登録
for name, value := range BuiltIns {
err = vm.Set(name, value)
if err != nil {
return
}
}
/*
//err = vm.Set("save", saveFile)
err = vm.Set("save", func(filePath string, bb []byte) {
saveFile(outdir, filePath, bb)
})
if err != nil {
return
}
*/
/*
values := map[string]interface{}{}
err = vm.Set("set", func(key string, value interface{}) {
values[key] = value
})
if err != nil {
return
}
err = vm.Set("get", func(key string) interface{} {
return values[key]
})
if err != nil {
return
}
*/
_, err = vm.Run(script)
if err != nil {
return
}
r = &JSCtx{
filePath: filePath,
vm: vm,
script: script,
}
return
}
// Packet パケットコンテナ
type Packet struct {
Ethernet *layers.Ethernet
ARP *layers.ARP
IPv4 *layers.IPv4
ICMPv4 *layers.ICMPv4
TCP *layers.TCP
UDP *layers.UDP
DNS *layers.DNS
/*
ADD HERE
NTP *layers.NTP
*/
}
func (p *JSCtx) processPacket(packet gopacket.Packet) (err error) {
if p.vm == nil {
err = fmt.Errorf("initializing error")
return
}
//fmt.Println("--------")
//pack := readPacket(packet)
pack := readPacket2(packet)
ts := packet.Metadata().Timestamp
if pack.TCP != nil {
_, err = p.vm.Call("TCP", nil, p.Count, ts, pack.TCP, pack.IPv4, pack.Ethernet)
//fmt.Println(err)
if err == nil {
return
}
//fmt.Printf("err.Error()[0:15] = [%s]\n", err.Error()[0:15])
if err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
}
if pack.DNS != nil {
_, err = p.vm.Call("DNS", nil, p.Count, ts, pack.DNS, pack.UDP, pack.IPv4, pack.Ethernet)
//fmt.Println(err)
if err == nil {
return
}
if err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
}
if pack.UDP != nil {
_, err = p.vm.Call("UDP", nil, p.Count, ts, pack.UDP, pack.IPv4, pack.Ethernet)
if err == nil {
return
}
if err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
}
if pack.ICMPv4 != nil {
_, err = p.vm.Call("ICMP", nil, p.Count, ts, pack.ICMPv4, pack.IPv4, pack.Ethernet)
if err == nil {
return
}
if err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
}
if pack.IPv4 != nil {
_, err = p.vm.Call("IP", nil, p.Count, ts, pack.IPv4, pack.Ethernet)
if err == nil {
return
}
if err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
}
if pack.ARP != nil {
_, err = p.vm.Call("ARP", nil, p.Count, ts, pack.ARP, pack.Ethernet)
if err == nil {
return
}
if err.Error()[0:15] == "ReferenceError:" {
err = nil
}
if err != nil {
return
}
}
if pack.Ethernet != nil {
_, err = p.vm.Call("Eth", nil, p.Count, ts, pack.Ethernet)
if err != nil && err.Error()[0:15] == "ReferenceError:" {
err = nil
}
}
return
}
func readPacket2(packet gopacket.Packet) (r Packet) {
r = Packet{}
for _, layer := range packet.Layers() {
switch layer.(type) {
case *layers.DNS:
r.DNS, _ = layer.(*layers.DNS)
case *layers.TCP:
r.TCP, _ = layer.(*layers.TCP)
case *layers.UDP:
r.UDP, _ = layer.(*layers.UDP)
case *layers.ICMPv4:
r.ICMPv4, _ = layer.(*layers.ICMPv4)
case *layers.ARP:
r.ARP, _ = layer.(*layers.ARP)
case *layers.IPv4:
r.IPv4, _ = layer.(*layers.IPv4)
case *layers.Ethernet:
r.Ethernet, _ = layer.(*layers.Ethernet)
/*
ADD HERE
case *layers.NTP:
r.NTP, _ = layer.(*layers.NTP)
*/
default:
debug("layer", layer.LayerType().String())
}
}
//fmt.Println(r)
return
}
/*
func readPacket(packet gopacket.Packet) (r Packet) {
r = Packet{}
layer := packet.Layer(layers.LayerTypeEthernet)
if layer != nil {
r.Ethernet, _ = layer.(*layers.Ethernet)
switch r.Ethernet.EthernetType {
case layers.EthernetTypeARP:
layer = packet.Layer(layers.LayerTypeARP)
if layer != nil {
r.ARP, _ = layer.(*layers.ARP)
}
case layers.EthernetTypeIPv4:
layer := packet.Layer(layers.LayerTypeIPv4)
if layer != nil {
r.IPv4, _ = layer.(*layers.IPv4)
switch r.IPv4.Protocol {
case layers.IPProtocolICMPv4:
layer = packet.Layer(layers.LayerTypeICMPv4)
if layer != nil {
r.ICMPv4, _ = layer.(*layers.ICMPv4)
}
case layers.IPProtocolTCP:
layer = packet.Layer(layers.LayerTypeTCP)
if layer != nil {
r.TCP, _ = layer.(*layers.TCP)
}
case layers.IPProtocolUDP:
layer = packet.Layer(layers.LayerTypeUDP)
if layer != nil {
r.UDP, _ = layer.(*layers.UDP)
}
}
}
}
}
return
}
*/
// saveFile saves byte sequence to file
func saveFile(outdir, filePath string, bb []byte) {
filePath = outdir + "/" + filepath.Base(filePath)
w, err := os.Create(filepath.Clean(filePath))
if err != nil {
panic(err)
}
_, err = w.Write(bb)
if err != nil {
panic(err)
}
err = w.Close()
if err != nil {
panic(err)
}
}
// http://mrtc0.hateblo.jp/entry/2016/03/19/232252
// https://godoc.org/github.com/google/gopacket
// https://godoc.org/github.com/google/gopacket/layers