Kirsle.net logo Kirsle.net

Get FAT Drive Serial Numbers in Unix

December 31, 2009 by Noah
There's not a lot of information about this on The Google, so hopefully this blog will help anyone who wants to know how to get the serial number of a FAT partition from within a Unix-like operating system (including Linux and Mac OS X).

First, this is what I mean about serial numbers. Suppose you're using a Windows system, have a floppy disk at drive A:/ and a regular USB flash drive at E:/, and you run these commands in the command prompt:

C:\>vol E:
Volume in drive E is CRUZER
Volume Serial Number is 955C-59BF

C:\>vol A:
Volume in drive A has no label.
Volume Serial Number is EC2B-36AF
These serial numbers are assigned when the drive is formatted; reformatting a floppy disk or flash drive will give it a different serial number.

According to The Wikipedia, the serial number (ID) is kept in two different places on the partition depending on the version of FAT being used.

In FAT12 and FAT16 (used with floppy disks), the ID begins at byte offset 0x27 (39 in decimal); in FAT32 (used with flash drives and external hard drives), the ID begins at 0x43 (67 in decimal).

So, with the handy dd utility that comes standard on pretty much any Unix-like system, you can extract this information and display it. Here are a couple of one-liners you can run in a Unix terminal. I'll explain how they work afterward.

# For FAT32 filesystems (modern flash drives)
dd if=/dev/sdb1 skip=67 bs=1 count=4 | hexdump -v -e '1/1 "%02X" " "' | xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'

# For FAT12/16 filesystems (old floppy drives)
dd if=testfloppy.img skip=39 bs=1 count=4 | hexdump -v -e '1/1 "%02X" " "' | xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'
I underlined the input file (if) and byte offset (skip) in both of these commands. In the first one, I ran the command on a real, physical, flash drive, that had a device node at /dev/sdb1 for its one and only partition. In the second one, I ran it on a floppy disk image file (who has a computer with a real floppy drive these days?)

If you're going to be using a physical device like in my first command, you need to run the command with root privileges (regular users can't read directly from the device node). My second example (using an image file) can be run as a regular user, however.

These commands printed in the terminal for me:

(for the flash drive)
4+0 records in
4+0 records out
4 bytes (4 B) copied, 3.3445e-05 s, 120 kB/s
Serial Number: 955C-59BF

(for the floppy image)
4+0 records in
4+0 records out
4 bytes (4 B) copied, 3.1551e-05 s, 127 kB/s
Serial Number: EC2B-36AF
And now, how the commands work. I'll use the flash drive command as the example. In this one-liner, three commands are being executed at once:
dd if=/dev/sdb1 skip=67 bs=1 count=4
hexdump -v -e '1/1 "%02X" " "'
xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'
The dd command gets the operating system to read raw data from the flash drive at /dev/sdb1, skipping the first 67 bytes, reading only 1 byte at a time, and reading a total of 4 bytes. This gets the 4 byte serial number; now we need to display it in hexadecimal like Windows and DOS.

The hexdump command takes the 4 binary bytes and displays them in hexadecimal. On my flash drive, it looks like this: BF 59 5C 95. Note that the hex codes are out of order; Windows shows them as 955C-59BF - basically, the reverse of what hexdump shows. Hexdump is showing the correct order; Windows and DOS reverse them when they show you the serial number.

So, we run it through xargs (which turns the four hex numbers into four separate parameters) and sends them to a quick Perl script, which prints out "Serial Number:" and puts the hex codes in the correct order, to give the same result as Windows and DOS.

One could use this information to make a vol command for Unix. If the command checks other places in the filesystem headers to determine the version of FAT, it could automatically use the correct byte offset and get the serial number from both floppy disks and flash drives.

Tags:

Comments

There are 15 comments on this page. Add yours.

Avatar image
mike posted on April 5, 2010 @ 05:40 UTC

have you ever heard about "/sbin/blkid"? It's in the core Linux package "util-linux", I am sure you have it :). The correct name for the "Serial Number:" is "UUID".

Avatar image
Christophe posted on June 30, 2010 @ 11:16 UTC

Hello, Very nice . I was looking for more then a week on such solution. Since I need the UUID from 16 or 32 fats on usb sticks On a mac pc. For synchronizing php script this is a security build in to avoid persons to synchronize on another stick then their own. Thank's

Avatar image
Kyle posted on August 11, 2010 @ 15:07 UTC

Tried it on Mac OS X, got the same serial number for 2 different flash drives.

Avatar image
rarerade posted on November 30, 2010 @ 07:47 UTC

Plz tell me how can i know my harddisk serial number on windowsXP packet 3 > i use this .pl but can't see my serial number...

use Win32::OLE qw(in); my $WMIServices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//."); my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive"); my @DV = in($DriveCollection); print $DV[0]->{SerialNumber};

Avatar image
rarerade posted on November 30, 2010 @ 07:48 UTC

Plz tell me how can i know my harddisk serial number on windowsXP packet 3 > i use this .pl but can't see my serial number...

