Skip to content

Instantly share code, notes, and snippets.

@x4fx77x4f
Last active September 28, 2024 17:06
Show Gist options
  • Save x4fx77x4f/db8a898dc328bbbfb945a56244233089 to your computer and use it in GitHub Desktop.
Save x4fx77x4f/db8a898dc328bbbfb945a56244233089 to your computer and use it in GitHub Desktop.
Decrypt Godot Engine GDEC files and Looking Up I See Only A Ceiling save files and The Relapse assets
#!/usr/bin/env luajit
local argparse = require("argparse")()
argparse:argument("input_path")
argparse:argument("output_path", nil, "-")
argparse:mutex(
argparse:option("--password"),
argparse:option("--key")
)
local arguments = argparse:parse()
local lib_gdec = require("lib_gdec")
local input_path = arguments.input_path
local input_file = io.stdin
if input_path ~= "-" then
input_file = assert(io.open(input_path, "rb"))
end
local output_path = arguments.output_path
local output_file = io.stdout
if output_path ~= "-" then
output_file = assert(io.open(output_path, "w+b"))
end
local key = arguments.key or lib_gdec.get_key_from_password(assert(arguments.password))
local data = lib_gdec.read_gdec(input_file, key)
input_file:close()
assert(output_file:write(data))
output_file:close()
#!/usr/bin/env luajit
local argparse = require("argparse")()
argparse:argument("input_path")
argparse:argument("output_path", nil, "-")
argparse:mutex(
argparse:option("--filename"),
argparse:option("--password"),
argparse:option("--key")
)
argparse:mutex(
argparse:flag("--is-save"),
argparse:flag("--is-not-save")
)
local arguments = argparse:parse()
local basexx = require("basexx")
local lib_gdec = require("lib_gdec")
local input_path = arguments.input_path
local input_file = io.stdin
if input_path ~= "-" then
input_file = assert(io.open(input_path, "rb"))
end
local output_path = arguments.output_path
local output_file = io.stdout
if output_path ~= "-" then
output_file = assert(io.open(output_path, "w+b"))
end
-- luisoac.pck//autoload/SilverUtilities.gd
local filename = arguments.filename or input_path
local password = arguments.password
local key = arguments.key
local is_save = arguments.is_save or (not arguments.is_not_save and string.find(filename, "%.sav$") ~= nil)
if key == nil then
if password == nil then
if is_save then
password = "oF3dWeLx-Slw5_Za!os9dCnz"
else
password = assert(string.match(input_path, "[^/]+$"))
password = string.reverse(password)
password = string.sub(password, 1, 3).."cNa250PXlaXPZw28Ccs8b2m5P10"..string.sub(password, 4)
end
end
key = lib_gdec.get_key_from_password(password)
end
local data = lib_gdec.read_gdec(input_file, key)
if not is_save then
data = assert(basexx.from_base64(data))
end
input_file:close()
assert(output_file:write(data))
output_file:close()
#pragma endian little
#include <std/sys.pat>
struct gdec_t {
char magic[4];
std::assert(magic == "GDEC", "bad magic");
u32 aes_mode;
std::assert(aes_mode < 4, "bad AES mode");
std::assert(aes_mode != 0, "bad AES mode");
std::assert_warn(aes_mode == 1, "bad AES mode");
char expected_decrypted_data_md5[16];
u64 encrypted_data_length;
} [[inline]];
gdec_t gdec @ 0;
local bit = require("bit")
local luagcrypt = require("luagcrypt")
local function read(file, data_length)
local data = assert(file:read(data_length))
assert(#data == data_length)
return data
end
local function read_u32_le(stream)
local byte_4, byte_3, byte_2, byte_1 = string.byte(read(stream, 4), 1, 4)
local number = bit.bor(
bit.lshift(byte_1, 8*3),
bit.lshift(byte_2, 8*2),
bit.lshift(byte_3, 8*1),
byte_4
)
if number < 0 then
number = number+2^32
end
return number
end
local function read_u64_le(stream)
local byte_8, byte_7, byte_6, byte_5, byte_4, byte_3, byte_2, byte_1 = string.byte(read(stream, 8), 1, 8)
return bit.bor(
bit.lshift(byte_1+0ull, 8*7),
bit.lshift(byte_2+0ull, 8*6),
bit.lshift(byte_3+0ull, 8*5),
bit.lshift(byte_4+0ull, 8*4),
bit.lshift(byte_5+0ull, 8*3),
bit.lshift(byte_6+0ull, 8*2),
bit.lshift(byte_7+0ull, 8*1),
byte_8+0ull
)
end
local function get_length(stream)
local offset = assert(stream:seek())
local length = assert(stream:seek("end"))
assert(stream:seek("set", offset))
return length
end
local function get_is_eof(stream)
local offset = assert(stream:seek())
local length = assert(stream:seek("end"))
assert(stream:seek("set", offset))
return offset == length
end
local function get_as_hex(data)
return (string.gsub(data, ".", function(byte)
return bit.tohex(string.byte(byte), 2)
end))
end
local function printf(...)
assert(io.stderr:write(string.format(...)))
end
local function get_md5(data)
local md = luagcrypt.Hash(luagcrypt.MD_MD5)
md:write(data)
local md5 = md:read(luagcrypt.MD_MD5)
return md5
end
-- This portion of this file is derived from Godot 3.5.3-stable.official.6c814135b.
--[[ Original copyright notices and license:
Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md).
Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
-- This portion of this file is derived from <https://github.com/piman51277/Godot-GDEC>.
-- Original copyright notice was "Copyright (c) 2024 piman51277". License is same as above.
-- core/crypto/aes_context.h
local aes_modes = { -- AESContext::Mode
"ecb_encrypt",
"ecb_decrypt",
"cbc_encrypt",
"cbc_decrypt",
}
-- core/io/file_access_encrypted.cpp
local block_length = 16
local function read_gdec(stream, key) -- FileAccessEncrypted::open_and_parse
local magic = read(stream, 4)
assert(magic == "GDEC")
local aes_mode = read_u32_le(stream)
assert(aes_mode < #aes_modes)
assert(aes_mode ~= 0)
assert(aes_mode == 1) -- This is the only mode I support, and also the only one used under normal circumstances. It corresponds to AES-256 ECB.
local expected_decrypted_data_md5 = read(stream, 16)
printf("expected decrypted data MD5: %s\n", get_as_hex(expected_decrypted_data_md5))
local encrypted_data_length = read_u64_le(stream)
printf("encrypted length: %s\n", encrypted_data_length)
local cipher = luagcrypt.Cipher(luagcrypt.CIPHER_AES256, luagcrypt.CIPHER_MODE_ECB)
cipher:setkey(key)
local block_count = encrypted_data_length/block_length
if encrypted_data_length%block_length ~= 0 then
block_count = block_count+1
end
printf("block count: %s\n", block_count)
local padding_length = block_count*block_length-encrypted_data_length
printf("padding length: %s\n", padding_length)
assert(block_count <= 2^53)
block_count = tonumber(block_count)
local blocks = {}
for block_index = 1, block_count do
local encrypted_block = read(stream, block_length)
local decrypted_block = cipher:decrypt(encrypted_block)
blocks[block_index] = decrypted_block
end
if padding_length ~= 0 then
assert(padding_length <= 2^53)
blocks[block_count] = string.sub(blocks[block_count], 1, tonumber(block_length-padding_length))
end
assert(get_is_eof(stream))
local decrypted_data = table.concat(blocks)
local decrypted_data_md5 = get_md5(decrypted_data)
if decrypted_data_md5 ~= expected_decrypted_data_md5 then
error(string.format("expected %s, got %s", get_as_hex(expected_decrypted_data_md5), get_as_hex(decrypted_data_md5)))
end
return decrypted_data
end
local function get_key_from_password(password) -- FileAccessEncrypted::open_and_parse_password
local md5 = get_md5(password)
local md5_hex = get_as_hex(md5)
return md5_hex
end
return {
read_gdec = read_gdec,
get_key_from_password = get_key_from_password,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment