52

I am struggling right now with using ubuntu and would like some help.

So right now i'm running ubuntu from a USB flash drive after Windows 7 somehow left.

How can I get my windows product key which is somewhere on my laptop hard drive, from within Ubuntu?

muru
  • 193,181
  • 53
  • 473
  • 722
frugge
  • 631
  • 1
  • 5
  • 5
  • 2
    Retrieving a Windows key has nothing to do with Ubuntu. You may ask at superuser.com –  Sep 05 '17 at 20:39
  • If this is a pre installed Windows, you can find the key on a sticker on the bottom, if it is a laptop or the back, if on a pc. – Mark Kirby Sep 05 '17 at 20:39
  • Thanks MichealBay, I will ask there too, but the problem is relevant to ubuntu since i am running ubuntu. and Mark Kirby the sticker got worn out !! – frugge Sep 05 '17 at 20:42
  • 19
    try `sudo cat /sys/firmware/acpi/tables/MSDM` – Charles Green Sep 05 '17 at 20:49
  • Charles Green, it says "no such file or directory". thanks anyway! – frugge Sep 05 '17 at 20:53
  • 2
    I edited your question down alot, as it has been confirmed the below answer works. As this is a quite common issue and not many people seem to know a solution, I edited to make it as clear as possable, so as many people can find it as possable and get help. – Mark Kirby Sep 05 '17 at 21:10
  • 1
    [In my opinion it would be best to consider this on-topic](https://askubuntu.com/questions/654606/know-my-windows-8-product-key#comment1519003_654606). That's not established policy--I don't think we have one for this--just my view. This is basically the same as [Retrieve Windows 8 Product Key from mainboard](https://askubuntu.com/questions/233181/retrieve-windows-8-product-key-from-mainboard) The answers--reading firmware storage vs. the Windows registry--are different but the general problem is the same. I suggest we close one as a duplicate of the other and maybe ask a mod to merge answers. – Eliah Kagan Sep 06 '17 at 02:25
  • 2
    @MichaelBay but retrieving a windows key from ubuntu is ;) – Rinzwind Sep 06 '17 at 06:51
  • @EliahKagan I agree, consolodating all those answers under one clear question would be a good thing, take it to meta for voting, I would support the merge. – Mark Kirby Sep 06 '17 at 09:38
  • At least with my Windows 8.1 installation, this results in a key different from that produced by [this other solution](https://superuser.com/questions/637971/how-do-i-get-out-my-embedded-windows-8-key-from-a-linux-environment). – caw Apr 30 '19 at 00:38

2 Answers2

61

First recover your Ubuntu with going to recovery mode and running

sudo apt install --reinstall ubuntu-desktop

This answer is not written by me but by Thomas on Superuser, please vote there, if you vote here thanks

There is a great tool available for Linux called chntpw. You can get it easily on Debian/Ubuntu via:

sudo apt install chntpw

To look into the relevant registry file mount the Windows disk and open it like so:

chntpw -e /path/to/windisk/Windows/System32/config/SOFTWARE

Now to get the decoded DigitalProductId enter this command:

dpi \Microsoft\Windows NT\CurrentVersion\DigitalProductId

A comment from below says

The path to the relevant registry file is /path/to/windisk/Windows/System32/config/RegBack/SOFTWARE

rzr
  • 415
  • 6
  • 10
Mark Kirby
  • 18,289
  • 19
  • 78
  • 113
9

So for anyone wondering how this actually works.

Essentially you'll have to grab the contents of the registry key

HKLM\Software\Microsoft\Windows NT\CurrentVersion\DigitalProductId

This is a so called REG_BINARY. Meaning it's just a collection of bytes. You could dump them via chntpw or copy them by hand.

Let's see what we have to do with those bytes in order to get our product key with the help of some pseudo code.

Once you have those in an Array, you need to extract the subset of bytes that encode the product id. In particular: the range between 52 and (52 + 14). That gives you 15 bytes.

EncodedId = DigitalProductId.Range(52, 52+14)

This is still a bunch of bytes, that don't at all resemble the product key. So let us decode it.

For that you need the collection of all the characters a product key can be made of:

Characters = "BCDFGHJKMPQRTVWXY2346789"

Yes this is not the whole alphabet. As it turns out a Windows product key doesn't use all of the alphanumerical symbols.

Now let's do the decoding. We'll need:

  • A variable to hold the product key
  • A loop over 0 to 24. For each character of our product key
  • An inner loop over 0 to 14 (In reverse) For each byte in our encoded id
  • Some bit fiddeling and arithmatic for the decoding process

ProductKey = ""
FOR i = 0 TO 24
    c = 0

    FOR j = 14 TO 0 STEP -1
        # Shift the current contents of c to the left by 1 byte 
        #  and xor it with the next byte of our id
        c = (c * 256) XOR EncodedId[j]

        # Put the result of the divison back into the array
        EncodedId[j] = FLOOR(c / 24)

        # Calculate remainder of c
        c = c MOD 24
    LOOP
    # Take character at position c and prepend it to the ProductKey
    ProductKey = Characters[c] + ProductKey
LOOP

Finally we insert the "-" character into the string at the appropriate places.

FOR i = 4 TO 1 STEP -1
    ProductKey = ProductKey.Insert(i * 5, "-")
LOOP

And we're done!

... Almost:

PRINT(ProductKey)

Now!


Capabilities of our pseudo code

  • $array.Range($from, $to) Get the contents of $array from $from to $to
  • $array.Insert($where, $what) Insert $what at $where
  • FOR $var = $start TO $stop [STEP $step] loop the variable $var from $start to $stop applying $step on each iteration
  • $a XOR $b Calculate bit-wise exclusive or on the numbers $a and $b
  • $a MOD $b Calculate remainder of the division of $a and $b
  • $array[$i] Take only the element at position $i from the array
  • #bla bla Is a comment and will be ignored
  • Strings are just char arrays.

You can see 3 actual implementations in C#, PowerShell and Python over at Super User

Lenar Hoyt
  • 143
  • 7
MrPaulch
  • 191
  • 3
  • Alright, quick tip! Let’s say you want to get the Windows license key from your machine and you’re running Linux. Simply open a terminal and run the following command. sudo hexdump -s 56 -e '"MSDM key: " /29 "%s\n"' /sys/firmware/acpi/tables/MSDM – dilshad Feb 22 '22 at 11:33
  • Is `/sys/firmware/acpi/tables/MSDM` only available for OEM licenses?? – King Midas May 18 '22 at 10:57