Expose and Rehide a Hidden Partition

banders7 0 Tallied Votes 576 Views Share

Here are 2 pieces of code. The first makes a hidden partition accessible by creating a path to it and assigning a drive letter.

The second removes the path definition, effectively rehiding the partition.

It is advised that caution be taken in the use of this code.

The core to this code is the DefineDosDevice() function.

// Code to expose the hidden partition
#include <stdlib.h>
#include <stdio.h>
#include <dir.h>
#include <string.h>
#include <conio.h>
#include <io.h>
#include <dos.h>
#include <time.h>
#include <sys\stat.h>
#include <windows.h>
#include "bruce.i"
int main(int argc, char *argv[])
{
FILE *in,*out;
char fni[MAXDIR]="",fno[MAXDIR]="";
char read[3]="rb",write[3]="wb",append[3]="ab";
char data[256];
char drive[4]="?:\\";
int i,j,k;
char devpath[256]="\\device\\harddiskX\\partitionY";
char dev[3]="?:";
bool success;
int physdrive=0;
int drvpart=-1;

// ------------------------------------
// Physical drive numbering starts at 0
// Partition numbering starts at 1
// ------------------------------------

// Simple attempt to prevent unhiding more that 1 partition at a time
if (access("parthide.data",0) == 0)
   {
   printf("The file PARTHIDE.DATA has been found\n");
   printf("It is normally deleted when PARTHIDE.EXE runs\n");
   printf("I (abitrarily) decided no more that 1 hidden partition should be\n");
   printf("exposed at a time. Hide the currently exposed partition first\n");
   printf("Please run PARTHIDE.EXE to hide an already exposed hidden partition\n");
   exit(0);
   }
   
argc--;
if (argc == 0)
   {
   printf("Enter a physical drive number & a partition number to expose\n");
   exit(0);
   }
if (argc == 1)
   drvpart = atoi(argv[1]);
else
if (argc == 2)
   {
   physdrive = atoi(argv[1]);
   drvpart = atoi(argv[2]);
   }

sprintf(devpath,"\\device\\harddisk%d\\partition%d",physdrive,drvpart);
printf("Confirm you wish to expose Drive %d, Partition %d\n",physdrive,drvpart);
printf("Use <CTRL+C> if you wish to CANCEL!!!\n");
getch();

// Start at Z, looking for an unused drive letter to assign to the partition
for (i='Z'; i>'C'; i--)
   {
   drive[0] = i;
   k = GetDriveTypeA(drive);
   if (k == DRIVE_NO_ROOT_DIR)
      {
      dev[0] = i;
      break;
      }
   }
printf("The partition will be visible as drive %s\n",dev);

success = DefineDosDevice(
          DDD_NO_BROADCAST_SYSTEM|DDD_RAW_TARGET_PATH,
          dev,
          devpath);
if (success)
   printf("Check drive %s now. Run PARTHIDE.EXE when finished\n",dev);
else
   {
   printf("Your attempt failed miserably - RC=%d\n",GetLastError());
   exit(0);
   }
// Write data to a file for parthide.exe. Ensure it gets correct data
sprintf(data,"%s %d %d\r\n",dev,physdrive,drvpart);
out = fopen("parthide.data","wb");
fputs(data,out);
fclose(out);

return 0;
}

// Code to rehide the exposed partition
#include <stdlib.h>
#include <stdio.h>
#include <dir.h>
#include <string.h>
#include <conio.h>
#include <io.h>
#include <dos.h>
#include <time.h>
#include <sys\stat.h>
#include <windows.h>
#include "bruce.i"
int main(int argc, char *argv[])
{
FILE *in,*out;
char fni[MAXDIR]="",fno[MAXDIR]="";
char read[3]="rb",write[3]="wb",append[3]="ab";
char data[256];
int i,j,k;
char devpath[256]="\\device\\harddisk0\\partition2";
char dev[20]="z:";
bool success;

if (access("parthide.data",0) != 0)
   {
   printf("The required file, PARTHIDE.DATA, created by PARTSHOW.EXE was not found\n");
   printf("The data format is: Drive letter (Z:), Physical drive #, Partition #\n");
   printf("e.g., U: 0 2 on a CR/LF terminated line\n");
   printf("The drive letter, disk number & partition number must match\n");
   printf("the specifications used when the partition was exposed.\n");
   exit(0);
   }

in = fopen("parthide.data","rb");
fgets(data,80,in);
fclose(in);

data[strlen(data)-2] = '\0'; // Remove CRLF
// word() is a private string function
strcpy(dev,word(1,data));
sprintf(devpath,"\\device\\harddisk%s\\partition%s",word(2,data),word(3,data));

success = DefineDosDevice(
          DDD_EXACT_MATCH_ON_REMOVE|
          DDD_REMOVE_DEFINITION|
          DDD_RAW_TARGET_PATH,
          dev,
          devpath);

if (success)
   {
   printf("Check that drive %s is GONE!\n",dev);
   printf("And that the partition is hidden again\n");
   unlink("parthide.data");
   }
else
   printf("Your attempt failed miserably - RC=%d\n",GetLastError());
return 0;
}
banders7 1 Newbie Poster

As it is, the show code makes the hidden partition visible to command line operations only. If you wish to make it visible to Windows Explorer, remove the DDD_NO_BROADCAST_SYSTEM flag.

success = DefineDosDevice(DDD_RAW_TARGET_PATH,dev,devpath);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.