1
0
forked from um/cli

feat(QMCv2): Add mapCipher & mflac/mgg key discovery

This commit is contained in:
MengYX 2021-12-13 22:54:08 +08:00
parent 1552a667f6
commit 38648d57e6
No known key found for this signature in database
GPG Key ID: E63F9C7303E8F604
17 changed files with 295 additions and 134 deletions

5
algo/qmc/cipher.go Normal file
View File

@ -0,0 +1,5 @@
package qmc
type streamCipher interface {
Decrypt(buf []byte, offset int)
}

39
algo/qmc/cipher_map.go Normal file
View File

@ -0,0 +1,39 @@
package qmc
import "errors"
type mapCipher struct {
key []byte
box []byte
size int
}
func NewMapCipher(key []byte) (*mapCipher, error) {
if len(key) == 0 {
return nil, errors.New("qmc/cipher_map: invalid key size")
}
c := &mapCipher{key: key, size: len(key)}
c.box = make([]byte, c.size)
return c, nil
}
func (c *mapCipher) getMask(offset int) byte {
if offset > 0x7FFF {
offset %= 0x7FFF
}
idx := (offset*offset + 71214) % c.size
return c.rotate(c.key[idx], byte(idx)&0x7)
}
func (c *mapCipher) rotate(value byte, bits byte) byte {
rotate := (bits + 4) % 8
left := value << rotate
right := value >> rotate
return left | right
}
func (c *mapCipher) Decrypt(buf []byte, offset int) {
for i := 0; i < len(buf); i++ {
buf[i] ^= c.getMask(offset + i)
}
}

View File

@ -0,0 +1,40 @@
package qmc
import (
"os"
"reflect"
"testing"
)
func loadTestMapCipherData() ([]byte, []byte, []byte, error) {
key, err := os.ReadFile("./testdata/mflac_map_key.bin")
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile("./testdata/mflac_map_raw.bin")
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile("./testdata/mflac_map_target.bin")
if err != nil {
return nil, nil, nil, err
}
return key, raw, target, nil
}
func Test_mapCipher_Decrypt(t *testing.T) {
key, raw, target, err := loadTestMapCipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("overall", func(t *testing.T) {
c, err := NewMapCipher(key)
if err != nil {
t.Errorf("init mapCipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}

View File

@ -17,7 +17,7 @@ type rc4Cipher struct {
func NewRC4Cipher(key []byte) (*rc4Cipher, error) { func NewRC4Cipher(key []byte) (*rc4Cipher, error) {
n := len(key) n := len(key)
if n == 0 { if n == 0 {
return nil, errors.New("crypto/rc4: invalid key size") return nil, errors.New("qmc/cipher_rc4: invalid key size")
} }
var c = rc4Cipher{key: key} var c = rc4Cipher{key: key}
@ -54,7 +54,7 @@ func (c *rc4Cipher) getHashBase() {
const rc4SegmentSize = 5120 const rc4SegmentSize = 5120
func (c *rc4Cipher) Process(src []byte, offset int) { func (c *rc4Cipher) Decrypt(src []byte, offset int) {
toProcess := len(src) toProcess := len(src)
processed := 0 processed := 0
markProcess := func(p int) (finished bool) { markProcess := func(p int) (finished bool) {

View File

@ -0,0 +1,90 @@
package qmc
import (
"os"
"reflect"
"testing"
)
func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
key, err := os.ReadFile("./testdata/mflac0_rc4_key.bin")
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile("./testdata/mflac0_rc4_raw.bin")
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile("./testdata/mflac0_rc4_target.bin")
if err != nil {
return nil, nil, nil, err
}
return key, raw, target, nil
}
func Test_rc4Cipher_Decrypt(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("overall", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("first-block(0~128)", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[:128], 0)
if !reflect.DeepEqual(raw[:128], target[:128]) {
t.Error("first-block(0~128)")
}
})
}
func Test_rc4Cipher_encASegment(t *testing.T) {
key, raw, target, err := loadTestRC4CipherData()
if err != nil {
t.Fatalf("load testing data failed: %s", err)
}
t.Run("align-block(128~5120)", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[128:5120], 128)
if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
t.Error("align-block(128~5120)")
}
})
t.Run("simple-block(5120~10240)", func(t *testing.T) {
c, err := NewRC4Cipher(key)
if err != nil {
t.Errorf("init rc4Cipher failed: %s", err)
return
}
c.Decrypt(raw[5120:10240], 5120)
if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
t.Error("align-block(128~5120)")
}
})
}

View File

@ -18,10 +18,14 @@ func simpleMakeKey(salt byte, length int) []byte {
} }
func DecryptKey(rawKey []byte) ([]byte, error) { func DecryptKey(rawKey []byte) ([]byte, error) {
rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey))) rawKeyDec := make([]byte, base64.StdEncoding.DecodedLen(len(rawKey)))
_, err := base64.StdEncoding.Decode(rawKeyDec, rawKey) n, err := base64.StdEncoding.Decode(rawKeyDec, rawKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if n < 16 {
return nil, errors.New("key length is too short")
}
rawKeyDec = rawKeyDec[:n]
simpleKey := simpleMakeKey(106, 8) simpleKey := simpleMakeKey(106, 8)
teaKey := make([]byte, 16) teaKey := make([]byte, 16)

View File

@ -1,6 +1,7 @@
package qmc package qmc
import ( import (
"fmt"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -14,38 +15,41 @@ func TestSimpleMakeKey(t *testing.T) {
} }
}) })
} }
func loadDecryptKeyData(name string) ([]byte, []byte, error) {
keyRaw, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key_raw.bin", name))
if err != nil {
return nil, nil, err
}
keyDec, err := os.ReadFile(fmt.Sprintf("./testdata/%s_key.bin", name))
if err != nil {
return nil, nil, err
}
return keyRaw, keyDec, nil
}
func TestDecryptKey(t *testing.T) { func TestDecryptKey(t *testing.T) {
rc4Raw, err := os.ReadFile("./testdata/rc4_key_raw.bin")
if err != nil {
t.Error(err)
}
rc4Dec, err := os.ReadFile("./testdata/rc4_key.bin")
if err != nil {
t.Error(err)
}
tests := []struct { tests := []struct {
name string name string
rawKey []byte filename string
want []byte
wantErr bool wantErr bool
}{ }{
{ {"mflac0_rc4(512)", "mflac0_rc4", false},
"512", {"mflac_map(256)", "mflac_map", false},
rc4Raw,
rc4Dec,
false,
},
} }
for _, tt := range tests { for _, tt := range tests {
raw, want, err := loadDecryptKeyData(tt.filename)
if err != nil {
t.Fatalf("load test data failed: %s", err)
}
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := DecryptKey(tt.rawKey) got, err := DecryptKey(raw)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("DecryptKey() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, want) {
t.Errorf("DecryptKey() got = %v..., want %v...", string(got[:32]), string(tt.want[:32])) t.Errorf("DecryptKey() got = %v..., want %v...",
string(got[:32]), string(want[:32]))
} }
}) })
} }

