2024-10-17 00:50:14 +00:00
|
|
|
// clang-format off
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <initguid.h>
|
|
|
|
#include <knownfolders.h>
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
#include <span>
|
|
|
|
|
|
|
|
#include "infra/infra.h"
|
|
|
|
#include "infra/sqlite_error.h"
|
|
|
|
#include "qmc2/qmc2.h"
|
|
|
|
|
|
|
|
using Infra::kgm_ekey_db_t;
|
|
|
|
|
|
|
|
constexpr std::array<uint8_t, 16> kMagicHeader{0x7C, 0xD5, 0x32, 0xEB, 0x86, 0x02, 0x7F, 0x4B,
|
|
|
|
0xA8, 0xAF, 0xA6, 0x8E, 0x0F, 0xFF, 0x99, 0x14};
|
|
|
|
|
|
|
|
void DecryptKGG(const kgm_ekey_db_t& ekey_db, const std::filesystem::path& kgg_file) {
|
|
|
|
std::ifstream ifs_kgg(kgg_file, std::ios::binary);
|
|
|
|
char header[0x100]{};
|
|
|
|
ifs_kgg.read(header, sizeof(ifs_kgg));
|
|
|
|
if (std::equal(kMagicHeader.cbegin(), kMagicHeader.cend(), header)) {
|
2024-10-17 20:35:17 +00:00
|
|
|
fprintf(stderr, "[WARN] ! invalid kgg header, skip.\n");
|
2024-10-17 00:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t offset_to_audio = *reinterpret_cast<uint32_t*>(&header[0x10]);
|
|
|
|
uint32_t encrypt_mode = *reinterpret_cast<uint32_t*>(&header[0x14]);
|
|
|
|
if (encrypt_mode != 5) {
|
2024-10-17 20:35:17 +00:00
|
|
|
fprintf(stderr, "[WARN] ! invalid enc_version (expect=0x05, got 0x%02x), skip.\n", encrypt_mode);
|
2024-10-17 00:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t audio_hash_len = *reinterpret_cast<uint32_t*>(&header[0x44]);
|
|
|
|
if (audio_hash_len != 0x20) {
|
2024-10-17 20:35:17 +00:00
|
|
|
fprintf(stderr, "[WARN] ! audio hash length invalid (expect=0x20, got 0x%02x), skip.\n", audio_hash_len);
|
2024-10-17 00:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string audio_hash(&header[0x48], &header[0x48 + audio_hash_len]);
|
|
|
|
std::string ekey{};
|
|
|
|
if (auto it = ekey_db.find(audio_hash); it != ekey_db.end()) {
|
|
|
|
ekey = it->second;
|
|
|
|
} else {
|
2024-10-17 20:35:17 +00:00
|
|
|
fprintf(stderr, "[WARN] ! ekey not found, skip.\n");
|
2024-10-17 00:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto qmc2 = QMC2::Create(ekey);
|
|
|
|
if (!qmc2) {
|
2024-10-17 20:35:17 +00:00
|
|
|
fprintf(stderr, "[WARN] ! create qmc2 failed, skip.\n");
|
2024-10-17 00:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifs_kgg.seekg(offset_to_audio, std::ios::beg);
|
|
|
|
std::vector<uint8_t> buffer(1024 * 1024);
|
2024-10-17 20:35:17 +00:00
|
|
|
ifs_kgg.read(reinterpret_cast<char*>(buffer.data()), 4);
|
2024-10-17 00:50:14 +00:00
|
|
|
auto n = static_cast<size_t>(ifs_kgg.gcount());
|
|
|
|
qmc2->Decrypt(std::span(buffer.begin(), n), 0);
|
|
|
|
|
|
|
|
auto decrypted_path = kgg_file;
|
|
|
|
auto magic = std::span(buffer.cbegin(), 4);
|
|
|
|
if (std::equal(magic.begin(), magic.end(), "fLaC")) {
|
|
|
|
decrypted_path.replace_filename(decrypted_path.stem().wstring() + L"_kgg-dec.flac");
|
|
|
|
} else if (std::equal(magic.begin(), magic.end(), "OggS")) {
|
|
|
|
decrypted_path.replace_filename(decrypted_path.stem().wstring() + L"_kgg-dec.ogg");
|
|
|
|
} else {
|
|
|
|
decrypted_path.replace_filename(decrypted_path.stem().wstring() + L"_kgg-dec.mp3");
|
|
|
|
}
|
|
|
|
if (exists(decrypted_path)) {
|
2024-10-17 20:35:17 +00:00
|
|
|
fprintf(stderr, "[WARN] ! output file '%s' exists, skip.\n", decrypted_path.filename().string().c_str());
|
2024-10-17 00:50:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream ofs_decrypted(decrypted_path, std::ios::binary);
|
|
|
|
ofs_decrypted.write(reinterpret_cast<char*>(buffer.data()), n);
|
|
|
|
size_t offset{n};
|
|
|
|
while (!ifs_kgg.eof()) {
|
|
|
|
ifs_kgg.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
|
|
|
|
n = static_cast<size_t>(ifs_kgg.gcount());
|
|
|
|
qmc2->Decrypt(std::span(buffer.begin(), n), offset);
|
|
|
|
ofs_decrypted.write(reinterpret_cast<char*>(buffer.data()), n);
|
|
|
|
offset += n;
|
|
|
|
}
|
2024-10-17 20:35:17 +00:00
|
|
|
qmc2.reset();
|
2024-10-17 00:50:14 +00:00
|
|
|
|
2024-10-17 20:35:17 +00:00
|
|
|
auto kgg_fname = kgg_file.filename();
|
|
|
|
auto decrypted_fname = decrypted_path.filename();
|
|
|
|
fwprintf(stderr, L"[INFO] * OK! %s --> %s\n", kgg_fname.c_str(), decrypted_fname.c_str());
|
2024-10-17 00:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WalkFileOrDir(const kgm_ekey_db_t& ekey_db, const std::filesystem::path& input_path) {
|
|
|
|
if (is_directory(input_path)) {
|
|
|
|
for (auto const& dir_entry : std::filesystem::directory_iterator{input_path}) {
|
|
|
|
DecryptKGG(ekey_db, dir_entry.path());
|
|
|
|
}
|
|
|
|
} else if (is_regular_file(input_path)) {
|
|
|
|
DecryptKGG(ekey_db, input_path);
|
2024-10-17 20:35:17 +00:00
|
|
|
} else {
|
|
|
|
fwprintf(stderr, L"[WARN] invalid path: %s\n", input_path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::vector<std::wstring>, std::unordered_map<std::wstring, std::wstring>> ParseCommandLine() {
|
|
|
|
int argc;
|
|
|
|
wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
|
|
|
|
|
|
|
std::vector<std::wstring> positional_args{};
|
|
|
|
std::unordered_map<std::wstring, std::wstring> named_args{};
|
|
|
|
|
|
|
|
bool positional_only{false};
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
std::wstring arg{argv[i]};
|
|
|
|
if (arg == L"--") {
|
|
|
|
positional_only = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!positional_only && arg.starts_with(L"--")) {
|
|
|
|
auto pos = arg.find(L'=');
|
|
|
|
if (pos != std::wstring::npos) {
|
|
|
|
named_args[arg.substr(2, pos - 2)] = arg.substr(pos + 1);
|
|
|
|
} else if (++i < argc) {
|
|
|
|
named_args[arg.substr(2)] = argv[i];
|
|
|
|
} else {
|
|
|
|
named_args[arg.substr(2)] = L"";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
positional_args.push_back(arg);
|
|
|
|
}
|
2024-10-17 00:50:14 +00:00
|
|
|
}
|
2024-10-17 20:35:17 +00:00
|
|
|
|
|
|
|
return std::make_pair(positional_args, named_args);
|
2024-10-17 00:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
|
|
setlocale(LC_ALL, ".UTF8");
|
|
|
|
|
|
|
|
fputs("kgg-dec v0.1 by LSR\n", stderr);
|
2024-10-17 20:35:17 +00:00
|
|
|
fputs(
|
|
|
|
"Usage: kgg-dec "
|
|
|
|
"[--infra-dll infra.dll] "
|
|
|
|
"[--db /path/to/KGMusicV3.db] "
|
|
|
|
"[--] [kgg-dir... = '.']\n\n",
|
|
|
|
stderr);
|
|
|
|
|
|
|
|
auto [positional_args, named_args] = ParseCommandLine();
|
|
|
|
if (positional_args.empty()) {
|
|
|
|
positional_args.emplace_back(L".");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::filesystem::path kgm_db_path{};
|
|
|
|
std::filesystem::path infra_dll_path{L"infra.dll"};
|
|
|
|
if (auto it = named_args.find(L"infra-dll"); it != named_args.end()) {
|
|
|
|
infra_dll_path = std::filesystem::path{it->second};
|
|
|
|
}
|
|
|
|
infra_dll_path = absolute(infra_dll_path);
|
|
|
|
if (!exists(infra_dll_path)) {
|
|
|
|
fputs("[ERR ] infra.dll not found\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto it = named_args.find(L"db"); it != named_args.end()) {
|
|
|
|
kgm_db_path = std::filesystem::path{it->second};
|
|
|
|
} else {
|
|
|
|
PWSTR pAppDirPath{};
|
|
|
|
SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &pAppDirPath);
|
|
|
|
kgm_db_path = std::filesystem::path{pAppDirPath} / L"Kugou8" / L"KGMusicV3.db";
|
|
|
|
CoTaskMemFree(pAppDirPath);
|
|
|
|
}
|
|
|
|
if (!exists(kgm_db_path)) {
|
|
|
|
fputs("[ERR ] KGMusicV3.db not found\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2024-10-17 00:50:14 +00:00
|
|
|
|
|
|
|
int error{-1};
|
2024-10-17 20:35:17 +00:00
|
|
|
Infra::KugouDb db{infra_dll_path, kgm_db_path};
|
2024-10-17 20:17:10 +00:00
|
|
|
if (!db.IsOpen()) {
|
2024-10-17 00:50:14 +00:00
|
|
|
fprintf(stderr, "[ERR ] db init error: is infra.dll ok?\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto ekey_db = db.dump_ekey(error);
|
|
|
|
if (error != 0) {
|
|
|
|
fprintf(stderr, "[ERR ] dump ekey failed %d (%s)", error, sqlite_get_error(error));
|
|
|
|
return 1;
|
|
|
|
}
|
2024-10-17 20:17:10 +00:00
|
|
|
db.Close();
|
2024-10-17 00:50:14 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("ekey_db:\n");
|
|
|
|
for (auto& [a, b] : ekey_db) {
|
|
|
|
printf("%s --> %s\n", a.c_str(), b.c_str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-10-17 20:35:17 +00:00
|
|
|
for (auto& positional_arg : positional_args) {
|
|
|
|
WalkFileOrDir(ekey_db, positional_arg);
|
|
|
|
}
|
2024-10-17 00:50:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|