-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.go
179 lines (163 loc) · 3.71 KB
/
decoder.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
package ssce
import (
"bytes"
"encoding/binary"
)
func (e *Encoder) decoderStub() []byte {
var decoder []byte
switch e.arch {
case 32:
decoder = x86Decoder
case 64:
decoder = x64Decoder
}
return e.miniXOR(decoder, e.stubKey)
}
func (e *Encoder) eraserStub() []byte {
var eraser []byte
switch e.arch {
case 32:
eraser = x86Eraser
case 64:
eraser = x64Eraser
}
return e.miniXOR(eraser, e.stubKey)
}
func (e *Encoder) cryptoKeyStub() []byte {
return e.miniXOR(e.key, e.stubKey)
}
func (e *Encoder) miniXOR(inst []byte, key interface{}) []byte {
switch e.arch {
case 32:
return e.miniXOR32(inst, key.(uint32))
case 64:
return e.miniXOR64(inst, key.(uint64))
default:
panic("invalid architecture")
}
}
func (e *Encoder) miniXOR32(inst []byte, key uint32) []byte {
// ensure the instructions length can be divisible by 4.
inst = bytes.Clone(inst)
numPad := len(inst) % 4
if numPad != 0 {
numPad = 4 - numPad
}
inst = append(inst, e.randBytes(numPad)...)
for i := 0; i < len(inst); i += 4 {
val := binary.LittleEndian.Uint32(inst[i:]) ^ key
binary.LittleEndian.PutUint32(inst[i:], val)
}
return inst
}
func (e *Encoder) miniXOR64(inst []byte, key uint64) []byte {
// ensure the instructions length can be divisible by 8.
inst = bytes.Clone(inst)
numPad := len(inst) % 8
if numPad != 0 {
numPad = 8 - numPad
}
inst = append(inst, e.randBytes(numPad)...)
for i := 0; i < len(inst); i += 8 {
val := binary.LittleEndian.Uint64(inst[i:]) ^ key
binary.LittleEndian.PutUint64(inst[i:], val)
}
return inst
}
func (e *Encoder) xsrl(inst []byte, seed, key uint32) []byte {
// ensure the instructions length can be divisible by 4.
inst = bytes.Clone(inst)
numPad := len(inst) % 4
if numPad != 0 {
numPad = 4 - numPad
}
inst = append(inst, e.randBytes(numPad)...)
for i := 0; i < len(inst); i += 4 {
val := binary.LittleEndian.Uint32(inst[i:])
switch e.arch {
case 32:
val ^= key
val = ror32(val, 17)
val ^= seed
val = rol32(val, 5)
case 64:
val ^= key
val = ror32(val, 7)
val ^= seed
val = rol32(val, 17)
}
binary.LittleEndian.PutUint32(inst[i:], val)
seed = xorShift32(seed)
}
return inst
}
func encrypt32(data, key []byte) []byte {
output := make([]byte, len(data))
last := binary.LittleEndian.Uint32(key[:4])
ctr := binary.LittleEndian.Uint32(key[4:])
keyIdx := int(last % uint32(len(key)))
for i := 0; i < len(data); i++ {
b := data[i]
b ^= byte(last)
b = rol(b, uint8(last%8))
b ^= key[keyIdx]
b += byte(ctr ^ last)
b = ror(b, uint8(last%8))
output[i] = b
// update key index
keyIdx++
if keyIdx >= len(key) {
keyIdx = 0
}
ctr++
last = xorShift32(last)
}
return output
}
func encrypt64(data, key []byte) []byte {
output := make([]byte, len(data))
last := binary.LittleEndian.Uint64(key[:8])
ctr := binary.LittleEndian.Uint64(key[8:])
keyIdx := int(last % uint64(len(key)))
for i := 0; i < len(data); i++ {
b := data[i]
b ^= byte(last)
b = rol(b, uint8(last%8))
b ^= key[keyIdx]
b += byte(ctr ^ last)
b = ror(b, uint8(last%8))
output[i] = b
// update key index
keyIdx++
if keyIdx >= len(key) {
keyIdx = 0
}
ctr++
last = xorShift64(last)
}
return output
}
func xorShift32(seed uint32) uint32 {
seed ^= seed << 13
seed ^= seed >> 17
seed ^= seed << 5
return seed
}
func xorShift64(seed uint64) uint64 {
seed ^= seed << 13
seed ^= seed >> 7
seed ^= seed << 17
return seed
}
func ror(value byte, bits uint8) byte {
return value>>bits | value<<(8-bits)
}
func rol(value byte, bits uint8) byte {
return value<<bits | value>>(8-bits)
}
func ror32(value uint32, bits uint8) uint32 {
return value>>bits | value<<(32-bits)
}
func rol32(value uint32, bits uint8) uint32 {
return value<<bits | value>>(32-bits)
}