Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 7.23 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /usr/bin/python
  2.  
  3. # ineptkey.pyw, version 3
  4.  
  5. # To run this program install Python 2.6 from http://www.python.org/download/
  6. # and PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
  7. # (make sure to install the version for Python 2.6).  Save this script file as
  8. # ineptkey.pyw and double-click on it to run it.  It will create a file named
  9. # adeptkey.der in the same directory.  This is your ADEPT user key.
  10.  
  11. # Revision history:
  12. #   1 - Initial release, for Adobe Digital Editions 1.7
  13. #   2 - Better algorithm for finding pLK; improved error handling
  14. #   3 - Rename to INEPT
  15.  
  16. """
  17. Retrieve Adobe ADEPT user key under Windows.
  18. """
  19.  
  20. from __future__ import with_statement
  21.  
  22. __license__ = 'GPL v3'
  23.  
  24. import sys
  25. import os
  26. from struct import pack
  27. from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
  28.     create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
  29.     string_at, Structure, c_void_p, cast
  30. import _winreg as winreg
  31. import Tkinter
  32. import Tkconstants
  33. import tkMessageBox
  34. import traceback
  35.  
  36. try:
  37.     from Crypto.Cipher import AES
  38. except ImportError:
  39.     AES = None
  40.  
  41.  
  42. DEVICE_KEY = 'Software\\Adobe\\Adept\\Device'
  43. PRIVATE_LICENCE_KEY_KEY = 'Software\\Adobe\\Adept\\Activation\\%04d\\%04d'
  44.  
  45. MAX_PATH = 255
  46.  
  47. kernel32 = windll.kernel32
  48. advapi32 = windll.advapi32
  49. crypt32 = windll.crypt32
  50.  
  51.  
  52. class ADEPTError(Exception):
  53.     pass
  54.  
  55.  
  56. def GetSystemDirectory():
  57.     GetSystemDirectoryW = kernel32.GetSystemDirectoryW
  58.     GetSystemDirectoryW.argtypes = [c_wchar_p, c_uint]
  59.     GetSystemDirectoryW.restype = c_uint
  60.     def GetSystemDirectory():
  61.         buffer = create_unicode_buffer(MAX_PATH + 1)
  62.         GetSystemDirectoryW(buffer, len(buffer))
  63.         return buffer.value
  64.     return GetSystemDirectory
  65. GetSystemDirectory = GetSystemDirectory()
  66.  
  67.  
  68. def GetVolumeSerialNumber():
  69.     GetVolumeInformationW = kernel32.GetVolumeInformationW
  70.     GetVolumeInformationW.argtypes = [c_wchar_p, c_wchar_p, c_uint,
  71.                                       POINTER(c_uint), POINTER(c_uint),
  72.                                       POINTER(c_uint), c_wchar_p, c_uint]
  73.     GetVolumeInformationW.restype = c_uint
  74.     def GetVolumeSerialNumber(path):
  75.         vsn = c_uint(0)
  76.         GetVolumeInformationW(path, None, 0, byref(vsn), None, None, None, 0)
  77.         return vsn.value
  78.     return GetVolumeSerialNumber
  79. GetVolumeSerialNumber = GetVolumeSerialNumber()
  80.  
  81.  
  82. def GetUserName():
  83.     GetUserNameW = advapi32.GetUserNameW
  84.     GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
  85.     GetUserNameW.restype = c_uint
  86.     def GetUserName():
  87.         buffer = create_unicode_buffer(32)
  88.         size = c_uint(len(buffer))
  89.         while not GetUserNameW(buffer, byref(size)):
  90.             buffer = create_unicode_buffer(len(buffer) * 2)
  91.             size.value = len(buffer)
  92.         return buffer.value.encode('utf-16-le')[::2]
  93.     return GetUserName
  94. GetUserName = GetUserName()
  95.  
  96.  
  97. CPUID0_INSNS = create_string_buffer("\x53\x31\xc0\x0f\xa2\x8b\x44\x24\x08\x89"
  98.                                     "\x18\x89\x50\x04\x89\x48\x08\x5b\xc3")
  99. def cpuid0():
  100.     buffer = create_string_buffer(12)
  101.     cpuid0__ = CFUNCTYPE(c_char_p)(addressof(CPUID0_INSNS))
  102.     def cpuid0():
  103.         cpuid0__(buffer)
  104.         return buffer.raw
  105.     return cpuid0
  106. cpuid0 = cpuid0()
  107.  
  108.  
  109. CPUID1_INSNS = create_string_buffer("\x53\x31\xc0\x40\x0f\xa2\x5b\xc3")
  110. cpuid1 = CFUNCTYPE(c_uint)(addressof(CPUID1_INSNS))
  111.  
  112.  
  113. class DataBlob(Structure):
  114.     _fields_ = [('cbData', c_uint),
  115.                 ('pbData', c_void_p)]
  116. DataBlob_p = POINTER(DataBlob)
  117.  
  118. def CryptUnprotectData():
  119.     _CryptUnprotectData = crypt32.CryptUnprotectData
  120.     _CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
  121.                                    c_void_p, c_void_p, c_uint, DataBlob_p]
  122.     _CryptUnprotectData.restype = c_uint
  123.     def CryptUnprotectData(indata, entropy):
  124.         indatab = create_string_buffer(indata)
  125.         indata = DataBlob(len(indata), cast(indatab, c_void_p))
  126.         entropyb = create_string_buffer(entropy)
  127.         entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
  128.         outdata = DataBlob()
  129.         if not _CryptUnprotectData(byref(indata), None, byref(entropy),
  130.                                    None, None, 0, byref(outdata)):
  131.             raise ADEPTError("Failed to decrypt user key key (sic)")
  132.         return string_at(outdata.pbData, outdata.cbData)
  133.     return CryptUnprotectData
  134. CryptUnprotectData = CryptUnprotectData()
  135.  
  136.  
  137. def retrieve_key(keypath):
  138.     root = GetSystemDirectory().split('\\')[0] + '\\'
  139.     serial = GetVolumeSerialNumber(root)
  140.     vendor = cpuid0()
  141.     signature = pack('>I', cpuid1())[1:]
  142.     user = GetUserName()
  143.     entropy = pack('>I12s3s13s', serial, vendor, signature, user)
  144.     cuser = winreg.HKEY_CURRENT_USER
  145.     try:
  146.         regkey = winreg.OpenKey(cuser, DEVICE_KEY)
  147.     except WindowsError:
  148.         raise ADEPTError("Adobe Digital Editions not activated")
  149.     device = winreg.QueryValueEx(regkey, 'key')[0]
  150.     keykey = CryptUnprotectData(device, entropy)
  151.     userkey = None
  152.     for i in xrange(0, 16):
  153.         for j in xrange(0, 16):
  154.             plkkey = PRIVATE_LICENCE_KEY_KEY % (i, j)
  155.             try:
  156.                 regkey = winreg.OpenKey(cuser, plkkey)
  157.             except WindowsError:
  158.                 break
  159.             type = winreg.QueryValueEx(regkey, None)[0]
  160.             if type != 'privateLicenseKey':
  161.                 continue
  162.             userkey = winreg.QueryValueEx(regkey, 'value')[0]
  163.             break
  164.         if userkey is not None:
  165.             break
  166.     if userkey is None:
  167.         raise ADEPTError('Could not locate privateLicenseKey')
  168.     userkey = userkey.decode('base64')
  169.     userkey = AES.new(keykey, AES.MODE_CBC).decrypt(userkey)
  170.     userkey = userkey[26:-ord(userkey[-1])]
  171.     with open(keypath, 'wb') as f:
  172.         f.write(userkey)
  173.     return
  174.  
  175. class ExceptionDialog(Tkinter.Frame):
  176.     def __init__(self, root, text):
  177.         Tkinter.Frame.__init__(self, root, border=5)
  178.         label = Tkinter.Label(self, text="Unexpected error:",
  179.                               anchor=Tkconstants.W, justify=Tkconstants.LEFT)
  180.         label.pack(fill=Tkconstants.X, expand=0)
  181.         self.text = Tkinter.Text(self)
  182.         self.text.pack(fill=Tkconstants.BOTH, expand=1)
  183.         self.text.insert(Tkconstants.END, text)
  184.  
  185.  
  186. def main(argv=sys.argv):
  187.     root = Tkinter.Tk()
  188.     root.withdraw()
  189.     progname = os.path.basename(argv[0])
  190.     if AES is None:
  191.         tkMessageBox.showerror(
  192.             "ADEPT Key",
  193.             "This script requires PyCrypto, which must be installed "
  194.             "separately.  Read the top-of-script comment for details.")
  195.         return 1
  196.     keypath = 'adeptkey.der'
  197.     try:
  198.         retrieve_key(keypath)
  199.     except ADEPTError, e:
  200.         tkMessageBox.showerror("ADEPT Key", "Error: " + str(e))
  201.         return 1
  202.     except Exception:
  203.         root.wm_state('normal')
  204.         root.title('ADEPT Key')
  205.         text = traceback.format_exc()
  206.         ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1)
  207.         root.mainloop()
  208.         return 1
  209.     tkMessageBox.showinfo(
  210.         "ADEPT Key", "Key successfully retrieved to %s" % (keypath))
  211.     return 0
  212.  
  213. if __name__ == '__main__':
  214.     sys.exit(main())