Click here to Skip to main content
Click here to Skip to main content

Changing volume's serial number

, 4 Mar 2004
Rate this:
Please Sign up or sign in to vote.
An article showing how to change drive's serial number

Introduction

This article will demonstrate how to change the drive's serial number on three different file systems: FAT, FAT32 and NTFS. The article's source code files come with a home made DiskSector read/write class for WinNt/2k/XP and Win9x system. The article also comes with a handy dialog based application that allows you change the serial number visually.

How it works

Usually, the serial number of a drive is generated every time you format your hard drive and there is no documented way to change it afterwards.

It is worthwhile noting that the serial number returned by the "dir" command or the GetVolumeInformation() API is not the hardware serial number that comes from the manufacturer, instead this serial number is assigned and stored in the hard drive (mostly in the boot sector) by the file system and *can* be changed by software.

Knowing that the serial number is stored in the hard drive, I started looking up information about the three different file systems: FAT, FAT32, NTFS. The most relevant part to look at was the boot sector format of each of the different file systems. Usually, the bootsector holds vital data for the integrity and the operation of the file system.

Among these fields, we list this table with the fields names, file system and the corresponding offset (in hex) of the field in the boot sector:

Table 1

Field/FS FAT FAT32 NTFS
Volume Label 0x2B 0x47 ?
File system ID 0x36 0x52 0x3
Serial number 0x27 0x43 0x48

For example, we can read an entry of this table as: "In the FAT file system, the 'Serial Number' is stored at offset 0x27 starting from the boot sector origin". It is important to know that the boot sector starts at sector zero and is 512 bytes long. The next part will explain how the ChangeSerialNumber() function can be written.

Explaining the code

First, I will explain the code flow:

  1. Open the hard drive device in order to read/write sectors.
  2. Read the boot sector
  3. Identify the file system of the drive in question
  4. Modify the serial number field according to the current file system (refer to table 1)
  5. Write back the modified boot sector
  6. Close the hard drive device
void CVolumeSerialDlg::ChangeSerialNumber(DWORD Drive, const DWORD newSerial)
{
  const max_pbsi = 3;

  struct partial_boot_sector_info
  {
    LPSTR Fs; // file system name
    DWORD FsOffs; // offset of file system name in the boot sector
    DWORD SerialOffs; // offset of the serialnumber in the boot sector
  };

  partial_boot_sector_info pbsi[max_pbsi] =
  {
   {"FAT32", 0x52, 0x43},
   {"FAT",   0x36, 0x27},
   {"NTFS",  0x03, 0x48}
  };

  TCHAR szDrive[12];

  char Sector[512];

  DWORD i;

  sprintf(szDrive, "%c:\\", Drive & 0xFF);

  if (!disk.Open(szDrive))
  {
    ShowErrorString("Could not open disk!");
    return;
  }

  // read sector
  if (!disk.ReadSector(0, Sector))
  {
    ShowErrorString("Could not read sector!");
    return;
  }

  // try to search for a valid boot sector
  for (i=0;i<max_pbsi;i++)
  {
    if (strncmp(pbsi[i].Fs, Sector+pbsi[i].FsOffs, strlen(pbsi[i].Fs)) == 0)
    {
      // we found a valid signature
      break;
    }
  }

  if (i >= max_pbsi)
  {
    MessageBox(_T("Cannot change serial number of this file system!"),
       _T("Error"), MB_ICONERROR);
    return;
  }
  
  // patch serial number
  *(PDWORD)(Sector+pbsi[i].SerialOffs) = newSerial;

  // write boot sector
  if (!disk.WriteSector(0, Sector))
  {
    ShowErrorString("Could not write sector!");
    return;
  }

  ShowErrorString("Volume serial number changed successfully!\r"
        "You might want to restart your system for changes to take effect!");
}

The core of this function is the 'partial_boot_sector_info/pbsi' table which is built from Table 1. This table will allow us to check what file system this boot sector belongs to and then it allows us to correctly patch the serial number field. It appears that if you change the serial number of an NTFS volume, changes won't take effect until you restart your system.

Also note that changing the partition's serial number will render some licensed programs useless, therefore write down the original serial number before attempting to changing it, just in case you wanted to restore it back.

Reference

Updates

  • v1.0
    • Initial version: only WinNT support
  • v1.2
    • Win9x and WinNT support added
    • Documented updated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Elias Bachaalany
Web Developer
United States United States
Elias (aka lallousx86, @0xeb) has always been interested in the making of things and their inner workings.
 
His computer interests include system programming, reverse engineering, writing libraries, tutorials and articles.
 
In his free time, and apart from researching, his favorite reading topics include: dreams, metaphysics, philosophy, psychology and any other human/mystical science.
 
Former employee of Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.
 
Elias currently works at Microsoft as a software security engineer.
 
More articles and blog posts can be found here:
 
- http://lallousx86.wordpress.com/
- http://0xeb.wordpress.com/
- http://www.hexblog.com/?author=3

