-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrif-extractor.pyw
223 lines (170 loc) · 7.41 KB
/
rif-extractor.pyw
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import tkinter as tk
from tkinter import filedialog, messagebox, Label, Button, Entry, LEFT, RIGHT, W, E, N, S, StringVar, END
from pathlib import Path
import argparse
import sys
import os
JP = ['G','C']
US = ['A', 'E']
EU = ['F', 'B']
KO = ['H']
nonpdrmheader = bytes.fromhex('0001000100010002efcdab8967452301')
parser = argparse.ArgumentParser(description='Extract key from NoNPDRM license.')
parser.add_argument('filepath', metavar='FILEPATH', type=Path, help='*.bin or .*rif license file.')
# If there are arguments, parse them. If not, run the GUI
if (len(sys.argv) > 1):
print("command line functionality not yet implemented, please use the GUI in the meantime.")
parser.parse_args()
def checkNoNPDRM(filepath):
with open(filepath, "rb") as f:
if f.read(16) != nonpdrmheader:
if (len(sys.argv) < 2):
messagebox.showerror("Error", "Not a valid NoNPDRM license.")
else:
print("Error: Not a valid NoNPDRM License.")
return False
else:
return True
def getContentID(filepath):
with open(filepath, "rb") as f:
# Get Content ID
f.seek(16)
contentid = f.read(36).decode('ascii')
return contentid
def getTitleID(filepath):
with open(filepath, "rb") as f:
# Get Title ID
f.seek(23)
titleid = f.read(9).decode('ascii')
return titleid
def getKey(filepath):
with open(filepath, "rb") as f:
# Get key
f.seek(80)
key = f.read(16)
return key
def getExtractedKey(filepath):
with open(filepath, "rb") as f:
# Get key
key = f.read(16)
return key
def getTitleIDFromContentID(contentid):
return contentid[7:16]
def getRegion(id):
if len(id) == 9:
i = 3
elif len(id) == 36:
i = 10
# Determine region
if id[i] in JP:
return "JP"
elif id[i] in US:
return "US"
elif id[i] in EU:
return "EU"
elif id[i] in KO:
return "KO"
def showPKGInfo(contentid, titleid, region, key):
pkg_info = ("Content ID: {}\nTitle ID: {}\nRegion: {}\nLicense Key: {}".format(contentid, titleid, region, key.hex().upper()))
if (len(sys.argv) < 2):
messagebox.showinfo("Info", pkg_info)
else:
print(pkg_info)
def saveBinary(key, out_dir, filename):
# Save filename.bin
with open(out_dir.joinpath("{}.bin".format(filename)), "wb") as keybin:
keybin.write(key)
def rebuildLicense(key, out_dir, contentid):
with open(out_dir.joinpath("6488b73b912a753a492e2714e9b38bc7.rif"), 'wb') as rebuiltlicense:
rebuiltlicense.write(nonpdrmheader)
rebuiltlicense.write(contentid)
rebuiltlicense.seek(80)
rebuiltlicense.write(key)
rebuiltlicense.seek(511)
rebuiltlicense.write(b'\x00')
class MainGUI:
def __init__(self, master):
self.filename = None
self.master = master
master.title("Vita RIF Tools")
self.open = Button(master, text="Open...", command=self.load)
self.open.grid(row=0)
self.contentid_text = StringVar()
self.contentid_label = Label(master, text="Content ID: ", pady=5, padx=5)
self.contentid_label.grid(row=1, sticky=W)
self.contentid_entry = Entry(master, width=45, textvariable=self.contentid_text)
self.contentid_entry.focus_set()
self.contentid_entry.grid(row=1, columnspan=2, sticky=E)
self.key_text = StringVar()
self.key_label = Label(master, text="Key: ", pady=5, padx=5)
self.key_label.grid(row=2, sticky=W)
self.key_entry = Entry(master, width=45, textvariable=self.key_text)
self.key_entry.focus_set()
self.key_entry.grid(row=2, columnspan=2, sticky=E)
self.label = Label(master, text="Choose an option:", padx=120, pady=5)
self.label.grid(row= 3, columnspan=2, sticky=W)
self.extract_button = Button(master, text="Extract", width=10, command=self.extract)
self.extract_button.grid(row=4)
self.recreate_button = Button(master, text="Recreate .rif file", width=15, command=self.recreate)
self.recreate_button.grid(row=4, column=1)
def extract(self):
if len(self.contentid_text.get()) != 36:
messagebox.showerror("Error", "Invalid content ID.")
return
elif len(self.key_text.get()) != 32:
messagebox.showerror("Error", "Invalid key.")
return
contentid = self.contentid_entry.get()
titleid = getTitleIDFromContentID(contentid)
region = getRegion(titleid)
key = bytes.fromhex(self.key_text.get())
#showPKGInfo(contentid, titleid, region, key)
out_dir = Path(__file__).parent.joinpath('rifout', '{}'.format(titleid))
out_dir.mkdir(exist_ok = True, parents = True)
# Save key as [contentid].bin
saveBinary(key, out_dir, contentid)
# Save info.txt
#out_dir.joinpath('info.txt').write_text(pkg_info)
# Save [contentid].txt with key
#out_dir.joinpath("{}.txt".format(contentid)).write_text(key.hex().upper())
pkg_info = ("Content ID: {}\nTitle ID: {}\nRegion: {}\nLicense Key: {}".format(contentid, titleid, region, key.hex().upper()))
messagebox.showinfo("Info", "Exported key successfully!\n\n{}\n\nOutput folder: {}".format(pkg_info, out_dir))
def recreate(self):
if len(self.contentid_text.get()) != 36:
messagebox.showerror("Error", "Cannot recreate license file with specified content ID.")
return
elif len(self.key_text.get()) != 32:
messagebox.showerror("Error", "Cannot recreate license file with specified key.")
return
contentid = bytes(self.contentid_text.get(), 'ascii')
titleid = getTitleIDFromContentID(self.contentid_text.get())
key = bytes.fromhex(self.key_text.get())
rebuild_dir = Path(__file__).parent.joinpath('rifrebuilt', '{}'.format(self.contentid_text.get()))
rebuild_dir.mkdir(exist_ok = True, parents = True)
rebuildLicense(key, rebuild_dir, contentid)
messagebox.showinfo("Info", "License recreated successfully!\n\nOutput folder: {}".format(rebuild_dir))
def load(self):
self.filename = filedialog.askopenfilename(initialdir = Path(__file__).parent, title = "Select license file",filetypes = (("bin files","*.bin"),("rif files","*.rif"),("all files","*.*")))
if self.filename:
abs_path = Path(self.filename)
else:
return
if len(abs_path.stem) == 36 and abs_path.stat().st_size == 16:
self.contentid_entry.delete(0, END) # Clear text entry
self.contentid_entry.insert(0, abs_path.stem)
self.key_entry.delete(0, END) # Clear text entry
self.key_binary = getExtractedKey(abs_path)
self.key_entry.insert(0, self.key_binary.hex().upper())
return
if checkNoNPDRM(abs_path) == False:
return
self.contentid_entry.delete(0, END) # Clear text entry
self.contentid_entry.insert(0, getContentID(abs_path))
self.key_entry.delete(0, END) # Clear text entry
self.key_entry.insert(0, getKey(abs_path).hex().upper())
root = tk.Tk()
maingui = MainGUI(root)
# If no arguments are passed through command line, run the GUI
if len(sys.argv) < 2:
print("Not enough arguments, running GUI...")
root.mainloop()