SQL server 2012 database serial number view

1. Where is the serial number stored?

--For SQL Server 2008, 2008 R2
use master
GO
exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup','ProductCode'
exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup','DigitalProductID'
GO
--For SQL Server 2012
use master
GO
exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup','ProductCode'
exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup','DigitalProductId'
GO

Don't be confused by ProductCode, even if only the SQL Server client is installed, there will be this key value in the registry, not the serial number, DigitalProductID is, but after Base24 encoding, it needs to be decoded.

It can be seen that for different versions, the path of the registry is different, but the keys are the same.

The Express version is free and has no serial number, thus no DigitalProductID key in the registry.

2. How to decode the serial number

Use Powershell to decode
The following powershell function is used to decode/retrieve the SQL Server serial number, and the test passed on the SQL Server 2008, 2008 R2 instance:

function Get-SQLServerKey {
  ## function to retrieve the license key of a SQL 2008 Server.
   param ($targets = ".")
  $hklm = 2147483650
  $regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup"
  $regValue1 = "DigitalProductId"
  $regValue2 = "PatchLevel"
  $regValue3 = "Edition"
  Foreach ($target in $targets) {
    $productKey = $null
    $win32os = $null
    $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
    $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue1)
    [string]$SQLver = $wmi.GetstringValue($hklm,$regPath,$regValue2).svalue
    [string]$SQLedition = $wmi.GetstringValue($hklm,$regPath,$regValue3).svalue
    $binArray = ($data.uValue)[52..66]
    $charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
    ## decrypt base24 encoded binary data
    For ($i = 24; $i -ge 0; $i--) {
      $k = 0
      For ($j = 14; $j -ge 0; $j--) {
        $k = $k * 256 -bxor $binArray[$j]
        $binArray[$j] = [math]::truncate($k / 24)
        $k = $k % 24
     }
      $productKey = $charsArray[$k] + $productKey
      If (($i % 5 -eq 0) -and ($i -ne 0)) {
        $productKey = "-" + $productKey
      }
    }
    $win32os = Get-WmiObject Win32_OperatingSystem -computer $target
    $obj = New-Object Object
    $obj | Add-Member Noteproperty Computer -value $target
    $obj | Add-Member Noteproperty OSCaption -value $win32os.Caption
    $obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
    $obj | Add-Member Noteproperty SQLver -value $SQLver
    $obj | Add-Member Noteproperty SQLedition -value $SQLedition
    $obj | Add-Member Noteproperty ProductKey -value $productkey
    $obj
  }
}

The format of the characters in the serial number of SQL Server 2012 has changed, bin A array = ( binArray = (binArray=( data.uValue)[0…16] is different from SQL Server 2008'sbin A array = ( binArray = (binArray=( data.uValue)[52…66], and don’t forget to change the registry path $regPath = “SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup”, the modification is as follows, tested on the SQL Server 2012 instance pass:

function Get-SQLServerKey {
## function to retrieve the license key of a SQL 2012 Server.
## by Jakob Bindslet ([email protected])
## 2012 Modification by Xian Wang ([email protected])
param ($targets = ".")
$hklm = 2147483650
$regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup"
$regValue1 = "DigitalProductId"
$regValue2 = "PatchLevel"
$regValue3 = "Edition"
Foreach ($target in $targets) {
$productKey = $null
$win32os = $null
$wmi = [WMIClass]"\\$target\root\default:stdRegProv"
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue1)
[string]$SQLver = $wmi.GetstringValue($hklm,$regPath,$regValue2).svalue
[string]$SQLedition = $wmi.GetstringValue($hklm,$regPath,$regValue3).svalue
$binArray = ($data.uValue)[0..16]
$charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
## decrypt base24 encoded binary data
For ($i = 24; $i -ge 0; $i--) {
$k = 0
For ($j = 14; $j -ge 0; $j--) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) {
$productKey = "-" + $productKey
}
}
$win32os = Get-WmiObject Win32_OperatingSystem -computer $target
$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty OSCaption -value $win32os.Caption
$obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$obj | Add-Member Noteproperty SQLver -value $SQLver
$obj | Add-Member Noteproperty SQLedition -value $SQLedition
$obj | Add-Member Noteproperty ProductKey -value $productkey
$obj
}
}

Call the powershell function and output the serial number
Open powershell, paste the above function in, press enter, enter Get-SQLServerKey and press enter;

Or save the above function as a .ps1 file for direct reference:

PS C:\Windows\system32> . C:\Users\username\Desktop\pk.ps1
PS C:\Windows\system32> Get-SQLserverKey

The output is as follows:
1589421254(1).jpg

According to the Python base24 decoding function translated from the powershell script:

import math
# x是16进制数
def base24encode(x):
    int_list = []
    str1 = str(bin(x)).replace('0b', '')
    for b in range(0, int(len(str1) / 8)):
        int_list.append(int(str1[(b * 8):(b * 8 + 8)], 2))
    print(int_list)

    i = 24
    productKey = ''
    sel = "BCDFGHJKMPQRTVWXY2346789"
    while i >= 0:
        k = 0
        j = 14
        while j>= 0:
            k = (k *256) ^ int(int_list[j])
            int_list[j] = math.modf(k / 24)[1]
            k = k % 24
            j = j - 1
        productKey = sel[k] + productKey
        i= i-1
    return productKey

 
x = 0x85443B934966ADCB433B3A1234264212
print(base24encode(x))

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/131942953