-
Notifications
You must be signed in to change notification settings - Fork 0
/
obytes.go
145 lines (125 loc) · 2.87 KB
/
obytes.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
// +build go1.12
// Package obytes provides simple byte obfuscation primitives with user-defined
// parameters.
package obytes
import (
"crypto/rand"
)
const (
HighNibble = 1
LowNibble = 0
)
/*
I NEED TO DEFINE MASKs as a pair of byte constants..
*/
var (
MaskDefault = [...]byte{0x55, 0x55}
MaskHalfFlaH = [...]byte{0xf0, 0x0f}
MaskFlahHalf = [...]byte{0x0f, 0xf0}
)
/* simple obfuscation keep you busy and suck your time. */
func obfuscateByte(in byte, mask_hi, mask_lo byte) (out []byte, err error) {
rnd := make([]byte, 2)
out = make([]byte, 2)
// get random bytes & apply the mask
_, err = rand.Read(rnd)
if err != nil {
return nil, ErrUnexpected //fmt.Errorf("rand error")
}
rnd[0] &= ^(mask_lo)
rnd[1] &= ^(mask_hi)
/* lo nibble */
for i := 0; i < 8; i++ {
if (mask_lo>>uint(i))&0x01 == 0x01 {
out[0] |= (in & 0x01) << uint(i)
in >>= 0x01
} else {
out[0] |= ((rnd[0] >> uint(i) & 0x01) << uint(i))
}
}
/* hi nibble */
for i := 0; i < 8; i++ {
if (mask_hi>>uint(i))&0x01 == 0x01 {
out[1] |= (in & 0x01) << uint(i)
in >>= 0x01
} else {
out[1] |= ((rnd[1] >> uint(i) & 0x01) << uint(i))
}
}
/*
fmt.Printf("MASKED RND[]: 0x%02x 0x%02x\n", rnd[1], rnd[0])
fmt.Printf("OBF IN[]: %02x\n", in[0])
fmt.Printf("OBF OUT[]: 0x%02x 0x%02x\n", out[1], out[0])
*/
return
}
func deobfuscateByte(in []byte, mask_hi, mask_lo byte) (out byte, err error) {
var cnt int
/*
if len(in) != 2 {
err = ErrInvalidInput
return
}
*/
for i := 7; i >= 0; i-- {
if (mask_hi>>uint(i))&0x01 == 0x01 {
out |= (in[1] >> uint(i)) & 0x01
if cnt != 7 {
out <<= 0x01
}
cnt++
}
}
for i := 7; i >= 0; i-- {
if (mask_lo>>uint(i))&0x01 == 0x01 {
out |= (in[0] >> uint(i)) & 0x01
//if i != 0 { // NO NEED TO SHIFT ON THE LAST BIT
if cnt != 7 { // THE EIGHT BIT MASK CAN BE DISTRIBUTED HOW YOU WANT INSIDE MASK_HI MASK_LO
out <<= 0x01
}
cnt++
}
}
return
}
func ObfuscateWithMask(in []byte, hi, lo byte) (out []byte, err error) {
if len(in) == 0 {
err = ErrInvalidInput
return
}
//out = make([]byte, len(in)*2)
for i := range in {
/* mask have to be provided out */
tmp, derr := obfuscateByte(in[i], hi, lo)
if derr != nil {
err = derr
return
}
out = append(out, tmp...)
}
return
}
/* can process it with any number of bytes */
func Obfuscate(in []byte) (out []byte, err error) {
return ObfuscateWithMask(in, 0x55, 0x55)
}
/* can process it with any number of bytes */
func DeobfuscateWithMask(in []byte, hi, lo byte) (out []byte, err error) {
if len(in) == 0 || len(in)%2 != 0 {
err = ErrInvalidInput
return
}
out = make([]byte, len(in)/2)
for i := range out {
tmp, derr := deobfuscateByte(in[i*2:(i*2)+2], hi, lo)
if derr != nil {
err = derr
return
}
out[i] = tmp
}
return
}
func Deobfuscate(in []byte) (out []byte, err error) {
return DeobfuscateWithMask(in, 0x55, 0x55)
}