Hey ihr,
ich versuche nun vergeblich seit 3 Tagen eine simple (native) dll in mein Notepad zu injektieren. Diese Dll soll und gibt eine MessageBox aus sobald sie injiziert ist. Das ganze habe ich basierend auf dem Tutorial von diesem Forum ausprobiert: [Ger-TuT] Code/Dll Injection mit dem Tool winject17b ausprobiert. Mein Problem ist das mein Code zwar die dll injeziert (das vermute ich zumindest) jedoch anschließend nichts passiert (es soll eine MessageBox ausgegeben werden).
Dies ist mein Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("kernel32")]
public static extern IntPtr CreateRemoteThread(
IntPtr hProcess,
IntPtr lpThreadAttributes,
uint dwStackSize,
UIntPtr lpStartAddress, // raw Pointer into remote process
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)]
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)]
static extern IntPtr VirtualAllocEx(
IntPtr hProcess,
IntPtr lpAddress,
uint dwSize,
uint flAllocationType,
uint flProtect
);
[DllImport("kernel32.dll")]
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 Int32 GetProcessId(String proc)
{
Process[] ProcList;
ProcList = Process.GetProcessesByName(proc);
return ProcList[0].Id;
}
public void InjectDLL(IntPtr hProcess, String strDLLName, Process proc)
{
IntPtr bytesout;
// Length of string containing the DLL file name +1 byte padding
Int32 LenWrite = strDLLName.Length + 1;
// Allocate memory within the virtual address space of the target process
IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40); //allocation pour WriteProcessMemory
// Write DLL file name to allocated memory in target process
WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
// Function pointer "Injector"
UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
if (Injector == null)
{
MessageBox.Show(" Injector Error! \n ");
// return failed
return;
}
// Create thread in target process, and store handle in hThread
IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
// Make sure thread handle is valid
if (hThread == null || hThread.ToInt32() == 0)
{
//incorrect thread handle ... return failed
MessageBox.Show(" hThread [ 1 ] Error! \n ");
return;
}
// Time-out is 10 seconds...
int Result = WaitForSingleObject(hThread, 10 * 1000);
// Check whether thread timed out...
if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
{
/* Thread timed out... */
MessageBox.Show(" hThread [ 2 ] Error! \n ");
// Make sure thread handle is valid before closing... prevents crashes.
if (hThread != null)
{
//Close thread in target process
CloseHandle(hThread);
}
return;
}
// Sleep thread for 1 second
Thread.Sleep(1000);
// Clear up allocated space ( Allocmem )
VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
MessageBox.Show(proc.Modules.Count.ToString());
// Make sure thread handle is valid before closing... prevents crashes.
if (hThread != null)
{
//Close thread in target process
CloseHandle(hThread);
}
// return succeeded
return;
}
private void button1_Click(object sender, EventArgs e)
{
String strDLLName = @"Message Box.dll";
String strProcessName = "notepad";
Int32 ProcID = GetProcessId(strProcessName);
Process proc = Process.GetProcessById(ProcID);
if (ProcID >= 0)
{
IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
if (hProcess == null)
{
MessageBox.Show("OpenProcess() Failed!");
return;
}
else
{
MessageBox.Show(proc.Modules.Count.ToString());
InjectDLL(hProcess, strDLLName, proc);
}
}
}
}
}
ich habe bereits viele tutorials gelesen, weiß jedoch nicht wieso ich eine simple dll nicht in das notepad injeziert bekomme. Es würde mich freuen wenn mir jemand sagen könnte wo mein Fehler liegt. Im Anhangf befindet sich noch die dll die ich injezieren möchte in mein Notepad.
Vielen Dank
Hi,
erstmal .net und dll Injection oder besser gesagt dll injection Allgemein = BÖÖÖÖÖSE !
Zweitens : Es fehlt mir der Parameter [unsafe]
Nur so sind Zeigeroperationen möglich.
Den Rest musste dir selber herleiten , ich unterstütze keine Fremdeinspeisung 😉
Danke für die hilfe. Ich weiß das .net nicht dfie perfekte wahl ist. Diese Sprache wird nur bei uns in der Firma verwendet und daher bin ich gezwungen zumindest den Hauptteil in c# zu schreiben. Ich benutze überhaupt keine Pointer, wieso sollte ich also "unsafe" code benutzen, ist das wirklich notwendig, ich bin mir nicht sicher ob es selbst mit unsafe code funktionieren würde? Das ganze soll dazu dienen im Wordpad einige zusätzliche Funktionen einzubauen (nichts illigales..)
Vielen Dank!
Edit:
habe meine beiden klassen + alle variablen und imports als unsafe deklariert, dennoch funktioniert es nicht, injeziert wird evtl aber es passiert nichts.
Hi,
damals zu c++ Zeiten 😉 musste man noch sich das Debugrecht für den aktuellen User holen.
Habe auf die schnelle nur nen c++ Beispiel gefunden, kannst aber daraus den c# Code herleiten
Grüße
Hey,
vielen Dank für diesen Hinweis, leider hat auch hinzufügen eines debug priviliges nichts an dem Problem geändert. Gehe ich der richtigen annahme, wenn ich die dll injecte, dass die dll in den Process Modulen auftauchen muss? wenn ja, dann klappt nichteinmal die injektion.
in Globale Hooks mit c# (nichtnur lowlevel) habe ich eine dll gepostet, die es erlaubt, c#code in anderen anwendungen auszuführen. Dazu habe ich auchnoch den code für dllinjection gepostet.
Was mir an deinem code auffällt:
Guck im taskmanager mal nach wo überall ein sternchen steht, und wo nicht.
Ob die dll für x64 compiliert ist, kannst du mit depends.exe herausfinden (das programm ist bei windows sdk und visual studio mit dabei)
stell probeweise mal die target cpu auf x86.
2.
[DllImport("kernel32.dll")]
static extern bool WriteProcessMemory(...
wenn du loadlibraryA verwendest, musst du im dllimport-attribut auch das charset=ansi setzen!
hm, ich bekomme auch mit deinem beispiel die dll nicht injected. Ich habe nun mal geguckt was mit dem Process passiert, dieser wird jedesmal wenn ich injecte um 40kb größer, finden kann ich die dll jedoch trotzdem nicht mit dem Process Explorer. Ich bin langsam echt verzweifelt. Muss ich die Dll nach dem injection noch aufrufen? Ich bekomme beim CreateRemoteThread(..) eine int wert zurück, dh würde bedeuten, dass die dll injectect wurde. Wieso verdammt nochmal wird nur einfach nichts ausgeführt?
Vielen Dank
Hi,
was mir zunächst auffällt: in "strDLLName" steht nicht der Pfad zur DLL. Finde ich etwas zu fehleranfällig.
Und was natürlich fehlt ist der Quelltext zur "Message Box.DLL".... Vielleicht wird die DLL ja injected, aber macht nix. Denn mit CreateRemoteThread wird die DLL ja nur in den Adressraum des anderen Prozesses geladen - ausgeführt wird da ja nix - mit Ausnahme der Funktion "DllMain() in der DLL. Was macht denn die DllMain() in Deiner "Message Box.DLL" ???
Bye,
Knick
Debuggers don't remove Bugs, they only show them in Slow-Motion.
hier der inhalt der c++ dll:
#include "windows.h"
void InjNachricht()//der erstellte Thread
{
MessageBox(0, "Siehe da! Es klappt :P", " Whohoo", 0);//4. Eine Message Box
}
int WINAPI DllMain(HINSTANCE hInst,DWORD reason,LPVOID reserved)//1. Beim öffnen der Dll wird diese funktion ausgeführt
{
if(reason==DLL_PROCESS_ATTACH)//2. Falls der Grund des Aufrufes der DllMain-Funktion das Anhängen an ein Prozesses ist..
{
CreateThread(0, 0, (LPTHREAD_START_ROUTINE) InjNachricht, 0, 0, 0);//3.erstelle einen SubProzess, der die übergebene Funktion ausführt
}
return true;
}
auch danke für deine hinweise, den pfad hatte ich bereits geändert auch dies hatte kein effekt. Habe als OS Win7, 64Bit. Mein Notepad ist auch eine 64 bit anwendung.
Das habe ich bereits versucht, meine dll taucht in den Modulen von dem Process "Notepad" nicht auf. Ich kann jedoch verststellen das der Pfad der dll in den address speicher geschrieben wurde und sich der Process um 40kb erweitert, was genau der größe meiner DLL entspricht.
EDIT:
Kann es sein das deswegen nichts passiert weil die DLL (welche ich injecten möchte) eine 32Bit dll ist und Meine Aplication und das Notepad 64bit sind?
Kann es sein das deswegen nichts passiert weil die DLL (welche ich injecten möchte) eine 32Bit dll ist und Meine Aplication und das Notepad 64bit sind?
Eine 64 Bit Applikation kann u. U. eine 32 Bit DLL verwenden - das setzt aber einiges voraus. Kannst ja mal hier http://www.dnjonline.com/article.aspx?id=jun07_access3264 lesen.
Ich frage mich allerdings, warum Du es nicht selber probierst und diese DLL einfach mal als 64 Bit DLL erstellst....
Bye,
Knick
Debuggers don't remove Bugs, they only show them in Slow-Motion.