Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   С/С++, C#, Delphi, .NET, Asm (https://forum.antichat.xyz/forumdisplay.php?f=24)
-   -   Dll Injection в C# (https://forum.antichat.xyz/showthread.php?t=190607)

Sharper 26.03.2010 21:05

Dll Injection в C#
 
Здравствуйте, хочу сделать Dll Injection в C#. Нашел код :

DLL in a process
Код:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Threading; 
 
namespace Dll_Injector 

    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", CharSetCharSet = 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", CharSetCharSet = 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) 
        { 
            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) 
            { 
                //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); 
            // 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 Form1_Load(object sender, EventArgs e) 
        { 
 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            String strDLLName = "C:\\Users\\JotaC\\Desktop\\C#\\Projects\\L2Soul\\L2Soul\\bin\\Release\\L2Soul.dll"; 
            String strProcessName = "notepad"; 
 
            Int32 ProcID = GetProcessId(strProcessName); 
            if (ProcID >= 0) 
            { 
                IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID); 
                if (hProcess == null) 
                { 
                    MessageBox.Show("OpenProcess() Failed!"); 
                    return; 
                } 
                else 
                    InjectDLL(hProcess, strDLLName); 
            } 
 
        } 
    } 
}

my DLL (class application):
Код:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
 
namespace L2Soul 

    public class Class1 
    { 
        [DllImport("user32.dll")] 
 
        public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vkey); 
 
        public void Main() 
        { 
            while (true) 
            { 
                if (Convert.ToInt32(GetAsyncKeyState(Keys.Z).ToString()) < 0) 
                    MessageBox.Show("OK"); 
                else if (Convert.ToInt32(GetAsyncKeyState(Keys.X).ToString()) < 0) 
                { 
                    MessageBox.Show("GOING DOWN"); 
                    break; 
                } 
            } 
        } 
    } 
}

Должна запустится DLL. Но не запускается =(

sn0w 26.03.2010 21:15

ну по логике все верно в коде

длл твоя должна быть в одной папке с ехе процесса к которому аттачишься, либо в аргументе LoadLibrary прописывай полный путь к длл

Sharper 26.03.2010 21:19

sn0w главное, что при повторной компиляции DLL оно говорит, что DLL используется. Значит оно привязало...

Копирование DLL в папку с ехе файлом не помогло.

sn0w 26.03.2010 21:20

возьми прогу processexplorer запусти - там в меню будет Find Handle/DLL - поищи куда она подгрузилась да убей

Sharper 26.03.2010 21:24

Цитата:

возьми прогу processexplorer запусти - там в меню будет Find Handle/DLL - поищи куда она подгрузилась да убей
Проблема чтоб не убить ! Оно привязало к ехе, но DLL не выполнилась...

sn0w 26.03.2010 21:30

я не знаю как настраивается C#, еслиб было C/C++ то подсказалбы. возможно что точка входа не та - или там библиотека нужна, цикл там какойто или хз - шарп я не знаю

Interceptor 08.04.2010 14:27

Поковырял я эту тему...
Исходник лоадера на C# рабочий, длл загружает в процесс как и положено, но код из длл не вызывается. Похоже, проблема в том, что на C# неизвестно как создать входную точку для длл типа DllMain на С++. Попробовал классическую длл на С++ - все подгружается и работает.

Вот несколько ссылок где ребята пробуют это решить на C# (но там все для более продвинутых дотнетчиков чем я :)):
http://stackoverflow.com/questions/1717318/creating-an-entry-point-in-a-c-dll-to-call-from-wix
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/9a972716-d6a3-43c2-b841-a3a78cf230e3
http://blogs.msdn.com/junfeng/archive/2005/11/19/494914.aspx
http://forum.sources.ru/index.php?showtopic=252227

noxjoker 10.04.2010 00:29

Все разобрался код робочий.


Время: 08:17