/
msrpc.go
96 lines (85 loc) · 1.95 KB
/
msrpc.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
package dcerpc
import (
"encoding/binary"
"math/rand"
"github.com/5amu/goad/pkg/encoder"
)
// PDU PacketType
// https://pubs.opengroup.org/onlinepubs/9629399/chap12.htm
const (
PDURequest = iota
PDUPing
PDUResponse
PDUFault
PDUWorking
PDUNoCall
PDUReject
PDUAck
PDUClCancel
PDUFack
PDUCancelAck
PDUBind
PDUBindAck
PDUBindNak
PDUAlterContext
PDUAlterContextResp
PDUShutdown
PDUCoCancel
PDUOrphaned
)
// PDU PacketFlags
// https://pubs.opengroup.org/onlinepubs/9629399/chap12.htm
const (
FirstFrag = 0x01
LastFrag = 0x02
PDUFlagPending = 0x03
CancelPending = 0x04
PDUFlagNoFack = 0x08
PDUFlagMayBe = 0x10
PDUFlagIdemPotent = 0x20
PDUFlagBroadcast = 0x40
PDUFlagReserved_80 = 0x80
)
// Supported version is 5.0
const (
PDUVersion = 5
PDUVersionMinor = 0
)
type HeaderStruct struct {
RpcVersion uint8
RpcVersionMinor uint8
PacketType uint8
PacketFlags byte
DataRepresentation []byte `smb:"fixed:4"`
FragLength uint16
AuthLength uint16
CallId uint32
}
func NewHeader() *HeaderStruct {
return &HeaderStruct{
RpcVersion: PDUVersion,
RpcVersionMinor: PDUVersionMinor,
PacketType: PDURequest,
PacketFlags: FirstFrag | LastFrag,
DataRepresentation: []byte{0x10, 0, 0, 0}, // Little-Endian, float = IEEE, char = ASCII
FragLength: 0, // must be updated before sending
AuthLength: 0,
CallId: rand.Uint32(),
}
}
type RequestStruct struct {
HeaderStruct
AllocHint uint32
ContextID uint16
OpNum uint16
Payload interface{}
}
func (req *RequestStruct) Bytes() []byte {
b, _ := encoder.Marshal(req)
sz := len(b)
// Set FragLength to the size of the RPC request
binary.LittleEndian.PutUint16(b[8:10], uint16(sz))
// Set AllocHint to the size of the RPC body (the header is 24 bytes)
binary.LittleEndian.PutUint32(b[24:28], uint32(sz)-24)
return b
}