Skip to content

Instantly share code, notes, and snippets.

@Spaceghost
Created March 19, 2011 01:06
Show Gist options
  • Save Spaceghost/877110 to your computer and use it in GitHub Desktop.
Save Spaceghost/877110 to your computer and use it in GitHub Desktop.
A script to decrypt windows product keys written in python
import _winreg
def DecodeKey(rpk):
rpkOffset = 52
i = 28
szPossibleChars = "BCDFGHJKMPQRTVWXY2346789"
szProductKey = ""
while i >= 0:
dwAccumulator = 0
j = 14
while j >= 0:
dwAccumulator = dwAccumulator * 256
d = rpk[j+rpkOffset]
if isinstance(d, str):
d = ord(d)
dwAccumulator = d + dwAccumulator
rpk[j+rpkOffset] = (dwAccumulator / 24) if (dwAccumulator / 24) <= 255 else 255
dwAccumulator = dwAccumulator % 24
j = j - 1
i = i - 1
szProductKey = szPossibleChars[dwAccumulator] + szProductKey
if ((29 - i) % 6) == 0 and i != -1:
i = i - 1
szProductKey = "-" + szProductKey
return szProductKey
def GetKeyFromRegLoc(key, value="DigitalProductID"):
key = _winreg.OpenKey(
_winreg.HKEY_LOCAL_MACHINE,key)
value, type = _winreg.QueryValueEx(key, value)
return DecodeKey(list(value))
def GetIEKey():
return GetKeyFromRegLoc("SOFTWARE\Microsoft\Internet Explorer\Registration")
def GetXPKey():
return GetKeyFromRegLoc("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
def GetWPAKey():
return GetKeyFromRegLoc("SYSTEM\WPA\Key-4F3B2RFXKC9C637882MBM")
print "XP: %s" % GetXPKey()
print "IE: %s" % GetIEKey()
print "WPA: %s" % GetWPAKey()
@HiddenAvocado
Copy link

Hi, can you please give me how to check Microsoft product keys with a Python Module? Like in bulk! Thanks!

@technikker
Copy link

I just wasted an hour myself on this problem. As @Deejan mentions, this method is broken in current version of windows 10.

This works to get the product key in pre-decoded form:

from os import system
from elevate import elevate

if name == 'main':
elevate()
system("wmic path softwarelicensingservice get OA3xOriginalProductKey")
system("PAUSE")

IDK how to set it, or even where in the windows registry, or how to decode it and I can't find any of this anywhere. The only idea I have at this point is to copy my registry, change my product key, then diff my windows registry, which is really stupid and only tells us where in the registry to look - there should be a better way to do this.

@deajan
Copy link

deajan commented Jul 27, 2021

I have created a windows package that does exactly that job, without the need of os.system for security reasons (wmi package is required though).

You can find the code here https://github.com/netinvent/windows_tools/blob/master/windows_tools/product_key/__init__.py which also contains this as a backup solution for elder windows versions.

You might install the whole package with pip install windows_tools.product_key then just use the following:

from windows_tools.product_key import *

pkey = get_windows_product_key_from_wmi()
if not wmi_key:
    pkey = get_windows_product_key_from_reg()

print(pkey)

[EDIT]
I also have a self elevating package that installs with this one, and works with vanilla / pyinstaller / nuitka python builds
You'll find it at https://github.com/netinvent/command_runner
[/EDIT]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment