查看windows进程(转自MSDN)

简介:
 
 
  1. #include <windows.h> 
  2. #include <tlhelp32.h> 
  3. #include <stdio.h> 
  4.  
  5. //  Forward declarations: 
  6. BOOL GetProcessList( ); 
  7. BOOL ListProcessModules( DWORD dwPID ); 
  8. BOOL ListProcessThreads( DWORD dwOwnerPID ); 
  9. void printError( TCHAR* msg ); 
  10.  
  11. void main( ) 
  12.   GetProcessList( ); 
  13.  
  14. BOOL GetProcessList( ) 
  15.   HANDLE hProcessSnap; 
  16.   HANDLE hProcess; 
  17.   PROCESSENTRY32 pe32; 
  18.   DWORD dwPriorityClass; 
  19.  
  20.   // Take a snapshot of all processes in the system. 
  21.   hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); 
  22.   if( hProcessSnap == INVALID_HANDLE_VALUE ) 
  23.   { 
  24.     printError( "CreateToolhelp32Snapshot (of processes)" ); 
  25.     return( FALSE ); 
  26.   } 
  27.  
  28.   // Set the size of the structure before using it. 
  29.   pe32.dwSize = sizeof( PROCESSENTRY32 ); 
  30.  
  31.   // Retrieve information about the first process, 
  32.   // and exit if unsuccessful 
  33.   if( !Process32First( hProcessSnap, &pe32 ) ) 
  34.   { 
  35.     printError( "Process32First" ); // Show cause of failure 
  36.     CloseHandle( hProcessSnap );    // Must clean up the 
  37.                                     //   snapshot object! 
  38.     return( FALSE ); 
  39.   } 
  40.  
  41.   // Now walk the snapshot of processes, and 
  42.   // display information about each process in turn 
  43.   do 
  44.   { 
  45.     printf( "\n\n" 
  46.       "=====================================================" ); 
  47.     printf( "\nPROCESS NAME:  %s", pe32.szExeFile ); 
  48.     printf( "\n" 
  49.       "-----------------------------------------------------" ); 
  50.  
  51.     // Retrieve the priority class. 
  52.     dwPriorityClass = 0; 
  53.     hProcess = OpenProcess( 
  54.       PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID ); 
  55.     if( hProcess == NULL ) 
  56.       printError( "OpenProcess" ); 
  57.     else 
  58.     { 
  59.       dwPriorityClass = GetPriorityClass( hProcess ); 
  60.       if( !dwPriorityClass ) 
  61.         printError( "GetPriorityClass" ); 
  62.       CloseHandle( hProcess ); 
  63.     } 
  64.  
  65.     printf( "\n  process ID        = 0x%08X", pe32.th32ProcessID ); 
  66.     printf( "\n  thread count      = %d",   pe32.cntThreads ); 
  67.     printf( "\n  parent process ID = 0x%08X"
  68.       pe32.th32ParentProcessID ); 
  69.     printf( "\n  Priority Base     = %d", pe32.pcPriClassBase ); 
  70.     if( dwPriorityClass ) 
  71.       printf( "\n  Priority Class    = %d", dwPriorityClass ); 
  72.  
  73.     // List the modules and threads associated with this process 
  74.     ListProcessModules( pe32.th32ProcessID ); 
  75.     ListProcessThreads( pe32.th32ProcessID ); 
  76.  
  77.   } while( Process32Next( hProcessSnap, &pe32 ) ); 
  78.  
  79.   CloseHandle( hProcessSnap ); 
  80.   return( TRUE ); 
  81.  
  82.  
  83. BOOL ListProcessModules( DWORD dwPID ) 
  84.   HANDLE hModuleSnap = INVALID_HANDLE_VALUE; 
  85.   MODULEENTRY32 me32; 
  86.  
  87.   // Take a snapshot of all modules in the specified process. 
  88.   hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID ); 
  89.   if( hModuleSnap == INVALID_HANDLE_VALUE ) 
  90.   { 
  91.     printError( "CreateToolhelp32Snapshot (of modules)" ); 
  92.     return( FALSE ); 
  93.   } 
  94.  
  95.   // Set the size of the structure before using it. 
  96.   me32.dwSize = sizeof( MODULEENTRY32 ); 
  97.  
  98.   // Retrieve information about the first module, 
  99.   // and exit if unsuccessful 
  100.   if( !Module32First( hModuleSnap, &me32 ) ) 
  101.   { 
  102.     printError( "Module32First" ); // Show cause of failure 
  103.     CloseHandle( hModuleSnap );    // Must clean up the 
  104.                                    //   snapshot object! 
  105.     return( FALSE ); 
  106.   } 
  107.  
  108.   // Now walk the module list of the process, 
  109.   // and display information about each module 
  110.   do 
  111.   { 
  112.     printf( "\n\n     MODULE NAME:     %s"
  113.       me32.szModule ); 
  114.     printf( "\n     executable     = %s"
  115.       me32.szExePath ); 
  116.     printf( "\n     process ID     = 0x%08X"
  117.       me32.th32ProcessID ); 
  118.     printf( "\n     ref count (g)  =     0x%04X"
  119.       me32.GlblcntUsage ); 
  120.     printf( "\n     ref count (p)  =     0x%04X"
  121.       me32.ProccntUsage ); 
  122.     printf( "\n     base address   = 0x%08X"
  123.       (DWORD) me32.modBaseAddr ); 
  124.     printf( "\n     base size      = %d"
  125.       me32.modBaseSize ); 
  126.  
  127.   } while( Module32Next( hModuleSnap, &me32 ) ); 
  128.  
  129.   CloseHandle( hModuleSnap ); 
  130.   return( TRUE ); 
  131.  
  132. BOOL ListProcessThreads( DWORD dwOwnerPID )  
  133. {  
  134.   HANDLE hThreadSnap = INVALID_HANDLE_VALUE;  
  135.   THREADENTRY32 te32;  
  136.   
  137.   // Take a snapshot of all running threads   
  138.   hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );  
  139.   if( hThreadSnap == INVALID_HANDLE_VALUE )  
  140.     return( FALSE );  
  141.   
  142.   // Fill in the size of the structure before using it.  
  143.   te32.dwSize = sizeof(THREADENTRY32 );  
  144.   
  145.   // Retrieve information about the first thread, 
  146.   // and exit if unsuccessful 
  147.   if( !Thread32First( hThreadSnap, &te32 ) )  
  148.   { 
  149.     printError( "Thread32First" ); // Show cause of failure 
  150.     CloseHandle( hThreadSnap );    // Must clean up the 
  151.                                    //   snapshot object! 
  152.     return( FALSE ); 
  153.   } 
  154.  
  155.   // Now walk the thread list of the system, 
  156.   // and display information about each thread 
  157.   // associated with the specified process 
  158.   do  
  159.   {  
  160.     if( te32.th32OwnerProcessID == dwOwnerPID ) 
  161.     { 
  162.       printf( "\n\n     THREAD ID      = 0x%08X"
  163.         te32.th32ThreadID );  
  164.       printf( "\n     base priority  = %d", te32.tpBasePri );  
  165.       printf( "\n     delta priority = %d", te32.tpDeltaPri );  
  166.     } 
  167.   } while( Thread32Next(hThreadSnap, &te32 ) );  
  168.  
  169.   CloseHandle( hThreadSnap ); 
  170.   return( TRUE ); 
  171.  
  172. void printError( TCHAR* msg ) 
  173.   DWORD eNum; 
  174.   TCHAR sysMsg[256]; 
  175.   TCHAR* p; 
  176.  
  177.   eNum = GetLastError( ); 
  178.   FormatMessage( 
  179.          FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
  180.          NULL, eNum, 
  181.          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang. 
  182.          sysMsg, 256, NULL ); 
  183.  
  184.   // Trim the end of the line and terminate it with a null 
  185.   p = sysMsg; 
  186.   while( ( *p > 31 ) || ( *p == 9 ) ) 
  187.     ++p; 
  188.   do { *p-- = 0; } while( ( p >= sysMsg ) && 
  189.                           ( ( *p == '.' ) || ( *p < 33 ) ) ); 
  190.  
  191.   // Display the message 
  192.   printf( "\n  WARNING: %s failed with error %d (%s)"
  193.     msg, eNum, sysMsg ); 

 










