user99545
user99545

Reputation: 1173

Writing raw data "signature" to disk without corrupting filesystem

I am trying to create a program that will write a series of 10-30 letters/numbers to a disk in raw format (not to a file that the OS will read). Perhaps to make my attempt clearer, if you were to open the disk in a hex editor, you would see the 10-30 letters/numbers but a file manager such as Windows Explorer would not see it (because the data is not a file).

My goal is to be able to "sign" a disk with a series of characters and to be able to read and write that "signature" in my program. I understand NTFS signs its partitions with a NTFS flag as do other file systems and I have to be careful to not write my signature to any of those critical parts.

Are there any libraries in C++/C that could help me write at a low level to a disk and how will I know a safe sector to start writing my signature to? To narrow this down, it only needs to be able to write to NTFS, FAT, FAT32, FAT16 and exFAT file systems and run on Windows. Any links or references are greatly appreciated!

Edit: After some research, USB drives allow only 1 partition without applying hacking tricks that would unfold further problems for the user. This rules out the "partition idea" unfortunately.

Upvotes: 0

Views: 1247

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490018

Though I think this is generally a pretty poor idea, the obvious way to do it would be to mark a cluster as "bad", then use it for your own purposes.

Problems with that:

  1. Marking it as bad is non-trivial (on NTFS bad clusters are stored in a file named something like $BadClus, but it's not accessible to user code (and I'm not even sure it's accessible to a device driver either).
  2. There are various programs to scan for (and attempt to repair) bad clusters/sectors. Since we don't even believe this one is really bad, almost any of these that works at all will find that it's good and put it back into use.
  3. Most of the reasons people think of doing things like this (like tying a particular software installation to a particular computer) are pretty silly anyway.
  4. You'd have to scan through all the "bad" sectors to see if any of them contained your signature.

Upvotes: 1

Nick
Nick

Reputation: 6846

First, as the commenters said, you should look at why you're trying to do this, and see if it's really a good idea. Most apps which try to circumvent the normal UI the user has for using his/her computer are "bad", in various ways.

That said, you could try finding a well-known file which will always be on the system and has some slack in the block size for the disk, and write to the slack. I don't think most filesystems would care about extra data in the slack, and it would probably even be copied if the file happens to be relocated (more efficient to copy the whole block at the disk level).

Just a thought; dunno how feasible it would be, but seems like it could work in theory.

Upvotes: 2

DanRedux
DanRedux

Reputation: 9349

This is very dangerous, however, zero-fill programs do the same thing so you can google how to wipe your hard drive with zero's in C++.

The hard part is finding a place you KNOW is unused and won't be used.

Upvotes: 0

Related Questions