Comments and Discussions

 
Questioncan it work on 2TB disk? Pinmembernhchmg15-Dec-10 22:43 
QuestionAnd how we can read another special sector? [modified] PinmemberAbolfazl Ghavidel16-Nov-10 19:53 
GeneralWorks great - thanks PinmemberAndy Mascord11-Sep-10 12:04 
GeneralError on Vista Pinmemberdarrelki6-Aug-09 22:56 
GeneralVolume Serial Number Demo Pinmemberliverwort460027-Jan-09 3:55 
QuestionExcellent Job Pinmemberccs-ed22-Jan-09 14:45 
Generalthanks Pinmemberboo-mmx13-Oct-08 8:40 
GeneralWindows Vista PinmemberCleiton A.27-Jan-07 19:24 
GeneralRe: Windows Vista PinmemberRP232-Mar-07 3:14 
GeneralDisk Partition Table PinmemberSana Nazimuddin Qureshi19-Dec-05 19:19 
GeneralRe: Disk Partition Table Pinmemberlallous19-Dec-05 20:17 
GeneralRe: Disk Partition Table PinmemberSteevej14-Jun-07 7:35 
QuestionHow to get manufacturer's ID of the hard disk? PinmemberHabeeballah16-Dec-05 21:27 
AnswerRe: How to get manufacturer's ID of the hard disk? Pinmemberlallous18-Dec-05 20:15 
AnswerRe: How to get manufacturer's ID of the hard disk? Pinmemberphilbnt6-Oct-06 1:58 
What about looking for the disk ID in the registry ?
e.g. in
HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port 0\\Scsi Bus 0\\Target Id 0\\Logical Unit Id 0\\Identifier
 
Phil
GeneralRe: How to get manufacturer's ID of the hard disk? PinmemberDouglas R. Keesler29-Dec-06 22:50 
GeneralRe: How to get manufacturer's ID of the hard disk? Pinmemberkollam200315-Mar-07 20:39 
Generaldead Hd serial recovery Pinmemberscatterlancer25-Sep-05 4:33 
GeneralRe: dead Hd serial recovery Pinmemberlallous27-Sep-05 21:56 
GeneralQuestion on VSN byte order PinmemberPiotrGutkowski29-Jun-05 9:44 
GeneralRe: Question on VSN byte order Pinmemberlallous30-Jun-05 21:06 
GeneralRe: Question on VSN byte order PinmemberPiotrGutkowski1-Jul-05 4:01 
Generalhelp me Pinmemberertaneren30-May-05 7:30 
GeneralRe: help me Pinmemberlallous30-May-05 21:26 
GeneralRe: help me Pinmemberertaneren30-May-05 23:24 
GeneralRe: help me Pinmemberlallous1-Jun-05 1:57 
GeneralGet Volume serial number of CD-R PinsussAnonymous17-Apr-05 21:39 
GeneralRe: Get Volume serial number of CD-R Pinsusssome1_genius11-Aug-05 15:28 
GeneralCould not open disk! Error PinmemberYo12345618-Feb-05 14:30 
GeneralBroken link to the Download Pinsussskkaubeu15-Feb-05 22:08 
GeneralI'm tired very much to find your program PinmemberDasaqSoft23-Jan-05 15:12 
GeneralRe: I'm tired very much to find your program Pinmemberlallous11-Feb-05 0:32 
QuestionError? Pinmemberpatient06-Aug-04 8:26 
AnswerRe: Error? Pinmemberlallous8-Aug-04 23:15 
GeneralProblem On WinME Pinsussantony.du7-Jul-04 2:24 
GeneralRe: Problem On WinME Pinmemberlallous8-Jul-04 23:11 
GeneralRe: Problem On WinME Pinmemberantony.du8-Jul-04 23:27 
GeneralRe: Problem On WinME Pinmemberlallous24-Aug-04 19:31 
QuestionGet NTFS drive volume label ? Pinmembereegiboo15-Jun-04 23:41 
AnswerRe: Get NTFS drive volume label ? Pinmemberlallous16-Jun-04 4:09 
GeneralRe: Get NTFS drive volume label ? Pinmembereegiboo16-Jun-04 22:52 
GeneralRe: Get NTFS drive volume label ? Pinmemberlallous17-Jun-04 1:12 
QuestionHow to get the manufacturer Serial number of a empty disc (CD-R)? Pinmembersuperpetta17-May-04 2:20 
AnswerRe: How to get the manufacturer Serial number of a empty disc (CD-R)? Pinsusskakan17-Apr-05 22:51 
AnswerRe: How to get the manufacturer Serial number of a empty disc (CD-R)? Pinsusssome1_genius11-Aug-05 15:23 
GeneralSimple handy perfect tool. Thanx PinmemberPeter Gliwa19-Apr-04 9:03 
GeneralGreat article, just one question PinmemberGuimaSun31-Mar-04 2:14 
GeneralRe: Great article, just one question Pinmemberlallous31-Mar-04 5:50 
Generalwhy Pinsusssaurgarg5-Mar-04 13:25 
GeneralRe: why Pinmemberrfmobile6-Mar-04 1:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 5 Mar 2004
Article Copyright 2004 by Elias Bachaalany
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid