[Source] Inyeccion Dll Microsoft Visual C# (Sharp) 2008

(1/1)

XSaMuXPH *-* Traigo uno encima! =D!:
Hola, guiandome de codigos de inyeccion dll en no se cuantos lenguajes pude lograr uno en C# :P.

Aqui se los dejo espero lo disfruten :xD:

Codigo InyectDll:

Código
       private void InjectDLL(IntPtr hProcess, String strDLLName)
       {
           IntPtr bytesout;
           Int32 LenWrite = strDLLName.Length + 1;
           IntPtr AllocMem = (IntPtr)APIS.VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
           APIS.WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
           UIntPtr Injector = (UIntPtr)APIS.GetProcAddress(APIS.GetModuleHandle("kernel32.dll"), "LoadLibraryA");
 
           if (Injector == null)
           {
                // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
               return;
           }
 
           IntPtr hThread = (IntPtr)APIS.CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
 
           if (hThread == null)
           {
                // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
               return;
           }
 
           int Result = APIS.WaitForSingleObject(hThread, 10 * 1000);
           if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFF)
           {
                // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
 
               if (hThread != null)
               {
                   APIS.CloseHandle(hThread);
               }
               return;
           }
 
           Thread.Sleep(1000);
           APIS.VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
 
           if (hThread != null)
           {
               APIS.CloseHandle(hThread);
           }
 
           return;
       }

Codigo clase APIS:

Código
       private struct APIS
       {
           [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
           public 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")]
           public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten);
 
           [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
           public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
 
           [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
           public static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
 
           [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", CharSet = CharSet.Auto)]
           public static extern IntPtr GetModuleHandle(string lpModuleName);
       }

Bien, ahora en using:

Código
using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.InteropServices;

Codigo para los procesos :P:

Código
       private Int32 GetProcessID(String proc)
       {
           Process[] ProcList;
           ProcList = Process.GetProcessesByName(proc);
           return ProcList[0].Id;
       }

Ahora Inyectemos la dll :xD:

Código
       private void button1_Click(object sender, EventArgs e)
       {
 
        string DllStrng = "C:\\Ladll.Dll";
 
        Int32 ProcID = GetProcessID("Explorer"); //Coloquen aqui el nombre del proceso :P sin la extensión ".exe".
           if (ProcID >= 0)
           {
               IntPtr hProcess = (IntPtr)APIS.OpenProcess(0x1F0FFF, 1, ProcID);
               if (hProcess == null)
               {
                // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
                   return;
               }
               else
               {
                   InjectDLL(hProcess, DllStrng);
               }
           }
       }

Listo! xD. como ven este codigo no puede inyectar dlls en procesos como svchost.exe pues necesita permisos (algo que no se hacer), me imagino que se hace con AdjustTokenPrivileges o algo parecido en fin si quieren permisos agreguenselo uds y si pueden muestrenlo aqui para ayudar a los demas :rolleyes:.

yo actualmente ando estudiando Kernel Module, para asi poder hacer fiesta con los procesos xD.

en fin disfrutenlo, hasta pronto!

Creditos: WhatsHappen?

:xD!

Navegación

[0] Índice de Mensajes