本文转自 hakuyo 51CTO博客,原文链接:http://blog.51cto.com/hakuyo/1134981,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
Windows
windows环境下根据端口号查询进程编号并杀掉此进程
windows环境下根据端口号查询进程编号并杀掉此进程
|
6月前
|
Windows
windows windows10 查看进程的命令行
windows windows10 查看进程的命令行
41 0
|
2月前
|
XML Go 数据格式
Windows自定义后台进程并设置为开机启动
可以在`Windows`上配置任意一个可执行文件后台启动,并且设置为开机启动。
Windows自定义后台进程并设置为开机启动
|
5月前
|
Windows
5.4 Windows驱动开发:内核通过PEB取进程参数
PEB结构`(Process Envirorment Block Structure)`其中文名是进程环境块信息,进程环境块内部包含了进程运行的详细参数信息,每一个进程在运行后都会存在一个特有的PEB结构,通过附加进程并遍历这段结构即可得到非常多的有用信息。在应用层下,如果想要得到PEB的基地址只需要取`fs:[0x30]`即可,TEB线程环境块则是`fs:[0x18]`,如果在内核层想要得到应用层进程的PEB信息我们需要调用特定的内核函数来获取。
49 0
5.4 Windows驱动开发:内核通过PEB取进程参数
|
5月前
|
监控 安全 API
7.1 Windows驱动开发:内核监控进程与线程回调
在前面的文章中`LyShark`一直在重复的实现对系统底层模块的枚举,今天我们将展开一个新的话题,内核监控,我们以`监控进程线程`创建为例,在`Win10`系统中监控进程与线程可以使用微软提供给我们的两个新函数来实现,此类函数的原理是创建一个回调事件,当有进程或线程被创建或者注销时,系统会通过回调机制将该进程相关信息优先返回给我们自己的函数待处理结束后再转向系统层。
61 0
7.1 Windows驱动开发:内核监控进程与线程回调
|
5月前
|
存储 Windows
4.6 Windows驱动开发:内核遍历进程VAD结构体
在上一篇文章`《内核中实现Dump进程转储》`中我们实现了ARK工具的转存功能,本篇文章继续以内存为出发点介绍`VAD`结构,该结构的全程是`Virtual Address Descriptor`即`虚拟地址描述符`,VAD是一个`AVL`自`平衡二叉树`,树的每一个节点代表一段虚拟地址空间。程序中的代码段,数据段,堆段都会各种占用一个或多个`VAD`节点,由一个`MMVAD`结构完整描述。
41 0
4.6 Windows驱动开发:内核遍历进程VAD结构体
|
5月前
|
存储 数据安全/隐私保护 Windows
4.5 Windows驱动开发:内核中实现进程数据转储
多数ARK反内核工具中都存在驱动级别的内存转存功能,该功能可以将应用层中运行进程的内存镜像转存到特定目录下,内存转存功能在应对加壳程序的分析尤为重要,当进程在内存中解码后,我们可以很容易的将内存镜像导出,从而更好的对样本进行分析,当然某些加密壳可能无效但绝大多数情况下是可以被转存的。
24 0
4.5 Windows驱动开发:内核中实现进程数据转储
|
5月前
|
监控 Windows
4.4 Windows驱动开发:内核监控进程与线程创建
当你需要在Windows操作系统中监控进程的启动和退出时,可以使用`PsSetCreateProcessNotifyRoutineEx`函数来创建一个`MyCreateProcessNotifyEx`回调函数,该回调函数将在每个进程的创建和退出时被调用。PsSetCreateProcessNotifyRoutineEx 用于在系统启动后向内核注册一个回调函数,以监视新进程的创建和退出,
41 0
4.4 Windows驱动开发:内核监控进程与线程创建
|
5月前
|
监控 安全 Windows
4.3 Windows驱动开发:监控进程与线程对象操作
在内核中,可以使用`ObRegisterCallbacks`这个内核回调函数来实现监控进程和线程对象操作。通过注册一个`OB_CALLBACK_REGISTRATION`回调结构体,可以指定所需的回调函数和回调的监控类型。这个回调结构体包含了回调函数和监控的对象类型,还有一个`Altitude`字段,用于指定回调函数的优先级。优先级越高的回调函数会先被调用,如果某个回调函数返回了一个非NULL值,后续的回调函数就不会被调用。当有进程或线程对象创建、删除、复制或重命名时,内核会调用注册的回调函数。回调函数可以访问被监控对象的信息,如句柄、进程ID等,并可以采取相应的操作,如打印日志、记录信息等。
32 0
4.3 Windows驱动开发:监控进程与线程对象操作
|
6月前
|
存储 安全 调度
4.2 Windows驱动开发:内核中进程线程与模块
内核进程线程和模块是操作系统内核中非常重要的概念。它们是操作系统的核心部分,用于管理系统资源和处理系统请求。在驱动安全开发中,理解内核进程线程和模块的概念对于编写安全的内核驱动程序至关重要。内核进程是在操作系统内核中运行的程序。每个进程都有一个唯一的进程标识符(PID),它用于在系统中唯一地标识该进程。在内核中,进程被表示为一个进程控制块(PCB),它包含有关进程的信息,如进程状态、优先级、内存使用情况等。枚举进程可以让我们获取当前系统中所有正在运行的进程的PID和其他有用的信息,以便我们可以监视和管理系统中的进程。
70 0
4.2 Windows驱动开发:内核中进程线程与模块