mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-04 14:08:37 +00:00
CLI: add decrypt option
This commit is contained in:
parent
2f9b930c6b
commit
5ae9de4e3b
7 changed files with 278 additions and 162 deletions
|
|
@ -48,7 +48,7 @@
|
|||
|
||||
#include "Crypto/unpkg.h"
|
||||
#include "Crypto/unself.h"
|
||||
#include "Crypto/unedat.h"
|
||||
#include "Crypto/decrypt_binaries.h"
|
||||
|
||||
#include "Loader/PUP.h"
|
||||
#include "Loader/TAR.h"
|
||||
|
|
@ -1298,169 +1298,46 @@ void main_window::DecryptSPRXLibraries()
|
|||
|
||||
m_gui_settings->SetValue(gui::fd_decrypt_sprx, QFileInfo(modules.first()).path());
|
||||
|
||||
gui_log.notice("Decrypting binaries...");
|
||||
|
||||
// Always start with no KLIC
|
||||
std::vector<u128> klics{u128{}};
|
||||
|
||||
if (const auto keys = g_fxo->try_get<loaded_npdrm_keys>())
|
||||
std::vector<std::string> vec_modules;
|
||||
for (const QString& mod : modules)
|
||||
{
|
||||
// Second klic: get it from a running game
|
||||
if (const u128 klic = keys->last_key())
|
||||
{
|
||||
klics.emplace_back(klic);
|
||||
}
|
||||
vec_modules.push_back(mod.toStdString());
|
||||
}
|
||||
|
||||
// Try to use the key that has been for the current running ELF
|
||||
klics.insert(klics.end(), Emu.klic.begin(), Emu.klic.end());
|
||||
|
||||
for (const QString& _module : modules)
|
||||
const auto input_cb = [this](std::string old_path, std::string path, bool tried) -> std::string
|
||||
{
|
||||
const std::string old_path = sstr(_module);
|
||||
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));
|
||||
|
||||
fs::file elf_file;
|
||||
|
||||
bool tried = false;
|
||||
bool invalid = false;
|
||||
usz key_it = 0;
|
||||
u32 file_magic{};
|
||||
|
||||
while (true)
|
||||
if (tried)
|
||||
{
|
||||
for (; key_it < klics.size(); key_it++)
|
||||
{
|
||||
if (!elf_file.open(old_path) || !elf_file.read(file_magic))
|
||||
{
|
||||
file_magic = 0;
|
||||
}
|
||||
|
||||
switch (file_magic)
|
||||
{
|
||||
case "SCE\0"_u32:
|
||||
{
|
||||
// First KLIC is no KLIC
|
||||
elf_file = decrypt_self(std::move(elf_file), key_it != 0 ? reinterpret_cast<u8*>(&klics[key_it]) : nullptr);
|
||||
|
||||
if (!elf_file)
|
||||
{
|
||||
// Try another key
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "NPD\0"_u32:
|
||||
{
|
||||
// EDAT / SDAT
|
||||
elf_file = DecryptEDAT(elf_file, old_path, key_it != 0 ? 8 : 1, reinterpret_cast<u8*>(&klics[key_it]), true);
|
||||
|
||||
if (!elf_file)
|
||||
{
|
||||
// Try another key
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
invalid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (invalid)
|
||||
{
|
||||
elf_file.close();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (elf_file)
|
||||
{
|
||||
const std::string exec_ext = _module.toLower().endsWith(".sprx") ? ".prx" : ".elf";
|
||||
const std::string new_path = file_magic == "NPD\0"_u32 ? old_path + ".unedat" :
|
||||
old_path.substr(0, old_path.find_last_of('.')) + exec_ext;
|
||||
|
||||
if (fs::file new_file{new_path, fs::rewrite})
|
||||
{
|
||||
new_file.write(elf_file.to_string());
|
||||
gui_log.success("Decrypted %s -> %s", old_path, new_path);
|
||||
}
|
||||
else
|
||||
{
|
||||
gui_log.error("Failed to create %s", new_path);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else if (!invalid)
|
||||
{
|
||||
// Allow the user to manually type KLIC if decryption failed
|
||||
|
||||
const std::string filename = old_path.substr(old_path.find_last_of(fs::delim) + 1);
|
||||
|
||||
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(filename));
|
||||
|
||||
if (tried)
|
||||
{
|
||||
gui_log.error("Failed to decrypt %s with specfied KLIC, retrying.\n%s", old_path, sstr(hint));
|
||||
}
|
||||
|
||||
input_dialog dlg(32, "", tr("Enter KLIC of %0").arg(qstr(filename)),
|
||||
tried ? tr("Decryption failed with provided KLIC.\n%0").arg(hint) : tr("Hexadecimal only."), "00000000000000000000000000000000", this);
|
||||
|
||||
QFont mono = QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
||||
mono.setPointSize(8);
|
||||
dlg.set_input_font(mono, true, '0');
|
||||
dlg.set_clear_button_enabled(false);
|
||||
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, false);
|
||||
dlg.set_validator(new QRegularExpressionValidator(QRegularExpression("^[a-fA-F0-9]*$"))); // HEX only
|
||||
|
||||
connect(&dlg, &input_dialog::text_changed, &dlg, [&dlg](const QString& text)
|
||||
{
|
||||
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, text.size() == 32);
|
||||
});
|
||||
|
||||
if (dlg.exec() == QDialog::Accepted)
|
||||
{
|
||||
const std::string text = sstr(dlg.get_input_text());
|
||||
|
||||
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
|
||||
gui_log.notice("KLIC entered for %s: %s", filename, klic);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
gui_log.notice("User has cancelled entering KLIC.");
|
||||
}
|
||||
}
|
||||
|
||||
gui_log.error("Failed to decrypt \"%s\".", old_path);
|
||||
break;
|
||||
gui_log.error("Failed to decrypt %s with specfied KLIC, retrying.\n%s", old_path, sstr(hint));
|
||||
}
|
||||
}
|
||||
|
||||
gui_log.notice("Finished decrypting all binaries.");
|
||||
input_dialog dlg(32, "", tr("Enter KLIC of %0").arg(qstr(path)),
|
||||
tried ? tr("Decryption failed with provided KLIC.\n%0").arg(hint) : tr("Hexadecimal only."), "00000000000000000000000000000000", this);
|
||||
|
||||
QFont mono = QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
||||
mono.setPointSize(8);
|
||||
dlg.set_input_font(mono, true, '0');
|
||||
dlg.set_clear_button_enabled(false);
|
||||
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, false);
|
||||
dlg.set_validator(new QRegularExpressionValidator(QRegularExpression("^[a-fA-F0-9]*$"))); // HEX only
|
||||
|
||||
connect(&dlg, &input_dialog::text_changed, &dlg, [&dlg](const QString& text)
|
||||
{
|
||||
dlg.set_button_enabled(QDialogButtonBox::StandardButton::Ok, text.size() == 32);
|
||||
});
|
||||
|
||||
if (dlg.exec() == QDialog::Accepted)
|
||||
{
|
||||
return sstr(dlg.get_input_text());
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
decrypt_sprx_libraries(vec_modules, input_cb);
|
||||
}
|
||||
|
||||
/** 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