天马阁

 找回密码
 立即注册
                                        →→→→→→→→→→→→ 1点击查看所有VIP教程目录长列表(总教程数269个) 2办理VIP详情进入 ←←←←←←←←←←←←
1 x64CE与x64dbg入门基础教程 7课 已完结 2 x64汇编语言基础教程 16课 已完结 3 x64辅助入门基础教程 9课 已完结 4 C++x64内存辅助实战技术教程 149课 已完结
5 C++x64内存检测与过检测技术教程 10课 已完结 6 C+x64二叉树分析遍历与LUA自动登陆教程 19课已完结 7 C++BT功能原理与x64实战教程 29课 已完结 8 C+FPS框透视与自瞄x64实现原理及防护思路 30课完结
64驱?封? 9 64反驱? 10 64位V? 11 绝? 12 ???课?
13 64透 ? 14 64U ? 15 64Q ? 16 64功 ?
17 64U ? 18 64模 ? 19 64多 ? 20 64网 ?
21 64注 ? 22 64火 ? 23 64棋 ? 24 64自二链L?
25 64破 ? VIP会员办理QQ: 89986068   
【请先加好友,然后到好友列表双击联系客服办理,不然可能无法接受到信息。】
27 加入2000人交流群637034024 3 28 免责声明?
查看: 1193|回复: 0

获得目标进程PEB,并获得进程各种信息

[复制链接]

13

主题

0

回帖

16

积分

编程入门

Rank: 1

天马币
26
发表于 2024-3-4 09:32:04 | 显示全部楼层 |阅读模式
本程序可完美获得x64和x86程序PEB,及环境变量等信息。

