飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2733|回复: 0

[其他源码] EnumHideProcess(枚举隐藏进程源码)

[复制链接]
  • TA的每日心情
    慵懒
    2020-9-27 20:02
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2012-5-18 21:57:29 | 显示全部楼层 |阅读模式
    源码发布概况
    编程语言: c++
    详细描述: EnumHideProcess(枚举隐藏进程源码)
    网址: http://bbs.chinapyg.com
    本帖最后由 爱民 于 2012-5-18 21:58 编辑

    1. //Author  :n0bele
    2. //HomePage:bbs.chinapyg.com

    3. #include<ntddk.h>
    4. #include <windef.h>
    5. #include "processenum.h"
    6. ///////////////////////////不同的windows版本下面的偏移值不同
    7. #define  EPROCESS_SIZE       0x0 //EPROCESS结构大小
    8. #define  PEB_OFFSET          0x1
    9. #define  FILE_NAME_OFFSET    0x2
    10. #define  PROCESS_LINK_OFFSET 0x3
    11. #define  PROCESS_ID_OFFSET   0x4
    12. #define  EXIT_TIME_OFFSET    0x5
    13. #define  OBJECT_HEADER_SIZE  0x018
    14. #define  OBJECT_TYPE_OFFSET  0x008
    15. //////////////////////////
    16. #define PDE_INVALID 2
    17. #define PTE_INVALID 1
    18. #define VALID 0

    19. Processinfo* pProcessPtr = NULL;
    20. int nProcessCount = 0;
    21. ULONG     pebAddress;         //PEB地址的前半部分
    22. PEPROCESS pSystem;            //system进程
    23. ULONG     pObjectTypeProcess; //进程对象类型
    24. ULONG   VALIDpage(ULONG addr) ;  //该函数直接复制自 Ring0下搜索内存枚举隐藏进程
    25. BOOLEAN IsaRealProcess(ULONG i); //该函数复制自 Ring0下搜索内存枚举隐藏进程
    26. VOID    WorkThread(IN PVOID pContext);
    27. //ULONG   GetPebAddress();          //得到PEB地址前半部分
    28. VOID    EnumProcess();            //枚举进程
    29. VOID    ShowProcess(ULONG pEProcess); //显示结果
    30. DWORD GetPlantformDependentInfo(DWORD eprocessflag);
    31. #define SYSNAME    "System"
    32. ULONG ProcessNameOffset = 0; //进程名偏移量
    33. ULONG   GetProcessNameOffset();
    34. BOOLEAN GetProcess(PCHAR Name);
    35. void EnumProcess2();
    36. WCHAR gDeviceName[]=L"\\Device\\safepsenum";
    37. WCHAR gDosDeviceName[]=L"\\??\\safepsenum";
    38. NTSTATUS SafePsEnumCreate(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
    39. NTSTATUS SafePsEnumClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp);
    40. NTSTATUS
    41. MyDeviceControl (
    42.     IN PDEVICE_OBJECT   DeviceObject,
    43.     IN PIRP             Irp );
    44. /////////////////////////////////////////////////////////
    45. VOID    OnUnload(IN PDRIVER_OBJECT DriverObject)
    46. {
    47. NTSTATUS status;
    48. UNICODE_STRING DosDeviceName;
    49. RtlInitUnicodeString(&DosDeviceName,gDosDeviceName);
    50. if(DriverObject->DeviceObject)
    51.   IoDeleteDevice(DriverObject->DeviceObject);
    52. status = IoDeleteSymbolicLink(&DosDeviceName);
    53. if(status)
    54.   DbgPrint("IoDeleteSymbolicLink Return %0x\n",status);
    55. if (pProcessPtr)
    56. {
    57.   ExFreePool(pProcessPtr);
    58. }
    59. }
    60. /////////////////////////////////////////////////////////
    61. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)
    62. {
    63. HANDLE hThread;
    64. UNICODE_STRING DeviceName;
    65. UNICODE_STRING DosDeviceName;
    66. PDEVICE_OBJECT pDeviceObject=NULL;
    67. NTSTATUS Status;

    68. pSystem    = PsGetCurrentProcess();
    69. // pebAddress = GetPebAddress();
    70.     pebAddress = 0x7FFD0000;       //取了一个通用的低位地址
    71. ProcessNameOffset = GetProcessNameOffset();

    72. pObjectTypeProcess = *(PULONG)((ULONG)pSystem - OBJECT_HEADER_SIZE +OBJECT_TYPE_OFFSET);  
    73. DbgPrint("type: %d \n", pObjectTypeProcess);

    74. DriverObject -> DriverUnload = OnUnload;
    75. DriverObject->MajorFunction[IRP_MJ_CREATE]         = SafePsEnumCreate;
    76. DriverObject->MajorFunction[IRP_MJ_CLOSE]          = SafePsEnumClose;
    77. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = MyDeviceControl;
    78. RtlInitUnicodeString(&DeviceName,gDeviceName);
    79. RtlInitUnicodeString(&DosDeviceName,gDosDeviceName);
    80. IoCreateDevice(DriverObject,0,&DeviceName,FILE_DEVICE_UNKNOWN,0,FALSE,&pDeviceObject);
    81. pDeviceObject->Flags|=DO_BUFFERED_IO;
    82. Status = IoCreateSymbolicLink(&DosDeviceName,&DeviceName);
    83. if(Status)
    84.   DbgPrint("IoCreateSymbolicLink Return %0x\n",Status);

    85. nProcessCount = 0;
    86. // 测试固定获取最大为1024
    87. pProcessPtr =  ExAllocatePoolWithTag(NonPagedPool, 1024*sizeof(Processinfo), 'HpcM');
    88. return STATUS_SUCCESS;
    89. }
    90. /////////////////////////////////////////////////////////
    91. NTSTATUS SafePsEnumCreate(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
    92. {
    93.   Irp->iOStatus.Status = STATUS_SUCCESS;
    94. Irp->IoStatus.Information = 0;
    95. IoCompleteRequest(Irp,IO_NO_INCREMENT);
    96. return STATUS_SUCCESS;
    97. }
    98. /////////////////////////////////////////////////////////
    99. NTSTATUS SafePsEnumClose(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp)
    100. {
    101.   Irp->IoStatus.Status = STATUS_SUCCESS;
    102. Irp->IoStatus.Information = 0;
    103. IoCompleteRequest(Irp,IO_NO_INCREMENT);
    104. return STATUS_SUCCESS;
    105. }
    106. /////////////////////////////////////////////////////////
    107. /*
    108. ULONG  GetPebAddress()
    109. {
    110. ULONG Address;
    111. PEPROCESS pEProcess;
    112. //由于system进程的peb总是零 我们只有到其他进程去找了
    113. pEProcess = (PEPROCESS)((ULONG)((PLIST_ENTRY)((ULONG)pSystem + PROCESS_LINK_OFFSET))->Flink - PROCESS_LINK_OFFSET);
    114. Address   = *(PULONG)((ULONG)pEProcess + PEB_OFFSET);
    115. return (Address & 0xFFFF0000);  
    116. }
    117. */
    118. ///////////////////////////////////////////////////////
    119. VOID EnumProcess()
    120. {
    121. ULONG  uSystemAddress = (ULONG)pSystem;
    122. ULONG  i;
    123. ULONG  Address;
    124. ULONG  ret;
    125. nProcessCount = 0;

    126. for(i = 0x80000000; i < uSystemAddress; i += 4) {//system进程的EPROCESS地址就是最大值了
    127.   
    128.   ret = VALIDpage(i);
    129.   if (ret == VALID) {
    130.    
    131.    Address = *(PULONG)i;
    132.    if (( Address & 0xFFFF0000) == 0x7FFD0000) {//每个进程的PEB地址都是在差不多的地方,地址前半部分是相同的      
    133.    
    134.     if(IsaRealProcess(i)) {
    135.    
    136.      ShowProcess(i - GetPlantformDependentInfo(PEB_OFFSET));
    137.      i += GetPlantformDependentInfo(EPROCESS_SIZE);               
    138.     }
    139.    }
    140.   }
    141.   else if(ret == PTE_INVALID) {
    142.    
    143.    i -=4;
    144.    i += 0x1000;//4k
    145.   }
    146.   else {
    147.     i-=4;
    148.     i+= 0x400000;//4mb
    149.   }
    150. }
    151.   ShowProcess(uSystemAddress);//system的PEB总是零 上面的方法是枚举不到的 不过我们用PsGetCurrentProcess就能得到了
    152. }
    153. /////////////////////////////////////////////////////////
    154. VOID    ShowProcess(ULONG pEProcess)
    155. {
    156. PLARGE_INTEGER ExitTime;
    157. ULONG PID;
    158. PUCHAR pFileName;

    159. ExitTime = (PLARGE_INTEGER)(pEProcess + GetPlantformDependentInfo(EXIT_TIME_OFFSET));  
    160. if(ExitTime->QuadPart != 0) //已经结束的进程的ExitTime为非零
    161.   return ;
    162. PID = *(PULONG)(pEProcess + GetPlantformDependentInfo(PROCESS_ID_OFFSET));
    163. pFileName = (PUCHAR)(pEProcess + GetPlantformDependentInfo(FILE_NAME_OFFSET));
    164.   pProcessPtr[nProcessCount].pEProcess = pEProcess;
    165. pProcessPtr[nProcessCount].PId = PID;
    166. strcpy(pProcessPtr[nProcessCount].Name, pFileName);  
    167. nProcessCount++;
    168. }
    169. /////////////////////////////////////////////////////////////
    170. ULONG VALIDpage(ULONG addr)
    171. {
    172. ULONG pte;
    173. ULONG pde;
    174. pde = 0xc0300000 + (addr>>22)*4;
    175. if((*(PULONG)pde & 0x1) != 0){
    176. //large page
    177. if((*(PULONG)pde & 0x80) != 0){
    178.   return VALID;
    179. }
    180. pte = 0xc0000000 + (addr>>12)*4;
    181. if((*(PULONG)pte & 0x1) != 0){
    182.   return VALID;
    183. }else{
    184.   return PTE_INVALID;
    185. }
    186. }
    187. return PDE_INVALID;
    188. }
    189. ////////////////////////////////////////////////////////////////
    190. BOOLEAN IsaRealProcess(ULONG i)
    191. {
    192. NTSTATUS STATUS;
    193. PUNICODE_STRING pUnicode;
    194. UNICODE_STRING Process;
    195. ULONG pObjectType;
    196. ULONG ObjectTypeAddress;
    197. if (VALIDpage(i- GetPlantformDependentInfo(PEB_OFFSET)) != VALID){
    198.   return FALSE;
    199. }
    200. ObjectTypeAddress = i - GetPlantformDependentInfo(PEB_OFFSET) - OBJECT_HEADER_SIZE + OBJECT_TYPE_OFFSET ;
    201. if (VALIDpage(ObjectTypeAddress) == VALID){
    202.   pObjectType = *(PULONG)ObjectTypeAddress;
    203. }else{
    204.   return FALSE;
    205. }
    206. if (pObjectTypeProcess == pObjectType){ //确定ObjectType是Process类型
    207.   return TRUE;
    208. }
    209. return FALSE;
    210. }
    211. ////////////////////////////////////////////////////////////////////
    212. #define  DWORD unsigned long
    213. NTSTATUS
    214. MyDeviceControl (
    215.     IN PDEVICE_OBJECT   DeviceObject,
    216.     IN PIRP             Irp
    217.     )
    218. {
    219.     PIO_STACK_LOCATION  io_stack;
    220.     NTSTATUS            status;
    221. Irp->IoStatus.Information = 0;
    222.     io_stack = IoGetCurrentIrpStackLocation(Irp);
    223. if (io_stack->MajorFunction==IRP_MJ_DEVICE_CONTROL)
    224. {
    225.   switch (io_stack->Parameters.DeviceIoControl.IoControlCode)
    226.   {
    227.   case IOCTL_GETPROCESSPTR:
    228.    {
    229.      EnumProcess();
    230.    // EnumProcess2();
    231.     RtlCopyMemory((unsigned char*)Irp->UserBuffer, (unsigned char*)&nProcessCount, sizeof(DWORD));
    232.     RtlCopyMemory((unsigned char*)Irp->UserBuffer+sizeof(DWORD), (unsigned char*)pProcessPtr, nProcessCount*sizeof(Processinfo));
    233.     Irp->IoStatus.Information = nProcessCount*sizeof(Processinfo)+sizeof(DWORD);
    234.    }
    235.   }
    236. }
    237. Irp->IoStatus.Status = STATUS_SUCCESS;
    238. IoCompleteRequest(Irp, IO_NO_INCREMENT);
    239. return STATUS_SUCCESS;
    240. }
    241. //----------------------------------------------------------------------
    242. //
    243. // GetProcessNameOffset
    244. //
    245. // In an effort to remain version-independent, rather than using a
    246. // hard-coded into the KPEB (Kernel Process Environment Block), we
    247. // scan the KPEB looking for the name, which should match that
    248. // of the GUI process
    249. //
    250. //----------------------------------------------------------------------
    251. ULONG GetProcessNameOffset()
    252. {
    253.     PEPROCESS       curproc;
    254.     int             i;
    255.     DbgPrint(("GetProcessNameOffset\n"));
    256.     curproc = PsGetCurrentProcess();
    257.     //
    258.     // Scan for 12KB, hopping the KPEB never grows that big!
    259.     //
    260.     for( i = 0; i < 3*PAGE_SIZE; i++ ) {
    261.   if( !strncmp( SYSNAME, (PCHAR) curproc + i, strlen(SYSNAME) )) {
    262.    DbgPrint("%d\n", i);
    263.    return i;
    264.   }
    265. }
    266. //
    267. // Name not found - oh, well
    268. //
    269. DbgPrint("0\n");
    270. return 0;
    271. }
    272. //----------------------------------------------------------------------
    273. //
    274. // GetProcess
    275. //
    276. // Uses undocumented data structure offsets to obtain the name of the
    277. // currently executing process.
    278. //
    279. //----------------------------------------------------------------------
    280. BOOLEAN GetProcess( PCHAR Name )
    281. {
    282. PEPROCESS curproc;
    283. char *nameptr;
    284. ULONG i;
    285. //
    286. // We only try and get the name if we located the name offset
    287. //
    288. if( ProcessNameOffset ) {
    289.   curproc = PsGetCurrentProcess();
    290.   nameptr = (PCHAR) curproc + ProcessNameOffset;
    291.   strncpy( Name, nameptr, 16 );
    292.   return TRUE;
    293. } else {
    294.   strcpy( Name, "???");
    295.   return FALSE;
    296. }
    297. }

    298. ////////////////////////////////////////////////////////////////////////
    299. // EnumProcess2
    300. #define BASE_PROCESS_PEB_OFFSET     0x01B0
    301. #define BASE_PEB_PROCESS_PARAMETER_OFFSET  0x0010
    302. #define BASE_PROCESS_PARAMETER_FULL_IMAGE_NAME 0x003C
    303. #define W2003_BASE_PROCESS_PEB_OFFSET   0x0190
    304. #define W2003_BASE_PROCESS_PEB_OFFSET_SP1  0x01A0
    305. #define VISTA_BASE_PROCESS_PEB_OFFSET   0x0188

    306. void EnumProcess2()
    307. {
    308.    ULONG OsMajorVersion;
    309.    ULONG OsMinorVersion ;
    310.    DWORD dwAddress;
    311.    PCWSTR Temp=NULL;
    312.    ULONG uSystemAddress = (ULONG) pSystem;
    313.    DWORD i;
    314.    if (KeGetCurrentIrql() != PASSIVE_LEVEL) {
    315.   return  ;
    316.    }

    317.    PsGetVersion( &OsMajorVersion,
    318.     &OsMinorVersion,
    319.     NULL,
    320.     NULL );
    321.    for(i = 0x80000000; i < uSystemAddress; i += 4) {//system进程的EPROCESS地址就是最大值了
    322.     try {
    323.      ULONG PID = 0;
    324.      if ( *(DWORD*)(i+GetPlantformDependentInfo(PROCESS_ID_OFFSET)) == PID)
    325.       continue;
    326.      if (!IsaRealProcess(i))
    327.       continue;
    328.      dwAddress = i;

    329.      if(dwAddress == 0 || dwAddress == 0xFFFFFFFF) {
    330.      return  ;
    331.      }
    332.      //目前只支持Win 2000/xp/2003/VISTA
    333.      if (OsMajorVersion < 5 || OsMinorVersion > 2 ) {
    334.       return  ;
    335.      }
    336.      //取得PEB,不同平台的位置是不同的。
    337.      //
    338.      //2000   0X0500         XP 0X0501
    339.      //
    340.      if( OsMajorVersion == 5 && OsMinorVersion < 2) {
    341.    
    342.       dwAddress += BASE_PROCESS_PEB_OFFSET;
    343.      }
    344.      //
    345.      //2003   0X0502
    346.      //
    347.      if (OsMajorVersion == 5 && OsMinorVersion ==2) {
    348.        dwAddress += W2003_BASE_PROCESS_PEB_OFFSET;
    349.      }
    350.      //
    351.      //VISTA   0X0600
    352.      //
    353.      if (OsMajorVersion == 6 && OsMinorVersion ==0) {
    354.        dwAddress += VISTA_BASE_PROCESS_PEB_OFFSET;
    355.      }
    356.      if ((dwAddress = *(DWORD*)dwAddress) == 0) {
    357.       continue;
    358.      }
    359.      //
    360.      // 通过peb取得RTL_USER_PROCESS_PARAMETERS
    361.      //
    362.      dwAddress += BASE_PEB_PROCESS_PARAMETER_OFFSET;
    363.      if((dwAddress = *(DWORD*)dwAddress) == 0) {
    364.       continue;
    365.      }
    366.      //
    367.      // 在RTL_USER_PROCESS_PARAMETERS->ImagePathName保存了路径,偏移为38,
    368.      //
    369.      dwAddress += BASE_PROCESS_PARAMETER_FULL_IMAGE_NAME;
    370.      if ((dwAddress = *(DWORD*)dwAddress) == 0) {
    371.       continue;
    372.      }
    373.     // [10/14/2006]
    374.      Temp=(PCWSTR)dwAddress;
    375.      if (wcslen(Temp)>4) {
    376.     if (Temp[0]==L'\\'&&
    377.        Temp[1]==L'?'&&
    378.        Temp[2]==L'?'&&
    379.        Temp[3]==L'\\') {
    380.        dwAddress+=8;
    381.       }
    382.       if (Temp[0]==L'\\'&&
    383.        Temp[1]==L'\\'&&
    384.        Temp[2]==L'?'&&
    385.        Temp[3]==L'\\') {
    386.        dwAddress+=8;
    387.       }
    388.       DbgPrint("%ws\n", dwAddress);
    389.       i = dwAddress;
    390.      }
    391.     }
    392.     except (EXCEPTION_EXECUTE_HANDLER) {
    393.    try {
    394.     if(OsMajorVersion == 5 && OsMinorVersion ==2) {
    395.      dwAddress = (DWORD)PsGetCurrentProcess();
    396.      dwAddress += W2003_BASE_PROCESS_PEB_OFFSET_SP1;
    397.      if((dwAddress = *(DWORD*)dwAddress) == 0) {
    398.         continue;
    399.      }
    400.      //
    401.      // 通过peb取得RTL_USER_PROCESS_PARAMETERS
    402.      //
    403.      dwAddress += BASE_PEB_PROCESS_PARAMETER_OFFSET;
    404.      if((dwAddress = *(DWORD*)dwAddress) == 0) {
    405.         continue;
    406.      }
    407.      //
    408.      // 在RTL_USER_PROCESS_PARAMETERS->ImagePathName保存了路径,偏移为38,
    409.      //
    410.      dwAddress += BASE_PROCESS_PARAMETER_FULL_IMAGE_NAME;
    411.      if((dwAddress = *(DWORD*)dwAddress) == 0) {
    412.         continue;
    413.      }
    414.      // [10/14/2006]
    415.      Temp=(PCWSTR)dwAddress;
    416.      if (wcslen(Temp)>4) {
    417.       if (Temp[0]==L'\\'&&
    418.          Temp[1]==L'?'&&
    419.          Temp[2]==L'?'&&
    420.          Temp[3]==L'\\') {
    421.          dwAddress+=8;
    422.         }
    423.         if (Temp[0]==L'\\'&&
    424.          Temp[1]==L'\\'&&
    425.          Temp[2]==L'?'&&
    426.          Temp[3]==L'\\') {
    427.          dwAddress+=8;
    428.         }
    429.      }
    430.      DbgPrint("%ws\n", dwAddress);
    431.      i = dwAddress;
    432.     }
    433.    }
    434.    except (EXCEPTION_EXECUTE_HANDLER) {
    435.    }     
    436.     }
    437.    }
    438. }
    439. DWORD GetPlantformDependentInfo(DWORD eprocessflag)
    440. {
    441. DWORD current_build;
    442. DWORD ans = 0;
    443. PsGetVersion(NULL, NULL, ¤t_build, NULL);
    444. switch(eprocessflag){
    445.   case EPROCESS_SIZE:
    446.    if (current_build ==  2195) //2000
    447.    {
    448.     ans = 0x1FC;
    449.    }
    450.    if (current_build ==  2600) //XP
    451.    {
    452.     ans = 0x25C;
    453.    }
    454.    if (current_build ==  3790) //2003
    455.    {
    456.     ans = 0x270;   
    457.    }
    458.    break;
    459.   case PEB_OFFSET:
    460.    if (current_build ==  2195) //2000
    461.    {
    462.     ans = 0x09c;
    463.    }
    464.    if (current_build ==  2600) //XP
    465.    {
    466.     ans = 0x1b0;
    467.    }
    468.    if (current_build ==  3790) //2003
    469.    {
    470.     ans = 0x1a0;
    471.    }
    472.    break;
    473.   case FILE_NAME_OFFSET:
    474.    if (current_build ==  2195) //2000
    475.    {
    476.     ans = 0x09c;      
    477.    }
    478.    if (current_build ==  2600) //XP
    479.    {
    480.     ans = 0x174;
    481.    }
    482.    if (current_build ==  3790) //2003
    483.    {
    484.     ans = 0x164;
    485.    }
    486.    break;
    487.   case PROCESS_LINK_OFFSET:
    488.    if (current_build ==  2195) //2000
    489.    {
    490.     ans = 0x09c;      
    491.    }
    492.    if (current_build ==  2600) //XP
    493.    {
    494.     ans = 0x088;
    495.    }
    496.    if (current_build ==  3790) //2003
    497.    {
    498.     ans = 0x098;
    499.    }
    500.    break;
    501.   case PROCESS_ID_OFFSET:
    502.    if (current_build ==  2195) //2000
    503.    {
    504.     ans = 0x09c;      
    505.    }
    506.    if (current_build ==  2600) //XP
    507.    {
    508.     ans = 0x084;
    509.    }
    510.    if (current_build ==  3790) //2003
    511.    {
    512.     ans = 0x094;
    513.    }
    514.    break;
    515.   case EXIT_TIME_OFFSET:
    516.    if (current_build ==  2195) //2000
    517.    {
    518.     ans = 0x09c;      
    519.    }
    520.    if (current_build ==  2600) //XP
    521.    {
    522.     ans = 0x078;
    523.    }
    524.    if (current_build ==  3790) //2003
    525.    {
    526.     ans = 0x088;
    527.    }
    528.    break;
    529.   default:
    530.    break;
    531. }
    532. return ans;
    533. }
    复制代码

    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

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