Advertisement
Stewie410

Extract Windows/Office Product Keys

Aug 6th, 2017
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '  Found this script at work recently, stuffed away on some forum (spiceworks, maybe?); figured I'd share it where it won't be blocked
  2. '  As such, in no way should I be credited for this script.
  3. '  Note: this does not generate any keys--all it does is extract your current license keys for Windows and Office...that's it.
  4. '  So far its been tested to work in Win7 (Ult, Prof), Win10 (Home, Prof), Office 2007, Office 2010 and Office 2013.
  5. '  This WILL NOT extract the product key for Office 2016.
  6. '  YMMV <3
  7.  
  8. Const HKLM = &H80000002
  9.  
  10. dim fso: set fso = CreateObject("Scripting.FileSystemObject")
  11.     dim CurrentDirectory
  12.     CurrentDirectory = fso.GetAbsolutePathName(".")
  13.  
  14.  
  15. Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\OfficeKeys.txt",8,True)
  16.  
  17. 'US = CreateObject("WScript.Shell").ExpandEnvironmentStrings(%UserProfile%)
  18. 'Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(US & "\Desktop\OfficeKeys.txt",8,True)
  19.  
  20. 'wscript.echo "View Product Keys | Microsoft Products" & vbCrLf
  21.  
  22. 'Install Date
  23. Computer = "."
  24. Set objWMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
  25. Set Obj = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
  26.  
  27. dim InsDate
  28.  
  29. For Each item in Obj
  30.   InsDate = item.InstallDate
  31.   ' Gather Operating System Information
  32.  Caption = Item.Caption
  33.   OSArchitecture = Item.OSArchitecture
  34.   CSDVersion = Item.CSDVersion
  35.   Version = Item.Version
  36.   Next
  37.  
  38. dim NewDate
  39.  
  40. NewDate = mid(InsDate,9,2) & ":" & mid(InsDate,11,2) & ":" & mid(InsDate,13,2)
  41. NewDate = NewDate & " " & mid(InsDate,7,2) & "/" & mid(InsDate,5,2) & "/" & mid(InsDate,1,4)
  42.  
  43. QueryWindowsProductKeys()
  44.  
  45. 'wscript.echo 'vbCrLf & "Office Keys" & vbCrLf
  46.  
  47. QueryOfficeProductKeys()
  48.  
  49. Function DecodeProductKey(arrKey, intKeyOffset)
  50.   If Not IsArray(arrKey) Then Exit Function
  51.     intIsWin8 = BitShiftRight(arrKey(intKeyOffset + 14),3) And 1    
  52.     arrKey(intKeyOffset + 14) = arrKey(intKeyOffset + 14) And 247 Or BitShiftLeft(intIsWin8 And 2,2)
  53.     i = 24
  54.     strChars = "BCDFGHJKMPQRTVWXY2346789"
  55.     strKeyOutput = ""
  56.     While i > -1
  57.         intCur = 0
  58.         intX = 14
  59.         While intX > -1
  60.             intCur = BitShiftLeft(intCur,8)
  61.             intCur = arrKey(intX + intKeyOffset) + intCur
  62.             arrKey(intX + intKeyOffset) = Int(intCur / 24)
  63.             intCur = intCur Mod 24
  64.             intX = intX - 1
  65.         Wend
  66.         i = i - 1
  67.         strKeyOutput = Mid(strChars,intCur + 1,1) & strKeyOutput
  68.         intLast = intCur
  69.     Wend
  70.     If intIsWin8 = 1 Then
  71.         strKeyOutput = Mid(strKeyOutput,2,intLast) & "N" & Right(strKeyOutput,Len(strKeyOutput) - (intLast + 1))    
  72.     End If
  73.     strKeyGUIDOutput = Mid(strKeyOutput,1,5) & "-" & Mid(strKeyOutput,6,5) & "-" & Mid(strKeyOutput,11,5) & "-" & Mid(strKeyOutput,16,5) & "-" & Mid(strKeyOutput,21,5)
  74.     DecodeProductKey = strKeyGUIDOutput
  75. End Function
  76.  
  77. Function RegReadBinary(strRegPath,strRegValue)
  78.     Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  79.     objReg.GetBinaryValue HKLM,strRegPath,strRegValue,arrRegBinaryData
  80.     RegReadBinary = arrRegBinaryData
  81.     Set objReg = Nothing
  82. End Function
  83.  
  84. Function BitShiftLeft(intValue,intShift)
  85.     BitShiftLeft = intValue * 2 ^ intShift
  86. End Function
  87.  
  88. Function BitShiftRight(intValue,intShift)
  89.     BitShiftRight = Int(intValue / (2 ^ intShift))
  90. End Function
  91.  
  92. Function QueryOfficeProductKeys()
  93.  
  94.         strBaseKey = "SOFTWARE\"
  95.  
  96.         strOfficeKey = strBaseKey & "Microsoft\Office"
  97.         Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  98.         objReg.EnumKey HKLM, strOfficeKey, arrOfficeVersionSubKeys
  99.         intProductCount = 1
  100.         If IsArray(arrOfficeVersionSubKeys) Then
  101.  
  102.             For Each strOfficeVersionKey In arrOfficeVersionSubKeys
  103.  
  104.                 Select Case strOfficeVersionKey
  105.                     Case "11.0"
  106.                         CheckOfficeKey strOfficeKey & "\11.0\Registration",52,intProductCount
  107.                     Case "12.0"
  108.                         CheckOfficeKey strOfficeKey & "\12.0\Registration",52,intProductCount
  109.                     Case "14.0"
  110.                         CheckOfficeKey strOfficeKey & "\14.0\Registration",808,intProductCount
  111.                     Case "15.0"
  112.                         CheckOfficeKey strOfficeKey & "\15.0\Registration",808,intProductCount
  113.                 End Select
  114.             Next
  115.         End If
  116.  
  117.         strBaseKey = "SOFTWARE\Wow6432Node\"
  118.  
  119.         strOfficeKey = strBaseKey & "Microsoft\Office"
  120.         Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  121.         objReg.EnumKey HKLM, strOfficeKey, arrOfficeVersionSubKeys
  122.         intProductCount = 1
  123.  
  124.         If IsArray(arrOfficeVersionSubKeys) Then
  125.  
  126.             For Each strOfficeVersionKey In arrOfficeVersionSubKeys
  127.  
  128.                 Select Case strOfficeVersionKey
  129.                     Case "11.0"
  130.                         CheckOfficeKey strOfficeKey & "\11.0\Registration",52,intProductCount
  131.                     Case "12.0"
  132.                         CheckOfficeKey strOfficeKey & "\12.0\Registration",52,intProductCount
  133.                     Case "14.0"
  134.                         CheckOfficeKey strOfficeKey & "\14.0\Registration",808,intProductCount
  135.                     Case "15.0"
  136.                         CheckOfficeKey strOfficeKey & "\15.0\Registration",808,intProductCount
  137.                 End Select
  138.             Next
  139.         End If
  140. End Function
  141.  
  142. 'Office Product Key
  143. Sub CheckOfficeKey(strRegPath,intKeyOffset,intProductCount)
  144.  
  145.     Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  146.     objReg.EnumKey HKLM, strRegPath, arrOfficeRegistrations
  147.     If IsArray(arrOfficeRegistrations) Then
  148.         For Each strOfficeRegistration In arrOfficeRegistrations
  149.  
  150.             objReg.GetStringValue HKLM,strRegPath & "\" & strOfficeRegistration,"ConvertToEdition",strOfficeEdition
  151.             objReg.GetBinaryValue HKLM,strRegPath & "\" & strOfficeRegistration,"DigitalProductID",arrProductID
  152.             If strOfficeEdition <> "" And IsArray(arrProductID) Then
  153.                 WriteData "Product", strOfficeEdition
  154.                 WriteData "Key", DecodeProductKey(arrProductID,intKeyOffset) & vbCrLf
  155.                 intProductCount = intProductCount + 1
  156.             End If
  157.         Next
  158.     End If
  159. End Sub
  160.  
  161.  
  162. 'Windows Product Key
  163. Sub QueryWindowsProductKeys()
  164.     strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId",52)
  165.     If strWinKey <> "" Then
  166.         WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  167.         'WriteData "Installation Date: " & NewDate
  168.        WriteData "Key", strWinKey & vbnewline
  169.         Exit Sub
  170.     End If
  171.     strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId4",808)
  172.     If strWinKey <> "" Then
  173.         WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  174.         'WriteData "Installation Date: " & NewDate
  175.        WriteData "Key", strWinKey & vbnewline
  176.         Exit Sub
  177.     End If
  178.     strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey","DigitalProductId",52)
  179.     If strWinKey <> "" Then
  180.         WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  181.         'WriteData"Installation Date: " & NewDate
  182.        WriteData "Key", strWinKey & vbnewline
  183.         Exit Sub
  184.     End If
  185.     strWinKey = CheckWindowsKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey","DigitalProductId4",808)
  186.     If strWinKey <> "" Then
  187.         WriteData "Product: " & Caption & Version & " (" & OSArchitecture & ")", ""
  188.         'WriteData "Installation Date: " & NewDate
  189.        WriteData "Key", strWinKey & vbnewline
  190.         Exit Sub
  191.     End If
  192.    
  193.    
  194.  
  195. End Sub
  196.  
  197.  
  198. Function CheckWindowsKey(strRegPath,strRegValue,intKeyOffset)
  199.     strWinKey = DecodeProductKey(RegReadBinary(strRegPath,strRegValue),intKeyOffset)
  200.     If strWinKey <> "BBBBB-BBBBB-BBBBB-BBBBB-BBBBB" And strWinKey <> "" Then
  201.         CheckWindowsKey = strWinKey
  202.     Else
  203.         CheckWindowsKey = ""
  204.     End If
  205. End Function
  206.  
  207. Function RegReadBinary(strRegPath,strRegValue)
  208.     Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  209.     objReg.GetBinaryValue HKLM,strRegPath,strRegValue,arrRegBinaryData
  210.     RegReadBinary = arrRegBinaryData
  211.     Set objReg = Nothing
  212. End Function
  213.  
  214. Function OsArch()
  215.     Set objShell = WScript.CreateObject("WScript.Shell")
  216.     If objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%") = "%ProgramFiles(x86)%" Then
  217.         OsArch = "x86"
  218.     Else
  219.         OsArch = "x64"
  220.     End If
  221.     Set objShell = Nothing
  222. End Function
  223.  
  224. Sub WriteData(strProperty,strValue)
  225.  
  226.     objFileToWrite.WriteLine strProperty & ": " & Trim(strValue)
  227.  
  228.     'Set objShell = CreateObject("WScript.Shell")
  229.    'strKey = "HKLM\SOFTWARE\CentraStage\Custom\" & strProperty
  230.    'objShell.RegWrite strKey,Trim(strValue),"REG_SZ"
  231.    'Set objShell = Nothing
  232.  
  233. End Sub
  234.  
  235. wscript.echo "Successfully Saved"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement