DllInjection geht nur teilweise

Hay ihr, ich hab da mal 'ne ziemlich doofe Frage:

Ich hab mir einen DLL-Injector in C# geschrieben:
Code:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace DLLInjector1
{
    public partial class Form1 : Form
    {
        private string DLLPfad;

        public Form1()
        {
            DllAuswahlButton = new Button
            {
                Location = new Point(13, 13),
                Name = "DllAuswahlButton",
                Size = new Size(259, 23),
                Text = @"Dll Auswählen"
            };
            DllAuswahlButton.Click += DllAuswahlButton_Click;

            InjectionButton = new Button
            {
                Enabled = false,
                Location = new Point(13, 69),
                Name = "InjectionButton",
                Size = new Size(260, 23),
                Text = @"Injection"
            };
            InjectionButton.Click += InjectionButton_Click;

            ProzessComboBox = new ComboBox
            {
                Enabled = false,
                Location = new Point(13, 42),
                Name = "ProzessComboBox",
                Size = new Size(259, 21),
            };
            ProzessComboBox.SelectedIndexChanged += ProzessComboBox_SelectedIndexChanged;
            SuspendLayout();

            AutoScaleDimensions = new SizeF(6F, 13F);
            ClientSize = new Size(284, 104);
            Controls.Add(ProzessComboBox);
            Controls.Add(InjectionButton);
            Controls.Add(DllAuswahlButton);
            ResumeLayout(false);

            var liste = Process.GetProcesses().Select(variable => variable.ProcessName).ToList();
            liste.Sort();
            foreach (var VARIABLE in liste)
            {
                ProzessComboBox.Items.Add(VARIABLE);
            }
        }

        [DllImport("kernel32")]
        public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId);

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, Int32 dwProcessId);

        [DllImport("kernel32.dll")]
        public static extern Int32 CloseHandle(IntPtr hObject);

        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        private static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);

        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
        public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);

        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);

        [DllImport("kernel32.dll")]
        private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer,
        UIntPtr nSize, out IntPtr lpNumberOfBytesWritten);

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

        [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
        internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);

        public int GetProcessId(string proc)
        {
            var pro = Process.GetProcessesByName(proc);

            return pro.Length == 0 ? 0 : pro[0].Id;
        }

        public void InjectDLL(IntPtr hProcess)
        {
            IntPtr bytesout;

            var LenWrite = DLLPfad.Length + 1;
            var AllocMem = VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
            WriteProcessMemory(hProcess, AllocMem, DLLPfad, (UIntPtr)LenWrite, out bytesout);

            var hThread = CreateRemoteThread(hProcess, (IntPtr)null, 0, GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"), AllocMem, 0, out bytesout);

            var Result = WaitForSingleObject(hThread, 10 * 1000);

            if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
            {
                MessageBox.Show(@" hThread [ 2 ] Error!  ");
                CloseHandle(hThread);
                return;
            }

            Thread.Sleep(1000);

            VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);

            CloseHandle(hThread);
        }

        private void DllAuswahlButton_Click(object sender, EventArgs e)
        {
            var result = new OpenFileDialog
            {
                FileName = "Dynamic Link Libary",
                DefaultExt = ".dll",
                Filter = @"Dynamic Link Libary (.dll)|*.dll"
            };

            result.ShowDialog();
            DLLPfad = result.FileName;

            ProzessComboBox.Enabled = true;
        }

        private void InjectionButton_Click(object sender, EventArgs e)
        {
            var ProcID = GetProcessId(ProzessComboBox.SelectedItem.ToString());

            if (ProcID > 0)
            {
                InjectDLL(OpenProcess(0x1F0FFF, 1, ProcID));
            }
            else
            {
                MessageBox.Show(@"GetProcess failed");
            }
        }

        private void ProzessComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            InjectionButton.Enabled = true;
        }
    }
}

und dazu einfach eine DLL in C++, da C# keine DllMain-Funktion hat:

Code:
BOOL WINAPI DllMain(__in HINSTANCE  hInstance,__in DWORD Reason,__in LPVOID     Reserved)
{             
    switch (Reason)
    {
    case DLL_PROCESS_ATTACH:
AllocConsole();
        break;
    }
    return TRUE;
}

Funktioniert kombiniert super, jetzt aber mal zu meine Frage:

Das funktioniert zB. mit Firefox, mit dem DllInjector selbst auch, aber dann mit zB. dem Explorer nicht mehr, und genau da wollte ich die DLL rein haben, auch wenn ich Adminrechte anfordere für den Injektor funktioniert das nicht...
Wisst ihr, wie ich damit klarkomme?

Danke schonmal
Sx
 
Grundsätzlich: entweder debuggen oder zumindest die Rückgabewerte prüfen (damit dürfte man ein deutlich detailierterers "funktioniert nicht" in der Hand haben). Ansonsten: 32/64 bit Mix?
 
Ich muss gestehen, das beschämt mich sehr, dass ich das nicht erwägt habe...
Jetzt wo dus sagst, DLLs müssen in der Bittigkeit mit dem Prozess stimmen, so simpel, und trotzden nicht dran gedacht... Hab die Konfiguration auf 64bit geändert, neu kompiliert und alles klappt!

Danke, und sorry für die Zeitverschwendung mit so ner einfachen Frage :thumb_up:
 
Zurück
Oben