Security
This is a fun little program we put together to shut down our laptops
if the correct file was not found. Essentially when you turn on your
laptop ( or desktop ) the program in the Startup folder runs. It will
look for a file named confirm.txt on any USB drive and if it does not
find it, the program will shut the computer down.
This program isn't designed to be 100% secure but it is pretty good. If the
user attempting to access your computer knows how to boot the computer
in Safe Mode, they will be able to delete the program from the Startup
folder and boot normally. For those who don't know how to do this, this program
will prevent access :)
When you create your new project, remember to select "Empty Project":)
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <stdio.h>
BOOL MySystemShutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
#if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken))
return( FALSE );
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all
//applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE;
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR pCmdLine,
int iCmdShow)
{
//this program will look for a confirm.txt file
//in the root of each removable drive it finds
//if it finds the confirm.txt file,
//it sets "found" to TRUE
//query windows for the drives...
//GetLogicalDriveStrings will give us an array
//back with each drive
//letter as part of the array. each drive letter
//will be followed by a null terminator.
//first we send 0, 0 and windows will return
//how big the string needs to be
int size = GetLogicalDriveStrings ( 0, 0 );
//now we know how big the string needs to be,
//dynamically allocate the amount of memeory
char *pDrives = new char[ size ];
//send windows a pointer to our string (char array)
//and have it fill it with all the drive letters
GetLogicalDriveStrings ( size, pDrives );
//now in memory it will look something like this...
//pDrives[ 0 ] == 'C'
//pDrives[ 1 ] == ':'
//pDrives[ 2 ] == '\'
//pDrives[ 3 ] == 0 //NULL TERMINATOR
//pDrives[ 4 ] == 'D'
//pDrives[ 5 ] == ':'
//pDrives[ 6 ] == '\'
//pDrives[ 7 ] == 0 //NULL TERMINATOR
//pDrives[ 8 ] == 'E'
//pDrives[ 9 ] == ':'
//pDrives[ 10 ] == '\'
//pDrives[ 11 ] == 0 //NULL TERMINATOR
//pDrives[ 12 ] == 0 //second null terminator in
//a row (signals end of the array)
//so don't be confused here, this is basically an array
//with a bunch of strings back to back. after you go
//past the null terminator in the first string, there
//is another one waiting there. you know you are
//finally at the end of the stringwhen you encounter 2
//null terminators in a row
BOOL found = FALSE;
//now we are going to search through each of the drive
//names and find one that matches the usb drive
char *d = pDrives;
//while it's not null, see what drive letter it is
while ( *d )
{
//we send the drive letter to GetDriveTypes
//and windows will return what type of drive it is
//look up the GetDriveType function in msdn
//to see the possible values we can check for
if ( GetDriveType( d ) == DRIVE_REMOVABLE )
{
//we found a removable drive, it could be our
//usb lets see if we can open a password
//file at the root of it
char fileName[ MAX_PATH ];
strcpy( fileName, d );
strcat( fileName, "confirm.txt" );
FILE *pFile = fopen( fileName, "rb" );
if ( pFile )
{
fclose( pFile );
//if we found our password file, then
//we know this is our usb drive
//and we can stop searching
found = TRUE;
break;
}
}
//if this wasn't a drive, then we want to advance
//to the first null terminator, then + 1 for one
//beyond the null terminator if + 1 gives us
//another null terminator, we know we are at the
//end of the array and the while loop will stop
d += strlen( d ) + 1;
}
//clean up our memeory
delete [] pDrives;
if ( found == FALSE )
{
//if it wasn't found, do what you want :)
MySystemShutdown();
}
return 0;
}
|