Click here to Skip to main content
15,896,359 members
Articles / Programming Languages / C++

Winlogon using Mobile Disk

Rate me:
Please Sign up or sign in to vote.
4.83/5 (25 votes)
30 Nov 2007CPOL6 min read 136.7K   2.6K   89  
This is a full set of applications that can be used to logon to Windows system using mobile disk. No password typing.
// UserInfo.cpp: implementation of the CUserInfo class.
//
//////////////////////////////////////////////////////////////////////

#include "UserInfo.h"
#include "stdio.h"
#include "math.h"
#include "globals.h"

#include "Shlwapi.h"
#pragma comment(lib,"Shlwapi.lib")

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CUserInfo::CUserInfo()
{
	for(int x=0;x<sizeof(m_dwMagicValues)/sizeof(DWORD);x++)
	{
		m_dwMagicValues[x]=GetMagicFunctionValue(x+1);
	}

	memset(m_lpLocalSecurityKey,0,200);

	/*char szTmpVal[80];
	DWORD dwBufLen=99;
	HKEY hKey;

	if (RegOpenKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK,&hKey)!=ERROR_SUCCESS)
	{
		RegCreateKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK, &hKey);
		return;
	}
	
	if(ERROR_SUCCESS==RegQueryValueEx( hKey, "SecurityKey", NULL, NULL,(LPBYTE) szTmpVal, &dwBufLen))
		strcpy(m_lpLocalSecurityKey,szTmpVal);
	*/
}

CUserInfo::~CUserInfo()
{

}

BOOL CUserInfo::CheckDisk(USER_INFO user_info, int nDrive)
{
	//Print(user_info);

	if(nDrive<2) return FALSE;

	if(!CheckMagic(user_info)) return FALSE;



	if(!CheckSecurityKey(user_info.lpPassword))
	{
		//MessageBox(0,user_info.lpPassword,0,0);
		return FALSE;
	}
	
	

	char	szVolName[MAX_PATH+1], szSysName[MAX_PATH+1], lpPath[5];


	sprintf(lpPath,"%c:\\",'A'+nDrive);
	
	
	
	DWORD	dwVolSerialNumber, dwMaxComponentLen, dwFileSysFlags;

	szVolName[0]=0;

	if(!GetVolumeInformation(lpPath ,
						szVolName, MAX_PATH, 
						&dwVolSerialNumber,
						&dwMaxComponentLen, &dwFileSysFlags, 
						szSysName, MAX_PATH ))
	{
		return FALSE;
	}

	
	if(strcmp(user_info.lpVolName,szVolName))
	{
			return FALSE;
	}

	if(strcmp(user_info.lpFileSystem,szSysName))
	{
		return FALSE;
	}
	
	if(user_info.dwVolSerialNo!=dwVolSerialNumber)
	{
		return FALSE;
	}

	return TRUE;
}

BOOL CUserInfo::CheckMagic(USER_INFO user_info)
{
	/*int id=user_info.dwVolSerialNo%sizeof(m_dwMagicValues)/sizeof(m_dwMagicValues[0])+1;
	return (GetMagicFunctionValue(id)==user_info.dwMagic);
	*/

	for(int x=0;x<sizeof(m_dwMagicValues)/sizeof(DWORD);x++)
	{
		if(user_info.dwMagic==m_dwMagicValues[x])
			return TRUE;
	}

	return FALSE;
}


DWORD CUserInfo::GetMagicFunctionValue(int id)
{

	if(id>sizeof(m_dwMagicValues)/sizeof(m_dwMagicValues[0]))
		return 0xBABABABE;

	int a=87,b=119,t;
	for(int i=0;i<id*id;i++)
	{
		t=b;
		b=a+b;
		a=t;	
	}

	return (a+b)*0x2111;
}

BOOL CUserInfo::CreateDiskFile(USER_INFO user_info, char *lpPath)
{
	FILE *fp=fopen(lpPath,"wb");

	if(fp==NULL){
		return FALSE;
	}

	if(!fwrite((void *)&user_info,sizeof(USER_INFO),1,fp))
		return FALSE;
	
	fclose(fp);

	return TRUE;
}

DWORD CUserInfo::GetRandomMagic()
{
	SYSTEMTIME tm;
	GetSystemTime(&tm);
	
	return m_dwMagicValues[tm.wSecond];
}

USER_INFO CUserInfo::LoadUserInfo(char *lpPath)
{

	USER_INFO user_info;

	memset(&user_info,0,sizeof(USER_INFO));


	FILE *fp=fopen(lpPath,"rb");

	if(fp==NULL){
		printf("Can not open disk file %s\n",lpPath);
		return user_info;
	}

	fread((void *)&user_info,sizeof(USER_INFO),1,fp);

	fclose(fp);

	return user_info;
}