View File

@ -13,7 +13,7 @@ type Mflac0Decoder struct {
audioLen int audioLen int
decodedKey []byte decodedKey []byte
rc4 *rc4Cipher cipher streamCipher
offset int offset int
rawMetaExtra1 int rawMetaExtra1 int
@ -21,6 +21,14 @@ type Mflac0Decoder struct {
} }
func (d *Mflac0Decoder) Read(p []byte) (int, error) { func (d *Mflac0Decoder) Read(p []byte) (int, error) {
if d.cipher != nil {
return d.readRC4(p)
} else {
panic("not impl")
//return d.readPlain(p)
}
}
func (d *Mflac0Decoder) readRC4(p []byte) (int, error) {
n := len(p) n := len(p)
if d.audioLen-d.offset <= 0 { if d.audioLen-d.offset <= 0 {
return 0, io.EOF return 0, io.EOF
@ -32,32 +40,37 @@ func (d *Mflac0Decoder) Read(p []byte) (int, error) {
return 0, err return 0, err
} }
d.rc4.Process(p[:m], d.offset) d.cipher.Decrypt(p[:m], d.offset)
d.offset += m d.offset += m
return m, err return m, err
} }
func NewMflac0Decoder(r io.ReadSeeker) (*Mflac0Decoder, error) { func NewMflac0Decoder(r io.ReadSeeker) (*Mflac0Decoder, error) {
d := &Mflac0Decoder{r: r} d := &Mflac0Decoder{r: r}
if err := d.searchKey(); err != nil { err := d.searchKey()
if err != nil {
return nil, err return nil, err
} }
if len(d.decodedKey) > 300 { if len(d.decodedKey) == 0 {
var err error return nil, errors.New("invalid decoded key")
d.rc4, err = NewRC4Cipher(d.decodedKey) } else if len(d.decodedKey) > 300 {
d.cipher, err = NewRC4Cipher(d.decodedKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
panic("not implement") //todo: impl d.cipher, err = NewMapCipher(d.decodedKey)
}
_, err := d.r.Seek(0, io.SeekStart)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
_, err = d.r.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
return d, nil return d, nil
} }
@ -73,7 +86,35 @@ func (d *Mflac0Decoder) searchKey() error {
if err := d.readRawMetaQTag(); err != nil { if err := d.readRawMetaQTag(); err != nil {
return err return err
} }
} // todo: ... } else {
size := binary.LittleEndian.Uint32(buf)
if size < 0x300 {
return d.readRawKey(int64(size))
} else {
// todo: try to use fixed key
panic("not impl")
}
}
return nil
}
func (d *Mflac0Decoder) readRawKey(rawKeyLen int64) error {
audioLen, err := d.r.Seek(-(4 + rawKeyLen), io.SeekEnd)
if err != nil {
return err
}
d.audioLen = int(audioLen)
rawKeyData, err := io.ReadAll(io.LimitReader(d.r, rawKeyLen))
if err != nil {
return err
}
d.decodedKey, err = DecryptKey(rawKeyData)
if err != nil {
return err
}
return nil return nil
} }
@ -104,13 +145,10 @@ func (d *Mflac0Decoder) readRawMetaQTag() error {
return errors.New("invalid raw meta data") return errors.New("invalid raw meta data")
} }
{
d.decodedKey, err = DecryptKey([]byte(items[0])) d.decodedKey, err = DecryptKey([]byte(items[0]))
if err != nil { if err != nil {
return err return err
} }
}
d.rawMetaExtra1, err = strconv.Atoi(items[1]) d.rawMetaExtra1, err = strconv.Atoi(items[1])
if err != nil { if err != nil {

View File

@ -2,23 +2,24 @@ package qmc
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"os" "os"
"reflect" "reflect"
"testing" "testing"
) )
func loadTestDataRC4Mflac0() ([]byte, []byte, error) { func loadTestDataQmcDecoder(filename string) ([]byte, []byte, error) {
encBody, err := os.ReadFile("./testdata/rc4_raw.bin") encBody, err := os.ReadFile(fmt.Sprintf("./testdata/%s_raw.bin", filename))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
encSuffix, err := os.ReadFile("./testdata/rc4_suffix_mflac0.bin") encSuffix, err := os.ReadFile(fmt.Sprintf("./testdata/%s_suffix.bin", filename))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
target, err := os.ReadFile("./testdata/rc4_target.bin") target, err := os.ReadFile(fmt.Sprintf("./testdata/%s_target.bin", filename))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -26,12 +27,22 @@ func loadTestDataRC4Mflac0() ([]byte, []byte, error) {
} }
func TestMflac0Decoder_Read(t *testing.T) { func TestMflac0Decoder_Read(t *testing.T) {
raw, target, err := loadTestDataRC4Mflac0() tests := []struct {
name string
filename string
wantErr bool
}{
{"mflac0_rc4(512)", "mflac0_rc4", false},
{"mflac_map(256)", "mflac_map", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
raw, target, err := loadTestDataQmcDecoder(tt.filename)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Run("mflac0-file", func(t *testing.T) {
d, err := NewMflac0Decoder(bytes.NewReader(raw)) d, err := NewMflac0Decoder(bytes.NewReader(raw))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -42,7 +53,9 @@ func TestMflac0Decoder_Read(t *testing.T) {
return return
} }
if !reflect.DeepEqual(buf, target) { if !reflect.DeepEqual(buf, target) {
t.Errorf("Process() got = %v, want %v", buf[:32], target[:32]) t.Errorf("Decrypt() got = %v, want %v", buf[:32], target[:32])
} }
}) })
}
} }

