forked from sijms/go-ora
/
main.go
147 lines (140 loc) · 3.5 KB
/
main.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/des"
"encoding/hex"
"fmt"
"log"
"strings"
)
func getKeyFromUserNameAndPassword(username string, password string) ([]byte, error) {
username = strings.ToUpper(username)
password = strings.ToUpper(password)
extendString := func(str string) []byte {
ret := make([]byte, len(str)*2)
for index, char := range []byte(str) {
ret[index*2] = 0
ret[index*2+1] = char
}
return ret
}
buffer := append(extendString(username), extendString(password)...)
if len(buffer)%8 > 0 {
buffer = append(buffer, make([]byte, 8-len(buffer)%8)...)
}
key := []byte{1, 35, 69, 103, 137, 171, 205, 239}
DesEnc := func(input []byte, key []byte) ([]byte, error) {
ret := make([]byte, 8)
enc, err := des.NewCipher(key)
if err != nil {
return nil, err
}
for x := 0; x < len(input)/8; x++ {
for y := 0; y < 8; y++ {
ret[y] = uint8(int(ret[y]) ^ int(input[x*8+y]))
}
output := make([]byte, 8)
enc.Encrypt(output, ret)
copy(ret, output)
}
return ret, nil
}
key1, err := DesEnc(buffer, key)
if err != nil {
return nil, err
}
key2, err := DesEnc(buffer, key1)
if err != nil {
return nil, err
}
// function OSLogonHelper.Method1_bytearray (DecryptSessionKey)
return append(key2, make([]byte, 8)...), nil
}
func decryptSessionKey2(encKey []byte, sessionKey string) ([]byte, error) {
result, err := hex.DecodeString(sessionKey)
if err != nil {
return nil, err
}
blk, err := des.NewCipher(encKey)
if err != nil {
return nil, err
}
enc := cipher.NewCBCDecrypter(blk, make([]byte, 8))
output := make([]byte, len(result))
enc.CryptBlocks(output, result)
return output, nil
}
func encryptPassword(password string, key []byte) (string, error) {
padding := 0
temp := []byte(password)
if len(password)%8 > 0 {
padding = 8 - (len(password) % 8)
temp = append(temp, bytes.Repeat([]byte{0}, padding)...)
}
blk, err := des.NewCipher(key)
if err != nil {
return "", err
}
enc := cipher.NewCBCDecrypter(blk, make([]byte, 8))
output := make([]byte, len(temp))
enc.CryptBlocks(output, temp)
encPassword := append(output, uint8(padding))
// [36, -90, -28, -115, -91, 95, -80, -2]
fmt.Println("enc password: ", encPassword)
return hex.EncodeToString(encPassword), nil
}
func decryptSessionKey(padding bool, encKey []byte, sessionKey string) ([]byte, error) {
result, err := hex.DecodeString(sessionKey)
if err != nil {
return nil, err
}
blk, err := aes.NewCipher(encKey)
if err != nil {
return nil, err
}
//if padding {
// result = PKCS5Padding(result, blk.BlockSize())
//}
enc := cipher.NewCBCDecrypter(blk, make([]byte, 16))
output := make([]byte, len(result))
enc.CryptBlocks(output, result)
cutLen := 0
if padding {
num := int(output[len(output)-1])
if num < enc.BlockSize() {
apply := true
for x := len(output) - num; x < len(output); x++ {
if output[x] != uint8(num) {
apply = false
break
}
}
if apply {
cutLen = int(output[len(output)-1])
}
}
}
return output[:len(output)-cutLen], nil
}
func main() {
username := "LAB"
password := "lab"
encSessionKey := "CC22A4B3963665FA"
//authSK, err := hex.DecodeString(authSKString)
//if err != nil {
// log.Fatalln(err)
//}
key, err := getKeyFromUserNameAndPassword(username, password)
if err != nil {
log.Fatalln(err)
}
sessionKey, err := decryptSessionKey2(key[:8], encSessionKey)
if err != nil {
log.Fatalln(err)
}
// [95 242 76 225 183 123 160 169]
fmt.Println(sessionKey)
fmt.Println(encryptPassword(password, sessionKey))
}