程序首先用OpenProcess打开目标进程,然后用Ntddl.dll导入表中微软未公开函数NtWow64QueryInformationProcess64(NtQueryInformationProcess)、NtWow64ReadVirtualMemory64(ReadProcessMemory)来获得目标进程信息。


  1. #include "stdafx.h"
  2. #include<Windows.h>
  3. #include<iostream>
  4. #include <Strsafe.h>
  5. using namespace std;

  6. #define NT_SUCCESS(x) ((x) >= 0)

  7. #define ProcessBasicInformation 0
  8. typedef
  9. NTSTATUS(WINAPI *pfnNtWow64QueryInformationProcess64)
  10. (HANDLE ProcessHandle, UINT32 ProcessInformationClass,
  11.         PVOID ProcessInformation, UINT32 ProcessInformationLength,
  12.         UINT32* ReturnLength);

  13. typedef
  14. NTSTATUS(WINAPI *pfnNtWow64ReadVirtualMemory64)
  15. (HANDLE ProcessHandle, PVOID64 BaseAddress,
  16.         PVOID BufferData, UINT64 BufferLength,
  17.         PUINT64 ReturnLength);

  18. typedef
  19. NTSTATUS(WINAPI *pfnNtQueryInformationProcess)
  20. (HANDLE ProcessHandle, ULONG ProcessInformationClass,
  21.         PVOID ProcessInformation, UINT32 ProcessInformationLength,
  22.         UINT32* ReturnLength);

  23. template <typename T>
  24. struct _UNICODE_STRING_T
  25. {
  26.         WORD Length;
  27.         WORD MaximumLength;
  28.         T Buffer;
  29. };

  30. template <typename T>
  31. struct _LIST_ENTRY_T
  32. {
  33.         T Flink;
  34.         T Blink;
  35. };

  36. template <typename T, typename NGF, int A>
  37. struct _PEB_T
  38. {
  39.         typedef T type;

  40.         union
  41.         {
  42.                 struct
  43.                 {
  44.                         BYTE InheritedAddressSpace;
  45.                         BYTE ReadImageFileExecOptions;
  46.                         BYTE BeingDebugged;
  47.                         BYTE BitField;
  48.                 };
  49.                 T dummy01;
  50.         };
  51.         T Mutant;
  52.         T ImageBaseAddress;     //进程加载基地址
  53.         T Ldr;                                    
  54.         T ProcessParameters;    //各种信息,环境变量,命令行等等
  55.         T SubSystemData;
  56.         T ProcessHeap;
  57.         T FastPebLock;
  58.         T AtlThunkSListPtr;
  59.         T IFEOKey;
  60.         T CrossProcessFlags;
  61.         T UserSharedInfoPtr;
  62.         DWORD SystemReserved;
  63.         DWORD AtlThunkSListPtr32;
  64.         T ApiSetMap;
  65.         T TlsExpansionCounter;
  66.         T TlsBitmap;
  67.         DWORD TlsBitmapBits[2];
  68.         T ReadOnlySharedMemoryBase;
  69.         T HotpatchInformation;
  70.         T ReadOnlyStaticServerData;
  71.         T AnsiCodePageData;
  72.         T OemCodePageData;
  73.         T UnicodeCaseTableData;
  74.         DWORD NumberOfProcessors;
  75.         union
  76.         {
  77.                 DWORD NtGlobalFlag;
  78.                 NGF dummy02;
  79.         };
  80.         LARGE_INTEGER CriticalSectionTimeout;
  81.         T HeapSegmentReserve;
  82.         T HeapSegmentCommit;
  83.         T HeapDeCommitTotalFreeThreshold;
  84.         T HeapDeCommitFreeBlockThreshold;
  85.         DWORD NumberOfHeaps;
  86.         DWORD MaximumNumberOfHeaps;
  87.         T ProcessHeaps;
  88.         T GdiSharedHandleTable;
  89.         T ProcessStarterHelper;
  90.         T GdiDCAttributeList;
  91.         T LoaderLock;
  92.         DWORD OSMajorVersion;
  93.         DWORD OSMinorVersion;
  94.         WORD OSBuildNumber;
  95.         WORD OSCSDVersion;
  96.         DWORD OSPlatformId;
  97.         DWORD ImageSubsystem;
  98.         DWORD ImageSubsystemMajorVersion;
  99.         T ImageSubsystemMinorVersion;
  100.         T ActiveProcessAffinityMask;
  101.         T GdiHandleBuffer[A];
  102.         T PostProcessInitRoutine;
  103.         T TlsExpansionBitmap;
  104.         DWORD TlsExpansionBitmapBits[32];
  105.         T SessionId;
  106.         ULARGE_INTEGER AppCompatFlags;
  107.         ULARGE_INTEGER AppCompatFlagsUser;
  108.         T pShimData;
  109.         T AppCompatInfo;
  110.         _UNICODE_STRING_T<T> CSDVersion;
  111.         T ActivationContextData;
  112.         T ProcessAssemblyStorageMap;
  113.         T SystemDefaultActivationContextData;
  114.         T SystemAssemblyStorageMap;
  115.         T MinimumStackCommit;
  116.         T FlsCallback;
  117.         _LIST_ENTRY_T<T> FlsListHead;
  118.         T FlsBitmap;
  119.         DWORD FlsBitmapBits[4];
  120.         T FlsHighIndex;
  121.         T WerRegistrationData;
  122.         T WerShipAssertPtr;
  123.         T pContextData;
  124.         T pImageHeaderHash;
  125.         T TracingFlags;
  126.         T CsrServerReadOnlySharedMemoryBase;
  127. };

  128. template <typename T>
  129. struct _STRING_T
  130. {
  131.         WORD Length;
  132.         WORD MaximumLength;
  133.         T    Buffer;
  134. };

  135. template <typename T>
  136. struct _RTL_DRIVE_LETTER_CURDIR_T
  137. {
  138.         WORD         Flags;
  139.         WORD         Length;
  140.         ULONG        TimeStamp;
  141.         _STRING_T<T> DosPath;
  142. };

  143. template <typename T>
  144. struct _CURDIR_T
  145. {
  146.         _UNICODE_STRING_T<T> DosPath;
  147.         T                                 Handle;
  148. };

  149. template <typename T>
  150. struct _RTL_USER_PROCESS_PARAMETERS_T
  151. {
  152.         ULONG MaximumLength;
  153.         ULONG Length;
  154.         ULONG Flags;
  155.         ULONG DebugFlags;
  156.         T ConsoleHandle;
  157.         ULONG  ConsoleFlags;
  158.         T StandardInput;
  159.         T StandardOutput;
  160.         T StandardError;
  161.         _CURDIR_T<T> CurrentDirectory;
  162.         _UNICODE_STRING_T<T> DllPath;
  163.         _UNICODE_STRING_T<T> ImagePathName; //进程完整路径
  164.         _UNICODE_STRING_T<T> CommandLine;   //进程命令行
  165.         T Environment;             //环境变量(地址)
  166.         ULONG StartingX;
  167.         ULONG StartingY;
  168.         ULONG CountX;
  169.         ULONG CountY;
  170.         ULONG CountCharsX;
  171.         ULONG CountCharsY;
  172.         ULONG FillAttribute;
  173.         ULONG WindowFlags;
  174.         ULONG ShowWindowFlags;
  175.         _UNICODE_STRING_T<T> WindowTitle;
  176.         _UNICODE_STRING_T<T> DesktopInfo;
  177.         _UNICODE_STRING_T<T> ShellInfo;
  178.         _UNICODE_STRING_T<T> RuntimeData;
  179.         _RTL_DRIVE_LETTER_CURDIR_T<T> CurrentDirectores[32];
  180.         ULONG EnvironmentSize;
  181. };

  182. typedef _PEB_T<DWORD, DWORD64, 34> _PEB32;
  183. typedef _PEB_T<DWORD64, DWORD, 30> _PEB64;
  184. typedef _RTL_USER_PROCESS_PARAMETERS_T<UINT32> _RTL_USER_PROCESS_PARAMETERS32;
  185. typedef _RTL_USER_PROCESS_PARAMETERS_T<UINT64> _RTL_USER_PROCESS_PARAMETERS64;

  186. typedef struct _PROCESS_BASIC_INFORMATION64 {
  187.         NTSTATUS ExitStatus;
  188.         UINT32 Reserved0;
  189.         UINT64 PebBaseAddress;
  190.         UINT64 AffinityMask;
  191.         UINT32 BasePriority;
  192.         UINT32 Reserved1;
  193.         UINT64 UniqueProcessId;
  194.         UINT64 InheritedFromUniqueProcessId;
  195. } PROCESS_BASIC_INFORMATION64;

  196. typedef struct _PROCESS_BASIC_INFORMATION32 {
  197.         NTSTATUS ExitStatus;
  198.         UINT32 PebBaseAddress;
  199.         UINT32 AffinityMask;
  200.         UINT32 BasePriority;
  201.         UINT32 UniqueProcessId;
  202.         UINT32 InheritedFromUniqueProcessId;
  203. } PROCESS_BASIC_INFORMATION32;


  204. typedef struct _PEB_INFO {
  205.         UINT64 ImageBaseAddress;  //进程加载基地址
  206.         UINT64 Ldr;               //模块加载基地址
  207.         UINT64 ProcessHeap;       //进程默认堆
  208.         UINT64 ProcessParameters; //进程信息
  209.         UINT64 Environment;       //环境变量
  210. }PEBInfo,*PPEBInfo;

  211. int main()
  212. {
  213.         printf("输入进程ID\r\n");
  214.         UINT32 ProcessID;
  215.         cin >> ProcessID;
  216.         HANDLE ProcessHandle = NULL;
  217.         //获得进程句柄
  218.         ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
  219.         PEBInfo PebInfo = {0};
  220.         BOOL bSource = FALSE;
  221.         BOOL bTarget = FALSE;
  222.         //判断自己的位数
  223.         IsWow64Process(GetCurrentProcess(), &bSource);
  224.         //判断目标的位数
  225.         IsWow64Process(ProcessHandle, &bTarget);

  226.         //自己是32  目标64
  227.         if (bTarget == FALSE && bSource == TRUE)
  228.         {
  229.                 HMODULE NtdllModule = GetModuleHandle("ntdll.dll");
  230.                 pfnNtWow64QueryInformationProcess64 NtWow64QueryInformationProcess64 = (pfnNtWow64QueryInformationProcess64)GetProcAddress(NtdllModule,
  231.                         "NtWow64QueryInformationProcess64");
  232.       
  233.                 pfnNtWow64ReadVirtualMemory64 NtWow64ReadVirtualMemory64 = (pfnNtWow64ReadVirtualMemory64)GetProcAddress(NtdllModule, "NtWow64ReadVirtualMemory64");
  234.                 PROCESS_BASIC_INFORMATION64 pbi = { 0 };
  235.                 UINT64 ReturnLength = 0;
  236.                 NTSTATUS Status = NtWow64QueryInformationProcess64(ProcessHandle, ProcessBasicInformation,
  237.                         &pbi, (UINT32)sizeof(pbi), (UINT32*)&ReturnLength);

  238.                 if (NT_SUCCESS(Status))
  239.                 {
  240.                        
  241.                         _PEB64* Peb = (_PEB64*)malloc(sizeof(_PEB64));
  242.                         Status = NtWow64ReadVirtualMemory64(ProcessHandle, (PVOID64)pbi.PebBaseAddress,
  243.                                 (_PEB64*)Peb, sizeof(_PEB64), &ReturnLength);      
  244.                         _RTL_USER_PROCESS_PARAMETERS64 Parameters64;
  245.                         Status = NtWow64ReadVirtualMemory64(ProcessHandle, (PVOID64)Peb->ProcessParameters,
  246.                                 &Parameters64, sizeof(_RTL_USER_PROCESS_PARAMETERS64), &ReturnLength);

  247.                         BYTE* Environment = new BYTE[Parameters64.EnvironmentSize * 2];
  248.                         Status = NtWow64ReadVirtualMemory64(ProcessHandle, (PVOID64)Parameters64.Environment, Environment, Parameters64.EnvironmentSize, NULL);
  249.                         //赋值
  250.                         PebInfo.ImageBaseAddress = Peb->ImageBaseAddress;
  251.                         PebInfo.Ldr = Peb->Ldr;
  252.                         PebInfo.ProcessHeap = Peb->ProcessHeap;
  253.                         PebInfo.ProcessParameters = Peb->ProcessParameters;
  254.                         PebInfo.Environment = Parameters64.Environment;

  255.                         printf("ImageBaseAddress:0x%x\r\n", PebInfo.ImageBaseAddress);
  256.                         printf("Ldr:0x%x\r\n", PebInfo.Ldr);
  257.                         printf("ProcessHeap:0x%x\r\n", PebInfo.ProcessHeap);
  258.                         printf("ProcessParameters:0x%x\r\n", PebInfo.ProcessParameters);
  259.                         printf("Environment:0x%x\r\n", PebInfo.Environment);
  260.                         while (Environment != NULL)
  261.                         {
  262.                                 char* v1 = NULL;
  263.                                 int DataLength = WideCharToMultiByte(CP_ACP, NULL, (WCHAR*)Environment, -1, NULL, 0, NULL, FALSE);
  264.                                 v1 = new char[DataLength + 1];
  265.                                 WideCharToMultiByte(CP_OEMCP, NULL, (WCHAR*)Environment, -1, v1, Parameters64.EnvironmentSize * 2, NULL, FALSE);
  266.                                 printf("%s\r\n", v1);
  267.                                 // 指针移动到字符串末尾  
  268.                                 while (*(WCHAR*)Environment != '\0')
  269.                                         Environment += 2;
  270.                                 Environment += 2;
  271.                                 // 是否是最后一个字符串  
  272.                                 if (*Environment == '\0')
  273.                                         break;
  274.                         }
  275.                         BYTE* CommandLine = new BYTE[Parameters64.CommandLine.Length];
  276.                         Status = NtWow64ReadVirtualMemory64(ProcessHandle, (PVOID64)Parameters64.CommandLine.Buffer, CommandLine, Parameters64.CommandLine.Length, NULL);
  277.                         if (CommandLine != NULL)
  278.                         {
  279.                                 char* v1 = NULL;
  280.                                 int DataLength = WideCharToMultiByte(CP_ACP, NULL, (WCHAR*)CommandLine, -1, NULL, 0, NULL, FALSE);
  281.                                 v1 = new char[DataLength];
  282.                                 WideCharToMultiByte(CP_OEMCP, NULL, (WCHAR*)CommandLine, -1, v1, Parameters64.CommandLine.Length, NULL, FALSE);
  283.                                 printf("CommandLine:%s\r\n", v1);
  284.                         }
  285.                         BYTE* ImagePathName = new BYTE[Parameters64.ImagePathName.Length];
  286.                         Status = NtWow64ReadVirtualMemory64(ProcessHandle, (PVOID64)Parameters64.ImagePathName.Buffer, ImagePathName, Parameters64.ImagePathName.Length, NULL);
  287.                         if (ImagePathName != NULL)
  288.                         {
  289.                                 char* v1 = NULL;
  290.                                 int DataLength = WideCharToMultiByte(CP_ACP, NULL, (WCHAR*)ImagePathName, -1, NULL, 0, NULL, FALSE);
  291.                                 v1 = new char[DataLength];
  292.                                 WideCharToMultiByte(CP_OEMCP, NULL, (WCHAR*)ImagePathName, -1, v1, Parameters64.ImagePathName.Length, NULL, FALSE);
  293.                                 printf("ImagePathName:%s\r\n", v1);
  294.                         }
  295.                 }
  296.                
  297.         }
  298.         //自己是32  目标是32
  299.         else if (bTarget == TRUE && bSource == TRUE)
  300.         {
  301.                 HMODULE NtdllModule = GetModuleHandle("ntdll.dll");
  302.                 pfnNtQueryInformationProcess NtQueryInformationProcess = (pfnNtQueryInformationProcess)GetProcAddress(NtdllModule,
  303.                         "NtQueryInformationProcess");
  304.                 PROCESS_BASIC_INFORMATION32 pbi = { 0 };
  305.                 UINT32  ReturnLength = 0;
  306.                 NTSTATUS Status = NtQueryInformationProcess(ProcessHandle,
  307.                         ProcessBasicInformation, &pbi, (UINT32)sizeof(pbi), (UINT32*)&ReturnLength);

  308.                 if (NT_SUCCESS(Status))
  309.                 {
  310.                         _PEB32* Peb = (_PEB32*)malloc(sizeof(_PEB32));
  311.                         Status  = ReadProcessMemory(ProcessHandle, (PVOID)pbi.PebBaseAddress, (_PEB32*)Peb, sizeof(_PEB32), NULL);

  312.                         _RTL_USER_PROCESS_PARAMETERS32 Parameters32;

  313.                         Status = ReadProcessMemory(ProcessHandle, (PVOID)Peb->ProcessParameters, &Parameters32, sizeof(_RTL_USER_PROCESS_PARAMETERS32), NULL);
  314.                         BYTE* Environment = new BYTE[Parameters32.EnvironmentSize*2];
  315.                         Status = ReadProcessMemory(ProcessHandle, (PVOID)Parameters32.Environment, Environment, Parameters32.EnvironmentSize, NULL);

  316.                         //赋值
  317.                         PebInfo.ImageBaseAddress = Peb->ImageBaseAddress;
  318.                         PebInfo.Ldr = Peb->Ldr;
  319.                         PebInfo.ProcessHeap = Peb->ProcessHeap;
  320.                         PebInfo.ProcessParameters = Peb->ProcessParameters;
  321.                         PebInfo.Environment = Parameters32.Environment;
  322.                         printf("ImageBaseAddress:0x%x\r\n", PebInfo.ImageBaseAddress);
  323.                         printf("Ldr:0x%x\r\n", PebInfo.Ldr);
  324.                         printf("ProcessHeap:0x%x\r\n", PebInfo.ProcessHeap);
  325.                         printf("ProcessParameters:0x%x\r\n", PebInfo.ProcessParameters);
  326.                         printf("Environment:0x%x\r\n", PebInfo.Environment);
  327.                         while (Environment !=NULL)
  328.                         {
  329.                                 char* v1 = NULL;
  330.                                 int DataLength = WideCharToMultiByte(CP_ACP, NULL, (WCHAR*)Environment, -1, NULL, 0, NULL, FALSE);
  331.                                 v1 = new char[DataLength + 1];
  332.                                 WideCharToMultiByte(CP_OEMCP, NULL, (WCHAR*)Environment, -1, v1, Parameters32.EnvironmentSize * 2, NULL, FALSE);
  333.                                 printf("%s\r\n", v1);
  334.                                 // 指针移动到字符串末尾  
  335.                                 while (*(WCHAR*)Environment != '\0')
  336.                                         Environment +=2;
  337.                                 Environment += 2;
  338.                                 // 是否是最后一个字符串  
  339.                                 if (*Environment == '\0')
  340.                                         break;
  341.                         }
  342.                        
  343.                         BYTE* CommandLine = new BYTE[Parameters32.CommandLine.Length];
  344.                         Status = ReadProcessMemory(ProcessHandle, (PVOID)Parameters32.CommandLine.Buffer, CommandLine, Parameters32.CommandLine.Length, NULL);
  345.                         if (CommandLine != NULL)
  346.                         {
  347.                                 char* v1 = NULL;
  348.                                 int DataLength = WideCharToMultiByte(CP_ACP, NULL, (WCHAR*)CommandLine, -1, NULL, 0, NULL, FALSE);
  349.                                 v1 = new char[DataLength];
  350.                                 WideCharToMultiByte(CP_OEMCP, NULL, (WCHAR*)CommandLine, -1, v1, Parameters32.CommandLine.Length, NULL, FALSE);
  351.                                 printf("CommandLine:%s\r\n", v1);
  352.                         }
  353.                         BYTE* ImagePathName = new BYTE[Parameters32.ImagePathName.Length];
  354.                         Status = ReadProcessMemory(ProcessHandle, (PVOID)Parameters32.ImagePathName.Buffer, ImagePathName, Parameters32.ImagePathName.Length, NULL);
  355.                         if (ImagePathName != NULL)
  356.                         {
  357.                                 char* v1 = NULL;
  358.                                 int DataLength = WideCharToMultiByte(CP_ACP, NULL, (WCHAR*)ImagePathName, -1, NULL, 0, NULL, FALSE);
  359.                                 v1 = new char[DataLength];
  360.                                 WideCharToMultiByte(CP_OEMCP, NULL, (WCHAR*)ImagePathName, -1, v1, Parameters32.ImagePathName.Length, NULL, FALSE);
  361.                                 printf("ImagePathName:%s\r\n", v1);
  362.                         }
  363.                 }
  364.         }      
  365. }

  366. 以上就是获得目标进程PEB,并获得进程各种信息的全文介绍,希望对您学习Windows的使用有所帮助.
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

天马阁|C/C++辅助教程|安卓逆向安全| 论坛导航|免责申明|Archiver||网站地图
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表天马阁立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2010-2021 All Right Reserved.
快速回复 返回顶部 返回列表