2,643
社区成员




int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
char szDriverName[32] = {0};
char szDriverPath[256] = {0};
/*
if (argc != 3)
{
printf("Usage: %s <DriverFilename> <DriverPath>\r\n", argv[0]);
exit(-1);
}*/
// ·½±ãµ÷ÊÔ
strcpy(szDriverPath, "C:\\hidedriver.sys");
printf("Input driver name:\n");
scanf("%s", szDriverName);
//printf("Input driver path:\n");
//scanf("%s", szDriverPath);
// Load ntdll
HMODULE hNtdll = NULL;
hNtdll = LoadLibrary( "ntdll.dll" );
//´Óntdll.dllÀï»ñÈ¡º¯Êý
if ( !hNtdll )
{
printf( "LoadLibrary( NTDLL.DLL ) Error:%d\n", GetLastError() );
return false;
}
RtlAnsiStringToUnicodeString = (RTLANSISTRINGTOUNICODESTRING)\
GetProcAddress( hNtdll, "RtlAnsiStringToUnicodeString");
RtlFreeUnicodeString = (RTLFREEUNICODESTRING)\
GetProcAddress( hNtdll, "RtlFreeUnicodeString");
ZwLoadDriver = (ZWLOADDRIVER)\
GetProcAddress( hNtdll, "ZwLoadDriver");
// ×¢²áÇý¶¯³ÌÐò
if (-1 == LoadDriver(szDriverName, szDriverPath) )
{
printf("Error to load driver!\nProgramme will quit...\n");
return -1;
}
//return 0;
}
return nRetCode;
}
int LoadDriver(char *szDrvName, char *szDrvPath)
{
// ÐÞ¸Ä×¢²á±íÆô¶¯Çý¶¯³ÌÐò
char szSubKey[200], szDrvFullPath[256];
LSA_UNICODE_STRING buf1;
LSA_UNICODE_STRING buf2;
int iBuffLen;
HKEY hkResult;
char Data[4] = {0};
DWORD dwOK;
iBuffLen = sprintf(szSubKey, "System\\CurrentControlSet\\Services\\%s", szDrvName);
szSubKey[iBuffLen] = 0;
dwOK = RegCreateKey(HKEY_LOCAL_MACHINE, szSubKey, &hkResult);
if (dwOK != ERROR_SUCCESS)
return -1;
dwOK = RegSetValueEx(hkResult, "Type", 0, 4, (const unsigned char *)Data, 4);
dwOK = RegSetValueEx(hkResult, "ErrorControl", 0, 4, (const unsigned char *)Data, 4);
dwOK = RegSetValueEx(hkResult, "Start", 0, 4, (const unsigned char *)Data, 4);
GetFullPathName(szDrvPath, 256, szDrvFullPath, NULL);
printf("Loading driver: %s\r\n", szDrvFullPath);
memset(szSubKey, 0, sizeof(szSubKey) );
iBuffLen = sprintf(szSubKey, "\\??\\%s", szDrvFullPath);
szSubKey[iBuffLen] = 0;
dwOK = RegSetValueEx(hkResult, "ImagePath", 0, 1, (const unsigned char *)szSubKey, iBuffLen);
RegCloseKey(hkResult);
iBuffLen = sprintf(szSubKey,"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\%s", szDrvName);
szSubKey[iBuffLen] = 0;
buf2.Buffer = (PVOID)szSubKey;
buf2.Length = iBuffLen;
RtlAnsiStringToUnicodeString(&buf1, &buf2, 1);
// ¼ÓÔØÇý¶¯³ÌÐò
dwOK = ZwLoadDriver(&buf1);
if (dwOK != ERROR_SUCCESS)
{
printf("Error code is:%d\n", dwOK);
return -1;
}
RtlFreeUnicodeString(&buf1);
// ɾ³ý×¢²á±íÏî
iBuffLen = sprintf(szSubKey, "%s%s\\Enum", "System\\CurrentControlSet\\Services\\", szDrvName);
szSubKey[iBuffLen] = 0;
RegDeleteKey(HKEY_LOCAL_MACHINE, szSubKey);
iBuffLen = sprintf(szSubKey, "%s%s\\Security", "System\\CurrentControlSet\\Services\\", szDrvName);
szSubKey[iBuffLen] = 0;
RegDeleteKey(HKEY_LOCAL_MACHINE, szSubKey);
iBuffLen = sprintf(szSubKey, "%s%s", "System\\CurrentControlSet\\Services\\", szDrvName);
szSubKey[iBuffLen] = 0;
RegDeleteKey(HKEY_LOCAL_MACHINE, szSubKey);
iBuffLen = sprintf(szSubKey, "\\\\.\\%s", szDrvName);
szSubKey[iBuffLen] = 0;
return 0;
}
bool _util_load_sysfile(char *theDriverName)
{
char aPath[1024];
char aCurrentDirectory[515];
SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!sh)
{
return false;
}
GetCurrentDirectory( 512, aCurrentDirectory);
_snprintf(aPath,
1022,
"\\??\\C:\\WINDOWS\\system32\\drivers\\%s.sys",
theDriverName);
printf("loading %s\n", aPath);
SC_HANDLE rh = CreateService(sh, \
theDriverName, \
theDriverName, \
SERVICE_ALL_ACCESS, \
SERVICE_KERNEL_DRIVER, \
SERVICE_DEMAND_START, \
SERVICE_ERROR_NORMAL, \
aPath, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL);
if (!rh)
{
if (GetLastError() == ERROR_SERVICE_EXISTS)
{
printf("Service exists.\n");
// service exists
rh = OpenService(sh, \
theDriverName, \
SERVICE_ALL_ACCESS);
if (!rh)
{
CloseServiceHandle(sh);
printf("Cann't open service.\n");
return false;
}
}
else
{
CloseServiceHandle(sh);
return false;
}
}
printf("Sevice handle is: %d\n", rh);
// start the drivers
if (rh)
{
if(0 == StartService(rh, 0, NULL))
{
DWORD errcode;
errcode = GetLastError();
if(ERROR_SERVICE_ALREADY_RUNNING == errcode)
{
// no real problem
}
else
{
printf("Error to start the service: %d\n", errcode);
CloseServiceHandle(sh);
CloseServiceHandle(rh);
return false;
}
}
CloseServiceHandle(sh);
CloseServiceHandle(rh);
}
return true;
}