📄 usbcontrol.cpp
字号:
#include "stdafx.h"
#include "USBControl.h"
#include "IOCTLCMD.H"
#define DOMAIN_NAME L"MYDOMAIN"
#define EDDrv_DRV _T("EDDrv.SYS")
static bool IsDriverRunning=FALSE;
static bool IsDomainValid=FALSE;
static bool IsDriverStartedOnce=FALSE;
static DWORD FinalUSBData;
#define SYS_NAME _T("EDDrv")
TCHAR DriverPath[ 256 ];
HANDLE SysHandle;
CUSBControl::CUSBControl()
{
//Start
this->m_szModuleName = _T("USBControl");
this->m_szServiceName = _T("USBControl");
this->m_szSrvDisplayName = _T("USB Control Service");
this->m_szSrvDependencies = _T("");
this->m_szDeviceChangeEventName = _T("USBCNTL_Device_Change");
this->m_hDeviceChange = NULL;
this->m_hAuthenticationThread = NULL;
//logger.Init();
//End
}
CUSBControl::~CUSBControl()
{
//Start
//End
}
/*//Initialization function
BOOL CUSBControl::Init()
{
//Start
//logger.LOG_DBG(L"CUSBControl::Init Start");
//logger.LOG_DBG(L"CUSBControl::Init End");
return TRUE;
//End
}*/
BOOL CUSBControl::Run(DWORD USBDrives)
{
//Start
//logger.LOG_DBG(L"CUSBControl::Init Start");
OutputDebugString(L"CUSBControl::Run Start");
HANDLE pHandles[2];
DWORD dwCnt = 0;
this->USBDrives = USBDrives;
//Initialize Authentication THread
//if(!this->InitAuthenticationThread())
//{
// OutputDebugString(L"InitAuthenticationThread Failed");
// return FALSE;
//}
//Creation of Device Change Event
this->m_hDeviceChange = new CUSBEvent();
if(this->m_hDeviceChange == NULL)
{
//logger.LOG_DBG(L"m_hDeviceEvent NULL");
return FALSE;
}
else if(!this->m_hDeviceChange->Create(this->m_szDeviceChangeEventName,FALSE))
{
return FALSE;
}
//Handles of Stop Event and Device Change Event
pHandles[dwCnt++] = m_hUSBControlStopEvent->GetEventHandle();
pHandles[dwCnt++] = this->m_hDeviceChange->GetEventHandle();
while(TRUE)
{
//Wait for Stop event or Device Change Event
DWORD dwRet = ::WaitForMultipleObjects(dwCnt,pHandles,FALSE,INFINITE);
if(dwRet = WAIT_OBJECT_0) //USB Control Stop Event Obtained
{
//Processing for stop event
MessageBox(NULL,L"Inside Run : Recieved Service Stop Event",L"Information",MB_OK);
break;
}
else if( dwRet = (WAIT_OBJECT_0+1))
{
//Processing for Device Change Event
MessageBox(NULL,L"Inside Run : Recieved Device Change Event",L"Information",MB_OK);
if(!this->InitAuthenticationThread())
{
OutputDebugString(L"InitAuthenticationThread Failed");
return FALSE;
}
}
else // Error condition
{
DWORD dwErr = ::GetLastError();
if(dwRet == WAIT_FAILED)
{
//logger.LOG_ERR(L"CUSBControl::Run :: WaitForMultipleObjects Failed",dwErr);
return FALSE;
}
else if(dwRet == WAIT_TIMEOUT)
{
//logger.LOG_DBG(L"CUSBControl::Run :: WaitForMultipleObjects timeout Failed");
}
}
}
//Perform Cleanup
if(this->m_hDeviceChange != NULL)
{
delete this->m_hDeviceChange;
this->m_hDeviceChange = NULL;
}
//logger.LOG_DBG(L"CUSBControl::Init End");
OutputDebugString(L"CUSBControl::Run End");
return TRUE;
//End
}
//Set Device Change Event
BOOL CUSBControl::SetDevChangeEvent()
{
//Start
//logger.LOG_DBG(L"CUSBControl::SetDevChangeEvent Start");
OutputDebugString(L"CUSBControl::SetDevChangeEven Start");
if(m_hDeviceChange != NULL)
{
if(!m_hDeviceChange->SetEvent())
{
//logger.LOG_DBG(L"CUSBControl::SetDevChangeEvent :: m_hDeviceChange->SetEvent() Failed");
return FALSE;
}
}
else{
//logger.LOG_DBG(L"CUSBControl::SetDevChangeEvent :: m_hDeviceChange NULL");
return FALSE;
}
return TRUE;
//logger.LOG_DBG(L"CUSBControl::SetDevChangeEvent End");
OutputDebugString(L"CUSBControl::SetDevChangeEven Start");
//End
}
//Initialize Authentication Thread
BOOL CUSBControl::InitAuthenticationThread()
{
//Start
OutputDebugString(L"CUSBControl::InitAuthenticationThread Start");
if(!BuildDomainNames())
{
OutputDebugString(L"BuildDomainNames Failed");
return FALSE;
}
unsigned id;
this->m_hAuthenticationThread = (HANDLE)::_beginthreadex(NULL,0,CUSBControl::ADAuthenticationThread,this,0,&id);
if(this->m_hAuthenticationThread == 0)
{
OutputDebugString(L"CUSBControl::InitAuthenticationThread beginthreadex failed");
return FALSE;
}
OutputDebugString(L"CUSBControl::InitAuthenticationThread End");
return TRUE;
//End
}
//Active Directory Authentication Thread
unsigned __stdcall CUSBControl::ADAuthenticationThread(void *lpParameter)
{
//Start
OutputDebugString(L"CUSBControl::ADAuthenticationThread Start");
CUSBControl *csubControl = (CUSBControl*)lpParameter;
WCHAR wszExecutableFilePath[_MAX_PATH];
WCHAR wszDrive[_MAX_DRIVE];
WCHAR wszDir[_MAX_DIR];
WCHAR wszPath[_MAX_PATH];
BOOL dwRet = FALSE;
PLUID sessions;
ULONG count;
NTSTATUS retVal;
WCHAR szMsg[256];
ULONG i =0;
if (!::GetModuleFileNameW(NULL, wszPath, MAX_PATH))
{
DWORD dwErr = ::GetLastError();
//LOG_ERRC(_T("GetModuleFileNameW Failed"),GetLastError());
return dwErr;
}
// The directory of the printer information file is acquired.
_wsplitpath_s( wszPath, wszDrive, _MAX_DRIVE, wszDir, _MAX_DIR, NULL, 0, NULL, 0);
_wmakepath_s( wszExecutableFilePath, _MAX_PATH, wszDrive, wszDir, EDDrv_DRV, NULL );
OutputDebugString(wszExecutableFilePath);
memset(DriverPath,0x00,256*sizeof(TCHAR));
wcscpy(DriverPath,wszExecutableFilePath);
retVal = LsaEnumerateLogonSessions(&count,&sessions);
if(retVal != STATUS_SUCCESS)
{
OutputDebugString(L"CUSBControl::ADAuthenticationThread LsaEnumerateLogonSessions Failed");
return dwRet;
}
_swprintf(szMsg,L"No of sessions returned :: %lu",count);
OutputDebugString(szMsg);
for (i =0;i < count; i++)
{
GetSessionData (&sessions[i],csubControl->USBDrives, csubControl);
}
//Deb
//Deb
LsaFreeReturnBuffer(sessions);
OutputDebugString(L"CUSBControl::ADAuthenticationThread End");
dwRet = TRUE;
return dwRet;
//End
}
//Get Session Data for all active sessions
VOID CUSBControl::GetSessionData(PLUID session,DWORD USBDrives, CUSBControl *pUsb)
{
PSECURITY_LOGON_SESSION_DATA sessionData = NULL;
NTSTATUS retval;
WCHAR buffer[256];
WCHAR pPrintBuf[256];
WCHAR *usBuffer;
int usLength;
BOOL IsFixedDomain = FALSE;
char str[30];
DWORD CurDrives = 0,LUSBDrives=0,PrevDrives=0;
Sleep(5000);
//sprintf(str,"\n GetSessionData : USB Drive=%lu",(pUsb->getUSBDrives()));
//OutputDebugStringA(str);
PrevDrives = pUsb->getUSBDrives();
CurDrives=GetLogicalDrives();
LUSBDrives=(PrevDrives^CurDrives);
sprintf(str,"\n At Device Arrival XXXXXXXX: Prev Drive = %lu,Cur Drive=%lu,USB Drive=%lu",PrevDrives,CurDrives,LUSBDrives);
FinalUSBData=LUSBDrives;
OutputDebugStringA(str);
// Check for a valid session.
if (!session ) {
OutputDebugString(L"Error - Invalid logon session identifier.\n");
return;
}
// Get the session information.
retval = LsaGetLogonSessionData (session, &sessionData);
if (retval != STATUS_SUCCESS) {
// An error occurred. Tell the world.
_swprintf(pPrintBuf,L"LsaGetLogonSessionData failed %lu \n",LsaNtStatusToWinError(retval));
OutputDebugString(pPrintBuf);
// If session information was returned, free it.
if (sessionData) {
LsaFreeReturnBuffer(sessionData);
}
return;
}
// Determine whether there is session data to parse.
if (!sessionData) { // no data for session
OutputDebugString(L"Invalid logon session data. \n");
return;
}
if (sessionData->UserName.Buffer != NULL) {
// Get the user name.
usBuffer = (sessionData->UserName).Buffer;
usLength = (sessionData->UserName).Length;
if(usLength < 256)
{
lstrcpyn (buffer, usBuffer, usLength);
lstrcat (buffer,L"");
}
else
{
OutputDebugString(L"\nUser name too long for buffer. Exiting program.");
return;
}
_swprintf(pPrintBuf,L"user %s was authenticated ",buffer);
OutputDebugString (pPrintBuf);
}
else
{
OutputDebugString (L"\nMissing user name.\n");
LsaFreeReturnBuffer(sessionData);
return;
}
if ((SECURITY_LOGON_TYPE) sessionData->LogonType == Interactive) {
OutputDebugString(L"interactively ");
}
if (sessionData->AuthenticationPackage.Buffer != NULL) {
// Get the authentication package name.
usBuffer = (sessionData->AuthenticationPackage).Buffer;
usLength = (sessionData->AuthenticationPackage).Length;
if(usLength < 256)
{
lstrcpyn (buffer, usBuffer, usLength);
lstrcat (buffer,L"");
}
else
{
OutputDebugString(L"\nAuthentication package too long for buffer. Exiting program.");
return;
}
_swprintf(pPrintBuf,L"using %s ",buffer);
OutputDebugString(pPrintBuf);
} else {
OutputDebugString (L"\nMissing authentication package.");
LsaFreeReturnBuffer(sessionData);
return;
}
if (sessionData->LogonDomain.Buffer != NULL) {
// Get the domain name.
usBuffer = (sessionData->LogonDomain).Buffer;
usLength = (sessionData->LogonDomain).Length;
if(usLength < 256)
{
lstrcpyn (buffer, usBuffer, usLength);
lstrcat (buffer,L"");
}
else
{
OutputDebugString(L"\nLogon domain too long for buffer. Exiting program.");
return;
}
_swprintf(pPrintBuf,L"in the %s domain.\n",buffer);
OutputDebugString(pPrintBuf);
//These below statement is for test purpose!!!!WARNING!!!
//_tcscpy(buffer,L"MYSTRING");
//DriverStart();
//DriverEnable(FALSE);
//IsDriverRunning=TRUE;
//drive = pUsb->getUSBDrives();
if(_tcsicmp(buffer,DOMAIN_NAME) == 0)
{
IsDomainValid=TRUE;
OutputDebugString(L"\nDomain Name Matches!Congrats!!!");
if(IsDriverRunning==FALSE)
{
DriverStart();
DriverEnable(TRUE);
IsDriverRunning=TRUE;
}
else
{
DriverEnable(TRUE);
}
}
else
{
if(IsDriverRunning==FALSE)
{
DriverStart(); //Later addition;
DriverEnable(FALSE);
IsDriverRunning=TRUE;
}
else
{
DriverEnable(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -