-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdriver_loader.c
More file actions
159 lines (131 loc) · 3.1 KB
/
driver_loader.c
File metadata and controls
159 lines (131 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "driver_loader.h"
#define SERVICE_NAME "MyProtectionDriver"
#define DEVICE_NAME "\\\\.\\MyProtectionDriver"
#ifndef __GNUC__ //Use scanf_s for Visual Studio, but not for gcc
#define scanf scanf_s
#endif
HANDLE hIoHandle = INVALID_HANDLE_VALUE;
/*
Purpose: Load the driver
Args: driver_file: Path to the driver file
Return: 0:Failed; 1: Succeed
*/
int load_driver(const char *driver_file) {
//Create service
if (Create_Service(SERVICE_NAME, driver_file) == 1) {
printf("Create_Service() succeed!\n");
}
else {
printf("Create_Service() failed!\n");
return 0;
}
//Start service
if (Start_Service(SERVICE_NAME) == 1) {
printf("Start_Service() succeed!\n");
}
else {
printf("Start_Service() failed!\n");
return 0;
}
//Get IO handle
hIoHandle = Get_IO_Handle(DEVICE_NAME);
if (hIoHandle != INVALID_HANDLE_VALUE) {
printf("Get_IO_Handle() succeed!\n");
}
else {
printf("Get_IO_Handle() failed: %i\n", GetLastError());
return 0;
}
return 1;
}
/*
Purpose: Send message to the device to start the protection
Args: pid: The process ID to protect
Return: 0:Failed; 1: Succeed
*/
int protect_process(int pid) {
char command[6] = { 0 };
command[0] = 'e'; //'e' for enable protection
*((int*)&command[1]) = pid;
if (Write_IO_Handle(hIoHandle, command, 5) == 1) {
printf("Write_IO_Handle() succeed!\n");
}
else {
printf("Write_IO_Handle() failed!\n");
return 0;
}
return 1;
}
int stop_protection() {
char command[1] = { 'd' }; //'d' for disable protection
if (Write_IO_Handle(hIoHandle, command, 1) == 1) {
printf("Write_IO_Handle() succeed!\n");
}
else {
printf("Write_IO_Handle() failed!\n");
return 0;
}
return 1;
}
void unload_driver() {
Close_IO_Handle(hIoHandle);
printf("Close_IO_Handle() called!\n");
if (Stop_Service(SERVICE_NAME) == 1) {
printf("Stop_Service() succeed!\n");
}
else {
printf("Stop_Service() failed!\n");
}
if (Delete_Service(SERVICE_NAME) == 1) {
printf("Delete_Service() succeed!\n");
}
else {
printf("Delete_Service() failed!\n");
}
}
int main(void) {
int selection;
int pid;
char driver_file[MAX_PATH];
printf(
"1: Load Driver\n"
"2: Protect Process\n"
"3: Stop Protection\n"
"4: Unload Driver\n"
"5: Bye!\n"
"\n"
);
GetCurrentDirectory(MAX_PATH, driver_file);
#ifndef __GNUC__ //Use strcat_s for Visual Studio, but not for gcc
strcat_s(driver_file, MAX_PATH, "\\protection.sys");
#else
strcat(driver_file, "\\protection.sys");
#endif
for (;;) {
scanf("%i", &selection);
switch (selection) {
case 1: //Load driver
load_driver(driver_file);
break;
case 2: //Protect process
printf("PID: ");
scanf("%i", &pid);
protect_process(pid);
break;
case 3: //Stop protection
stop_protection();
break;
case 4: //Unload driver
unload_driver();
break;
case 5: //Exit
stop_protection();
unload_driver();
return 0;
break;
default:
printf("What?\n");
}
}
return 0;
}