Click here to Skip to main content
15,881,715 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.1K   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.
// SetupDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Setup.h"
#include "SetupDlg.h"
#include "..\globals.h"
#include <winsvc.h>
#include "Shlwapi.h"
#include "..\UserInfo.h"
#pragma comment(lib,"Shlwapi.lib")

#include "stdio.h"

//#pragma comment(lib,ODBCINST.LIB)

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


#define DEV_MON_NAME "DevMon.exe"
#define GINA_NAME "KLGinaEx.dll"
 
/////////////////////////////////////////////////////////////////////////////
// CSetupDlg dialog

CSetupDlg::CSetupDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupDlg)
	m_szKey = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSetupDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetupDlg)
	DDX_Text(pDX, IDC_SKEY_EDIT, m_szKey);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSetupDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INSTALL_BUTTON, OnInstallButton)
	ON_BN_CLICKED(IDC_UNINST_BUTTON, OnUninstButton)
	ON_BN_CLICKED(IDC_CLEAN_BUTTON, OnCleanButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetupDlg message handlers

BOOL CSetupDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	char szName[100];

	DWORD dwSize=99;
	GetComputerName((LPTSTR)szName,&dwSize);

	UpdateData(FALSE);

	char lpUser[200]="";
	
	dwSize=199;
		
	GetUserName(lpUser,&dwSize);

	if(dwSize<200)
	{
		SetDlgItemText(IDC_USER_NAME_STATIC,lpUser);
	}

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSetupDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSetupDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSetupDlg::OnInstallButton() 
{
	UpdateData();

	int na=0;
	for(int x=0;x<m_szKey.GetLength();x++)
	{
		if(!isalnum(m_szKey[x]))
		{
			na=1;
			break;
		}
	}

	if(m_szKey.GetLength()<5 || m_szKey.GetLength()>20 || na )
	{
		MessageBox("Security key length should be between 5 and 20\t\t\nand all characters must be alpha numeric\t\t","Error",MB_OK|MB_ICONSTOP);

		CWnd *pWnd=GetDlgItem(IDC_SKEY_EDIT);
		pWnd->SetFocus();
		return;
	}

	InstallClient();
	exit(0);

}



BOOL CSetupDlg::InstallClient()
{

	UpdateData();

	CUserInfo ui;

	if(!ui.SaveSecurityKey(ui.EncodeData((LPSTR)(LPCSTR)m_szKey,m_szKey.GetLength())))
	{
		MessageBox("Can not install\t\t","Error",MB_OK|MB_ICONSTOP);
		return FALSE;
	}

	char szSys32Path[256];

	CString szSystem32Path, szCopyTo;

	DWORD dwBufLen=80;
	HKEY hKey;


	GetSystemDirectory(szSys32Path,255);

	szSystem32Path.Format("%s",szSys32Path);

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

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



		HRSRC hRes;
		HGLOBAL hg;

		CFile file;
//////////////////////////////////////////////////////////

		hRes=FindResource(NULL,MAKEINTRESOURCE(IDR_DEV_MON_EXE),"EXE");

		hg=LoadResource(NULL,hRes);

		szCopyTo.Format("%s\\%s",szSys32Path,DEV_MON_NAME);

		if(hg==NULL)
		{
			MessageBox("Can not load resource from setup.exe\nInfected with virus?","Error",MB_OK|MB_ICONSTOP);
			return FALSE;
		}
	
		if(!file.Open((LPCSTR)szCopyTo,CFile::modeCreate|CFile::modeWrite))
		{
			MessageBox("Can not copy DeviceMonitor\t\t","File Copy Error",MB_OK|MB_ICONSTOP);

		}

		file.Write(hg,SizeofResource(NULL, hRes)); 
		file.Close();
		

/////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////

		hRes=FindResource(NULL,MAKEINTRESOURCE(IDR_GINA_DLL_EXE),"EXE");

		hg=LoadResource(NULL,hRes);

		szCopyTo.Format("%s\\%s",szSys32Path,GINA_NAME);

		if(hg==NULL)
		{
			MessageBox("Can not load resource from setup.exe\nInfected with virus?","Error",MB_OK|MB_ICONSTOP);
			return FALSE;
		}
	
		if(!file.Open((LPCSTR)szCopyTo,CFile::modeCreate|CFile::modeWrite))
		{
			MessageBox("Can not install PCLock\t\t","File Copy Error",MB_OK|MB_ICONSTOP);
			return FALSE;

		}

		file.Write(hg,SizeofResource(NULL, hRes)); 
		file.Close();
		

////////////////////////////////////////////////////////////////
		if (RegOpenKey(HKEY_LOCAL_MACHINE, KEY_WinLogon,&hKey)!=ERROR_SUCCESS)
		{
			if(ERROR_SUCCESS!=RegCreateKey(HKEY_LOCAL_MACHINE, KEY_WinLogon, &hKey))
			{
				//MessageBox("can not create registry entry..","");				
				return FALSE;
			}
		}

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

/////////////////////////////////////////////////////////


	}catch(...)
	{
		return FALSE;
	}

	RegCloseKey(hKey);

	if(IDYES==MessageBox("Installation finished\nSystem must be restarted.\nPlease restart the system when you are ready\t\t","Done",MB_OK|MB_ICONINFORMATION))
	{
		//ExitWindowsEx(EWX_REBOOT|EWX_FORCEIFHUNG,0);
	}

	return TRUE;
}




