‹ARES offensive division vulnerability research

Evasão de AV/EDRAV/EDR Evasion

Bypass de API hooking no Windows com syscallsBypassing Windows API Hooking with Syscalls

A maioria dos AV/EDR hooka funções no nível de usuário pra flaggar código malicioso. Se a gente fala direto com o kernel via syscalls, simplesmente não passa por esses hooks — e o Windows Defender não vê o que está acontecendo.Most AV/EDR products hook functions at the user level to flag malicious code. If you talk straight to the kernel via syscalls, you never touch those hooks — and Windows Defender never sees what is going on.


CVE ID
Severity
Affected
Detecção por API hooking em user-land (AV/EDR)User-land API-hooking detection (AV/EDR)
Vendor
Disclosure
Researcher
Marcelo Benesciutti — Vultus

Recentemente comecei a pesquisar sobre bypass de AV/EDR e Windows internals (um salve pro meu amigo Thiago Peixoto nessa parte, que me ajudou demais). Nos meus estudos esbarrei num método de detecção bem comum usado pela maioria das soluções de AV/EDR: o API hooking do Windows.Recently I started doing some research on AV/EDR bypass and Windows internals (shoutout to my friend Thiago Peixoto on this part, who helped me a lot). During my studies I stumbled onto a very common detection method used by most AV/EDR solutions: Windows API hooking.

ConteúdoContents

Como o API hooking detecta o seu PEHow API hooking flags your PE

Basicamente, essas soluções hookam funções comuns usadas em código malicioso, tipo OpenProcess, VirtualAllocEx, WriteProcessMemory, CreateRemoteThread, entre outras. Se um PE desconhecido usa essas funções, ele tem boas chances de ser flaggado como malicioso.Basically, these solutions hook common functions used by malicious code, such as OpenProcess, VirtualAllocEx, WriteProcessMemory, CreateRemoteThread, among others. If an unknown PE makes use of these functions, it is likely to get flagged as malicious.

O ponto fraco desse método de detecção é que esses hooks são colocados no nível de usuário do SO. Então, se a gente conseguir não usar nenhuma função ou biblioteca em user level, usando só instruções de kernel level — ou seja, system calls — a gente deveria conseguir escapar dessa detecção, como descrito pelo Cn33liz neste blogpost.The downside of this detection method is that these hooks are set at the user level of the OS. So if we manage to avoid any user-level function or library and use only kernel-level instructions — that is, system calls — we should be able to evade this detection method, as described by Cn33liz in this blogpost.

O caso de estudo: injeção de shellcode no notepadThe case study: shellcode injection into notepad

No meu caso de estudo, o Windows Defender estava flaggando o meu PE como malicioso porque eu percorria a lista de processos rodando, copiava o PID do notepad.exe e usava ele pra abrir o processo, com a intenção de injetar shellcode nele carregando uma dll maliciosa. Pra escapar da detecção, eu implementei uma função pra fazer um syscall direto, mirando a função ZwOpenProcess no kernel (syscall 26 no Windows 10).In my case study, Windows Defender was flagging my PE as malicious because I walked the list of running processes, copied the PID of notepad.exe and used it to open the process, in order to inject shellcode into it by loading a malicious dll. To evade detection, I implemented a function to make a direct syscall targeting the ZwOpenProcess function in the kernel (syscall 26 on Windows 10).

Assembly of the ZwOpenProcess direct syscall stub
A função em assembly que faz o syscall direto pro ZwOpenProcess — passando os argumentos direto pra interface de syscall, sem encostar em kernel32 ou ntdll.The assembly function that performs the direct ZwOpenProcess syscall — passing the arguments straight to the syscall interface, without touching kernel32 or ntdll.

Antes e depois, lado a ladoBefore and after, side by side

No vídeo, o API monitor vai flaggar o uso de OpenProcess e NtOpenProcess na 1ª execução do PE, já que eu estava usando a função OpenProcess do kernel32.dll. Essa era a versão do PE que estava sendo flaggada pelo Windows Defender.In the video, the API monitor flags the use of OpenProcess and NtOpenProcess on the 1st execution of the PE, since I was using the kernel32.dll OpenProcess function. This was the PE version being flagged by Windows Defender.

Na 2ª execução, a função ZwOpenProcess que eu chamo passa os argumentos direto pro syscall, sem usar o OpenProcess do kernel32 nem o NtOpenProcess do ntdll — escapando efetivamente do API hooking de userland em ação e evadindo o Windows Defender.On the 2nd run, the ZwOpenProcess function I call passes the arguments directly to the syscall, without making use of kernel32's OpenProcess or ntdll's NtOpenProcess — effectively escaping the user-land API hooking in progress and evading Windows Defender.

NotaNote

A demonstração completa está no vídeo no YouTube linkado nas referências. Repare na diferença entre as duas execuções no API monitor: na primeira aparecem as chamadas hookadas, na segunda elas somem.The full demonstration is in the YouTube video linked in the references. Watch the difference between the two runs in the API monitor: the hooked calls show up on the first one, and disappear on the second.

AvisoWarning

Isto é material de pesquisa pra entender mecanismos de detecção. Só rode essas técnicas em ambientes autorizados e sob seu controle — nunca contra sistemas que não são seus.This is research material for understanding detection mechanisms. Only run these techniques in authorized environments under your control — never against systems you do not own.

ReferênciasReferences

  1. Cn33liz (Outflank) — Red Team Tactics: combinando direct system calls e sRDI pra dar bypass em AV/EDR.Cn33liz (Outflank) — Red Team Tactics: combining direct system calls and sRDI to bypass AV/EDR. https://outflank.nl/blog/2019/06/19/red-team-tactics-combining-direct-system-calls-and-srdi-to-bypass-av-edr/
  2. Microsoft Docs — função ZwOpenProcess.Microsoft Docs — ZwOpenProcess function. https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/nf-ntddk-zwopenprocess
  3. Wikipedia — Portable Executable (PE).Wikipedia — Portable Executable (PE). https://en.wikipedia.org/wiki/Portable_Executable
  4. Demonstração em vídeo — syscall direto evadindo o Windows Defender.Video demonstration — direct syscall evading Windows Defender. https://www.youtube.com/watch?v=qgq1BH4BzVc

AgradecimentosThanks

Valeu demais pro Thiago Peixoto pela ajuda com Windows internals. Hack the planet!!!Big thanks to Thiago Peixoto for the help with Windows internals. Hack the planet!!!