From 066e613ceeb4cf569b2b2a832a95b86213d6382c Mon Sep 17 00:00:00 2001 From: NoDRM Date: Mon, 15 Nov 2021 10:47:09 +0100 Subject: [PATCH] Add UUID to adobekey DER file names --- DeDRM_plugin/__init__.py | 20 ++++++++++++++------ DeDRM_plugin/adobekey.py | 39 +++++++++++++++++++++++++++++---------- DeDRM_plugin/wineutils.py | 4 +++- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/DeDRM_plugin/__init__.py b/DeDRM_plugin/__init__.py index 8af84b1..4437862 100644 --- a/DeDRM_plugin/__init__.py +++ b/DeDRM_plugin/__init__.py @@ -355,12 +355,12 @@ class DeDRM(FileTypePlugin): if iswindows or isosx: from calibre_plugins.dedrm.adobekey import adeptkeys - defaultkeys = adeptkeys() + defaultkeys, defaultnames = adeptkeys() else: # linux from .wineutils import WineGetKeys scriptpath = os.path.join(self.alfdir,"adobekey.py") - defaultkeys = WineGetKeys(scriptpath, ".der",dedrmprefs['adobewineprefix']) + defaultkeys, defaultnames = WineGetKeys(scriptpath, ".der",dedrmprefs['adobewineprefix']) self.default_key = defaultkeys[0] except: @@ -369,9 +369,13 @@ class DeDRM(FileTypePlugin): self.default_key = "" newkeys = [] + newnames = [] + idx = 0 for keyvalue in defaultkeys: if codecs.encode(keyvalue, 'hex').decode('ascii') not in dedrmprefs['adeptkeys'].values(): newkeys.append(keyvalue) + newnames.append(defaultnames[idx]) + idx += 1 if len(newkeys) > 0: try: @@ -394,7 +398,7 @@ class DeDRM(FileTypePlugin): # Store the new successful key in the defaults print("{0} v{1}: Saving a new default key".format(PLUGIN_NAME, PLUGIN_VERSION)) try: - dedrmprefs.addnamedvaluetoprefs('adeptkeys','default_key',codecs.encode(keyvalue, 'hex').decode('ascii')) + dedrmprefs.addnamedvaluetoprefs('adeptkeys','default_key_uuid_' + newnames[i], codecs.encode(userkey, 'hex').decode('ascii')) dedrmprefs.writeprefs() print("{0} v{1}: Saved a new default key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION,time.time()-self.starttime)) except: @@ -458,12 +462,12 @@ class DeDRM(FileTypePlugin): if iswindows or isosx: from calibre_plugins.dedrm.adobekey import adeptkeys - defaultkeys = adeptkeys() + defaultkeys, defaultnames = adeptkeys() else: # linux from .wineutils import WineGetKeys scriptpath = os.path.join(self.alfdir,"adobekey.py") - defaultkeys = WineGetKeys(scriptpath, ".der",dedrmprefs['adobewineprefix']) + defaultkeys, defaultnames = WineGetKeys(scriptpath, ".der",dedrmprefs['adobewineprefix']) self.default_key = defaultkeys[0] except: @@ -472,9 +476,13 @@ class DeDRM(FileTypePlugin): self.default_key = "" newkeys = [] + newnames = [] + idx = 0 for keyvalue in defaultkeys: if codecs.encode(keyvalue,'hex') not in dedrmprefs['adeptkeys'].values(): newkeys.append(keyvalue) + newnames.append(defaultnames[idx]) + idx += 1 if len(newkeys) > 0: try: @@ -497,7 +505,7 @@ class DeDRM(FileTypePlugin): # Store the new successful key in the defaults print("{0} v{1}: Saving a new default key".format(PLUGIN_NAME, PLUGIN_VERSION)) try: - dedrmprefs.addnamedvaluetoprefs('adeptkeys','default_key',codecs.encode(keyvalue,'hex')) + dedrmprefs.addnamedvaluetoprefs('adeptkeys','default_key_uuid_' + newnames[i], codecs.encode(userkey,'hex').decode('ascii')) dedrmprefs.writeprefs() print("{0} v{1}: Saved a new default key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION,time.time()-self.starttime)) except: diff --git a/DeDRM_plugin/adobekey.py b/DeDRM_plugin/adobekey.py index 489c595..5412b86 100644 --- a/DeDRM_plugin/adobekey.py +++ b/DeDRM_plugin/adobekey.py @@ -362,6 +362,7 @@ if iswindows: keykey = CryptUnprotectData(device, entropy) userkey = None keys = [] + names = [] try: plkroot = winreg.OpenKey(cuser, PRIVATE_LICENCE_KEY_PATH) except WindowsError: @@ -374,12 +375,15 @@ if iswindows: ktype = winreg.QueryValueEx(plkparent, None)[0] if ktype != 'credentials': continue + uuid_name = "Unknown" for j in range(0, 16): try: plkkey = winreg.OpenKey(plkparent, "%04d" % (j,)) except WindowsError: break ktype = winreg.QueryValueEx(plkkey, None)[0] + if ktype == 'user': + uuid_name = winreg.QueryValueEx(plkkey, 'value')[0] if ktype != 'privateLicenseKey': continue userkey = winreg.QueryValueEx(plkkey, 'value')[0] @@ -387,12 +391,13 @@ if iswindows: aes = AES(keykey) userkey = aes.decrypt(userkey) userkey = userkey[26:-ord(userkey[-1:])] - #print "found key:",userkey.encode('hex') + # print ("found " + uuid_name + " key: " + str(userkey)) keys.append(userkey) + names.append(uuid_name[9:]) if len(keys) == 0: raise ADEPTError('Could not locate privateLicenseKey') print("Found {0:d} keys".format(len(keys))) - return keys + return keys, names elif isosx: @@ -431,19 +436,25 @@ elif isosx: tree = etree.parse(actpath) adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag) expr = '//%s/%s' % (adept('credentials'), adept('privateLicenseKey')) + exprUUID = '//%s/%s' % (adept('credentials'), adept('user')) userkey = tree.findtext(expr) + userUUID = "Unknown" + try: + userUUID = tree.findtext(exprUUID) + except: + pass userkey = b64decode(userkey) userkey = userkey[26:] - return [userkey] + return [userkey], [userUUID[9:]] else: def adeptkeys(): raise ADEPTError("This script only supports Windows and Mac OS X.") - return [] + return [], [] # interface for Python DeDRM def getkey(outpath): - keys = adeptkeys() + keys, names = adeptkeys() if len(keys) > 0: if not os.path.isdir(outpath): outfile = outpath @@ -452,15 +463,17 @@ def getkey(outpath): print("Saved a key to {0}".format(outfile)) else: keycount = 0 + name_index = 0 for key in keys: while True: keycount += 1 - outfile = os.path.join(outpath,"adobekey_{0:d}.der".format(keycount)) + outfile = os.path.join(outpath,"adobekey{0:d}_uuid_{1}.der".format(keycount, names[name_index])) if not os.path.exists(outfile): break with open(outfile, 'wb') as keyfileout: keyfileout.write(key) print("Saved a key to {0}".format(outfile)) + name_index += 1 return True return False @@ -506,7 +519,7 @@ def cli_main(): # make sure the outpath is the outpath = os.path.realpath(os.path.normpath(outpath)) - keys = adeptkeys() + keys, names = adeptkeys() if len(keys) > 0: if not os.path.isdir(outpath): outfile = outpath @@ -515,15 +528,17 @@ def cli_main(): print("Saved a key to {0}".format(outfile)) else: keycount = 0 + name_index = 0 for key in keys: while True: keycount += 1 - outfile = os.path.join(outpath,"adobekey_{0:d}.der".format(keycount)) + outfile = os.path.join(outpath,"adobekey{0:d}_uuid_{1}.der".format(keycount, names[name_index])) if not os.path.exists(outfile): break with open(outfile, 'wb') as keyfileout: keyfileout.write(key) print("Saved a key to {0}".format(outfile)) + name_index += 1 else: print("Could not retrieve Adobe Adept key.") return 0 @@ -556,12 +571,15 @@ def gui_main(): progpath, progname = os.path.split(argv[0]) success = False try: - keys = adeptkeys() + keys, names = adeptkeys() + print(keys) + print(names) keycount = 0 + name_index = 0 for key in keys: while True: keycount += 1 - outfile = os.path.join(progpath,"adobekey_{0:d}.der".format(keycount)) + outfile = os.path.join(progpath,"adobekey{0:d}_uuid_{1}.der".format(keycount, names[name_index])) if not os.path.exists(outfile): break @@ -569,6 +587,7 @@ def gui_main(): keyfileout.write(key) success = True tkinter.messagebox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile)) + name_index += 1 except ADEPTError as e: tkinter.messagebox.showerror(progname, "Error: {0}".format(str(e))) except Exception: diff --git a/DeDRM_plugin/wineutils.py b/DeDRM_plugin/wineutils.py index f48f4b1..071e0da 100644 --- a/DeDRM_plugin/wineutils.py +++ b/DeDRM_plugin/wineutils.py @@ -93,6 +93,7 @@ def WineGetKeys(scriptpath, extension, wineprefix=""): # try finding winekeys anyway, even if above code errored winekeys = [] + winekey_names = [] # get any files with extension in the output dir files = [f for f in os.listdir(outdirpath) if f.endswith(extension)] for filename in files: @@ -104,9 +105,10 @@ def WineGetKeys(scriptpath, extension, wineprefix=""): else: new_key_value = keyfile.read() winekeys.append(new_key_value) + winekey_names.append(filename) except: print("{0} v{1}: Error loading file {2}".format(PLUGIN_NAME, PLUGIN_VERSION, filename)) traceback.print_exc() os.remove(fpath) print("{0} v{1}: Found and decrypted {2} {3}".format(PLUGIN_NAME, PLUGIN_VERSION, len(winekeys), "key file" if len(winekeys) == 1 else "key files")) - return winekeys + return winekeys, winekey_names