Ejemplo n.º 1
0
        private static void PatchImports(PortableExecutable image, IntPtr hProcess, int processId)
        {
            string empty  = string.Empty;
            string empty2 = string.Empty;

            foreach (IMAGE_IMPORT_DESCRIPTOR current in image.EnumImports())
            {
                if (image.ReadString((long)((ulong)image.GetPtrFromRVA(current.Name)), SeekOrigin.Begin, out empty, -1, null))
                {
                    IntPtr intPtr   = IntPtr.Zero;
                    IntPtr arg_4F_0 = IntPtr.Zero;
                    intPtr = ManualMap.GetRemoteModuleHandle(empty, processId);
                    if (intPtr.IsNull())
                    {
                        throw new FileNotFoundException(string.Format("Unable to load dependent module '{0}'.", empty));
                    }
                    uint             num  = image.GetPtrFromRVA(current.FirstThunkPtr);
                    uint             num2 = (uint)Marshal.SizeOf(typeof(IMAGE_THUNK_DATA));
                    IMAGE_THUNK_DATA iMAGE_THUNK_DATA;
                    while (image.Read <IMAGE_THUNK_DATA>((long)((ulong)num), SeekOrigin.Begin, out iMAGE_THUNK_DATA) && iMAGE_THUNK_DATA.u1.AddressOfData > 0u)
                    {
                        IntPtr intPtr2 = IntPtr.Zero;
                        object obj;
                        if ((iMAGE_THUNK_DATA.u1.Ordinal & 2147483648u) == 0u)
                        {
                            if (!image.ReadString((long)((ulong)(image.GetPtrFromRVA(iMAGE_THUNK_DATA.u1.AddressOfData) + 2u)), SeekOrigin.Begin, out empty2, -1, null))
                            {
                                throw image.GetLastError();
                            }
                            obj = empty2;
                        }
                        else
                        {
                            obj = (ushort)(iMAGE_THUNK_DATA.u1.Ordinal & 65535u);
                        }
                        if (!(intPtr2 = WinAPI.GetModuleHandleA(empty)).IsNull())
                        {
                            IntPtr ptr = obj.GetType().Equals(typeof(string)) ? WinAPI.GetProcAddress(intPtr2, (string)obj) : WinAPI.GetProcAddress(intPtr2, (uint)((ushort)obj & 65535));
                            if (!ptr.IsNull())
                            {
                                intPtr2 = intPtr.Add((long)ptr.Subtract((long)intPtr2.ToInt32()).ToInt32());
                            }
                        }
                        else
                        {
                            intPtr2 = WinAPI.GetProcAddressEx(hProcess, intPtr, obj);
                        }
                        if (intPtr2.IsNull())
                        {
                            throw new EntryPointNotFoundException(string.Format("Unable to locate imported function '{0}' from module '{1}' in the remote process.", empty2, empty));
                        }
                        image.Write <int>((long)((ulong)num), SeekOrigin.Begin, intPtr2.ToInt32());
                        num += num2;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // Token: 0x06000123 RID: 291 RVA: 0x0000DEE4 File Offset: 0x0000C0E4
        private static bool LoadDependencies(PortableExecutable image, IntPtr hProcess, int processId)
        {
            List <string> list   = new List <string>();
            string        empty  = string.Empty;
            bool          result = false;

            foreach (IMAGE_IMPORT_DESCRIPTOR image_IMPORT_DESCRIPTOR in image.EnumImports())
            {
                if (image.ReadString((long)((ulong)image.GetPtrFromRVA(image_IMPORT_DESCRIPTOR.Name)), SeekOrigin.Begin, out empty, -1, null) && !string.IsNullOrEmpty(empty) && ManualMap.GetRemoteModuleHandle(empty, processId).IsNull())
                {
                    list.Add(empty);
                }
            }
            if (list.Count > 0)
            {
                byte[] array = ManualMap.ExtractManifest(image);
                string text  = string.Empty;
                if (array == null)
                {
                    if (string.IsNullOrEmpty(image.FileLocation) || !File.Exists(Path.Combine(Path.GetDirectoryName(image.FileLocation), Path.GetFileName(image.FileLocation) + ".manifest")))
                    {
                        InjectionMethod injectionMethod = InjectionMethod.Create(InjectionMethodType.Standard);
                        IntPtr[]        array2          = injectionMethod.InjectAll(list.ToArray(), hProcess);
                        foreach (IntPtr ptr in array2)
                        {
                            if (ptr.IsNull())
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                    text = Path.Combine(Path.GetDirectoryName(image.FileLocation), Path.GetFileName(image.FileLocation) + ".manifest");
                }
                else
                {
                    text = Utils.WriteTempData(array);
                }
                if (string.IsNullOrEmpty(text))
                {
                    return(false);
                }
                IntPtr intPtr     = WinAPI.VirtualAllocEx(hProcess, IntPtr.Zero, (uint)ManualMap.RESOLVER_STUB.Length, 12288, 64);
                IntPtr lpAddress  = WinAPI.CreateRemotePointer(hProcess, Encoding.ASCII.GetBytes(text + "\0"), 4);
                IntPtr lpAddress2 = WinAPI.CreateRemotePointer(hProcess, Encoding.ASCII.GetBytes(string.Join("\0", list.ToArray()) + "\0"), 4);
                if (!intPtr.IsNull())
                {
                    byte[] array4 = (byte[])ManualMap.RESOLVER_STUB.Clone();
                    uint   num    = 0u;
                    BitConverter.GetBytes(ManualMap.FN_CREATEACTCTXA.Subtract(intPtr.Add(63L)).ToInt32()).CopyTo(array4, 59);
                    BitConverter.GetBytes(ManualMap.FN_ACTIVATEACTCTX.Subtract(intPtr.Add(88L)).ToInt32()).CopyTo(array4, 84);
                    BitConverter.GetBytes(ManualMap.FN_GETMODULEHANDLEA.Subtract(intPtr.Add(132L)).ToInt32()).CopyTo(array4, 128);
                    BitConverter.GetBytes(ManualMap.FN_LOADLIBRARYA.Subtract(intPtr.Add(146L)).ToInt32()).CopyTo(array4, 142);
                    BitConverter.GetBytes(ManualMap.FN_DEACTIVATEACTCTX.Subtract(intPtr.Add(200L)).ToInt32()).CopyTo(array4, 196);
                    BitConverter.GetBytes(ManualMap.FN_RELEASEACTCTX.Subtract(intPtr.Add(209L)).ToInt32()).CopyTo(array4, 205);
                    BitConverter.GetBytes(lpAddress.ToInt32()).CopyTo(array4, 31);
                    BitConverter.GetBytes(list.Count).CopyTo(array4, 40);
                    BitConverter.GetBytes(lpAddress2.ToInt32()).CopyTo(array4, 49);
                    if (WinAPI.WriteProcessMemory(hProcess, intPtr, array4, array4.Length, out num) && num == (uint)array4.Length)
                    {
                        uint num2 = WinAPI.RunThread(hProcess, intPtr, 0u, 5000);
                        result = (num2 != uint.MaxValue && num2 != 0u);
                    }
                    WinAPI.VirtualFreeEx(hProcess, lpAddress2, 0, 32768);
                    WinAPI.VirtualFreeEx(hProcess, lpAddress, 0, 32768);
                    WinAPI.VirtualFreeEx(hProcess, intPtr, 0, 32768);
                }
            }
            return(result);
        }