summaryrefslogtreecommitdiffstats
path: root/DeDRM_plugin/adobekey_get_passhash.py
blob: 1e9b8e2dbd300398179312f28acba793e929eab1 (plain)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# adobekey_get_passhash.py, version 1
# based on adobekey.pyw, version 7.2
# Copyright © 2009-2021 i♥cabbages, Apprentice Harper et al.
# Copyright © 2021 noDRM

# Released under the terms of the GNU General Public Licence, version 3
# <http://www.gnu.org/licenses/>

# Revision history:
#   1 - Initial release

"""
Retrieve Adobe ADEPT user passhash keys
"""

__license__ = 'GPL v3'
__version__ = '1'

import sys, os, time
import base64, hashlib
try: 
    from Cryptodome.Cipher import AES
except ImportError:
    from Crypto.Cipher import AES


def unpad(data, padding=16):
    if sys.version_info[0] == 2:
        pad_len = ord(data[-1])
    else:
        pad_len = data[-1]

    return data[:-pad_len]

PASS_HASH_SECRET = "9ca588496a1bc4394553d9e018d70b9e"


try:
    from calibre.constants import iswindows, isosx
except:
    iswindows = sys.platform.startswith('win')
    isosx = sys.platform.startswith('darwin')


class ADEPTError(Exception):
    pass

def decrypt_passhash(passhash, fp):

    serial_number = base64.b64decode(fp).hex()

    hash_key = hashlib.sha1(bytearray.fromhex(serial_number + PASS_HASH_SECRET)).digest()[:16]

    encrypted_cc_hash = base64.b64decode(passhash)
    cc_hash = unpad(AES.new(hash_key, AES.MODE_CBC, encrypted_cc_hash[:16]).decrypt(encrypted_cc_hash[16:]))
    return base64.b64encode(cc_hash).decode("ascii")


if iswindows:
    try:
        import winreg
    except ImportError:
        import _winreg as winreg

    PRIVATE_LICENCE_KEY_PATH = r'Software\Adobe\Adept\Activation'

    def passhash_keys():
        cuser = winreg.HKEY_CURRENT_USER
        keys = []
        names = []
        try:
            plkroot = winreg.OpenKey(cuser, PRIVATE_LICENCE_KEY_PATH)
        except WindowsError:
            raise ADEPTError("Could not locate ADE activation")
        except FileNotFoundError:
            raise ADEPTError("Could not locate ADE activation")

        idx = 1

        fp = None

        i = -1
        while True:
            i = i + 1   # start with 0
            try:
                plkparent = winreg.OpenKey(plkroot, "%04d" % (i,))
            except:
                # No more keys
                break
                
            ktype = winreg.QueryValueEx(plkparent, None)[0]

            if ktype == "activationToken":
                # find fingerprint for hash decryption
                j = -1
                while True:
                    j = j + 1   # start with 0
                    try:
                        plkkey = winreg.OpenKey(plkparent, "%04d" % (j,))
                    except WindowsError:
                        break
                    except FileNotFoundError:
                        break
                    ktype = winreg.QueryValueEx(plkkey, None)[0]
                    if ktype == 'fingerprint':
                        fp = winreg.QueryValueEx(plkkey, 'value')[0]
                        #print("Found fingerprint: " + fp)


            # Note: There can be multiple lists, with multiple entries each.
            if ktype == 'passHashList':
            
                # Find operator (used in key name)
                j = -1
                lastOperator = "Unknown"
                while True:
                    j = j + 1   # start with 0
                    try:
                        plkkey = winreg.OpenKey(plkparent, "%04d" % (j,))
                    except WindowsError:
                        break
                    except FileNotFoundError:
                        break
                    ktype = winreg.QueryValueEx(plkkey, None)[0]
                    if ktype == 'operatorURL':
                        operatorURL = winreg.QueryValueEx(plkkey, 'value')[0]
                        try: 
                            lastOperator = operatorURL.split('//')[1].split('/')[0]
                        except:
                            pass
                
                
                # Find hashes
                j = -1
                while True:
                    j = j + 1   # start with 0
                    try:
                        plkkey = winreg.OpenKey(plkparent, "%04d" % (j,))
                    except WindowsError:
                        break
                    except FileNotFoundError:
                        break
                    ktype = winreg.QueryValueEx(plkkey, None)[0]

                    if ktype == "passHash":
                        passhash_encrypted = winreg.QueryValueEx(plkkey, 'value')[0]
                        names.append("ADE_key_" + lastOperator + "_" + str(int(time.time())) + "_" + str(idx))
                        idx = idx + 1
                        keys.append(passhash_encrypted)

        if fp is None:
            #print("Didn't find fingerprint for decryption ...")
            return [], []

        print("Found {0:d} passhashes".format(len(keys)), file=sys.stderr)

        keys_decrypted = []

        for key in keys:
            decrypted = decrypt_passhash(key, fp)
            #print("Input key: " + key)
            #print("Output key: " + decrypted)
            keys_decrypted.append(decrypted)

        return keys_decrypted, names

   
else:
    def passhash_keys():
        raise ADEPTError("This script only supports Windows.")
        #TODO: Add MacOS support by parsing the activation.xml file.
        return [], []


if __name__ == '__main__':
    print("This is a python calibre plugin. It can't be directly executed.")