void CSetupDlg::OnUninstButton() 
{
	if(IDOK!=MessageBox("Are you sure you want to uninstall PCLock?\t\t","Confirm uninstallation",MB_OKCANCEL|MB_ICONQUESTION))
		return;

	UpdateData();
	CUserInfo ui;

	if(!ui.CheckSecurityKey((LPSTR)(LPCSTR)m_szKey))
	{
		MessageBox("Can not uninstall\t\t\nSecurity Key you provide is invalid\t","Error",MB_OK|MB_ICONSTOP);
		CWnd *pWnd=GetDlgItem(IDC_SKEY_EDIT);
		pWnd->SetFocus();
		return;
	}

	HKEY hKey;

		if (RegOpenKey(HKEY_LOCAL_MACHINE, KEY_WinLogon,&hKey)!=ERROR_SUCCESS)
		{
			if(ERROR_SUCCESS!=RegCreateKey(HKEY_LOCAL_MACHINE, KEY_WinLogon, &hKey))
			{
				MessageBox("Can not uninstall PCLock\t\t\nAre you administrator?\t\t","Error 201",MB_OK|MB_ICONSTOP); 
				return;
			}
		}

		if (ERROR_SUCCESS!=RegDeleteValue(hKey,"GinaDLL"))
		{
			MessageBox("Can not uninstall PCLock\t\t\nAre you administrator?\t\t","Error 201",MB_OK|MB_ICONSTOP); 
			return;
		}

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

		if (ERROR_SUCCESS!=RegDeleteValue(hKey,"DeviceMon"))
		{
			MessageBox("Can not uninstall DeviceMonitor\t\t","Error 202",MB_OK|MB_ICONSTOP); 
			return;
		}

		if (ERROR_SUCCESS!=RegOpenKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK,&hKey))
		{
			if(ERROR_SUCCESS!=RegCreateKey(HKEY_LOCAL_MACHINE, KEY_PCLOCK, &hKey))
			{
				MessageBox("Can not remove Security Key","Error 203",MB_OK|MB_ICONSTOP);

				return;
			}
		}

		if (ERROR_SUCCESS!=RegDeleteValue(hKey,"SecurityKey"))
		{
			MessageBox("Can not remove Security Key\t\t?","Error 204",MB_OK|MB_ICONSTOP); 
			return;
		}

		MessageBox("Uninstall Complete\t\t\nSystem must be restarted\nAfter restart run setup for an optioanl clean up\t\t","",MB_OK|MB_ICONINFORMATION);

		exit(0);
}

void CSetupDlg::OnCleanButton() 
{
	char szSys32Path[256];

	CString szSystem32Path, szCopyTo;

	DWORD dwBufLen=80;

	BOOL err=FALSE;
	GetSystemDirectory(szSys32Path,255);

	szSystem32Path.Format("%s",szSys32Path);


	szCopyTo.Format("%s\\%s",szSys32Path,GINA_NAME);

	if(PathFileExists((LPCSTR)szCopyTo))
	if(!DeleteFile((LPCSTR)szCopyTo))
	{
		MessageBox("Can not remove GINA DLL\t\t","Error",MB_OK|MB_ICONSTOP);
		err=TRUE;
	}

	szCopyTo.Format("%s\\%s",szSys32Path,DEV_MON_NAME);

	if(PathFileExists((LPCSTR)szCopyTo))
	if(!DeleteFile((LPCSTR)szCopyTo))
	{
		MessageBox("Can not remove Device Monitor\t\t","Error",MB_OK|MB_ICONSTOP);
		err=TRUE;
	}

	if(!err)
	{
		MessageBox("Clean up complete\nYou do not need to restart the system\t\t","Clean up",MB_OK|MB_ICONINFORMATION);
	}
	else
	{
		MessageBox("One or more files can not be removed.\nPlease restart and try again.\t\t","Error",MB_OK|MB_ICONSTOP);
	}

}

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