use Win32::OLE qw(in); my $WMIServices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//."); my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive"); my @DV = in($DriveCollection); print $DV[0]->{SerialNumber};

Avatar image
Noah (@kirsle) posted on November 30, 2010 @ 08:33 UTC

my ($serial) = vol E:\\ =~ m/Volume Serial Number is ([A-Fa-f0-9-]{9})/;

;)

Avatar image
rarerade posted on December 1, 2010 @ 02:56 UTC

Volume Serial Number and Harddisk Number is the same ?..

Sir.kirsle I want to ask you some thing. How can i make .pl to use just 1 computer. About .pl can check the computer key or serial number or Volume Serial Number.

EX. I give .pl program to my friend, I want .pl can use just his computer. If my friend save .pl to another computer he can't use it. ( How can I do like this Sir.)

ํํYou are very very important person,because no one help me like you. Thank You For Helpping.

Avatar image
MANAS PRADHAN posted on March 13, 2011 @ 21:46 UTC

TELL ME HOW I CAN CHECK THE SERIAL NO GIVEN TO MY PEN DRIVE AT THE TIME OF PURCHASE , I NEED 2 KNOW AND MATCH THE ORIGINAL SERIAL NO WITH THE PEN DRIVE?? KINDLY REPLY ME SOON

Avatar image
Noah (@kirsle) posted on March 13, 2011 @ 21:48 UTC

This only gets the DOS serial number given when the drive was formatted. I don't know how to get the hardware serial number given by the manufacturer though, sorry. :(

Avatar image
Niko posted on December 28, 2011 @ 11:53 UTC

I follow your step, but on Macintosh HD my serial number is 0000-0000 and on DVDs\CDs it response: "Resource busy". If I search this SN with Windows on Mac HD or optical support I read my real SN why?

Thanks for your blog that is really important.

Avatar image
Adam posted on April 22, 2012 @ 07:25 UTC

Who still has floppy drives? I do lol!

Thanks for the one-liner for FAT12/16 serial numbers. It even adds that "-" in the right place!

I really appreciate people who put the time into these little things that add to a wealth of knowledge! Keep up the good work.

(If you need a floppy drive, I've got about 20 extras...)

Avatar image
sanket posted on January 22, 2013 @ 06:58 UTC

I have a usb pen drive. I am developing an application which stores the serial number of the pen drive, so that it can check it the next time the pen drive is plugged in. Please provide me with all the steps required to find the serial number on a linux system. Thanks !

Avatar image
frank posted on February 2, 2022 @ 22:00 UTC

I got a question. I heard, and have seen it done, that some license company that put licenses on usb sticks, use the serial number or UUID, and you then can't just clone the usb drive to get a second license. so, I know this was a Russian hack, does anyone know how to do this. clone a usb stick with a license, so you can have 2 licenses...I know its been done before. please send me a link with the crack.

Avatar image
Noah (@kirsle) posted on February 2, 2022 @ 22:32 UTC

Hey @frank:

If they are using the serial numbers as described on this blog post it should be trivially easy to clone the USB drive: these serial numbers are encoded as part of the partition headers on the filesystem. When you (re)format a drive as FAT it rolls new numbers for the serial number but you could clone the whole disk including the serial easily from a Linux terminal like:

# assuming /dev/sdb1 is the FAT partition of your USB drive

# copy the full partition image to a file
dd if=/dev/sdb1 of=disk.img

# swap out your USB drive for a blank one and write the image file back onto it
# here it's assuming /dev/sdc1 is the partition of the target drive
dd if=disk.img of=/dev/sdc1

As always with dd, be sure you have your if (input file) and of (output file) in the correct spots! You wouldn't want to accidentally nuke your 'good' flash drive by putting these the wrong way around!

Or, without needing to copy disk images or reformat an existing FAT partition on the new USB stick, you could just hex edit the serial number in. See the original blog post for the filesystem offsets where the serial is kept for FAT16 and FAT32, you could copy the serial from the original disk and paste that same value into the same offset on the target disk.

Note: this is all assuming the software is using the FAT serial number for its DRM; it could be checking a lower level, hardware serial number which is a whole other kind of creature. The hardware serial may be difficult to modify on a regular USB stick, but - with an Arduino teensy USB development board or similar, you can program the USB device yourself to present itself as anything you want. For example a device like this could be programmed to present to your computer as being a USB hub with a USB keyboard+mouse attached plus a USB flash drive attached, or any other such shenanigans. So by programming it to present as a USB flash drive it could tell the computer anything it wants regarding its hardware serial number and can pretend to be the original flash drive which may be enough to defeat such licensing issues. I don't have any direct personal experience in that domain, however!

Avatar image
frank posted on February 4, 2022 @ 00:37 UTC

Man, thank you for the reply. But I just had a quick chat with the guy who did the deed. he doesn't remember what the software was called either. BUT, he says it was the hardware serial, and he said only certain types of usb sticks he could use, to copy the hardware serial... so, if anyone knows anything about finding hardware serials on usb sticks, and knows of a Russian software that can do this copy for us. Man please let me know!!!! thx

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.