{"componentChunkName":"component---src-templates-post-template-js","path":"/win32api-getprocesslist-en","result":{"data":{"markdownRemark":{"id":"c8e29a33-d130-505e-a2b6-524c9106b552","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/win32api-getprocesslist\">original page</a>.</p>\n</blockquote>\n<p>While studying the behavior of code injection in Malware, I came to wonder how Malware identifies the PID to reference in the OpenProcess API.</p>\n<p>The Injection section on <a href=\"https://malapi.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">MalAPI.io</a> lists the following three APIs, and one implementation example appears to be using <code class=\"language-text\">CreateToolhelp32Snapshot</code>.</p>\n<ul>\n<li>CreateToolhelp32Snapshot</li>\n<li>Process32First</li>\n<li>Process32Next</li>\n</ul>\n<p>So, this article summarizes how to enumerate process information in the system using the Win32 API’s <code class=\"language-text\">CreateToolhelp32Snapshot</code>.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Taking a Snapshot and Viewing Processes - Win32 apps | Microsoft Learn</a></p>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/toolhelp/snapshots-of-the-system\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Snapshots of the System - Win32 apps | Microsoft Learn</a></p>\n<!-- omit in toc -->\n<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of Contents</h2>\n<ul>\n<li><a href=\"#preface\">Preface</a></li>\n<li><a href=\"#apis-used-in-this-article\">APIs Used in This Article</a></li>\n<li><a href=\"#createtoolhelp32snapshot\">CreateToolhelp32Snapshot</a></li>\n<li><a href=\"#process32first\">Process32First</a></li>\n<li><a href=\"#process32next\">Process32Next</a></li>\n<li><a href=\"#sample-program\">Sample Program</a></li>\n<li><a href=\"#summary\">Summary</a></li>\n</ul>\n<h2 id=\"preface\" style=\"position:relative;\"><a href=\"#preface\" aria-label=\"preface permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Preface</h2>\n<p>All content in this article is created based solely on publicly available information, published books, or results from testing in personal verification environments.</p>\n<p>Related articles:</p>\n<ul>\n<li><a href=\"/windbg-basics-001\">WinDbg でダンプ解析、ライブデバッグを行う時のチートシート</a></li>\n<li><a href=\"/windbg-process-001\">WinDbg で Windows のプロセス情報を読むためのメモ書き</a></li>\n<li><a href=\"/windbg-gflags-tutorial-en\">Knowledge Base for Advanced Debugging with GFlags Global Flags</a></li>\n</ul>\n<h2 id=\"apis-used-in-this-article\" style=\"position:relative;\"><a href=\"#apis-used-in-this-article\" aria-label=\"apis used in this article permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>APIs Used in This Article</h2>\n<h3 id=\"createtoolhelp32snapshot\" style=\"position:relative;\"><a href=\"#createtoolhelp32snapshot\" aria-label=\"createtoolhelp32snapshot permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>CreateToolhelp32Snapshot</h3>\n<p>The <code class=\"language-text\">CreateToolhelp32Snapshot</code> function is a function that can take a snapshot of the heap, modules, and threads of a specified process.</p>\n<p>This function retrieves information for the process with the PID specified in <code class=\"language-text\">th32ProcessID</code>, but if <code class=\"language-text\">dFlags</code> is given <code class=\"language-text\">TH32CS_SNAPHEAPLIST</code>, <code class=\"language-text\">TH32CS_SNAPMODULE</code>, <code class=\"language-text\">TH32CS_SNAPMODULE32</code>, or <code class=\"language-text\">TH32CS_SNAPALL</code>, it is ignored and returns information for all processes.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">HANDLE <span class=\"token function\">CreateToolhelp32Snapshot</span><span class=\"token punctuation\">(</span>\n  <span class=\"token punctuation\">[</span>in<span class=\"token punctuation\">]</span> DWORD dwFlags<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">[</span>in<span class=\"token punctuation\">]</span> DWORD th32ProcessID\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>If this function executes successfully, it returns a handle to the obtained snapshot as the return value.</p>\n<p>You can reference information within the snapshot using helper functions like <code class=\"language-text\">Process32First</code> described later.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">CreateToolhelp32Snapshot function (tlhelp32.h) - Win32 apps | Microsoft Learn</a></p>\n<p>I investigated what this function actually does by enumerating the functions it calls using WinDbg.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">KERNEL32<span class=\"token operator\">!</span>CreateToolhelp32Snapshot\n<span class=\"token operator\">==</span><span class=\"token operator\">=</span><span class=\"token operator\">></span>\ncall to KERNELBASE<span class=\"token operator\">!</span>GetCurrentProcessId <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3ae04080<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>ThpCreateRawSnap <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3fe480<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>ULongMult <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3ffd84<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>ULongMult <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3ffd84<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>ThpAllocateSnapshotSection <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3fd8c4<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>ThpProcessToSnap <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d3fb274<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span>BaseSetLastNTError <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d3f30e0<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>NtClose <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d66d230<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>NtUnmapViewOfSection <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d66d590<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>NtFreeVirtualMemory <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d66d410<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlDestroyQueryDebugBuffer <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d6a76d0<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlDestroyQueryDebugBuffer <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d6a76d0<span class=\"token punctuation\">)</span></code></pre></div>\n<p>This alone doesn’t make things very clear, but the <code class=\"language-text\">ThpCreateRawSnap</code> function and <code class=\"language-text\">ThpAllocateSnapshotSection</code> function seem to be closely related to the snapshot retrieval process.</p>\n<p>The <code class=\"language-text\">ThpCreateRawSnap</code> function appears to be retrieving system information and process information, judging by the names of the functions it calls.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">KERNEL32<span class=\"token operator\">!</span>ThpCreateRawSnap\n<span class=\"token operator\">==</span><span class=\"token operator\">=</span><span class=\"token operator\">></span>\ncall to ntdll<span class=\"token operator\">!</span>NtAllocateVirtualMemory <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d66d350<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>NtQuerySystemInformation <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d66d710<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlDestroyQueryDebugBuffer <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d6a76d0<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlCreateQueryDebugBuffer <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d6a7420<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlQueryProcessDebugInformation <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d6a78a0<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>NtFreeVirtualMemory <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d66d410<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlCreateQueryDebugBuffer <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d6a7420<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlQueryProcessDebugInformation <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d6a78a0<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>NtFreeVirtualMemory <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\"><span class=\"token variable\">`</span>3d66d410<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span>RtlDestroyQueryDebugBuffer <span class=\"token punctuation\">(</span>00007fff<span class=\"token variable\">`</span></span>3d6a76d0<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntquerysysteminformation\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">NtQuerySystemInformation function (winternl.h) - Win32 apps | Microsoft Learn</a></p>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntqueryinformationprocess\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">NtQueryInformationProcess function (winternl.h) - Win32 apps | Microsoft Learn</a></p>\n<p>Also, the <code class=\"language-text\">ThpAllocateSnapshotSection</code> function was calling the following functions.</p>\n<p>I wonder if this is where the area to store the handle is actually allocated?</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">KERNEL32<span class=\"token operator\">!</span>ThpAllocateSnapshotSection\n<span class=\"token operator\">==</span><span class=\"token operator\">=</span><span class=\"token operator\">></span>\ncall to KERNELBASE<span class=\"token operator\">!</span><span class=\"token function\">BaseFormatObjectAttributes</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>addf5f0<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span><span class=\"token function\">NtCreateSection</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d66d990<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span><span class=\"token function\">NtMapViewOfSection</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d66d550<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntcreatesection\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">NtCreateSection 関数 (ntifs.h) - Windows drivers | Microsoft Learn</a></p>\n<p>Reference: <a href=\"https://www.ired.team/offensive-security/code-injection-process-injection/ntcreatesection-+-ntmapviewofsection-code-injection\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">NtCreateSection + NtMapViewOfSection Code Injection - Red Team Notes</a></p>\n<p>Reference: <a href=\"https://malapi.io/winapi/NtMapViewOfSection\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">MalAPI.io NtMapViewOfSection</a></p>\n<h3 id=\"process32first\" style=\"position:relative;\"><a href=\"#process32first\" aria-label=\"process32first permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Process32First</h3>\n<p>A function to retrieve information about the first process in the snapshot.</p>\n<p>If the function executes successfully it returns True, and if it fails it returns False. (It returns an error value when no process exists, etc.)</p>\n<p>The retrieved process information is stored in the area pointed to by the pointer address of the <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PROCESSENTRY32 (tlhelp32.h)</a> structure given as the second argument.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">BOOL <span class=\"token function\">Process32First</span><span class=\"token punctuation\">(</span>\n  <span class=\"token punctuation\">[</span>in<span class=\"token punctuation\">]</span>      HANDLE           hSnapshot<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">[</span>in<span class=\"token punctuation\">,</span> out<span class=\"token punctuation\">]</span> LPPROCESSENTRY32 lppe\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PROCESSENTRY32 (tlhelp32.h)</a> structure contains the following information:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token keyword\">typedef</span> <span class=\"token keyword\">struct</span> <span class=\"token class-name\">tagPROCESSENTRY32</span> <span class=\"token punctuation\">{</span>\n  DWORD     dwSize<span class=\"token punctuation\">;</span>\n  DWORD     cntUsage<span class=\"token punctuation\">;</span>\n  DWORD     th32ProcessID<span class=\"token punctuation\">;</span>\n  ULONG_PTR th32DefaultHeapID<span class=\"token punctuation\">;</span>\n  DWORD     th32ModuleID<span class=\"token punctuation\">;</span>\n  DWORD     cntThreads<span class=\"token punctuation\">;</span>\n  DWORD     th32ParentProcessID<span class=\"token punctuation\">;</span>\n  LONG      pcPriClassBase<span class=\"token punctuation\">;</span>\n  DWORD     dwFlags<span class=\"token punctuation\">;</span>\n  CHAR      szExeFile<span class=\"token punctuation\">[</span>MAX_PATH<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span> PROCESSENTRY32<span class=\"token punctuation\">;</span></code></pre></div>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Process32First function (tlhelp32.h) - Win32 apps | Microsoft Learn</a></p>\n<p>The functions being called are like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">KERNEL32<span class=\"token operator\">!</span>Process32FirstW\n<span class=\"token operator\">==</span><span class=\"token operator\">=</span><span class=\"token operator\">></span>\ncall to ntdll<span class=\"token operator\">!</span><span class=\"token function\">NtMapViewOfSection</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d66d550<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span><span class=\"token function\">NtUnmapViewOfSection</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d66d590<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span><span class=\"token function\">memset</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d408147<span class=\"token punctuation\">)</span>\ncall to ntdll<span class=\"token operator\">!</span><span class=\"token function\">RtlSetLastWin32Error</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d6207c0<span class=\"token punctuation\">)</span>\ncall to KERNEL32<span class=\"token operator\">!</span><span class=\"token function\">BaseSetLastNTError</span> <span class=\"token punctuation\">(</span><span class=\"token number\">00007fff</span>`<span class=\"token number\">3</span>d3f30e0<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Looking in the debugger, data that appears to be from <code class=\"language-text\">PROCESSENTRY32->szExeFile</code> was stored at the pointer address given as an argument after the call to <code class=\"language-text\">ntdll!NtMapViewOfSection</code>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 606px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9e06824fd4e6c16c8aebbdb367377940/4d4a2/image-20230503221321369.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 31.666666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA9ElEQVQY002RaYqEQAxGPYn7ioq7liviJXp6Woe+/yW+8Qso/giVCqmXF9Ve7w+yLJNo2xZhGML3fURRBM/z5M7gncE8jmPpsSzrDtM0JbT97wulOhRFgWVZBFpVFdZ1lZy1vu8xjiPmeZbatm3I8/yGMGzbhq7r0H5+d9R1jbIsMQyDgGlLCGs8OaBpGnRdd/elaQrXdcU4CAKxpqm2H9+zsRXQNE3ymI9oxEF8TCuCmbNGaw4lKEkSgRIuhu/PIdMvCM8nnJDLTiklNa5OQ65KEMNxHBiGwZWPs6lEdn6T58rM+ZhmtKIlgddgmhHw/DE0/AcIXr0za3r+QgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/9e06824fd4e6c16c8aebbdb367377940/8ac56/image-20230503221321369.webp 240w,\n/static/9e06824fd4e6c16c8aebbdb367377940/d3be9/image-20230503221321369.webp 480w,\n/static/9e06824fd4e6c16c8aebbdb367377940/18e7e/image-20230503221321369.webp 606w\"\n              sizes=\"(max-width: 606px) 100vw, 606px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/9e06824fd4e6c16c8aebbdb367377940/8ff5a/image-20230503221321369.png 240w,\n/static/9e06824fd4e6c16c8aebbdb367377940/e85cb/image-20230503221321369.png 480w,\n/static/9e06824fd4e6c16c8aebbdb367377940/4d4a2/image-20230503221321369.png 606w\"\n            sizes=\"(max-width: 606px) 100vw, 606px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/9e06824fd4e6c16c8aebbdb367377940/4d4a2/image-20230503221321369.png\"\n            alt=\"image-20230503221321369\"\n            title=\"image-20230503221321369\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<h3 id=\"process32next\" style=\"position:relative;\"><a href=\"#process32next\" aria-label=\"process32next permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Process32Next</h3>\n<p>Retrieves information about the next process recorded in the system snapshot.</p>\n<p>This function can retrieve the <code class=\"language-text\">PROCESSENTRY32</code> of the next process by providing the same snapshot handle given to the <code class=\"language-text\">Process32FirstW</code> function.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\">BOOL <span class=\"token function\">Process32Next</span><span class=\"token punctuation\">(</span>\n  <span class=\"token punctuation\">[</span>in<span class=\"token punctuation\">]</span>  HANDLE           hSnapshot<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">[</span>out<span class=\"token punctuation\">]</span> LPPROCESSENTRY32 lppe\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PROCESSENTRY32 (tlhelp32.h) - Win32 apps | Microsoft Learn</a></p>\n<p>The snapshot handle appears to have a buffer for identifying the currently referenced process information. When the <code class=\"language-text\">Process32First</code> function is executed, it points to the first one, and when the <code class=\"language-text\">Process32Next</code> function is executed, it sequentially points to the next process one by one.</p>\n<p>Therefore, if you execute the <code class=\"language-text\">Process32First</code> function in the middle of a loop executing the <code class=\"language-text\">Process32Next</code> function, the reference buffer returns to the first one, causing the process to loop continuously.</p>\n<h2 id=\"sample-program\" style=\"position:relative;\"><a href=\"#sample-program\" aria-label=\"sample program permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sample Program</h2>\n<p>I created a program that enumerates process information using these APIs.</p>\n<p>This program retrieves a snapshot of all processes by specifying <code class=\"language-text\">TH32CS_SNAPPROCESS</code> as an argument to the <code class=\"language-text\">CreateToolhelp32Snapshot</code> function, and loops the helper function to sequentially output the process name, PID, and number of running threads from the <code class=\"language-text\">PROCESSENTRY32</code> structure of each process from the beginning.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;windows.h></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;tlhelp32.h></span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">include</span> <span class=\"token string\">&lt;tchar.h></span></span>\n\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">pragma</span> <span class=\"token expression\"><span class=\"token function\">comment</span><span class=\"token punctuation\">(</span>lib<span class=\"token punctuation\">,</span> </span><span class=\"token string\">\"advapi32.lib\"</span><span class=\"token expression\"><span class=\"token punctuation\">)</span></span></span>\n\n<span class=\"token keyword\">int</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n<span class=\"token comment\">//HANDLE CreateToolhelp32Snapshot(</span>\n<span class=\"token comment\">//[in] DWORD dwFlags,</span>\n<span class=\"token comment\">//[in] DWORD th32ProcessID</span>\n<span class=\"token comment\">//);</span>\n\nHANDLE hToolhelp32Snapshot <span class=\"token operator\">=</span> <span class=\"token constant\">NULL</span><span class=\"token punctuation\">;</span>\nhToolhelp32Snapshot <span class=\"token operator\">=</span> <span class=\"token function\">CreateToolhelp32Snapshot</span><span class=\"token punctuation\">(</span>\nTH32CS_SNAPPROCESS<span class=\"token punctuation\">,</span>\n<span class=\"token constant\">NULL</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>hToolhelp32Snapshot <span class=\"token operator\">==</span> INVALID_HANDLE_VALUE<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"ERROR: Could not get a Toolhelp32Snapshot\\n\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"Faild with %u.\\n\"</span><span class=\"token punctuation\">,</span> <span class=\"token function\">GetLastError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">return</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">//typedef struct tagPROCESSENTRY32</span>\n<span class=\"token comment\">//{</span>\n<span class=\"token comment\">//DWORD   dwSize;</span>\n<span class=\"token comment\">//DWORD   cntUsage;</span>\n<span class=\"token comment\">//DWORD   th32ProcessID;</span>\n<span class=\"token comment\">//ULONG_PTR th32DefaultHeapID;</span>\n<span class=\"token comment\">//DWORD   th32ModuleID;</span>\n<span class=\"token comment\">//DWORD   cntThreads;</span>\n<span class=\"token comment\">//DWORD   th32ParentProcessID;</span>\n<span class=\"token comment\">//LONG    pcPriClassBase;</span>\n<span class=\"token comment\">//DWORD   dwFlags;</span>\n<span class=\"token comment\">//CHAR    szExeFile[MAX_PATH];</span>\n<span class=\"token comment\">//} PROCESSENTRY32;</span>\n\n<span class=\"token comment\">/*BOOL Process32First(\n[in]      HANDLE           hSnapshot,\n[in, out] LPPROCESSENTRY32 lppe\n);*/</span>\n\nPROCESSENTRY32 pe32<span class=\"token punctuation\">;</span>\npe32<span class=\"token punctuation\">.</span>dwSize <span class=\"token operator\">=</span> <span class=\"token keyword\">sizeof</span><span class=\"token punctuation\">(</span>PROCESSENTRY32<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token function\">Process32First</span><span class=\"token punctuation\">(</span>hToolhelp32Snapshot<span class=\"token punctuation\">,</span> <span class=\"token operator\">&amp;</span>pe32<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n<span class=\"token keyword\">do</span> <span class=\"token punctuation\">{</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"Process name is : %s\\n\"</span><span class=\"token punctuation\">,</span> pe32<span class=\"token punctuation\">.</span>szExeFile<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"=====> PID : %d\\n\"</span><span class=\"token punctuation\">,</span> pe32<span class=\"token punctuation\">.</span>th32ProcessID<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"=====> Threads count : %d\\n\"</span><span class=\"token punctuation\">,</span> pe32<span class=\"token punctuation\">.</span>cntThreads<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">while</span> <span class=\"token punctuation\">(</span><span class=\"token function\">Process32Next</span><span class=\"token punctuation\">(</span>hToolhelp32Snapshot<span class=\"token punctuation\">,</span> <span class=\"token operator\">&amp;</span>pe32<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"Got all process entries.\\n\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"ERROR: Could not get the first PROCESSENTRY32.\\n\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">wprintf</span><span class=\"token punctuation\">(</span>L<span class=\"token string\">\"Faild with %u.\\n\"</span><span class=\"token punctuation\">,</span> <span class=\"token function\">GetLastError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token function\">CloseHandle</span><span class=\"token punctuation\">(</span>hToolhelp32Snapshot<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">return</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">return</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The execution result looked like this:</p>\n<p>I’m running it from a command prompt launched with administrator privileges, but it also worked effectively at Medium Integrity.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 456px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/5bce525415364dfe730416305b316fb5/7f664/image-20230503222907883.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 91.25000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsTAAALEwEAmpwYAAABvElEQVQ4y5WU13LCMBBF6c29F1wwgQzk//9vo7OMPJk8BOVBY9ngo7NXKy/q4S7T5SLP55ckSSKpGVmeS55nslgsdGw2m3n+dnTTQ6qykLIs5Xw+SxRFEsexBEGgC+QGXte1zg+Hw3tg0U4S+J6+0LatgpumUShmnufJer3+h+H1oeVhMk2TwrIsU3iaplJVlYzjqFff93UhBv+hCgb386Jld5Uw8PUP1gwYC2B4Op1ktVrNBsyXy+X8jOvP31+GWao2mABlTukswpVcd7udW8lkGBptjDDj5a7rFEYZgKyVE3C4fUlZFGo1DIPuNGVjxiI2q/1+7wbM24tmWBgohrwMGBAZAsLU2bC/PiUzdliRYd/32kL0HlCeH49H2W63bsC6/zCnIzaG5VwuGdJ/QIDZHJ2A7fgpUfjqJYD2tNB3ZEi2ACndCVoPN20bAGQXhqFmSbnMaWaO3O9+/NMwjkJj9SoVQ6zIkA0CStnOJVdkmCZSGQAbAgRDNoYcGRg6b0oz3jXDIAjnDMkOW8pkAYC0ENC3nzJKLszHwX4E2BCamnt78G1zW9u3m+J7J80KCA2OIVdsyM/502XGN7nNkhh7cZckAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/5bce525415364dfe730416305b316fb5/8ac56/image-20230503222907883.webp 240w,\n/static/5bce525415364dfe730416305b316fb5/646b7/image-20230503222907883.webp 456w\"\n              sizes=\"(max-width: 456px) 100vw, 456px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/5bce525415364dfe730416305b316fb5/8ff5a/image-20230503222907883.png 240w,\n/static/5bce525415364dfe730416305b316fb5/7f664/image-20230503222907883.png 456w\"\n            sizes=\"(max-width: 456px) 100vw, 456px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/5bce525415364dfe730416305b316fb5/7f664/image-20230503222907883.png\"\n            alt=\"image-20230503222907883\"\n            title=\"image-20230503222907883\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>It seems that when retrieving process information on Windows, you need to first obtain a snapshot object and then pass it to the program.</p>\n<p>I wonder if Task Manager uses the same implementation?</p>\n<p>I’d like to attach a debugger and investigate next time.</p>","fields":{"slug":"/win32api-getprocesslist-en","tagSlugs":["/tag/win-32-api-en/","/tag/malware-en/","/tag/win-dbg-en/","/tag/reversing-en/","/tag/english/"]},"frontmatter":{"date":"2023-05-03","description":"I tried enumerating process information in the system using Win32 API.","tags":["Win32API (en)","Malware (en)","WinDbg (en)","Reversing (en)","English"],"title":"Enumerating Process Information in the System with Win32 API","socialImage":{"publicURL":"/static/64aef3f47d21bab71d5b71e5debe5b03/win32api-getprocesslist.png"}}}},"pageContext":{"slug":"/win32api-getprocesslist-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}