Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Unpack SteamStub 2.1 builds that push between the field loads (#35)

tslater200649590ebb407d
diff --git a/src/runtime/steam_stub.cpp b/src/runtime/steam_stub.cpp
index dd1eade..54b1b63 100644
--- a/src/runtime/steam_stub.cpp
+++ b/src/runtime/steam_stub.cpp
@@ -262,6 +262,34 @@ std::optional<std::size_t> FindPattern(const std::vector<unsigned char>& data, c
return std::nullopt;
}
+// After the fifth field load, SteamDRMP.dll stores it, then takes the key with "lea reg,
+// [payload+key]" and the IV with "add eax, iv". Some builds (Final Fantasy XIII) push registers
+// and load a global in between, so the two are found by stepping over what sits there rather
+// than at fixed positions. False when the tail holds something this does not step over.
+bool TailFields21(const unsigned char* tail, std::size_t size, std::int32_t* key_at, std::int32_t* iv_at) {
+ bool key = false, iv = false;
+ for (std::size_t at = 0; at + 6 <= size && !(key && iv);) {
+ const unsigned char op = tail[at];
+ if (op >= 0x50 && op <= 0x5F) { at += 1; continue; } // push/pop reg
+ if (op == 0x31 || op == 0x33 || op == 0x8A) { at += 2; continue; } // xor reg, reg; mov r8, r/m8 (register form)
+ if (op == 0xA3 || op == 0xA1) { at += 5; continue; } // mov [abs], eax; mov eax, [abs]
+ if (op == 0x05) { // add eax, imm32
+ if (key && !iv) { *iv_at = static_cast<std::int32_t>(U32(tail + at + 1)); iv = true; }
+ at += 5;
+ continue;
+ }
+ if (op == 0x89 || op == 0x8B || op == 0x8D) {
+ const unsigned char modrm = tail[at + 1];
+ const bool disp32 = (modrm & 0xC0) == 0x80, absolute = (modrm & 0xC7) == 0x05;
+ if (op == 0x8D && !key && disp32) { *key_at = static_cast<std::int32_t>(U32(tail + at + 2)); key = true; }
+ at += disp32 || absolute ? 6 : (modrm & 0xC0) == 0x40 ? 3 : 2;
+ continue;
+ }
+ break;
+ }
+ return key && iv;
+}
+
SteamStubInfo Inspect21(const Reader& read, const Layout& layout, const Row& bind, Wrapper* out) {
SteamStubInfo info;
info.kind = SteamStubKind::kUnsupported;
@@ -325,8 +353,9 @@ SteamStubInfo Inspect21(const Reader& read, const Layout& layout, const Row& bin
const std::int32_t entry_at = static_cast<std::int32_t>(U32(block + (fallback ? 25 : 26)));
const std::int32_t code_va_at = static_cast<std::int32_t>(U32(block + (fallback ? 36 : 38)));
const std::int32_t code_size_at = static_cast<std::int32_t>(U32(block + (fallback ? 47 : 50)));
- const std::int32_t key_at = static_cast<std::int32_t>(U32(block + (fallback ? 61 : 62)));
- const std::int32_t iv_at = static_cast<std::int32_t>(U32(block + (fallback ? 72 : 67)));
+ std::int32_t key_at = static_cast<std::int32_t>(U32(block + (fallback ? 61 : 62)));
+ std::int32_t iv_at = static_cast<std::int32_t>(U32(block + (fallback ? 72 : 67)));
+ if (!fallback && !TailFields21(block + 54, 64, &key_at, &iv_at)) return refuse("SteamDRMP.dll tail unknown");
const auto in_range = [&](std::int32_t at, int size) { return at >= 0 && in_payload(static_cast<std::uint64_t>(at), size); };
if (!in_range(flags_at, 4) || !in_range(entry_at, 4)) return refuse("payload fields out of range");
const std::uint32_t flags = U32(payload.data() + flags_at);
diff --git a/tests/runtime_steam_stub_test.cpp b/tests/runtime_steam_stub_test.cpp
index e224baa..fb1136f 100644
--- a/tests/runtime_steam_stub_test.cpp
+++ b/tests/runtime_steam_stub_test.cpp
@@ -216,6 +216,19 @@ int main() {
CHECK(fx::Get32(bytes, fx::kOptional + 16) == fx::kOriginalEntry);
CHECK(Sections(bytes) == 2 && bytes.size() == fx::kBindRaw);
}
+ { // 2.1 whose SteamDRMP.dll pushes registers between the field loads and the key (Final Fantasy XIII).
+ fx::Spec21 spec;
+ spec.pushes_in_tail = true;
+ auto bytes = fx::Build21(spec);
+ const auto root = base / "v21-ff13";
+ Write(root / "ffxiiiimg.exe", bytes);
+ const auto info = InspectSteamStub((root / "ffxiiiimg.exe").string());
+ CHECK(info.kind == SteamStubKind::kSupported && info.detail == "2.1 x86");
+ std::string why;
+ CHECK(UnpackSteamStub(&bytes, &why));
+ CHECK(bytes.substr(fx::kCodeRaw, fx::kCodeSize) == fx::PlainCode());
+ CHECK(fx::Get32(bytes, fx::kOptional + 16) == fx::kOriginalEntry);
+ }
{ // 2.1 without encryption (Call of Juarez Gunslinger): only the entry point comes back.
fx::Spec21 spec;
spec.flags = 0x26;
diff --git a/tests/steam_stub_fixture.h b/tests/steam_stub_fixture.h
index e880ef3..235d9b5 100644
--- a/tests/steam_stub_fixture.h
+++ b/tests/steam_stub_fixture.h
@@ -174,6 +174,10 @@ struct Spec21 {
std::uint32_t flags = 0x20;
std::uint32_t app_id = 208670;
bool d0_header = false; // the 0xD0-dword header, without LoadLibraryW
+ // SteamDRMP.dll builds differ after the fifth field load: Blades of Time stores then takes
+ // the key and IV straight away; Final Fantasy XIII pushes registers and loads a global in
+ // between, which moves the lea and the add.
+ bool pushes_in_tail = false;
};
constexpr std::size_t kBind21Size = 0x1000;
@@ -246,19 +250,32 @@ inline std::string Build21(const Spec21& spec = {}) {
// the payload offsets from the operands.
std::string drmp(drmp_size, '\0');
constexpr std::size_t block = 0x40;
+ // mov edx, [eax+disp32] (8B 90) and mov [abs32], edx (89 15), as the real builds encode them.
for (int i = 0; i < 5; ++i) {
drmp[block + 12 * i] = '\x8B';
+ drmp[block + 12 * i + 1] = '\x90';
drmp[block + 12 * i + 6] = '\x89';
+ drmp[block + 12 * i + 7] = '\x15';
}
- drmp[block + 60] = '\x8D';
- drmp[block + 66] = '\x05';
Put(&drmp, block + 2, 0x00, 4); // flags
Put(&drmp, block + 14, 0xF0, 4); // app id (unused)
Put(&drmp, block + 26, 0x04, 4); // original entry point
Put(&drmp, block + 38, 0x08, 4); // code section address
Put(&drmp, block + 50, 0x0C, 4); // encrypted size
- Put(&drmp, block + 62, 0x10, 4); // AES key
- Put(&drmp, block + 67, 0x30, 4); // AES IV, then the code's first block
+ if (!spec.pushes_in_tail) {
+ drmp[block + 60] = '\x8D'; drmp[block + 61] = '\x90'; // lea edx, [eax+key]
+ Put(&drmp, block + 62, 0x10, 4); // AES key
+ drmp[block + 66] = '\x05'; // add eax, iv
+ Put(&drmp, block + 67, 0x30, 4); // AES IV, then the code's first block
+ } else {
+ // ... 8B 90 <size> | 53 | 89 15 <abs> | 8D 90 <key> | 56 | 8B 35 <abs> | 05 <iv> ...
+ drmp[block + 54] = '\x53'; // push ebx
+ drmp[block + 55] = '\x89'; drmp[block + 56] = '\x15'; // mov [abs], edx
+ drmp[block + 61] = '\x8D'; drmp[block + 62] = '\x90'; Put(&drmp, block + 63, 0x10, 4); // lea edx, [eax+key]
+ drmp[block + 67] = '\x56'; // push esi
+ drmp[block + 68] = '\x8B'; drmp[block + 69] = '\x35'; Put(&drmp, block + 70, 0x1005c018u, 4); // mov esi, [abs]
+ drmp[block + 74] = '\x05'; Put(&drmp, block + 75, 0x30, 4); // add eax, iv
+ }
std::uint32_t keys[4];
for (int i = 0; i < 4; ++i) keys[i] = Get32(payload, 0x60 + 4 * i);
std::uint32_t chain1 = 0x55555555, chain2 = 0x55555555;