-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpack_sis.py
98 lines (93 loc) · 4.92 KB
/
unpack_sis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
from argparse import ArgumentParser
from binascii import hexlify, unhexlify
from io import BytesIO
from os import path, makedirs
from re import sub
from steam.core.crypto import symmetric_encrypt, symmetric_encrypt_with_iv
from struct import iter_unpack, pack
from sys import argv
from vdf import loads
from chunkstore import Chunkstore
def unpack_chunkstore(target, key=None, key_hex=None):
if key == True:
key, key_hex = find_key(depot)
chunkstore = Chunkstore(target)
with open(chunkstore.csdname, "rb") as csdfile:
def unpacker(chunkstore, sha, offset, length):
print("extracting chunk %s from offset %s in file %s" % (hexlify(sha).decode(), offset, target + ".csd"))
csdfile.seek(offset)
if key:
with open("./depots/%s/%s" % (chunkstore.depot, hexlify(sha).decode()), "wb") as f:
print("writing %s bytes re-encrypted using key %s and random IV" % (length, key_hex))
f.write(symmetric_encrypt(csdfile.read(length), key))
elif chunkstore.is_encrypted:
with open("./depots/%s/%s" % (chunkstore.depot, hexlify(sha).decode()), "wb") as f:
print("writing %s bytes encrypted" % length)
f.write(csdfile.read(length))
else:
with open("./depots/%s/%s_decrypted" % (chunkstore.depot, hexlify(sha).decode()), "wb") as f:
print("writing %s bytes unencrypted" % length)
f.write(csdfile.read(length))
makedirs("./depots/%s" % chunkstore.depot, exist_ok=True)
chunkstore.unpack(unpacker)
def find_key(depot):
if path.exists("./depot_keys.txt"):
with open("./depot_keys.txt", "r", encoding="utf-8") as f:
for line in f.read().split("\n"):
line = line.split("\t")
if line[0] == depot:
key_hex = line[2]
return unhexlify(key_hex), key_hex
if not key:
print("couldn't find key for depot", depot)
def unpack_sis(sku, chunkstore_path, use_key = False):
need_manifests = {}
chunkstore_path = sub(r'Disk_\d+', '', chunkstore_path)
if "sku" in sku.keys():
sku = sku["sku"]
# unpack each depot
for depot in sku["manifests"]:
key, key_hex = None, None
if use_key and sku["backup"] == "1":
key, key_hex = find_key(depot)
need_manifests[depot] = sku["manifests"][depot]
for chunkstore in sku["chunkstores"][depot]:
print("unpacking chunkstore %s" % chunkstore)
target = chunkstore_path + "/%s_depotcache_%s" % (depot, chunkstore)
if not path.exists(target + ".csm"):
# maybe it's in a disk folder?
target = chunkstore_path + "/Disk_%s/%s_depotcache_%s" % (chunkstore, depot, chunkstore)
if not path.exists(target + ".csm"):
# try again with other disk folders?
disk = 1
while disk <= int(sku["disks"]):
target = chunkstore_path + "/Disk_%s/%s_depotcache_%s" % (disk, depot, chunkstore)
print("searching for depot %s in disk %s" % (depot, disk))
if path.exists(target + ".csm"):
break
disk += 1
if not path.exists(target + ".csm"):
# welp
print("couldn't find depot %s chunkstore %s" % (depot, chunkstore))
return False
# unpack this chunkstore
unpack_chunkstore(target, key, key_hex)
print("done unpacking, to extract with depot_extractor you will need these manifests:")
for depot, manifest in need_manifests.items():
print("depot %s manifest %s" % (depot, manifest))
return True
if __name__ == "__main__":
parser = ArgumentParser(description='Unpacks game data chunks from a SteamPipe retail master or game backup.')
parser.add_argument("target", type=str, help="Path to the sku.sis file defining the master to unpack (or path to csd or csm file if only unpacking one chunkstore.)")
parser.add_argument("-e", action='store_true', help="Re-encrypt the chunks with a key from depot_keys.txt (if one is available) after extracting. (The primary reason you would want to do this is to serve the chunks to a Steam client over a LAN cache.)", dest="key")
args = parser.parse_args()
if args.target.endswith(".sis"):
with open(args.target, "r") as f:
sku = loads(f.read())
chunkstore_path = path.dirname(args.target)
if chunkstore_path == "":
chunkstore_path = "."
exit(0 if unpack_sis(sku, chunkstore_path, args.key) else 1)
else:
unpack_chunkstore(args.target.replace(".csm","").replace(".csd",""), args.key)