| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <windows.h> |
| 25 |
#include <winioctl.h> |
| 26 |
|
| 27 |
#include "dhahelper.h" |
| 28 |
|
| 29 |
#define DRV_NAME "DhaHelper" |
| 30 |
#define DRV_FILENAME "dhahelper.sys" |
| 31 |
#define DRV_VERSION "1.0" |
| 32 |
|
| 33 |
static void print_last_error(const char *s) |
| 34 |
{ |
| 35 |
LPTSTR lpMsgBuf; |
| 36 |
|
| 37 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 38 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 39 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 40 |
NULL, GetLastError(), |
| 41 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 42 |
(LPTSTR) & lpMsgBuf, 0, NULL); |
| 43 |
|
| 44 |
lpMsgBuf[strlen(lpMsgBuf)-2] = '\0'; |
| 45 |
|
| 46 |
printf("%s (%lu -> %s)\n", s, (unsigned long)GetLastError(), lpMsgBuf); |
| 47 |
} |
| 48 |
|
| 49 |
static int dha_start(void) |
| 50 |
{ |
| 51 |
SC_HANDLE hSCManager = NULL; |
| 52 |
SC_HANDLE hService = NULL; |
| 53 |
int result; |
| 54 |
|
| 55 |
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 56 |
hService = OpenService(hSCManager, DRV_NAME, SERVICE_ALL_ACCESS); |
| 57 |
|
| 58 |
result = StartService(hService, 0, NULL); |
| 59 |
if(!result) print_last_error("Error while starting service"); |
| 60 |
|
| 61 |
CloseServiceHandle(hService); |
| 62 |
CloseServiceHandle(hSCManager); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int dha_stop(void) |
| 68 |
{ |
| 69 |
SC_HANDLE hSCManager = NULL; |
| 70 |
SC_HANDLE hService = NULL; |
| 71 |
SERVICE_STATUS ServiceStatus; |
| 72 |
int result; |
| 73 |
|
| 74 |
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 75 |
hService = OpenService(hSCManager, DRV_NAME, SERVICE_ALL_ACCESS); |
| 76 |
|
| 77 |
result = ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus); |
| 78 |
if(!result) print_last_error("Error while stopping service"); |
| 79 |
|
| 80 |
CloseServiceHandle(hService); |
| 81 |
CloseServiceHandle(hSCManager); |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int dha_install(void) |
| 87 |
{ |
| 88 |
SC_HANDLE hSCManager = NULL; |
| 89 |
SC_HANDLE hService = NULL; |
| 90 |
char szPath[MAX_PATH]; |
| 91 |
int result = 0; |
| 92 |
|
| 93 |
GetWindowsDirectory(szPath, MAX_PATH); |
| 94 |
strcpy(szPath + strlen(szPath), "\\system32\\drivers\\" DRV_FILENAME); |
| 95 |
|
| 96 |
if(!CopyFile(DRV_FILENAME, szPath, FALSE)) |
| 97 |
{ |
| 98 |
printf("Copying " DRV_FILENAME " failed.\nEither " DRV_FILENAME " is not in the current directory or you lack sufficient\nprivileges to write to %s.", szPath); |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 104 |
hService = CreateService(hSCManager, |
| 105 |
DRV_NAME, |
| 106 |
DRV_NAME, |
| 107 |
SERVICE_ALL_ACCESS, |
| 108 |
SERVICE_KERNEL_DRIVER, |
| 109 |
SERVICE_SYSTEM_START, |
| 110 |
SERVICE_ERROR_NORMAL, |
| 111 |
szPath, |
| 112 |
NULL, |
| 113 |
NULL, |
| 114 |
NULL, |
| 115 |
NULL, |
| 116 |
NULL); |
| 117 |
if(!hService) |
| 118 |
{ |
| 119 |
print_last_error("Unable to register DhaHelper Service"); |
| 120 |
result = -1; |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
printf("Success!\n"); |
| 125 |
result = 0; |
| 126 |
} |
| 127 |
|
| 128 |
CloseServiceHandle(hService); |
| 129 |
CloseServiceHandle(hSCManager); |
| 130 |
|
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
static int dha_uninstall(void) |
| 135 |
{ |
| 136 |
SC_HANDLE hSCManager = NULL; |
| 137 |
SC_HANDLE hService = NULL; |
| 138 |
char szPath[MAX_PATH]; |
| 139 |
int result = 0; |
| 140 |
|
| 141 |
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 142 |
hService = OpenService(hSCManager, DRV_NAME, SERVICE_ALL_ACCESS); |
| 143 |
|
| 144 |
dha_stop(); |
| 145 |
|
| 146 |
result = DeleteService(hService); |
| 147 |
if(!result) print_last_error("Error while deleting service"); |
| 148 |
|
| 149 |
CloseServiceHandle(hService); |
| 150 |
CloseServiceHandle(hSCManager); |
| 151 |
|
| 152 |
GetWindowsDirectory(szPath, MAX_PATH); |
| 153 |
strcpy(szPath + strlen(szPath), "\\system32\\drivers\\" DRV_FILENAME); |
| 154 |
DeleteFile(szPath); |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int dha_detect(int* result) |
| 160 |
{ |
| 161 |
HANDLE hDriver; |
| 162 |
*result = 0; |
| 163 |
|
| 164 |
hDriver = CreateFile("\\\\.\\DhaHelper", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 165 |
if(hDriver == INVALID_HANDLE_VALUE) |
| 166 |
{ |
| 167 |
hDriver = CreateFile("\\\\.\\DhaHelper", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 168 |
if(hDriver != INVALID_HANDLE_VALUE) |
| 169 |
{ |
| 170 |
CloseHandle(hDriver); |
| 171 |
hDriver = INVALID_HANDLE_VALUE; |
| 172 |
*result = 1; |
| 173 |
} |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
CloseHandle(hDriver); |
| 178 |
hDriver = INVALID_HANDLE_VALUE; |
| 179 |
*result = 1; |
| 180 |
} |
| 181 |
|
| 182 |
printf(DRV_NAME "%sfound.\n", *result ? " " : " not "); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
static int dha_enable(void) |
| 187 |
{ |
| 188 |
HANDLE hDriver; |
| 189 |
DWORD BytesReturned; |
| 190 |
int iError; |
| 191 |
int result = 0; |
| 192 |
|
| 193 |
hDriver = CreateFile("\\\\.\\DhaHelper", |
| 194 |
GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 195 |
if(hDriver == INVALID_HANDLE_VALUE) |
| 196 |
{ |
| 197 |
printf("Couldn't access " DRV_NAME " driver; Please ensure driver is installed/loaded."); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
iError = DeviceIoControl(hDriver, IOCTL_DHAHELPER_ENABLEDIRECTIO, NULL, 0, NULL, 0, &BytesReturned, NULL); |
| 202 |
if(!iError) |
| 203 |
{ |
| 204 |
print_last_error("DhaHelper: error %d occurred in IOCTL_DHAHELPER_ENABLEDIRECTIO"); |
| 205 |
result = -1; |
| 206 |
} |
| 207 |
else |
| 208 |
printf("I/O ports have been enabled.\n"); |
| 209 |
|
| 210 |
CloseHandle(hDriver); |
| 211 |
hDriver = INVALID_HANDLE_VALUE; |
| 212 |
|
| 213 |
return result; |
| 214 |
} |
| 215 |
|
| 216 |
static int dha_disable(void) |
| 217 |
{ |
| 218 |
HANDLE hDriver; |
| 219 |
DWORD BytesReturned; |
| 220 |
int iError; |
| 221 |
int result = 0; |
| 222 |
|
| 223 |
hDriver = CreateFile("\\\\.\\DhaHelper", |
| 224 |
GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 225 |
if(hDriver == INVALID_HANDLE_VALUE) |
| 226 |
{ |
| 227 |
printf("Couldn't access " DRV_NAME " driver; Please ensure driver is installed/loaded."); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
iError = DeviceIoControl(hDriver, IOCTL_DHAHELPER_DISABLEDIRECTIO, NULL, 0, NULL, 0, &BytesReturned, NULL); |
| 232 |
if(!iError) |
| 233 |
{ |
| 234 |
print_last_error("DhaHelper: error %d occurred in IOCTL_DHAHELPER_DISABLEDIRECTIO"); |
| 235 |
result = -1; |
| 236 |
} |
| 237 |
else |
| 238 |
printf("I/O ports have been disabled.\n"); |
| 239 |
|
| 240 |
CloseHandle(hDriver); |
| 241 |
hDriver = INVALID_HANDLE_VALUE; |
| 242 |
|
| 243 |
return result; |
| 244 |
} |
| 245 |
|
| 246 |
int main(int argc,char* argv[]) |
| 247 |
{ |
| 248 |
SC_HANDLE hSCManager = NULL; |
| 249 |
SC_HANDLE hService = NULL; |
| 250 |
|
| 251 |
printf("dhasetup (c) 2004 Sascha Sommer\n"); |
| 252 |
|
| 253 |
if(argc==1) |
| 254 |
{ |
| 255 |
char szPath[MAX_PATH]; |
| 256 |
int result = 0; |
| 257 |
|
| 258 |
GetWindowsDirectory(szPath, MAX_PATH); |
| 259 |
strcpy(szPath + strlen(szPath), "\\system32\\drivers\\"); |
| 260 |
|
| 261 |
printf("Usage:\n"); |
| 262 |
printf("dhasetup install - Copies dhahelper.sys from the current directory to system \n directory and configures it to start at boot.\n"); |
| 263 |
printf("dhasetup remove - Removes the DhaHelper driver.\n"); |
| 264 |
printf("dhasetup start - Start driver.\n"); |
| 265 |
printf("dhasetup stop - Stop driver.\n"); |
| 266 |
printf("dhasetup check - Check if driver is present and loaded.\n"); |
| 267 |
printf("\n"); |
| 268 |
printf("System directory: %s\n", szPath); |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
if(!strcmp(argv[1], "install")) |
| 274 |
{ |
| 275 |
printf("Installing DhaHelper...\n"); |
| 276 |
|
| 277 |
dha_install(); |
| 278 |
dha_start(); |
| 279 |
} |
| 280 |
else if(!strcmp(argv[1], "remove")) |
| 281 |
{ |
| 282 |
printf("Removing DhaHelper...\n"); |
| 283 |
|
| 284 |
dha_stop(); |
| 285 |
dha_uninstall(); |
| 286 |
} |
| 287 |
else if(!strcmp(argv[1], "start")) |
| 288 |
{ |
| 289 |
printf("Starting DhaHelper...\n"); |
| 290 |
dha_start(); |
| 291 |
printf("Done!\n"); |
| 292 |
} |
| 293 |
else if(!strcmp(argv[1], "stop")) |
| 294 |
{ |
| 295 |
printf("Stopping DhaHelper...\n"); |
| 296 |
dha_stop(); |
| 297 |
printf("Done!\n"); |
| 298 |
} |
| 299 |
else if(!strcmp(argv[1], "check")) |
| 300 |
{ |
| 301 |
int result; |
| 302 |
|
| 303 |
dha_detect(&result); |
| 304 |
printf("DhaHelper driver is%spresent.\n", result ? " " : " not "); |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
printf("unknown parameter: %s\n",argv[1]); |
| 309 |
} |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |