forked from sensepost/ruler
/
utils.go
215 lines (193 loc) · 5.51 KB
/
utils.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
package utils
import (
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"hash/fnv"
"reflect"
)
var (
put32 = binary.LittleEndian.PutUint32
put16 = binary.LittleEndian.PutUint16
//EncBase64 wrapper for encoding to base64
EncBase64 = base64.StdEncoding.EncodeToString
//DecBase64 wrapper for decoding from base64
DecBase64 = base64.StdEncoding.DecodeString
)
//FromUnicode read unicode and convert to byte array
func FromUnicode(uni []byte) string {
st := ""
for _, k := range uni {
if k != 0x00 {
st += string(k)
}
}
return st
}
//UniString converts a string into a unicode string byte array
func UniString(str string) []byte {
bt := make([]byte, (len(str) * 2))
cnt := 0
for _, v := range str {
bt[cnt] = byte(v)
cnt++
bt[cnt] = 0x00
cnt++
}
bt = append(bt, []byte{0x00, 0x00}...)
return bt
}
//UTF16BE func to encode strings for the CRuleElement
func UTF16BE(str string, trail int) []byte {
bt := make([]byte, (len(str) * 2))
cnt := 0
for _, v := range str {
bt[cnt] = byte(v)
cnt++
bt[cnt] = 0x00
cnt++
}
if trail == 1 {
bt = append(bt, []byte{0x01}...)
}
byteNum := new(bytes.Buffer)
binary.Write(byteNum, binary.BigEndian, uint16(len(bt)/2))
bt = append(byteNum.Bytes(), bt...)
return bt
}
//DecodeUint32 decode 4 byte value into uint32
func DecodeUint32(num []byte) uint32 {
var number uint32
bf := bytes.NewReader(num)
binary.Read(bf, binary.LittleEndian, &number)
return number
}
//DecodeUint16 decode 2 byte value into uint16
func DecodeUint16(num []byte) uint16 {
var number uint16
bf := bytes.NewReader(num)
binary.Read(bf, binary.LittleEndian, &number)
return number
}
//DecodeUint8 decode 1 byte value into uint8
func DecodeUint8(num []byte) uint8 {
var number uint8
bf := bytes.NewReader(num)
binary.Read(bf, binary.LittleEndian, &number)
return number
}
//EncodeNum encode a number as a byte array
func EncodeNum(v interface{}) []byte {
byteNum := new(bytes.Buffer)
binary.Write(byteNum, binary.LittleEndian, v)
return byteNum.Bytes()
}
//BodyToBytes func
func BodyToBytes(DataStruct interface{}) []byte {
dumped := []byte{}
v := reflect.ValueOf(DataStruct)
var value []byte
//check if we have a slice of structs
if reflect.TypeOf(DataStruct).Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
if v.Index(i).Kind() == reflect.Uint8 || v.Index(i).Kind() == reflect.Uint16 || v.Index(i).Kind() == reflect.Uint32 {
byteNum := new(bytes.Buffer)
binary.Write(byteNum, binary.LittleEndian, v.Index(i).Interface())
dumped = append(dumped, byteNum.Bytes()...)
} else {
if v.Index(i).Kind() == reflect.Struct || v.Index(i).Kind() == reflect.Slice || v.Index(i).Kind() == reflect.Interface {
value = BodyToBytes(v.Index(i).Interface())
} else {
value = v.Index(i).Bytes()
}
dumped = append(dumped, value...)
}
}
} else {
for i := 0; i < v.NumField(); i++ {
if v.Field(i).Kind() == reflect.Uint8 || v.Field(i).Kind() == reflect.Uint16 || v.Field(i).Kind() == reflect.Uint32 {
byteNum := new(bytes.Buffer)
binary.Write(byteNum, binary.LittleEndian, v.Field(i).Interface())
dumped = append(dumped, byteNum.Bytes()...)
} else {
if v.Field(i).Kind() == reflect.Struct || v.Field(i).Kind() == reflect.Slice || v.Field(i).Kind() == reflect.Interface {
value = BodyToBytes(v.Field(i).Interface())
} else {
fmt.Println(v.Field(i).Kind())
value = v.Field(i).Bytes()
}
dumped = append(dumped, value...)
}
}
}
return dumped
}
//ReadUint32 read 4 bytes and return as uint32
func ReadUint32(pos int, buff []byte) (uint32, int) {
return DecodeUint32(buff[pos : pos+4]), pos + 4
}
//ReadUint16 read 2 bytes and return as uint16
func ReadUint16(pos int, buff []byte) (uint16, int) {
return DecodeUint16(buff[pos : pos+2]), pos + 2
}
//ReadUint8 read 1 byte and return as uint8
func ReadUint8(pos int, buff []byte) (uint8, int) {
return DecodeUint8(buff[pos : pos+2]), pos + 2
}
//ReadBytes read and return count number o bytes
func ReadBytes(pos, count int, buff []byte) ([]byte, int) {
return buff[pos : pos+count], pos + count
}
//ReadByte read and return a single byte
func ReadByte(pos int, buff []byte) (byte, int) {
return buff[pos : pos+1][0], pos + 1
}
//ReadUnicodeString read and return a unicode string
func ReadUnicodeString(pos int, buff []byte) ([]byte, int) {
//stupid hack as using bufio and ReadString(byte) would terminate too early
//would terminate on 0x00 instead of 0x0000
index := bytes.Index(buff[pos:], []byte{0x00, 0x00})
str := buff[pos : pos+index]
return []byte(str), pos + index + 2
}
//ReadASCIIString returns a string as ascii
func ReadASCIIString(pos int, buff []byte) ([]byte, int) {
bf := bytes.NewBuffer(buff[pos:])
str, _ := bf.ReadString(0x00)
return []byte(str), pos + len(str)
}
//ReadTypedString reads a string as either Unicode or ASCII depending on type value
func ReadTypedString(pos int, buff []byte) ([]byte, int) {
var t = buff[pos]
if t == 0 { //no string
return []byte{}, pos + 1
}
if t == 1 {
return []byte{}, pos + 1
}
if t == 3 {
str, p := ReadASCIIString(pos+1, buff)
return str, p
}
if t == 4 {
str, p := ReadUnicodeString(pos+1, buff)
return str, p
}
str, _ := ReadBytes(pos+1, 4, buff)
return str, pos + len(str)
}
//Hash Calculate a 32byte hash
func Hash(s string) uint32 {
h := fnv.New32()
h.Write([]byte(s))
return h.Sum32()
}
//Obfuscate traffic using XOR and the magic byte as specified in RPC docs
func Obfuscate(data []byte) []byte {
bnew := make([]byte, len(data))
for k := range data {
bnew[k] = data[k] ^ 0xA5
}
return bnew
}