View File

@ -1,72 +0,0 @@
package qmc
import (
"os"
"reflect"
"testing"
)
func loadTestData() (*rc4Cipher, []byte, []byte, error) {
key, err := os.ReadFile("./testdata/rc4_key.bin")
if err != nil {
return nil, nil, nil, err
}
raw, err := os.ReadFile("./testdata/rc4_raw.bin")
if err != nil {
return nil, nil, nil, err
}
target, err := os.ReadFile("./testdata/rc4_target.bin")
if err != nil {
return nil, nil, nil, err
}
c, err := NewRC4Cipher(key)
if err != nil {
return nil, nil, nil, err
}
return c, raw, target, nil
}
func Test_rc4Cipher_Process(t *testing.T) {
c, raw, target, err := loadTestData()
if err != nil {
t.Errorf("load testing data failed: %s", err)
}
t.Run("overall", func(t *testing.T) {
c.Process(raw, 0)
if !reflect.DeepEqual(raw, target) {
t.Error("overall")
}
})
}
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
c, raw, target, err := loadTestData()
if err != nil {
t.Errorf("load testing data failed: %s", err)
}
t.Run("first-block(0~128)", func(t *testing.T) {
c.Process(raw[:128], 0)
if !reflect.DeepEqual(raw[:128], target[:128]) {
t.Error("first-block(0~128)")
}
})
}
func Test_rc4Cipher_encASegment(t *testing.T) {
c, raw, target, err := loadTestData()
if err != nil {
t.Errorf("load testing data failed: %s", err)
}
t.Run("align-block(128~5120)", func(t *testing.T) {
c.Process(raw[128:5120], 128)
if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
t.Error("align-block(128~5120)")
}
})
t.Run("simple-block(5120~10240)", func(t *testing.T) {
c.Process(raw[5120:10240], 5120)
if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
t.Error("align-block(128~5120)")
}
})
}

BIN
algo/qmc/testdata/mflac0_rc4_raw.bin vendored Normal file

Binary file not shown.

BIN
algo/qmc/testdata/mflac0_rc4_target.bin vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.