mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-08 07:51:28 +12:00
Qt: Improve PS3 Binaries Decryption tool
This commit is contained in:
parent
b2842dcd52
commit
c0e97b4e96
5 changed files with 152 additions and 81 deletions
|
@ -11,34 +11,66 @@
|
||||||
|
|
||||||
LOG_CHANNEL(dec_log, "DECRYPT");
|
LOG_CHANNEL(dec_log, "DECRYPT");
|
||||||
|
|
||||||
void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std::string(std::string old_path, std::string path, bool tried)> input_cb)
|
usz decrypt_binaries_t::decrypt(std::string klic_input)
|
||||||
{
|
{
|
||||||
if (modules.empty())
|
if (m_index >= m_modules.size())
|
||||||
{
|
{
|
||||||
std::cout << "No paths specified" << std::endl; // For CLI
|
std::cout << "No paths specified" << std::endl; // For CLI
|
||||||
return;
|
m_index = umax;
|
||||||
|
return umax;
|
||||||
}
|
}
|
||||||
|
|
||||||
dec_log.notice("Decrypting binaries...");
|
if (m_klics.empty())
|
||||||
std::cout << "Decrypting binaries..." << std::endl; // For CLI
|
|
||||||
|
|
||||||
// Always start with no KLIC
|
|
||||||
std::vector<u128> klics{u128{}};
|
|
||||||
|
|
||||||
if (const auto keys = g_fxo->try_get<loaded_npdrm_keys>())
|
|
||||||
{
|
{
|
||||||
// Second klic: get it from a running game
|
dec_log.notice("Decrypting binaries...");
|
||||||
if (const u128 klic = keys->last_key())
|
std::cout << "Decrypting binaries..." << std::endl; // For CLI
|
||||||
|
|
||||||
|
// Always start with no KLIC
|
||||||
|
m_klics.emplace_back(u128{});
|
||||||
|
|
||||||
|
if (const auto keys = g_fxo->try_get<loaded_npdrm_keys>())
|
||||||
{
|
{
|
||||||
klics.emplace_back(klic);
|
// Second klic: get it from a running game
|
||||||
|
if (const u128 klic = keys->last_key())
|
||||||
|
{
|
||||||
|
m_klics.emplace_back(klic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to use the key that has been for the current running ELF
|
||||||
|
m_klics.insert(m_klics.end(), Emu.klic.begin(), Emu.klic.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std::string_view text = std::string_view{klic_input}.substr(klic_input.find_first_of('x') + 1); text.size() == 32)
|
||||||
|
{
|
||||||
|
// Allowed to fail (would simply repeat the operation if fails again)
|
||||||
|
u64 lo = 0;
|
||||||
|
u64 hi = 0;
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
|
if (auto res = std::from_chars(text.data() + 0, text.data() + 16, lo, 16); res.ec == std::errc() && res.ptr == text.data() + 16)
|
||||||
|
{
|
||||||
|
if (res = std::from_chars(text.data() + 16, text.data() + 32, hi, 16); res.ec == std::errc() && res.ptr == text.data() + 32)
|
||||||
|
{
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
lo = std::bit_cast<be_t<u64>>(lo);
|
||||||
|
hi = std::bit_cast<be_t<u64>>(hi);
|
||||||
|
|
||||||
|
if (u128 input_key = ((u128{hi} << 64) | lo))
|
||||||
|
{
|
||||||
|
m_klics.emplace_back(input_key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to use the key that has been for the current running ELF
|
while (m_index < m_modules.size())
|
||||||
klics.insert(klics.end(), Emu.klic.begin(), Emu.klic.end());
|
|
||||||
|
|
||||||
for (const std::string& _module : modules)
|
|
||||||
{
|
{
|
||||||
|
const std::string& _module = m_modules[m_index];
|
||||||
const std::string old_path = _module;
|
const std::string old_path = _module;
|
||||||
|
|
||||||
fs::file elf_file;
|
fs::file elf_file;
|
||||||
|
@ -50,7 +82,7 @@ void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std:
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
for (; key_it < klics.size(); key_it++)
|
for (; key_it < m_klics.size(); key_it++)
|
||||||
{
|
{
|
||||||
if (!elf_file.open(old_path) || !elf_file.read(file_magic))
|
if (!elf_file.open(old_path) || !elf_file.read(file_magic))
|
||||||
{
|
{
|
||||||
|
@ -62,7 +94,7 @@ void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std:
|
||||||
case "SCE\0"_u32:
|
case "SCE\0"_u32:
|
||||||
{
|
{
|
||||||
// First KLIC is no KLIC
|
// First KLIC is no KLIC
|
||||||
elf_file = decrypt_self(std::move(elf_file), key_it != 0 ? reinterpret_cast<u8*>(&klics[key_it]) : nullptr);
|
elf_file = decrypt_self(std::move(elf_file), key_it != 0 ? reinterpret_cast<u8*>(&m_klics[key_it]) : nullptr);
|
||||||
|
|
||||||
if (!elf_file)
|
if (!elf_file)
|
||||||
{
|
{
|
||||||
|
@ -75,7 +107,7 @@ void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std:
|
||||||
case "NPD\0"_u32:
|
case "NPD\0"_u32:
|
||||||
{
|
{
|
||||||
// EDAT / SDAT
|
// EDAT / SDAT
|
||||||
elf_file = DecryptEDAT(elf_file, old_path, key_it != 0 ? 8 : 1, reinterpret_cast<u8*>(&klics[key_it]), true);
|
elf_file = DecryptEDAT(elf_file, old_path, key_it != 0 ? 8 : 1, reinterpret_cast<u8*>(&m_klics[key_it]), true);
|
||||||
|
|
||||||
if (!elf_file)
|
if (!elf_file)
|
||||||
{
|
{
|
||||||
|
@ -111,11 +143,13 @@ void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std:
|
||||||
new_file.write(elf_file.to_string());
|
new_file.write(elf_file.to_string());
|
||||||
dec_log.success("Decrypted %s -> %s", old_path, new_path);
|
dec_log.success("Decrypted %s -> %s", old_path, new_path);
|
||||||
std::cout << "Decrypted " << old_path << " -> " << new_path << std::endl; // For CLI
|
std::cout << "Decrypted " << old_path << " -> " << new_path << std::endl; // For CLI
|
||||||
|
m_index++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dec_log.error("Failed to create %s", new_path);
|
dec_log.error("Failed to create %s", new_path);
|
||||||
std::cout << "Failed to create " << new_path << std::endl; // For CLI
|
std::cout << "Failed to create " << new_path << std::endl; // For CLI
|
||||||
|
m_index = umax;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -124,42 +158,16 @@ void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std:
|
||||||
if (!invalid)
|
if (!invalid)
|
||||||
{
|
{
|
||||||
// Allow the user to manually type KLIC if decryption failed
|
// Allow the user to manually type KLIC if decryption failed
|
||||||
|
return m_index;
|
||||||
const std::string filename = old_path.substr(old_path.find_last_of(fs::delim) + 1);
|
|
||||||
const std::string text = input_cb ? input_cb(old_path, filename, tried) : "";
|
|
||||||
|
|
||||||
if (!text.empty())
|
|
||||||
{
|
|
||||||
auto& klic = (tried ? klics.back() : klics.emplace_back());
|
|
||||||
|
|
||||||
ensure(text.size() == 32);
|
|
||||||
|
|
||||||
// It must succeed (only hex characters are present)
|
|
||||||
u64 lo_ = 0;
|
|
||||||
u64 hi_ = 0;
|
|
||||||
std::from_chars(&text[0], &text[16], lo_, 16);
|
|
||||||
std::from_chars(&text[16], &text[32], hi_, 16);
|
|
||||||
|
|
||||||
be_t<u64> lo = std::bit_cast<be_t<u64>>(lo_);
|
|
||||||
be_t<u64> hi = std::bit_cast<be_t<u64>>(hi_);
|
|
||||||
|
|
||||||
klic = (u128{+hi} << 64) | +lo;
|
|
||||||
|
|
||||||
// Retry with specified KLIC
|
|
||||||
key_it -= +std::exchange(tried, true); // Rewind on second and above attempt
|
|
||||||
dec_log.notice("KLIC entered for %s: %s", filename, klic);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
dec_log.notice("User has cancelled entering KLIC.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dec_log.error("Failed to decrypt \"%s\".", old_path);
|
dec_log.error("Failed to decrypt \"%s\".", old_path);
|
||||||
std::cout << "Failed to decrypt \"" << old_path << "\"." << std::endl; // For CLI
|
std::cout << "Failed to decrypt \"" << old_path << "\"." << std::endl; // For CLI
|
||||||
break;
|
return m_index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dec_log.notice("Finished decrypting all binaries.");
|
dec_log.notice("Finished decrypting all binaries.");
|
||||||
std::cout << "Finished decrypting all binaries." << std::endl; // For CLI
|
std::cout << "Finished decrypting all binaries." << std::endl; // For CLI
|
||||||
|
return m_index;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,26 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
void decrypt_sprx_libraries(std::vector<std::string> modules, std::function<std::string(std::string old_path, std::string path, bool tried)> input_cb);
|
class decrypt_binaries_t
|
||||||
|
{
|
||||||
|
std::vector<u128> m_klics;
|
||||||
|
std::vector<std::string> m_modules;
|
||||||
|
usz m_index = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
decrypt_binaries_t(std::vector<std::string> modules) noexcept
|
||||||
|
: m_modules(std::move(modules))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
usz decrypt(std::string klic_input = {});
|
||||||
|
|
||||||
|
bool done() const
|
||||||
|
{
|
||||||
|
return m_index >= m_klics.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& operator[](usz index) const
|
||||||
|
{
|
||||||
|
return ::at32(m_modules, index);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -468,7 +468,7 @@ error_code npDrmIsAvailable(vm::cptr<u8> k_licensee_addr, vm::cptr<char> drm_pat
|
||||||
if (k_licensee_addr)
|
if (k_licensee_addr)
|
||||||
{
|
{
|
||||||
std::memcpy(&k_licensee, k_licensee_addr.get_ptr(), sizeof(k_licensee));
|
std::memcpy(&k_licensee, k_licensee_addr.get_ptr(), sizeof(k_licensee));
|
||||||
sceNp.notice("npDrmIsAvailable(): KLicense key %s", std::bit_cast<be_t<u128>>(k_licensee));
|
sceNp.notice("npDrmIsAvailable(): KLicense key or KLIC=%s", std::bit_cast<be_t<u128>>(k_licensee));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Emu.GetFakeCat() == "PE")
|
if (Emu.GetFakeCat() == "PE")
|
||||||
|
|
|
@ -1103,29 +1103,45 @@ int main(int argc, char** argv)
|
||||||
std::cout << "Not a file: " << mod.toStdString() << std::endl;
|
std::cout << "Not a file: " << mod.toStdString() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec_modules.push_back(fi.absoluteFilePath().toStdString());
|
vec_modules.push_back(fi.absoluteFilePath().toStdString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto input_cb = [](std::string old_path, std::string path, bool tried) -> std::string
|
Emu.Init();
|
||||||
{
|
|
||||||
const std::string hint = fmt::format("Hint: KLIC (KLicense key) is a 16-byte long string. (32 hexadecimal characters)"
|
|
||||||
"\nAnd is logged with some sceNpDrm* functions when the game/application which owns \"%0\" is running.", path);
|
|
||||||
|
|
||||||
if (tried)
|
std::shared_ptr<decrypt_binaries_t> decrypter = std::make_shared<decrypt_binaries_t>(std::move(vec_modules));
|
||||||
|
|
||||||
|
usz mod_index = decrypter->decrypt();
|
||||||
|
usz repeat_count = mod_index == 0 ? 1 : 0;
|
||||||
|
|
||||||
|
while (!decrypter->done())
|
||||||
|
{
|
||||||
|
const std::string& path = (*decrypter)[mod_index];
|
||||||
|
const std::string filename = path.substr(path.find_last_of(fs::delim) + 1);
|
||||||
|
|
||||||
|
const std::string hint = fmt::format("Hint: KLIC (KLicense key) is a 16-byte long string. (32 hexadecimal characters)"
|
||||||
|
"\nAnd is logged with some sceNpDrm* functions when the game/application which owns \"%0\" is running.", filename);
|
||||||
|
|
||||||
|
if (repeat_count >= 2)
|
||||||
{
|
{
|
||||||
std::cout << "Failed to decrypt " << old_path << " with specfied KLIC, retrying.\n" << hint << std::endl;
|
std::cout << "Failed to decrypt " << path << " with specfied KLIC, retrying.\n" << hint << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Enter KLIC of " << path << "\nHexadecimal only, 32 characters:" << std::endl;
|
std::cout << "Enter KLIC of " << filename << "\nHexadecimal only, 32 characters:" << std::endl;
|
||||||
|
|
||||||
std::string input;
|
std::string input;
|
||||||
std::cin >> input;
|
std::cin >> input;
|
||||||
|
|
||||||
return input;
|
if (input.empty())
|
||||||
};
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const usz new_index = decrypter->decrypt(input);
|
||||||
|
repeat_count = new_index == mod_index ? repeat_count + 1 : 0;
|
||||||
|
mod_index = new_index;
|
||||||
|
}
|
||||||
|
|
||||||
Emu.Init();
|
|
||||||
decrypt_sprx_libraries(vec_modules, input_cb);
|
|
||||||
Emu.Quit(true);
|
Emu.Quit(true);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1584,40 +1584,64 @@ void main_window::DecryptSPRXLibraries()
|
||||||
vec_modules.push_back(mod.toStdString());
|
vec_modules.push_back(mod.toStdString());
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto input_cb = [this](std::string old_path, std::string path, bool tried) -> std::string
|
auto iterate = std::make_shared<std::function<void(usz, usz)>>();
|
||||||
{
|
const auto decrypter = std::make_shared<decrypt_binaries_t>(std::move(vec_modules));
|
||||||
const QString hint = tr("Hint: KLIC (KLicense key) is a 16-byte long string. (32 hexadecimal characters)"
|
|
||||||
"\nAnd is logged with some sceNpDrm* functions when the game/application which owns \"%0\" is running.").arg(qstr(path));
|
|
||||||
|
|
||||||
if (tried)
|
*iterate = [this, iterate, decrypter](usz mod_index, usz repeat_count)
|
||||||
|
{
|
||||||
|
const std::string& path = (*decrypter)[mod_index];
|
||||||
|
const std::string filename = path.substr(path.find_last_of(fs::delim) + 1);
|
||||||
|
|
||||||
|
const QString hint = tr("Hint: KLIC (KLicense key) is a 16-byte long string. (32 hexadecimal characters, can be prefixed with \"KLIC=0x\" from the log message)"
|
||||||
|
"\nAnd is logged with some sceNpDrm* functions when the game/application which owns \"%0\" is running.").arg(qstr(filename));
|
||||||
|
|
||||||
|
if (repeat_count >= 2)
|
||||||
{
|
{
|
||||||
gui_log.error("Failed to decrypt %s with specfied KLIC, retrying.\n%s", old_path, sstr(hint));
|
gui_log.error("Failed to decrypt %s with specified KLIC, retrying.\n%s", path, sstr(hint));
|
||||||
}
|
}
|
||||||
|
|
||||||
input_dialog dlg(32, "", tr("Enter KLIC of %0").arg(qstr(path)),
|
input_dialog* dlg = new input_dialog(39, "", tr("Enter KLIC of %0").arg(qstr(filename)),
|
||||||
tried ? tr("Decryption failed with provided KLIC.\n%0").arg(hint) : tr("Hexadecimal only."), "00000000000000000000000000000000", this);
|
repeat_count >= 2 ? tr("Decryption failed with provided KLIC.\n%0").arg(hint) : tr("Hexadecimal value."), "KLIC=0x00000000000000000000000000000000", this);
|
||||||
|
|
||||||
QFont mono = QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
QFont mono = QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
||||||
mono.setPointSize(8);
|
mono.setPointSize(8);
|
||||||
dlg.set_input_font(mono, true, '0');
|
dlg->set_input_font(mono, true, '0');
|
||||||
dlg.set_clear_button_enabled(false);
|
dlg->set_clear_button_enabled(false);
|
||||||
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, false);
|
dlg->set_button_enabled(QDialogButtonBox::StandardButton::Ok, false);
|
||||||
dlg.set_validator(new QRegularExpressionValidator(QRegularExpression("^[a-fA-F0-9]*$"))); // HEX only
|
dlg->set_validator(new QRegularExpressionValidator(QRegularExpression("^((((((K?L)?I)?C)?=)?0)?x)?[a-fA-F0-9]{0,32}$"))); // HEX only (with additional KLIC=0x prefix for convenience)
|
||||||
|
dlg->setAttribute(Qt::WA_DeleteOnClose);
|
||||||
|
|
||||||
connect(&dlg, &input_dialog::text_changed, &dlg, [&dlg](const QString& text)
|
connect(dlg, &input_dialog::text_changed, dlg, [dlg](const QString& text)
|
||||||
{
|
{
|
||||||
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, text.size() == 32);
|
dlg->set_button_enabled(QDialogButtonBox::StandardButton::Ok, text.size() - (text.indexOf('x') + 1) == 32);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (dlg.exec() == QDialog::Accepted)
|
connect(dlg, &QDialog::accepted, this, [this, iterate, dlg, mod_index, decrypter, repeat_count]()
|
||||||
{
|
{
|
||||||
return sstr(dlg.get_input_text());
|
std::string text = sstr(dlg->get_input_text());
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
if (usz new_index = decrypter->decrypt(std::move(text)); !decrypter->done())
|
||||||
|
{
|
||||||
|
QTimer::singleShot(0, [iterate, mod_index, repeat_count, new_index]()
|
||||||
|
{
|
||||||
|
// Increase repeat count if "stuck" on the same file
|
||||||
|
(*iterate)(new_index, new_index == mod_index ? repeat_count + 1 : 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(dlg, &QDialog::rejected, this, []()
|
||||||
|
{
|
||||||
|
gui_log.notice("User has cancelled entering KLIC.");
|
||||||
|
});
|
||||||
|
|
||||||
|
dlg->show();
|
||||||
};
|
};
|
||||||
|
|
||||||
decrypt_sprx_libraries(vec_modules, input_cb);
|
if (usz new_index = decrypter->decrypt(); !decrypter->done())
|
||||||
|
{
|
||||||
|
(*iterate)(new_index, new_index == 0 ? 1 : 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Needed so that when a backup occurs of window state in gui_settings, the state is current.
|
/** Needed so that when a backup occurs of window state in gui_settings, the state is current.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue