-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathncmdump.go
257 lines (211 loc) · 5.23 KB
/
ncmdump.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package ncmdump
import (
"bytes"
"crypto/aes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"math"
"os"
)
var (
aesCoreKey = []byte{0x68, 0x7A, 0x48, 0x52, 0x41, 0x6D, 0x73, 0x6F, 0x35, 0x6B, 0x49, 0x6E, 0x62, 0x61, 0x78, 0x57}
aesModifyKey = []byte{0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21, 0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28}
)
func buildKeyBox(key []byte) []byte {
box := make([]byte, 256)
for i := 0; i < 256; i++ {
box[i] = byte(i)
}
keyLen := byte(len(key))
var c, lastByte, keyOffset byte
for i := 0; i < 256; i++ {
c = (box[i] + lastByte + key[keyOffset]) & 0xff
keyOffset++
if keyOffset >= keyLen {
keyOffset = 0
}
box[i], box[c] = box[c], box[i]
lastByte = c
}
return box
}
func fixBlockSize(src []byte) []byte {
return src[:len(src)/aes.BlockSize*aes.BlockSize]
}
func _PKCS7UnPadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}
func decryptAes128Ecb(key, data []byte) ([]byte, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, err
}
dataLen := len(data)
decrypted := make([]byte, dataLen)
bs := block.BlockSize()
for i := 0; i <= dataLen-bs; i += bs {
block.Decrypt(decrypted[i:i+bs], data[i:i+bs])
}
return _PKCS7UnPadding(decrypted), nil
}
func readUint32(rBuf []byte, fp *os.File) (uint32, error) {
if _, err := fp.Read(rBuf); err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(rBuf), nil
}
func NCMFile(fp *os.File) (bool, error) {
// Jump to begin of file
if _, err := fp.Seek(0, io.SeekStart); err != nil {
return false, err
}
var rBuf = make([]byte, 4)
uLen, _ := readUint32(rBuf, fp)
if uLen != 0x4e455443 {
return false, fmt.Errorf("%s isn't netease cloud music copyright file", fp.Name())
}
uLen, _ = readUint32(rBuf, fp)
if uLen != 0x4d414446 {
return false, fmt.Errorf("%s isn't netease cloud music copyright file", fp.Name())
}
return true, nil
}
func Decode(fp *os.File) ([]byte, error) {
// detect whether ncm file
if result, err := NCMFile(fp); err != nil || !result {
return nil, err
}
// jump over the magic head(4*2) and the gap(2).
if _, err := fp.Seek(4*2+2, io.SeekStart); err != nil {
return nil, err
}
var rBuf = make([]byte, 4)
uLen, err := readUint32(rBuf, fp)
if err != nil {
return nil, err
}
var keyData = make([]byte, uLen)
if _, err := fp.Read(keyData); err != nil {
return nil, err
}
for i := range keyData {
keyData[i] ^= 0x64
}
deKeyData, err := decryptAes128Ecb(aesCoreKey, fixBlockSize(keyData))
if err != nil {
return nil, err
}
// 17 = len("neteasecloudmusic")
return deKeyData[17:], nil
}
func DumpMeta(fp *os.File) (Meta, error) {
// detect whether ncm file
if result, err := NCMFile(fp); err != nil || !result {
return Meta{}, err
}
// jump over the magic head(4*2) and the gap(2).
if _, err := fp.Seek(4*2+2, io.SeekStart); err != nil {
return Meta{}, err
}
// whether decode key is successful
if _, err := Decode(fp); err != nil {
return Meta{}, err
}
var rBuf = make([]byte, 4)
uLen, err := readUint32(rBuf, fp)
if err != nil {
return Meta{}, err
}
if uLen <= 0 {
format := "flac"
if info, err := fp.Stat(); err != nil && info.Size() < int64(math.Pow(1024, 2)*16) {
format = "mp3"
}
return Meta{
Format: format,
}, nil
}
var modifyData = make([]byte, uLen)
if _, err = fp.Read(modifyData); err != nil {
return Meta{}, err
}
for i := range modifyData {
modifyData[i] ^= 0x63
}
// 22 = len(`163 key(Don't modify):`)
deModifyData := make([]byte, base64.StdEncoding.DecodedLen(len(modifyData)-22))
if _, err = base64.StdEncoding.Decode(deModifyData, modifyData[22:]); err != nil {
return Meta{}, err
}
deData, err := decryptAes128Ecb(aesModifyKey, fixBlockSize(deModifyData))
if err != nil {
return Meta{}, err
}
// 6 = len("music:")
deData = deData[6:]
var album Album
if err := json.Unmarshal(deData, &album); err != nil {
return Meta{}, err
}
var meta Meta
if err := json.Unmarshal(deData, &meta); err != nil {
return Meta{}, err
}
meta.Album = &album
return meta, nil
}
func DumpCover(fp *os.File) ([]byte, error) {
if result, err := NCMFile(fp); !result || err != nil {
return nil, err
}
if _, err := DumpMeta(fp); err != nil {
return nil, err
}
// jump over crc32 check
if _, err := fp.Seek(9, io.SeekCurrent); err != nil {
return nil, err
}
var rBuf = make([]byte, 4)
if imgLen, err := readUint32(rBuf, fp); err != nil {
return nil, err
} else {
var imgData = make([]byte, imgLen)
if _, err = fp.Read(imgData); err != nil {
return nil, err
}
return imgData, nil
}
}
func Dump(fp *os.File) ([]byte, error) {
if result, err := NCMFile(fp); !result || err != nil {
return nil, err
}
// whether decode key is successful
deKeyData, err := Decode(fp)
if err != nil {
return nil, err
}
if _, err := DumpCover(fp); err != nil {
return nil, err
}
box := buildKeyBox(deKeyData)
n := 0x8000
var writer bytes.Buffer
var tb = make([]byte, n)
for {
if _, err := fp.Read(tb); err != nil {
break // read EOF
}
for i := 0; i < n; i++ {
j := byte((i + 1) & 0xff)
tb[i] ^= box[(box[j]+box[(box[j]+j)&0xff])&0xff]
}
writer.Write(tb) // write to memory
}
return writer.Bytes(), nil
}