<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Baboon on Medium]]></title>
        <description><![CDATA[Stories by Baboon on Medium]]></description>
        <link>https://medium.com/@faischer.02?source=rss-407cf0b78e07------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EGdOhl89mR5lZo_JV3NSCA@2x.jpeg</url>
            <title>Stories by Baboon on Medium</title>
            <link>https://medium.com/@faischer.02?source=rss-407cf0b78e07------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:32:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@faischer.02/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Block ProcDump from Dumping Your Process Memory]]></title>
            <link>https://medium.com/@faischer.02/how-to-block-procdump-from-dumping-your-process-memory-0dcbc43baabb?source=rss-407cf0b78e07------2</link>
            <guid isPermaLink="false">https://medium.com/p/0dcbc43baabb</guid>
            <category><![CDATA[window-security-solutions]]></category>
            <category><![CDATA[memory-dump]]></category>
            <category><![CDATA[procdump]]></category>
            <category><![CDATA[windows-api]]></category>
            <dc:creator><![CDATA[Baboon]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 07:03:41 GMT</pubDate>
            <atom:updated>2025-03-06T04:43:16.610Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Memory dumping allows developers and security analysts to capture a process’s memory for debugging and forensic analysis. However, attackers can also use this technique to steal sensitive data. A widely used tool for memory dumping on Windows is <strong>ProcDump</strong>, a Sysinternals utility designed for monitoring applications and capturing crash dumps. To safeguard sensitive information, it’s crucial to prevent unauthorized memory dumping of a process.</p><p>In this blog, we will demonstrate how to modify the <strong>Discretionary Access Control List (DACL)</strong> of a process to prevent ProcDump (or similar tools) from dumping its memory. This method leverages Windows API functions to modify access rights dynamically.</p><h3>Understanding the Concept</h3><p>Windows security allows you to manage access to objects such as processes using <strong>Access Control Lists (ACLs)</strong>. A DACL contains <strong>Access Control Entries (ACEs)</strong> that specify which users or groups can perform actions on a process.</p><p>By modifying a process’s DACL, we can restrict access to prevent tools like <strong>ProcDump</strong> from reading its memory. This ensures that only authorized users or processes can interact with the target process.</p><h3>Implementing the Protection</h3><h4>ProcDump Permission Denied Example</h4><p>Below is an example of running <strong>ProcDump</strong> against a protected process, resulting in an Access is denied (0x00000005) error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*v6CndbGwH3xbBBgMOON9Xw.png" /></figure><p><strong>Note:</strong> If ProcDump is executed with <strong>Administrator privileges</strong>, it may still be able to dump memory despite the ACL restrictions.</p><p>Below is a C++ implementation that modifies the DACL of the current process to restrict access and prevent memory dumps.</p><h4>Full Code</h4><pre>#include &lt;windows.h&gt;<br>#include &lt;aclapi.h&gt;<br>#include &lt;iostream&gt;<br>#define SECURITY_MAX_SID_SIZE 68  // Maximum SID <br>    HANDLE hToken = nullptr;<br>    PTOKEN_USER pTokenUser = nullptr; // Pointer to token user<br>    DWORD cbBufferSize = 0; // Variable to store the size of token info<br>    PSID pLocalSystemSid = nullptr; // LocalSystem SID (higher than Administrator)<br>    DWORD pLocalSystemSidSize = SECURITY_MAX_SID_SIZE;<br>    PSID pOwnerRightsSid = nullptr; // SID of the process owner<br>    DWORD pOwnerRightsSidSize = SECURITY_MAX_SID_SIZE;<br>    PACL pACL = nullptr; // Pointer to Access Control List (ACL)<br>    DWORD cbACL = 0; // Variable to store ACL size<br>    <br>    bool bSuccess = false;<br>// Open the access token of the current process<br>    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &amp;hToken)) {<br>        goto Cleanup;<br>    }<br>    // Retrieve the token size<br>    GetTokenInformation(hToken, TokenUser, nullptr, 0, &amp;cbBufferSize);<br>    pTokenUser = static_cast&lt;PTOKEN_USER&gt;(HeapAlloc(GetProcessHeap(), 0, cbBufferSize));<br>    if (pTokenUser == nullptr) {<br>        goto Cleanup;<br>    }<br>    // Retrieve token user information<br>    if (!GetTokenInformation(hToken, TokenUser, pTokenUser, cbBufferSize, &amp;cbBufferSize)) {<br>        HeapFree(GetProcessHeap(), 0, pTokenUser);<br>        goto Cleanup;<br>    }<br>    // Retrieve LocalSystem SID<br>    pLocalSystemSid = static_cast&lt;PSID&gt;(HeapAlloc(GetProcessHeap(), 0, pLocalSystemSidSize));<br>    if (pLocalSystemSid == nullptr) {<br>        goto Cleanup;<br>    }<br>    if (!CreateWellKnownSid(WinLocalSystemSid, nullptr, pLocalSystemSid, &amp;pLocalSystemSidSize)) {<br>        goto Cleanup;<br>    }<br>    <br>    // Retrieve Creator Owner Rights SID<br>    pOwnerRightsSid = static_cast&lt;PSID&gt;(HeapAlloc(GetProcessHeap(), 0, pOwnerRightsSidSize));<br>    if (pOwnerRightsSid == nullptr) {<br>        goto Cleanup;<br>    }<br>    if (!CreateWellKnownSid(WinCreatorOwnerRightsSid, nullptr, pOwnerRightsSid, &amp;pOwnerRightsSidSize)) {<br>        goto Cleanup;<br>    }<br>    // Calculate the required ACL size<br>    cbACL = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pTokenUser-&gt;User.Sid)<br>                + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pLocalSystemSid) + sizeof(ACCESS_ALLOWED_ACE)<br>                + GetLengthSid(pOwnerRightsSid);<br>    pACL = static_cast&lt;PACL&gt;(HeapAlloc(GetProcessHeap(), 0, cbACL));<br>    if (pACL == nullptr) {<br>        goto Cleanup;<br>    }<br>    if (!InitializeAcl(pACL, cbACL, ACL_REVISION)) {<br>        goto Cleanup;<br>    }<br>    // Restrict memory read and query access to block ProcDump<br>    if (!AddAccessAllowedAce(<br>                pACL,<br>                ACL_REVISION,<br>                SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_TERMINATE, // Permissions<br>                pTokenUser-&gt;User.Sid <br>                )) {<br>        goto Cleanup;<br>    }<br>    // Explicitly set &quot;Process Owner&quot; rights to Read Only<br>    if (!AddAccessAllowedAce(pACL, ACL_REVISION, READ_CONTROL, pOwnerRightsSid)) {<br>        goto Cleanup;<br>    }<br>    // Apply the modified DACL to the current process<br>    bSuccess = ERROR_SUCCESS == SetSecurityInfo(<br>                    GetCurrentProcess(), // Object handle<br>                    SE_KERNEL_OBJECT, // Type of object<br>                    DACL_SECURITY_INFORMATION, // Change only the object&#39;s DACL<br>                    nullptr, // Do not change owner<br>                    nullptr, // Do not change group<br>                    pACL, // DACL specified<br>                    nullptr // Do not change SACL<br>    );<br>    <br>    std::cout &lt;&lt; &quot;SetSecurityInfo: &quot; &lt;&lt; bSuccess &lt;&lt; std::endl;   <br>    if (bSuccess) {<br>        std::cout &lt;&lt; &quot;Process is now protected from memory dumping!&quot; &lt;&lt; std::endl;<br>    }<br>Cleanup:<br>    // Free allocated resources<br>    if (pACL) HeapFree(GetProcessHeap(), 0, pACL);<br>    if (pLocalSystemSid) HeapFree(GetProcessHeap(), 0, pLocalSystemSid);<br>    if (pOwnerRightsSid) HeapFree(GetProcessHeap(), 0, pOwnerRightsSid);<br>    if (pTokenUser) HeapFree(GetProcessHeap(), 0, pTokenUser);<br>    if (hToken) CloseHandle(hToken);<br>    <br>    return 0;<br>}</pre><h3>How It Works</h3><ul><li><strong>Retrieves the current process token</strong> to obtain security information.</li><li><strong>Fetches the process owner’s SID</strong> and <strong>well-known SIDs</strong> for LocalSystem and Owner Rights.</li><li><strong>Creates and configures an ACL</strong> with restricted permissions, preventing unauthorized access to process memory.</li><li><strong>Modifies the DACL</strong> of the process to enforce the restrictions.</li></ul><h3>Access Rights to Avoid for Preventing Memory Dump</h3><ol><li><strong>PROCESS_VM_REA</strong>– Allows reading process memory, which is the main way dumping tools like ProcDump work.</li><li><strong>PROCESS_DUP_HANDLE</strong> – Lets other processes copy handles, which can be used to access memory.</li><li><strong>PROCESS_ALL_ACCESS</strong> – Grants full control over the process, including memory access, making it easy to dump.</li></ol><h3>Compiling the Code</h3><p>To compile this C++ program, use <strong>Microsoft Visual Studio</strong> with the <strong>Windows 10 SDK</strong> installed.</p><h4>Steps to Compile using MSVC</h4><ol><li>Open the <strong>x64 Native Tools Command Prompt for VS</strong>.</li></ol><p>2. Navigate to the folder containing the C++ file.</p><p>3. Run the following command :</p><p>$ cl /EHsc &lt;file&gt; /link Advapi32.lib</p><pre>C:\Tools\test&gt;cl /EHsc full.cpp /link Advapi32.lib<br>Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34808 for x64<br>Copyright (C) Microsoft Corporation.  All rights reserved.<br><br>full.cpp<br>full.cpp(8): warning C4005: &#39;SECURITY_MAX_SID_SIZE&#39;: macro redefinition<br>C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\winnt.h(9903): note: see previous definition of &#39;SECURITY_MAX_SID_SIZE&#39;<br>Microsoft (R) Incremental Linker Version 14.43.34808.0<br>Copyright (C) Microsoft Corporation.  All rights reserved.<br><br>/out:full.exe<br>Advapi32.lib<br>full.obj</pre><h4>Steps to Test Memory Protection</h4><p>4. Run the executable file :</p><pre>C:\Tools\test&gt;full.exe<br>SetSecurityInfo: 1<br>Process is now protected from memory dumping!<br>PID: 20692<br>Press any key to continue . . .</pre><p>5. dumping process using procdump.exe :</p><p>$ procdump.exe -ma &lt;PID&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*JHi9DkX3Tg4T0c30o_DCuA.png" /></figure><h3>Additional Resources</h3><p>If you want to learn more about implementing Windows API security and coding best practices, check out our previous blog post:</p><p><a href="https://medium.com/@faischer.02/understanding-and-implementing-windows-dacl-in-c-c92b92539fb5">Understanding and Implementing Windows DACL in C++</a></p><h3>Conclusion</h3><p>By implementing this method, we can effectively prevent tools like <strong>ProcDump</strong> from dumping our process’s memory. This is a useful security enhancement for applications handling sensitive data, ensuring that unauthorized debugging or forensic analysis is not possible.</p><h3>References</h3><ul><li>Microsoft Docs: <a href="https://learn.microsoft.com/en-us/sysinternals/downloads/procdump">ProcDump — Sysinternals</a></li><li>Microsoft Docs: <a href="https://learn.microsoft.com/en-us/windows/win32/secauthz/security-descriptors-and-acls">Windows Security Descriptors and ACLs</a></li><li>Microsoft Docs: <a href="https://learn.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-setsecurityinfo">SetSecurityInfo Function</a></li><li>Microsoft Docs: <a href="https://learn.microsoft.com/th-th/windows/win32/procthread/process-security-and-access-rights">Protected Processes Flag</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0dcbc43baabb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding and Implementing Windows DACL in C++]]></title>
            <link>https://medium.com/@faischer.02/understanding-and-implementing-windows-dacl-in-c-c92b92539fb5?source=rss-407cf0b78e07------2</link>
            <guid isPermaLink="false">https://medium.com/p/c92b92539fb5</guid>
            <dc:creator><![CDATA[Baboon]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 06:40:20 GMT</pubDate>
            <atom:updated>2025-03-05T06:40:20.676Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog post, we will explore a C++ program that demonstrates how to work with Discretionary Access Control Lists (DACLs) in Windows. This program allows you to retrieve, modify, and update the DACL of the current process to control access permissions. Let’s break it down step by step.</p><h3>What is a DACL?</h3><p>A <strong>Discretionary Access Control List (DACL)</strong> is a list of Access Control Entries (ACEs) that specify which users and groups are allowed or denied access to an object. In this case, we are modifying the DACL of the current process.</p><h3><strong>Full Code</strong></h3><pre>#include &lt;windows.h&gt;<br>#include &lt;aclapi.h&gt;<br>#include &lt;sddl.h&gt;  <br>#include &lt;iostream&gt;<br><br>#define SECURITY_MAX_SID_SIZE 68<br><br>// Function to print DACL<br>void printDACL(PACL pACL, const char* message) {<br>    if (!pACL) {<br>        std::cout &lt;&lt; message &lt;&lt; &quot; DACL: NULL (Inherits default permissions)&quot; &lt;&lt; std::endl;<br>        return;<br>    }<br><br>    ACL_SIZE_INFORMATION aclSizeInfo;<br>    if (!GetAclInformation(pACL, &amp;aclSizeInfo, sizeof(aclSizeInfo), AclSizeInformation)) {<br>        std::cerr &lt;&lt; &quot;Failed to get ACL information. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        return;<br>    }<br><br>    std::cout &lt;&lt; message &lt;&lt; &quot; DACL Entries: &quot; &lt;&lt; aclSizeInfo.AceCount &lt;&lt; std::endl;<br>    <br>    for (DWORD i = 0; i &lt; aclSizeInfo.AceCount; i++) {<br>        LPVOID pAce;<br>        if (GetAce(pACL, i, &amp;pAce)) {<br>            ACCESS_ALLOWED_ACE* ace = (ACCESS_ALLOWED_ACE*)pAce;<br>            std::cout &lt;&lt; &quot;  ACE &quot; &lt;&lt; i &lt;&lt; &quot;: Access Mask = &quot; &lt;&lt; std::hex &lt;&lt; ace-&gt;Mask &lt;&lt; std::dec &lt;&lt; std::endl;<br>        }<br>    }<br>}<br><br>bool createWindowsDACL() {<br>    HANDLE hToken = nullptr;<br>    PTOKEN_USER pTokenUser = nullptr;<br>    DWORD cbBufferSize = 0;<br>    PSID pLocalSystemSid = nullptr;<br>    DWORD pLocalSystemSidSize = SECURITY_MAX_SID_SIZE;<br>    PSID pOwnerRightsSid = nullptr;<br>    DWORD pOwnerRightsSidSize = SECURITY_MAX_SID_SIZE;<br>    PACL pACL = nullptr;<br>    DWORD cbACL = 0;<br>    bool bSuccess = false;<br>    PACL pOldDACL = nullptr;  <br><br>    // Get the current DACL before modifying it<br>    if (GetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,<br>                        nullptr, nullptr, &amp;pOldDACL, nullptr, nullptr) == ERROR_SUCCESS) {<br>        printDACL(pOldDACL, &quot;[BEFORE] Current&quot;);<br>    } else {<br>        std::cerr &lt;&lt; &quot;Failed to retrieve current DACL. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>    }<br><br>    //  Open the process token<br>    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &amp;hToken)) {<br>        std::cerr &lt;&lt; &quot;Failed to open process token. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br><br>    //  Get token size<br>    GetTokenInformation(hToken, TokenUser, nullptr, 0, &amp;cbBufferSize);<br>    pTokenUser = static_cast&lt;PTOKEN_USER&gt;(HeapAlloc(GetProcessHeap(), 0, cbBufferSize));<br>    if (!pTokenUser || !GetTokenInformation(hToken, TokenUser, pTokenUser, cbBufferSize, &amp;cbBufferSize)) {<br>        std::cerr &lt;&lt; &quot;Failed to retrieve token user. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br><br>    //  Create Local System SID<br>    pLocalSystemSid = static_cast&lt;PSID&gt;(HeapAlloc(GetProcessHeap(), 0, pLocalSystemSidSize));<br>    if (!pLocalSystemSid || !CreateWellKnownSid(WinLocalSystemSid, nullptr, pLocalSystemSid, &amp;pLocalSystemSidSize)) {<br>        std::cerr &lt;&lt; &quot;Failed to create LocalSystem SID. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br><br>    //  Create Owner Rights SID<br>    pOwnerRightsSid = static_cast&lt;PSID&gt;(HeapAlloc(GetProcessHeap(), 0, pOwnerRightsSidSize));<br>    if (!pOwnerRightsSid || !CreateWellKnownSid(WinCreatorOwnerRightsSid, nullptr, pOwnerRightsSid, &amp;pOwnerRightsSidSize)) {<br>        std::cerr &lt;&lt; &quot;Failed to create Owner Rights SID. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br><br>    //  Calculate ACL size<br>    cbACL = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) * 3 + GetLengthSid(pTokenUser-&gt;User.Sid) +<br>            GetLengthSid(pLocalSystemSid) + GetLengthSid(pOwnerRightsSid);<br><br>    //  Create and initialize ACL<br>    pACL = static_cast&lt;PACL&gt;(HeapAlloc(GetProcessHeap(), 0, cbACL));<br>    if (!pACL || !InitializeAcl(pACL, cbACL, ACL_REVISION)) {<br>        std::cerr &lt;&lt; &quot;Failed to initialize ACL. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br>    //  Grant FULL CONTROL to the process user<br>    if (!AddAccessAllowedAce(pACL, ACL_REVISION, PROCESS_ALL_ACCESS | GENERIC_ALL, pTokenUser-&gt;User.Sid)) {<br>        std::cerr &lt;&lt; &quot;Failed to add FULL ACCESS ACE for token user. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br><br>    //  Grant FULL CONTROL to the process owner<br>    if (!AddAccessAllowedAce(pACL, ACL_REVISION, PROCESS_ALL_ACCESS | GENERIC_ALL, pOwnerRightsSid)) {<br>        std::cerr &lt;&lt; &quot;Failed to add FULL ACCESS ACE for owner rights. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>        goto Cleanup;<br>    }<br><br>   <br>    //  Apply the new DACL<br>    bSuccess = ERROR_SUCCESS == SetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT,<br>                                                DACL_SECURITY_INFORMATION ,<br>                                                nullptr, nullptr, pACL, nullptr);<br><br>    if (!bSuccess) {<br>        std::cerr &lt;&lt; &quot;SetSecurityInfo failed. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>    }<br><br>    // Get the updated DACL after modification<br>    PACL pNewDACL = nullptr;<br>    if (GetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,<br>                        nullptr, nullptr, &amp;pNewDACL, nullptr, nullptr) == ERROR_SUCCESS) {<br>        printDACL(pNewDACL, &quot;[AFTER] Updated&quot;);<br>    } else {<br>        std::cerr &lt;&lt; &quot;Failed to retrieve updated DACL. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>    }<br><br>Cleanup:<br>    if (pACL) HeapFree(GetProcessHeap(), 0, pACL);<br>    if (pLocalSystemSid) HeapFree(GetProcessHeap(), 0, pLocalSystemSid);<br>    if (pOwnerRightsSid) HeapFree(GetProcessHeap(), 0, pOwnerRightsSid);<br>    if (pTokenUser) HeapFree(GetProcessHeap(), 0, pTokenUser);<br>    if (hToken) CloseHandle(hToken);<br><br>    return bSuccess;<br>}<br><br>int main() {<br>    if (createWindowsDACL()) {<br>        std::cout &lt;&lt; &quot;DACL successfully applied!&quot; &lt;&lt; std::endl;<br>    } else {<br>        std::cerr &lt;&lt; &quot;Failed to apply DACL!&quot; &lt;&lt; std::endl;<br>    }<br><br>    std::cout &lt;&lt; &quot;Process ID: &quot; &lt;&lt; GetCurrentProcessId() &lt;&lt; std::endl;<br>    system(&quot;pause&quot;);<br>    return 0;<br>}</pre><h3>Code Breakdown</h3><p>The key function in this program is createWindowsDACL(), which performs the following operations:</p><h3>1. Retrieve Current DACL</h3><p>Before modifying the DACL, the program retrieves and prints the current DACL of the process:</p><pre>if (GetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,<br>                    nullptr, nullptr, &amp;pOldDACL, nullptr, nullptr) == ERROR_SUCCESS) {<br>    printDACL(pOldDACL, &quot;[BEFORE] Current&quot;);<br>}</pre><p>This helps us understand the existing permissions before applying changes.</p><h3>2. Open Process Token</h3><p>To get the current user’s security identifier (SID), we need to open the process token:</p><pre>if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &amp;hToken)) {<br>    std::cerr &lt;&lt; &quot;Failed to open process token. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>    goto Cleanup;<br>}</pre><p>The process token contains security-related information, including the user SID.</p><h3>3. Retrieve Token User Information</h3><p>Next, we allocate memory and retrieve token information:</p><pre>GetTokenInformation(hToken, TokenUser, nullptr, 0, &amp;cbBufferSize);<br>pTokenUser = static_cast&lt;PTOKEN_USER&gt;(HeapAlloc(GetProcessHeap(), 0, cbBufferSize));<br>if (!pTokenUser || !GetTokenInformation(hToken, TokenUser, pTokenUser, cbBufferSize, &amp;cbBufferSize)) {<br>    std::cerr &lt;&lt; &quot;Failed to retrieve token user. Error: &quot; &lt;&lt; GetLastError() &lt;&lt; std::endl;<br>    goto Cleanup;<br>}</pre><p>This step extracts the user’s SID, which will be used to modify the DACL.</p><h3>4. Create Well-Known SIDs</h3><p>We create well-known SIDs for Local System and Owner Rights to grant specific permissions:</p><pre>CreateWellKnownSid(WinLocalSystemSid, nullptr, pLocalSystemSid, &amp;pLocalSystemSidSize);<br>CreateWellKnownSid(WinCreatorOwnerRightsSid, nullptr, pOwnerRightsSid, &amp;pOwnerRightsSidSize);</pre><p>These SIDs are used to grant read and control permissions to the process.</p><h3>5. Initialize a New ACL</h3><p>We calculate the required size for a new ACL and allocate memory for it:</p><pre>cbACL = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) * 3 + GetLengthSid(pTokenUser-&gt;User.Sid) +<br>        GetLengthSid(pLocalSystemSid) + GetLengthSid(pOwnerRightsSid);<br>pACL = static_cast&lt;PACL&gt;(HeapAlloc(GetProcessHeap(), 0, cbACL));<br>InitializeAcl(pACL, cbACL, ACL_REVISION);</pre><p>This ensures enough space for the new access control entries (ACEs).</p><h3>6. Add ACEs (Access Control Entries)</h3><p>We then add access control entries to allow specific permissions:</p><pre>AddAccessAllowedAce(pACL, ACL_REVISION, SYNCHRONIZE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, pTokenUser-&gt;User.Sid);<br>AddAccessAllowedAce(pACL, ACL_REVISION, READ_CONTROL | PROCESS_VM_READ, pOwnerRightsSid);</pre><p>Here, we allow the process owner to read memory (PROCESS_VM_READ) and query information (PROCESS_QUERY_INFORMATION).</p><h3>7. Apply the Updated DACL</h3><p>Finally, we set the new security information for the process:</p><pre>bSuccess = ERROR_SUCCESS == SetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT,<br>                                            DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION,<br>                                            nullptr, nullptr, pACL, nullptr);</pre><p>If successful, we print the updated DACL:</p><pre>printDACL(pNewDACL, &quot;[AFTER] Updated&quot;);</pre><h3>Running the Program</h3><p>When executed, the program:</p><ol><li>Retrieves and prints the current DACL.</li><li>Updates the DACL with custom permissions.</li><li>Prints the new DACL for verification.</li><li>Cleans up allocated memory and handles.</li></ol><p>At the end, the process ID is displayed:</p><pre>std::cout &lt;&lt; &quot;Process ID: &quot; &lt;&lt; GetCurrentProcessId() &lt;&lt; std::endl;<br>system(&quot;pause&quot;);</pre><h3>Conclusion</h3><p>This program is a great example of how to manage access control on Windows using DACLs. By modifying permissions dynamically, you can control access to processes securely. If you’re working with system security, understanding and manipulating DACLs is crucial for effective permission management.</p><h3>References</h3><ul><li>Microsoft Documentation: <a href="https://docs.microsoft.com/en-us/windows/win32/secauthz/access-control-lists">Access Control Lists</a></li><li>Microsoft Documentation: <a href="https://docs.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-setsecurityinfo">SetSecurityInfo function</a></li><li>Microsoft Documentation: <a href="https://docs.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-getsecurityinfo">GetSecurityInfo function</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c92b92539fb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>