[Blog] Heap-based AMSI bypass for MS Excel VBA and others
The AMSI context, a heap-allocated structure starting with the ASCII characters 'AMSI', can be corrupted by overwriting those bytes, causing AmsiScanBuffer to fail and return an error. An attacker with VBA code execution in Excel could then run malicious macros without AMSI scanning them.
code-white.com
Published on code-white.com
This blog post describes how to bypass Microsoft’s AMSI (Antimalware Scan Interface) in Excel using VBA (Visual Basic for Applications). In contrast to other bypasses this approach does not use hardcoded offsets or opcodes but identifies crucial data on the heap and modifies it.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the AMSI context is stored on the heap with a fixed magic value, and AmsiScanBuffer only checks that value; corrupting it makes the scan fail, and the client treats the failure as 'not malicious'.
Impact
An attacker who can run VBA code in Excel could bypass AMSI scanning, but the bypass initially took too long (up to minutes) and was only made practical (0.02 seconds) with shellcode; the text does not claim a full remote exploit.
What the attacker needed
Code execution in the target process (e.g. VBA macro); AMSI client initialises context once and reuses it; client treats scan failure as not malicious
The fix
The text does not state a fix; it notes that Microsoft says AMSI is not a security boundary and the authors are curious whether Microsoft will add detections.
How it was found
- Implement a small AMSI client in C that calls AmsiInitialize and AmsiScanBuffer on a test string.
- Walk the process heap looking for chunks that start with the ASCII characters 'AMSI'.
- Overwrite the first bytes of such a chunk with garbage.
- Port the heap-walking and overwriting logic to VBA, using imported heap functions and a memory-copy function.
- Replace the memory-copy function with CryptBinaryToStringA to avoid triggering AMSI, and measure runtime.
- Convert the bypass loop to shellcode and execute it from VBA to reduce runtime to about 0.02 seconds.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
AMSI is a Windows feature that lets antivirus software inspect scripts before they run. This research shows that if an attacker can already run code inside Excel, they can damage a small data structure that AMSI relies on, so later scans fail and malicious code is not checked. The attacker still needs a way to run code first, such as a malicious macro in a document.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.