USER_INFO CUserInfo::Encode(USER_INFO user_info)
{
	USER_INFO usr_ret=user_info;

	int i, len;
	len=sizeof(USER_INFO);
	char *p=(char *)&usr_ret;

	for(i=0;i<len;i++)
		p[i]+=98;

	return usr_ret;
}

USER_INFO CUserInfo::Decode(USER_INFO user_info)
{
	USER_INFO usr_ret=user_info;

	int i, len;
	len=sizeof(USER_INFO);
	char *p=(char *)&usr_ret;

	for(i=0;i<len;i++)
		p[i]-=98;

	return usr_ret;
}

BOOL CUserInfo::ValidDiskPresent()
{
	char	drive[10], lpPath[100];

	DWORD dwDrives=GetLogicalDrives();

	for(int i=2;i<26;i++)
	{
		int bit=(int)pow((double)2,i);

		if(dwDrives&bit)
		{
			sprintf(drive,"%c:",'A'+i);			
			if(FIND_DRIVE_TYPE==GetDriveType(drive))
			{
				sprintf(lpPath,"%c:\\%s",'A'+i,DISK_FILE_NAME);
				if(!PathFileExists(lpPath))continue;

				USER_INFO user_info=LoadUserInfo(lpPath);
				user_info=Decode(user_info);
				if(CheckDisk(user_info,i))
					return i;
			}
		}
	}
	return 0;
}

BOOL CUserInfo::CheckSecurityKey(char *lpSecurityKey)
{
	char szTmpVal[80];
	DWORD dwBufLen=80;
	HKEY hKey;

	char temp[100], *t;
	strcpy(temp,lpSecurityKey);
	
	if(!strlen(m_lpLocalSecurityKey))
	{
		
		if (RegOpenKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK,&hKey)!=ERROR_SUCCESS)
		{
			RegCreateKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK, &hKey);
			return FALSE;
		}
	
		RegQueryValueEx( hKey, "SecurityKey", NULL, NULL,(LPBYTE) szTmpVal, &dwBufLen);
		strcpy(m_lpLocalSecurityKey,szTmpVal);
	}

	t=EncodeData(temp,strlen(temp));

	return !strcmp(t,m_lpLocalSecurityKey);
}

void CUserInfo::Print(USER_INFO user_info)
{
#ifdef _DEBUG
/*
	CString msg="", tmp;

	tmp.Format("Magic:\t%lu\n",user_info.dwMagic);
	msg+=tmp;

	tmp.Format("Name:\t%s\n",user_info.lpUserName);
	msg+=tmp;

	tmp.Format("Passowrd:\t%s\n",user_info.lpPassword);
	msg+=tmp;

	tmp.Format("Logon Name:\t%s\n",user_info.lpWindowsUser);
	msg+=tmp;

	tmp.Format("Logon Password:\t%s\n",user_info.lpWindowsPassword);
	msg+=tmp;

	tmp.Format("Domain:\t%s\n",user_info.lpDomain);
	msg+=tmp;

	tmp.Format("Volume Name:\t%s\n",user_info.lpVolName);
	msg+=tmp;

	tmp.Format("File System:\t%s\n",user_info.lpFileSystem);
	msg+=tmp;

	tmp.Format("Serial:\t%lu\n",user_info.dwVolSerialNo);
	msg+=tmp;

	tmp.Format("Logon Type:\t%lu\n",user_info.dwLogonType);
	msg+=tmp;

	MessageBox(0,msg,0,0);
*/
#endif
}


char* CUserInfo::EncodeData(void *lpData, int len)
{
	int i;

	char *p=(char *)lpData;

	for(i=0;i<len;i++)
		p[i]+=7;

	return p;
}

BOOL CUserInfo::SaveSecurityKey(char *lpKey)
{
	DWORD dwBufLen=80;
	HKEY hKey;

	//strcpy(temp,lpKey);

	if (RegOpenKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK,&hKey)!=ERROR_SUCCESS)
	{
		if(ERROR_SUCCESS!=RegCreateKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK, &hKey))
		return FALSE;
	}

	//EncodeData(temp,strlen(temp));

	if (RegSetValueEx(hKey,             // subkey handle 
          "SecurityKey",       // value name 
          0,                        // must be zero 
          REG_SZ,            // value type 
          (LPBYTE)(LPCSTR)lpKey,           // pointer to value data 
           strlen(lpKey)+ 1))       // length of value data 
		{
			//MessageBox(""); 
			return FALSE;
		}

	return TRUE;
}
	


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
United States United States
Have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).

Story books (specially Masud Rana series), tourism, songs and programming is most favorite.

Blog:
Maruf Notes
http://blog.kuashaonline.com

Comments and Discussions