{"componentChunkName":"component---src-templates-post-template-js","path":"/amsi-learn-en","result":{"data":{"markdownRemark":{"id":"f3fc7cf9-ed8a-521f-aa39-67cce920ca65","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/amsi-learn\">original page</a>.</p>\n</blockquote>\n<p>In this post, I put together a brief set of notes on the overview of AMSI (Windows Antimalware Scan Interface) and how it works on Windows.</p>\n<p>All content here is based on information in official documentation and other websites, or in publicly available books and similar sources.</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>\n<p><a href=\"#about-amsi\">About AMSI</a></p>\n<ul>\n<li><a href=\"#details-of-protection-provided-by-amsi\">Details of Protection Provided by AMSI</a></li>\n</ul>\n</li>\n<li>\n<p><a href=\"#how-amsi-works-when-integrated-with-powershell\">How AMSI Works When Integrated with PowerShell</a></p>\n<ul>\n<li><a href=\"#testing-amsi-behavior\">Testing AMSI Behavior</a></li>\n<li><a href=\"#examining-the-powershell-side-implementation\">Examining the PowerShell-side Implementation</a></li>\n<li><a href=\"#understanding-how-amsidll-works\">Understanding How amsi.dll Works</a></li>\n</ul>\n</li>\n<li><a href=\"#summary\">Summary</a></li>\n</ul>\n<h2 id=\"about-amsi\" style=\"position:relative;\"><a href=\"#about-amsi\" aria-label=\"about amsi 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>About AMSI</h2>\n<p>AMSI (Windows Antimalware Scan Interface) is a feature that provides an interface allowing applications to use the AV providers registered on the system to determine whether the data they process is malicious.</p>\n<p>According to the public documentation, AMSI supports a call structure that enables scanning files, memory, and streams, as well as reputation checks for URLs/IPs.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows/win32/amsi/antimalware-scan-interface-portal\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Antimalware Scan Interface (AMSI) - Win32 apps | Microsoft Learn</a></p>\n<p>AMSI is also integrated into several Windows components such as UAC, PowerShell, and Office VBA, helping protect those applications.</p>\n<p>Through this kind of integration with applications, AMSI is used mainly to address script-based threats.</p>\n<p>Application developers can use AMSI to implement scan requests for arbitrary content from their own applications.</p>\n<p>Also, AMSI itself is independent of any specific antimalware vendor, so any antimalware vendor can receive scan requests through AMSI.</p>\n<p>Reference: <a href=\"https://i.blackhat.com/briefings/asia/2018/asia-18-Tal-Liberman-Documenting-the-Undocumented-The-Rise-and-Fall-of-AMSI.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">The Rise and Fall of AMSI</a></p>\n<h3 id=\"details-of-protection-provided-by-amsi\" style=\"position:relative;\"><a href=\"#details-of-protection-provided-by-amsi\" aria-label=\"details of protection provided by amsi 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>Details of Protection Provided by AMSI</h3>\n<p>AMSI appeared at the same time as the release of Windows 10, especially as a countermeasure against detection-evasion techniques in script-based malware such as string concatenation and obfuscation.</p>\n<p>If you look at the archived blog posts from when AMSI was released, you can see that AMSI was introduced as a solution to a protection gap caused by the increase in script-based threats and fileless attacks using tools such as PowerShell, along with the wide variety of evasion techniques used to avoid detection.</p>\n<p>Reference: <a href=\"https://msrc.microsoft.com/blog/2015/06/advances-in-scripting-security-and-protection-in-windows-10-and-powershell-v5/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Advances in Scripting Security and Protection in Windows 10 and PowerShell V5 | MSRC Blog | Microsoft Security Response Center</a></p>\n<p>Reference: <a href=\"https://web.archive.org/web/20150916092128/http://blogs.technet.com/b/mmpc/archive/2015/06/09/windows-10-to-offer-application-developers-new-malware-defenses.aspx\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Windows 10 to offer application developers new malware defenses - Microsoft Malware Protection Center - Site Home - TechNet Blogs</a></p>\n<p>Applications such as PowerShell can send scan requests through AMSI to the antimalware engine registered on the system for the plain attack code produced after deobfuscation, before that code is executed.</p>\n<p>This makes it possible to effectively detect things like heavily obfuscated attack scripts and fileless threats where malicious code exists only in memory.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 727px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e87f35181a8cebdab74ae60b73149236/ce6ee/amsi7archi.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 44.166666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAJABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAIBBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAHtZUSBL//EABgQAAIDAAAAAAAAAAAAAAAAAAACARAS/9oACAEBAAEFAmINDX//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEQH/2gAIAQMBAT8Bq4//xAAVEQEBAAAAAAAAAAAAAAAAAAAAIf/aAAgBAgEBPwFX/8QAFxAAAwEAAAAAAAAAAAAAAAAAAAExIP/aAAgBAQAGPwIhHj//xAAaEAACAgMAAAAAAAAAAAAAAAAAARExEEHh/9oACAEBAAE/Ib8GhrYmF8//2gAMAwEAAgADAAAAEBff/8QAFhEBAQEAAAAAAAAAAAAAAAAAAFFh/9oACAEDAQE/EIZP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQBxof/aAAgBAgEBPxBUbfL/xAAdEAACAQQDAAAAAAAAAAAAAAAAAREhQVFxMZHB/9oACAEBAAE/EElW8QFXVNVYWC7OHXpkV9n/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e87f35181a8cebdab74ae60b73149236/8ac56/amsi7archi.webp 240w,\n/static/e87f35181a8cebdab74ae60b73149236/d3be9/amsi7archi.webp 480w,\n/static/e87f35181a8cebdab74ae60b73149236/34975/amsi7archi.webp 727w\"\n              sizes=\"(max-width: 727px) 100vw, 727px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e87f35181a8cebdab74ae60b73149236/09b79/amsi7archi.jpg 240w,\n/static/e87f35181a8cebdab74ae60b73149236/7cc5e/amsi7archi.jpg 480w,\n/static/e87f35181a8cebdab74ae60b73149236/ce6ee/amsi7archi.jpg 727w\"\n            sizes=\"(max-width: 727px) 100vw, 727px\"\n            type=\"image/jpeg\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/e87f35181a8cebdab74ae60b73149236/ce6ee/amsi7archi.jpg\"\n            alt=\"img\"\n            title=\"img\"\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<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows/win32/amsi/how-amsi-helps\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">How AMSI helps defend against malware - Win32 apps | Microsoft Learn</a></p>\n<h2 id=\"how-amsi-works-when-integrated-with-powershell\" style=\"position:relative;\"><a href=\"#how-amsi-works-when-integrated-with-powershell\" aria-label=\"how amsi works when integrated with powershell 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>How AMSI Works When Integrated with PowerShell</h2>\n<p>From here, I will walk through the sequence of operations by which PowerShell uses AMSI to prevent the execution of malicious scripts.</p>\n<p>Fortunately, because PowerShell is open source and its source code is public, we can examine the details of its AMSI integration in depth.</p>\n<p>Reference: <a href=\"https://github.com/PowerShell/PowerShell/tree/master\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PowerShell/PowerShell: PowerShell for every system!</a></p>\n<h3 id=\"testing-amsi-behavior\" style=\"position:relative;\"><a href=\"#testing-amsi-behavior\" aria-label=\"testing amsi behavior 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>Testing AMSI Behavior</h3>\n<p>First, as described in the official documentation, let’s test AMSI’s execution blocking by saving the following sample code as <code class=\"language-text\">AMSI_PoSh_script.ps1</code> and running it.</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\"><span class=\"token comment\"># Save this sample AMSI powershell script as AMSI_PoSh_script.ps1</span>\n<span class=\"token variable\">$testString</span> = <span class=\"token string\">\"AMSI Test Sample: \"</span> <span class=\"token operator\">+</span> <span class=\"token string\">\"7e72c3ce-861b-4339-8740-0ac1484c1386\"</span>\n<span class=\"token function\">Invoke-Expression</span> <span class=\"token variable\">$testString</span></code></pre></div>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/defender-endpoint/mde-demonstration-amsi\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Demonstration of AMSI using Microsoft Defender for Endpoint - Microsoft Defender for Endpoint | Microsoft Learn</a></p>\n<p>When you run this, you can confirm that script execution is blocked together with the error <code class=\"language-text\">This script contains malicious content and has been blocked by your antivirus software.</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: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f3555d669c7b6c971cb233a150ac05f4/e8814/image-20250619150701366.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: 17.5%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAtUlEQVQY01WO2w6CMBBEeVVASsECclFEFMIt6v9/2zhbNMGHk+lktzPrzJcaPRlqanVBl58wXAs0ZYZblaHOE/ieh53rYr9BvEs8zn4qOGN0xExGLWqwxAkmbTDQj/QTaXQEcwgsaaCsnqiB7/+F2cCnyfAii0ktD4bnKkSm1Ao/lvTaP0AxQDSkStjue+kW582wjiFXXiGtrY7Rx8b6NoptwZ2Iykx2R85LpVGwSMrP4fqW4g/4eGO5Dbg05gAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/f3555d669c7b6c971cb233a150ac05f4/8ac56/image-20250619150701366.webp 240w,\n/static/f3555d669c7b6c971cb233a150ac05f4/d3be9/image-20250619150701366.webp 480w,\n/static/f3555d669c7b6c971cb233a150ac05f4/e46b2/image-20250619150701366.webp 960w,\n/static/f3555d669c7b6c971cb233a150ac05f4/f3c7c/image-20250619150701366.webp 1392w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/f3555d669c7b6c971cb233a150ac05f4/8ff5a/image-20250619150701366.png 240w,\n/static/f3555d669c7b6c971cb233a150ac05f4/e85cb/image-20250619150701366.png 480w,\n/static/f3555d669c7b6c971cb233a150ac05f4/d9199/image-20250619150701366.png 960w,\n/static/f3555d669c7b6c971cb233a150ac05f4/e8814/image-20250619150701366.png 1392w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/f3555d669c7b6c971cb233a150ac05f4/d9199/image-20250619150701366.png\"\n            alt=\"image-20250619150701366\"\n            title=\"image-20250619150701366\"\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<p>Next, I also tested AMSI detection using a PowerShell build that I compiled myself from the cloned source code. (This time I used the <code class=\"language-text\">release/v7.5</code> code.)</p>\n<p>Building PowerShell succeeded easily by using the dedicated build tool. (Although some errors were output, <code class=\"language-text\">.\\src\\powershell-win-core\\bin\\Debug\\net9.0\\win7-x64\\publish\\pwsh.exe</code> was created successfully.)</p>\n<div class=\"gatsby-highlight\" data-language=\"powershell\"><pre class=\"language-powershell\"><code class=\"language-powershell\"><span class=\"token function\">Import-Module</span> <span class=\"token punctuation\">.</span>\\build<span class=\"token punctuation\">.</span>psm1\n<span class=\"token function\">Start-PSBuild</span> <span class=\"token operator\">-</span>Clean <span class=\"token operator\">-</span>PSModuleRestore <span class=\"token operator\">-</span>UseNuGetOrg <span class=\"token operator\">-</span>Configuration Debug</code></pre></div>\n<p>Reference: <a href=\"https://github.com/PowerShell/PowerShell/blob/master/docs/building/windows-core.md#build-using-our-module\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PowerShell/docs/building/windows-core.md at master · PowerShell/PowerShell</a></p>\n<p>I then copied the entire <code class=\"language-text\">.\\src\\powershell-win-core\\bin\\Debug\\net9.0\\win7-x64</code> directory containing the program built here to a test machine and ran the test script again. I was able to confirm that execution blocking by AMSI also worked there in the same way.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/a207f254003108431623d1c33e364bb8/5819f/image-20250619152750959.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: 47.91666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABg0lEQVQoz31S2W6sMBTjgbliSQiBYQj7Emhhbh9GI7X//2fuyWkZVVXbB8vkiDi2E69pGgzDgHEcMc8zc9/3iPIQvvJxyk44xT58n3D65D/giUQgkQnSNIVKFVJFrBXiLoI8C8hMQqYSQhBLx+JPeLOdoZIEURQhjmNmIQXqoWaB4F+AMAwZQRA8/vsN3r7vuBQF/3wMlVJYlxXWWqzrimVZuI5926ieEV3XcS19P6BtW3bm9rHDaZpwPp+R5znHdg4cX69XFjk2V1XFcIf9FPUh+Pb2ivv9jtvthm17ZmG3yV2UO8iJa62RUC1HikPgK46ZN1GskZwM5DTVHw5dd6UpSTjh7qIo5PkBNztExMGPS6lqLIS17WCnEU/UmZ0tlrnHy/+VsW8W132hyAZFcUFZlpwi/ibGgoZ6MaWBqRsUzQcMua3JdU4bXVytM3KvGbzOMnoJEtG32G7tuZ4sCS5VgydT45nAXFaoSaiiN1omChXBfLIDz+jNNqnGhb5bYrd+B1fb80rXTBdPAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/a207f254003108431623d1c33e364bb8/8ac56/image-20250619152750959.webp 240w,\n/static/a207f254003108431623d1c33e364bb8/d3be9/image-20250619152750959.webp 480w,\n/static/a207f254003108431623d1c33e364bb8/e46b2/image-20250619152750959.webp 960w,\n/static/a207f254003108431623d1c33e364bb8/20e4c/image-20250619152750959.webp 1042w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/a207f254003108431623d1c33e364bb8/8ff5a/image-20250619152750959.png 240w,\n/static/a207f254003108431623d1c33e364bb8/e85cb/image-20250619152750959.png 480w,\n/static/a207f254003108431623d1c33e364bb8/d9199/image-20250619152750959.png 960w,\n/static/a207f254003108431623d1c33e364bb8/5819f/image-20250619152750959.png 1042w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/a207f254003108431623d1c33e364bb8/d9199/image-20250619152750959.png\"\n            alt=\"image-20250619152750959\"\n            title=\"image-20250619152750959\"\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=\"examining-the-powershell-side-implementation\" style=\"position:relative;\"><a href=\"#examining-the-powershell-side-implementation\" aria-label=\"examining the powershell side implementation 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>Examining the PowerShell-side Implementation</h3>\n<p>Looking at the image attached to the official documentation below, you can see that PowerShell uses AMSI by calling <code class=\"language-text\">AmsiScanBuffer</code> or <code class=\"language-text\">AmsiScanString</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: 727px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e87f35181a8cebdab74ae60b73149236/ce6ee/amsi7archi.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 44.166666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAJABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAIBBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAHtZUSBL//EABgQAAIDAAAAAAAAAAAAAAAAAAACARAS/9oACAEBAAEFAmINDX//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEQH/2gAIAQMBAT8Bq4//xAAVEQEBAAAAAAAAAAAAAAAAAAAAIf/aAAgBAgEBPwFX/8QAFxAAAwEAAAAAAAAAAAAAAAAAAAExIP/aAAgBAQAGPwIhHj//xAAaEAACAgMAAAAAAAAAAAAAAAAAARExEEHh/9oACAEBAAE/Ib8GhrYmF8//2gAMAwEAAgADAAAAEBff/8QAFhEBAQEAAAAAAAAAAAAAAAAAAFFh/9oACAEDAQE/EIZP/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQBxof/aAAgBAgEBPxBUbfL/xAAdEAACAQQDAAAAAAAAAAAAAAAAAREhQVFxMZHB/9oACAEBAAE/EElW8QFXVNVYWC7OHXpkV9n/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/e87f35181a8cebdab74ae60b73149236/8ac56/amsi7archi.webp 240w,\n/static/e87f35181a8cebdab74ae60b73149236/d3be9/amsi7archi.webp 480w,\n/static/e87f35181a8cebdab74ae60b73149236/34975/amsi7archi.webp 727w\"\n              sizes=\"(max-width: 727px) 100vw, 727px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/e87f35181a8cebdab74ae60b73149236/09b79/amsi7archi.jpg 240w,\n/static/e87f35181a8cebdab74ae60b73149236/7cc5e/amsi7archi.jpg 480w,\n/static/e87f35181a8cebdab74ae60b73149236/ce6ee/amsi7archi.jpg 727w\"\n            sizes=\"(max-width: 727px) 100vw, 727px\"\n            type=\"image/jpeg\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/e87f35181a8cebdab74ae60b73149236/ce6ee/amsi7archi.jpg\"\n            alt=\"img\"\n            title=\"img\"\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<p>Tracing where these functions are called shows that they are invoked in the order <code class=\"language-text\">PerformSecurityChecks() -> ScanContent() -> WinScanContent() -> AmsiScanBuffer()</code> from the <code class=\"language-text\">PerformSecurityChecks</code> function in <code class=\"language-text\">System.Management.Automation/engine/runtime/CompiledScriptBlock.cs</code>.</p>\n<p>Reference: <a href=\"https://github.com/PowerShell/PowerShell/blob/release/v7.5/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs?plain=1\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">PowerShell/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs at release/v7.5 · PowerShell/PowerShell</a></p>\n<p>The <code class=\"language-text\">PerformSecurityChecks</code> function is called more directly from the <code class=\"language-text\">ReallyCompile</code> function in <code class=\"language-text\">System.Management.Automation</code>, which performs compilation before script execution.</p>\n<p>Looking at the implementation of <code class=\"language-text\">PerformSecurityChecks</code>, it appears that an exception containing a <code class=\"language-text\">ParseError</code> is returned when <code class=\"language-text\">AmsiUtils.ScanContent(scriptExtent.Text, scriptFile)</code> returns <code class=\"language-text\">AMSI_RESULT_DETECTED</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c#\"><pre class=\"language-c#\"><code class=\"language-c#\">private void PerformSecurityChecks()\n{\n    /* 省略 */\n    \n    // Call the AMSI API to determine if the script block has malicious content\n    var amsiResult = AmsiUtils.ScanContent(scriptExtent.Text, scriptFile);\n\n    if (amsiResult == AmsiUtils.AmsiNativeMethods.AMSI_RESULT.AMSI_RESULT_DETECTED)\n    {\n        var parseError = new ParseError(\n            scriptExtent,\n            &quot;ScriptContainedMaliciousContent&quot;,\n            ParserStrings.ScriptContainedMaliciousContent);\n        throw new ParseException(new[] { parseError });\n    }\n\n    /* 省略 */  \n}</code></pre></div>\n<p>Within <code class=\"language-text\">PerformSecurityChecks</code>, a scan request is sent to AMSI by using <code class=\"language-text\">ScanContent</code> on the initialized <code class=\"language-text\">AmsiUtils</code> class.</p>\n<p>In <code class=\"language-text\">WinScanContent</code>, which is called from <code class=\"language-text\">ScanContent</code>, the scan is ultimately performed by using the <code class=\"language-text\">AmsiScanBuffer</code> function loaded from <code class=\"language-text\">amsi.dll</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c#\"><pre class=\"language-c#\"><code class=\"language-c#\">AmsiNativeMethods.AMSI_RESULT result = AmsiNativeMethods.AMSI_RESULT.AMSI_RESULT_CLEAN;\n\n// Run AMSI content scan\nint hr;\nunsafe\n{\n    fixed (char* buffer = content)\n    {\n        var buffPtr = new IntPtr(buffer);\n        hr = AmsiNativeMethods.AmsiScanBuffer(\n            s_amsiContext,\n            buffPtr,\n            (uint)(content.Length * sizeof(char)),\n            sourceMetadata,\n            s_amsiSession,\n            ref result);\n    }\n}</code></pre></div>\n<p>The <code class=\"language-text\">AmsiScanBuffer</code> function receives a buffer for reading the data to be scanned.</p>\n<p>It also receives session information as <code class=\"language-text\">amsiSession</code>, which is used to associate multiple scan requests.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanbuffer\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">AmsiScanBuffer function (amsi.h) - Win32 apps | Microsoft Learn</a></p>\n<p>Reference: <a href=\"https://redcanary.com/blog/threat-detection/better-know-a-data-source/amsi/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Better know a data source: Antimalware Scan Interface</a></p>\n<p>This session information is implemented so that antimalware products can use it to associate various scan requests.</p>\n<p>As a result, threats that cannot be judged from fragmented data alone may become detectable by associating each piece of data.</p>\n<h3 id=\"understanding-how-amsidll-works\" style=\"position:relative;\"><a href=\"#understanding-how-amsidll-works\" aria-label=\"understanding how amsidll works 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>Understanding How amsi.dll Works</h3>\n<p>Fortunately, symbol information for <code class=\"language-text\">amsi.dll</code>, which PowerShell loads, is distributed through Microsoft’s public symbol server, so it is relatively easy to debug.</p>\n<p>However, because this article is intended to stay within the bounds of publicly available information, this time I will summarize the behavior on the <code class=\"language-text\">amsi.dll</code> side based on Chapter 10, <strong>ANTIMALWARE SCAN INTERFACE</strong>, in <a href=\"https://nostarch.com/evading-edr\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Evading EDR</a>.</p>\n<p>First, <code class=\"language-text\">amsi.dll</code> loads the AMSI provider DLL by referring to the information registered in the registry.</p>\n<p>Antimalware vendors register their AMSI providers in the registry by using the COM GUIDs registered under the registry key <code class=\"language-text\">HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\AMSI\\Providers</code>.</p>\n<p>Reference: <a href=\"https://learn.microsoft.com/ja-jp/windows/win32/amsi/dev-audience#third-party-creators-of-antimalware-products\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Register a provider DLL with AMSI</a></p>\n<p>For example, by default <code class=\"language-text\">HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\AMSI\\Providers\\{2781761E-28E0-4109-99FE-B9D127C57AFE}</code> is registered. If you inspect this GUID and the corresponding registry keys, you can see information related to Microsoft Defender has been written there, and from the value of the <code class=\"language-text\">InprocServer32</code> key you can confirm the path to <code class=\"language-text\">MpOav.dll</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: 960px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/38a65/image-20250620145458719.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: 19.166666666666664%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAo0lEQVQY05WP6w7CIAyFef8HVHcT4iSZM+O6ABuGI2uiv7XJl55yOW2ZlAKX7kqc274y4NT0VHPOISrT9IBSCjlnhBCgjUGMkfTBRx+ZratGIwR6uWB8etxmBz4ZyGWFrR+tVkjbBu89Silkauq5c+7LcWetJVNGXbXAfRRIMWDfEoCC8spw9eG/wfKeoOYBTdtBSomlrmatqytqmqR602S/8gYKSzNivioDIgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/8ac56/image-20250620145458719.webp 240w,\n/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/d3be9/image-20250620145458719.webp 480w,\n/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/e46b2/image-20250620145458719.webp 960w,\n/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/4690a/image-20250620145458719.webp 1075w\"\n              sizes=\"(max-width: 960px) 100vw, 960px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/8ff5a/image-20250620145458719.png 240w,\n/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/e85cb/image-20250620145458719.png 480w,\n/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/d9199/image-20250620145458719.png 960w,\n/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/38a65/image-20250620145458719.png 1075w\"\n            sizes=\"(max-width: 960px) 100vw, 960px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/3d1e4bb3f985a11ac4ee37ee1ea17fd0/d9199/image-20250620145458719.png\"\n            alt=\"image-20250620145458719\"\n            title=\"image-20250620145458719\"\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<p>Reference: <a href=\"https://redcanary.com/blog/threat-detection/better-know-a-data-source/amsi/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Better know a data source: Antimalware Scan Interface</a></p>\n<p>Once the provider DLL is loaded, initialization is complete, and AMSI is ready to use, it becomes able to receive scan requests such as those made through <code class=\"language-text\">AmsiScanBuffer</code>, together with information such as the AMSI session, from applications like PowerShell.</p>\n<p>At this point, AMSI validates the parameters it received as input, and if they pass validation it calls <code class=\"language-text\">amsi!CAmsiAntimalware::Scan</code>.</p>\n<p>When using Microsoft’s AMSI module (<code class=\"language-text\">MpOav.dll</code>), which is registered by default, the AMSI side performs initialization and then delegates processing to <code class=\"language-text\">MpClient.dll</code>, the client interface for Microsoft Defender Antivirus.</p>\n<p>After that, the scan result from the Microsoft Defender Antivirus side is returned to the application, and if <code class=\"language-text\">AMSI_RESULT_DETECTED</code> is returned, execution is blocked.</p>\n<p>Reference: <a href=\"https://nostarch.com/evading-edr\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Evading EDR | No Starch Press</a></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>I summarized the overview of AMSI here.</p>\n<p>I had planned to continue by writing about <a href=\"/amsi-create-interface-en\">how to issue AMSI scan requests from your own application</a> and <a href=\"/amsi-create-provider-en\">how to register a custom AMSI provider</a>, but this post was getting long, so I decided to split those topics into separate articles.</p>","fields":{"slug":"/amsi-learn-en","tagSlugs":["/tag/windows/","/tag/anti-virus/","/tag/amsi/","/tag/english/"]},"frontmatter":{"date":"2025-06-20","description":"A brief summary of AMSI and how it works.","tags":["Windows","AntiVirus","AMSI","English"],"title":"Overview of AMSI and How It Works","socialImage":{"publicURL":"/static/8d9d553760151823be4779943d5b6ba9/amsi-learn.png"}}}},"pageContext":{"slug":"/amsi-learn-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}