1. Main
메인 함수입니다.
int main(int argc, char* argv[])
{
// LoadLibrary(TEXT("injdllx64.dll"));
//
// return 0;
//
SetConsoleCtrlHandler(&CtrlCHandlerRoutine, TRUE);
//
// Stop any previous trace session (if exists).
//
TraceStop();
//
// Parse command-line parameters.
//
if (argc == 2)
{
TCHAR DriverLocation[MAX_PATH];
SetupDriverName(DriverLocation, sizeof(DriverLocation));
if (!strcmp(argv[1], "-i"))
{
printf("Installing driver...\n");
if (DoInstallUninstall(TRUE))
{
printf("Driver installed!\n");
}
else
{
printf("Error!\n");
return EXIT_FAILURE;
}
}
else if (!strcmp(argv[1], "-u"))
{
printf("Uninstalling driver...\n");
DoInstallUninstall(FALSE);
return EXIT_SUCCESS;
}
}
printf("Starting tracing session...\n");
ULONG ErrorCode = TraceStart();
return ErrorCode == ERROR_SUCCESS
? EXIT_SUCCESS
: EXIT_FAILURE;
}
C++
복사
1.1 SetupDriverName
드라이버의 이름을 문자열로 생성하고 드라이버 핸들을 획득하는 함수입니다.
BOOLEAN
SetupDriverName(
_Inout_updates_bytes_all_(BufferLength) PTCHAR DriverLocation,
_In_ ULONG BufferLength
)
{
HANDLE fileHandle;
DWORD driverLocLen = 0;
//
// Get the current directory.
//
driverLocLen = GetCurrentDirectory(BufferLength,
DriverLocation);
if (driverLocLen == 0)
{
printf("GetCurrentDirectory failed! Error = %d \n", GetLastError());
return FALSE;
}
//
// Setup path name to driver file.
//
if (FAILED(StringCbCat(DriverLocation, BufferLength, TEXT("\\" DRIVER_NAME ".sys"))))
{
return FALSE;
}
//
// Insure driver file is in the specified directory.
//
if ((fileHandle = CreateFile(DriverLocation,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
printf("%s.sys is not loaded.\n", DRIVER_NAME);
//
// Indicate failure.
//
return FALSE;
}
//
// Close open file handle.
//
if (fileHandle)
{
CloseHandle(fileHandle);
}
//
// Indicate success.
//
return TRUE;
}
C++
복사