-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.go
737 lines (602 loc) · 19.3 KB
/
messages.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
package rpcap
import (
"bytes"
"encoding/binary"
"io"
"io/ioutil"
)
// MessageType is type RPCAP message
type MessageType uint8
const (
msgError MessageType = 1 // Message that keeps an error notification
msgFindallIfReq MessageType = 2 // Request to list all the remote interfaces
msgOpenReq MessageType = 3 // Request to open a remote device
msgStartCapReq MessageType = 4 // Request to start a capture on a remote device
msgUpdateFilterReq MessageType = 5 // Send a compiled filter into the remote device
msgClose MessageType = 6 // Close the connection with the remote peer
msgPacket MessageType = 7 // This is a 'data' message, which carries a network packet
msgAuthReq MessageType = 8 // Message that keeps the authentication parameters
msgStatsReq MessageType = 9 // It requires to have network statistics
msgEndCapReq MessageType = 10 // Stops the current capture, keeping the device open
msgSetSamplingReq MessageType = 11 // Set sampling parameters
msgFindAllIfReply MessageType = (128 + msgFindallIfReq) // Keeps the list of all the remote interfaces
msgOpenReply MessageType = (128 + msgOpenReq) // The remote device has been opened correctly
msgStartCapReply MessageType = (128 + msgStartCapReq) // The capture is starting correctly
msgUpdateFilterReply MessageType = (128 + msgUpdateFilterReq) // The filter has been applied correctly on the remote device
msgAuthReply MessageType = (128 + msgAuthReq) // Sends a message that says 'ok, authorization successful'
msgStatsReply MessageType = (128 + msgStatsReq) // Message that keeps the network statistics */
msgEndCapReply MessageType = (128 + msgEndCapReq) // Confirms that the capture stopped succesfully
msgSetsamplingReply MessageType = (128 + msgSetSamplingReq) // Confirms that the capture stopped succesfully
)
// Message is a interface used to represent all messages (request and replies)
type Message interface {
header() *Header
encode() ([]byte, error)
decode(io.Reader) error
ProtocolVersion() uint8
}
var headerSize = binary.Size(Header{})
func checkLargePacket(data []byte) error {
if len(data) > MaxPacketSize {
return ErrLargeMessage
}
return nil
}
// Common header for all the RPCAP messages.
type Header struct {
// RPCAP version number
Version uint8
// RPCAP message type (error, findalldevs, ...)
Type MessageType
// Message-dependent value (not always used)
Value uint16
// Length of the payload of this RPCAP message
pLength uint32
}
func (hdr *Header) ProtocolVersion() uint8 {
return hdr.Version
}
func (hdr *Header) header() *Header {
return hdr
}
func (hdr *Header) writeHeader(t MessageType, data []byte) {
data[0] = hdr.Version
data[1] = byte(t)
binary.BigEndian.PutUint16(data[2:], hdr.Value)
binary.BigEndian.PutUint32(data[4:], uint32(len(data)-headerSize))
}
// Request to list all the remote interfaces
type FindAllInterfaceRequest struct {
Header
}
func (msg *FindAllInterfaceRequest) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgFindallIfReq, data)
return data, nil
}
func (msg *FindAllInterfaceRequest) decode(r io.Reader) error {
return nil
}
// Message that keeps an error notification
type ErrorMsg struct {
Header
//Code uint16 // Error code. Fake field. Real value is stored in Header.Value
Description string // Error description
}
func (msg *ErrorMsg) Code() uint16 {
return msg.Header.Value
}
func (msg *ErrorMsg) SetCode(code uint16) {
msg.Header.Value = code
}
func (msg *ErrorMsg) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, []byte(msg.Description))
data := w.Bytes()
msg.writeHeader(msgError, data)
return data, checkLargePacket(data)
}
func (msg *ErrorMsg) decode(r io.Reader) (err error) {
desc := make([]byte, msg.Header.pLength)
if err = binary.Read(r, binary.BigEndian, desc); err != nil {
return err
}
msg.Description = string(desc)
return
}
// Request to open a remote device
type OpenRequest struct {
Header
Interface string
}
func (msg *OpenRequest) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, []byte(msg.Interface))
data := w.Bytes()
msg.writeHeader(msgOpenReq, data)
return data, checkLargePacket(data)
}
func (msg *OpenRequest) decode(r io.Reader) (err error) {
iface := make([]byte, msg.Header.pLength)
if err = binary.Read(r, binary.BigEndian, iface); err != nil {
return err
}
msg.Interface = string(iface)
return
}
type Filter struct {
Type uint16
Dummy uint16 // must be zero
ItemsCount uint32
Data []byte
}
func (f *Filter) write(w io.Writer) {
binary.Write(w, binary.BigEndian, f.Type)
binary.Write(w, binary.BigEndian, f.Dummy)
binary.Write(w, binary.BigEndian, f.ItemsCount)
binary.Write(w, binary.BigEndian, f.Data)
}
const (
// Enables promiscuous mode (default: disabled)
FlagStartCapturePromisc uint16 = 1
// Use a datagram (i.e. UDP) connection for the data stream
// (default: use TCP)
FlagStartCaptureDgram uint16 = 2
// The server has to open the data connection toward the client
FlagStartCaptureServerOpen uint16 = 4
// Capture only inbound packets
// (take care: the flag has no effects with promiscuous enabled)
FlagStartCaptureInbound uint16 = 8
// Capture only outbound packets
// (take care: the flag has no effects with promiscuous enabled)
FlagStartCaptureOutbound uint16 = 16
)
// Request to start a capture on a remote device
type StartCaptureRequest struct {
Header
SnapLength uint32
ReadTimout uint32
Flags uint16
Port uint16
Filter Filter
}
func (msg *StartCaptureRequest) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, msg.SnapLength)
binary.Write(w, binary.BigEndian, msg.ReadTimout)
binary.Write(w, binary.BigEndian, msg.Flags)
binary.Write(w, binary.BigEndian, msg.Port)
msg.Filter.write(w)
data := w.Bytes()
msg.writeHeader(msgStartCapReq, data)
return data, checkLargePacket(data)
}
func (msg *StartCaptureRequest) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.SnapLength,
&msg.ReadTimout,
&msg.Flags,
&msg.Port,
&msg.Filter.Type,
&msg.Filter.Dummy,
&msg.Filter.ItemsCount,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
msg.Filter.Data, err = ioutil.ReadAll(r)
return err
}
// Send a compiled filter into the remote device
type UpdateFilterRequest struct {
Header
Filter Filter
}
func (msg *UpdateFilterRequest) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
msg.Filter.write(w)
data := w.Bytes()
msg.writeHeader(msgUpdateFilterReq, data)
return data, checkLargePacket(data)
}
func (msg *UpdateFilterRequest) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.Filter.Type,
&msg.Filter.Dummy,
&msg.Filter.ItemsCount,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
msg.Filter.Data, err = ioutil.ReadAll(r)
/*
if msg.FilterType != FILTER_BPF {
return fmt.Errorf("Only BPF/NPF filters are currently supported")
}*/
// if err = binary.Read(r, binary.BigEndian, &msg.BpfItems); err != nil {
// return err
// }
return err
}
// Request to close a remote device
type CloseMsg struct {
Header
}
func (msg *CloseMsg) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgClose, data)
return data, nil
}
func (msg *CloseMsg) decode(r io.Reader) (err error) {
return
}
// This is a 'data' message, which carries a network packet
type PacketMsg struct {
Header
PacketHeader
Data []byte
}
func (msg *PacketMsg) encode() ([]byte, error) {
return []byte{}, nil
}
func (msg *PacketMsg) decode(r io.Reader) (err error) {
if err = binary.Read(r, binary.BigEndian, &msg.PacketHeader); err != nil {
return err
}
_, err = io.ReadFull(r, msg.Data)
return err
}
// Captured packet header
type PacketHeader struct {
Seconds uint32
Microseconds uint32
CaptureLength uint32
RealLength uint32
PacketNumber uint32
}
const (
AuthNullType uint16 = 0
AuthPasswordType uint16 = 1
)
// Message that keeps the authentication parameters
type AuthRequest struct {
Header
Type uint16 // Authentication type
Dummy uint16 // Must be zero
sLen1 uint16 // Length of the first authentication item (e.g. username)
sLen2 uint16 // Length of the second authentication item (e.g. password)
FirstAuthItem []byte // First authentication item (e.g. username)
SecondAuthItem []byte // Second authentication item (e.g. password)
}
func (msg *AuthRequest) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, msg.Type)
binary.Write(w, binary.BigEndian, msg.Dummy)
binary.Write(w, binary.BigEndian, uint16(len(msg.FirstAuthItem)))
binary.Write(w, binary.BigEndian, uint16(len(msg.SecondAuthItem)))
w.Write(msg.FirstAuthItem)
w.Write(msg.SecondAuthItem)
data := w.Bytes()
msg.writeHeader(msgAuthReq, data)
return data, checkLargePacket(data)
}
func (msg *AuthRequest) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.Type,
&msg.Dummy,
&msg.sLen1,
&msg.sLen2,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
if msg.sLen1 != 0 {
msg.FirstAuthItem = make([]byte, msg.sLen1)
if err = binary.Read(r, binary.BigEndian, msg.FirstAuthItem); err != nil {
return err
}
}
if msg.sLen2 != 0 {
msg.SecondAuthItem = make([]byte, msg.sLen2)
err = binary.Read(r, binary.BigEndian, msg.SecondAuthItem)
}
return
}
// Request statistics
type StatsRequest struct {
Header
}
func (msg *StatsRequest) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgStatsReq, data)
return data, checkLargePacket(data)
}
func (msg *StatsRequest) decode(r io.Reader) (err error) {
return
}
// Stops the current capture, keeping the device open
type EndCaptureRequest struct {
Header
}
func (msg *EndCaptureRequest) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgEndCapReq, data)
return data, checkLargePacket(data)
}
func (msg *EndCaptureRequest) decode(r io.Reader) (err error) {
return
}
// Set sampling parameters
type SetSamplingRequest struct {
Header
Method uint8 // Sampling method
Dummy1 uint8 // Must be zero
Dummy2 uint16 // Must be zero
Value uint32 // Depends on sampling method
}
func (msg *SetSamplingRequest) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, msg.Method)
binary.Write(w, binary.BigEndian, msg.Dummy1)
binary.Write(w, binary.BigEndian, msg.Dummy2)
binary.Write(w, binary.BigEndian, msg.Value)
data := w.Bytes()
msg.writeHeader(msgSetSamplingReq, data)
return data, checkLargePacket(data)
}
func (msg *SetSamplingRequest) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.Method,
&msg.Dummy1,
&msg.Dummy2,
&msg.Value,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
return
}
// FindAllInterfaceReply keeps the list of all the remote interfaces
type FindAllInterfaceReply struct {
Header
InterfaceList []InterfaceInfo
}
func (msg *FindAllInterfaceReply) encode() (data []byte, err error) {
// msg.Header.Value - specifies number of interfaces
msg.Header.Value = uint16(len(msg.InterfaceList))
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
for i := uint16(0); i < msg.Header.Value; i++ {
err = msg.InterfaceList[i].encode(w)
if err != nil {
return []byte{}, err
}
}
data = w.Bytes()
msg.writeHeader(msgFindAllIfReply, data)
return data, checkLargePacket(data)
}
func (msg *FindAllInterfaceReply) decode(r io.Reader) (err error) {
// msg.Header.Value - specifies number of interfaces
ifaceCount := msg.Header.Value
msg.InterfaceList = make([]InterfaceInfo, ifaceCount, ifaceCount)
for i := uint16(0); i < ifaceCount; i++ {
err = msg.InterfaceList[i].decode(r)
if err != nil {
return err
}
}
return nil
}
const addressLen = 512
// Interface description
type InterfaceInfo struct {
nameLength uint16 // Length of the interface name
descLength uint16 // Length of the interface description
Flags uint32 // Interface flags
numberOfAddr uint16 // Number of addresses
Dummy uint16 // Must be zero
Name []byte // Interface name
Description []byte // Interface description
Addresses [][addressLen]byte // List of addresses
}
func (iface *InterfaceInfo) encode(w io.Writer) (err error) {
binary.Write(w, binary.BigEndian, uint16(len(iface.Name)))
binary.Write(w, binary.BigEndian, uint16(len(iface.Description)))
binary.Write(w, binary.BigEndian, iface.Flags)
binary.Write(w, binary.BigEndian, uint16(len(iface.Addresses)))
binary.Write(w, binary.BigEndian, iface.Dummy)
binary.Write(w, binary.BigEndian, []byte(iface.Name))
binary.Write(w, binary.BigEndian, []byte(iface.Description))
for _, addr := range iface.Addresses {
binary.Write(w, binary.BigEndian, addr)
}
return err
}
func (msg *InterfaceInfo) decode(r io.Reader) (err error) {
data := make([]byte, 12) // 12 = nameLength + descLength + Flags + numberOfAddr + Dummy
err = binary.Read(r, binary.BigEndian, data)
if err != nil {
return err
}
msg.nameLength = binary.BigEndian.Uint16(data[0:])
msg.descLength = binary.BigEndian.Uint16(data[2:])
msg.Flags = binary.BigEndian.Uint32(data[4:])
msg.numberOfAddr = binary.BigEndian.Uint16(data[8:])
msg.Dummy = binary.BigEndian.Uint16(data[10:])
msg.Name = make([]byte, msg.nameLength)
if err = binary.Read(r, binary.BigEndian, &msg.Name); err != nil {
return err
}
msg.Description = make([]byte, msg.descLength)
if err = binary.Read(r, binary.BigEndian, &msg.Description); err != nil {
return err
}
if msg.numberOfAddr == 0 {
return
}
msg.Addresses = make([][addressLen]byte, msg.numberOfAddr, msg.numberOfAddr)
for i := uint16(0); i < msg.numberOfAddr || err != nil; i++ {
err = binary.Read(r, binary.BigEndian, &msg.Addresses[i])
}
return
}
// OpenReply confirms remote device has been opened correctly
type OpenReply struct {
Header
LinkType uint32
Timezone int32
}
func (msg *OpenReply) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, msg.LinkType)
binary.Write(w, binary.BigEndian, msg.Timezone)
data := w.Bytes()
msg.writeHeader(msgOpenReply, data)
return data, checkLargePacket(data)
}
func (msg *OpenReply) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.LinkType,
&msg.Timezone,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
return
}
// StartCaptureReply devoted to start a remote capture (startcap reply command)
type StartCaptureReply struct {
Header
Bufsize int32 // Size of the user buffer allocated by WinPcap; it can be different from the one we choose
Port uint16 // Network port on which the server is waiting at (passive mode only)
Dummy uint16 // Must be zero
}
func (msg *StartCaptureReply) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, msg.Bufsize)
binary.Write(w, binary.BigEndian, msg.Port)
binary.Write(w, binary.BigEndian, msg.Dummy)
data := w.Bytes()
msg.writeHeader(msgStartCapReply, data)
return data, checkLargePacket(data)
}
func (msg *StartCaptureReply) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.Bufsize,
&msg.Port,
&msg.Dummy,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
return
}
// UpdateFilterReply confirms filter has been applied correctly on the remote device
type UpdateFilterReply struct {
Header
}
func (msg *UpdateFilterReply) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgUpdateFilterReply, data)
return data, checkLargePacket(data)
}
func (msg *UpdateFilterReply) decode(r io.Reader) (err error) {
return
}
// AuthReply says 'ok, authorization successful'
type AuthReply struct {
Header
}
func (msg *AuthReply) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgAuthReply, data)
return data, checkLargePacket(data)
}
func (msg *AuthReply) decode(r io.Reader) (err error) {
return
}
// StatsReply keeps the network statistics about the number of packets captured, dropped, etc.
type StatsReply struct {
Header
IfRecv uint32 // Packets received by the kernel filter (i.e. pcap_stats.ps_recv)
IfDrop uint32 // Packets dropped by the network interface (e.g. not enough buffers) (i.e. pcap_stats.ps_ifdrop)
KrnlDrop uint32 // Packets dropped by the kernel filter (i.e. pcap_stats.ps_drop)
SvrCapt uint32 // Packets captured by the RPCAP daemon and sent on the network
}
func (msg *StatsReply) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
binary.Write(w, binary.BigEndian, msg.IfRecv)
binary.Write(w, binary.BigEndian, msg.IfDrop)
binary.Write(w, binary.BigEndian, msg.KrnlDrop)
binary.Write(w, binary.BigEndian, msg.SvrCapt)
data := w.Bytes()
msg.writeHeader(msgStatsReply, data)
return data, checkLargePacket(data)
}
func (msg *StatsReply) decode(r io.Reader) (err error) {
params := []interface{}{
&msg.IfRecv,
&msg.IfDrop,
&msg.KrnlDrop,
&msg.SvrCapt,
}
for _, param := range params {
if err = binary.Read(r, binary.BigEndian, param); err != nil {
return err
}
}
return
}
// EndCaptureReply confirms that the capture stopped succesfully
type EndCaptureReply struct {
Header
}
func (msg *EndCaptureReply) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgAuthReply, data)
return data, checkLargePacket(data)
}
func (msg *EndCaptureReply) decode(r io.Reader) (err error) {
return
}
// SetSamplingReply confirms that the capture stopped succesfully
type SetSamplingReply struct {
Header
}
func (msg *SetSamplingReply) encode() ([]byte, error) {
data := make([]byte, headerSize, headerSize)
msg.writeHeader(msgSetsamplingReply, data)
return data, checkLargePacket(data)
}
func (msg *SetSamplingReply) decode(r io.Reader) (err error) {
return
}
// UnknownMessage is used to transfer non standard messages
type UnknownMessage struct {
Header
Payload []byte
}
func (msg *UnknownMessage) encode() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, headerSize, headerSize))
w.Write(msg.Payload)
data := w.Bytes()
msg.writeHeader(msg.header().Type, data)
return data, checkLargePacket(data)
}
func (msg *UnknownMessage) decode(r io.Reader) (err error) {
msg.Payload = make([]byte, msg.header().pLength, msg.header().pLength)
_, err = io.ReadFull(r, msg.Payload)
return err
}