[Source] Inyector de Dll's en C#

(1/1)

Hendrix:
Bueno, esta tarde como ya estaba cansado de programar otra cosa me decidi a traducir el codigo en C de MazarD sobre inyectar una Dll con el metodo de CreateRemoteThread. Antes lo habia intentado en VB pero me daba error en una API, con este lenguaje no e tenido problemas en traducirlo (lo que mas me a costado a sido buscar las declaraciones de las Apis.... :xD).

Aqui va el codigo:

Código
/* Inyección de Dll escrito por Hendrix
 * en base al codigo en C de MazarD por
 * CreateRemoteThread.
 *
 * Fecha: 2-6-2007
 *
 * Dll echa en Vc++
 */
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
 
namespace ConsoleApplication1
{
   class Apis
   {
       [DllImport("kernel32.dll", EntryPoint = "OpenProcess")]
       public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
 
       [DllImport("kernel32", CharSet = CharSet.Ansi)]
       public extern static int GetProcAddress(int hwnd, string procedureName);
 
       [DllImport("kernel32.dll", EntryPoint = "GetModuleHandle")]
       public static extern int GetModuleHandle(string lpModuleName);
 
       [DllImport("kernel32.dll", EntryPoint = "VirtualAllocEx")]
       public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
 
       [DllImport("kernel32")]
       [return: MarshalAs(UnmanagedType.Bool)]
       public static extern bool CloseHandle(IntPtr hObject);
 
       [DllImport("kernel32", EntryPoint = "CreateRemoteThread")]
       public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);
 
       [DllImport("kernel32.dll",EntryPoint = "WriteProcessMemory")]
       public static extern IntPtr WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, IntPtr lpNumberOfBytesWritten);
 
   }
 
 
 
   class Program
   {
       static int Main(string[] args)
       {
           uint pid;
           int load;
           uint tama;
           string ladll;
           string nom;
 
           const uint PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
           const uint MEM_COMMIT = 0x1000;
           const uint MEM_RESERVE = 0x2000;
           const uint PAGE_READWRITE = 0x40;
 
           try
           {
               Console.Write("Introduce el nombre del proceso (sin la extension): ");
               nom = Console.ReadLine();
               Console.Write("\nIntroduce la ruta de la dll a inyectar: ");
               ladll = Console.ReadLine();
 
               if (File.Exists(ladll) == false)
               {
                   Console.WriteLine("La ruta de la Dll no existe");
                   Console.Read();
                   return 0;
               }
 
               Byte[] dll = Encoding.ASCII.GetBytes(ladll);
               tama = Convert.ToUInt32(ladll.Length);
 
               Process[] nombre = Process.GetProcessesByName(nom);
               if (nombre.Length == 0)
               {
                   Console.WriteLine("El proceso no existe");
                   Console.Read();
                   return 0;
               }
               pid = Convert.ToUInt32(nombre[0].Id);
 
               IntPtr proc = Apis.OpenProcess(PROCESS_ALL_ACCESS, false, pid);
               load = Apis.GetProcAddress(Apis.GetModuleHandle("kernel32.dll"), "LoadLibraryA");
               IntPtr nLoad = new IntPtr(load);
               IntPtr rems = Apis.VirtualAllocEx(proc, IntPtr.Zero, Convert.ToUInt32(ladll.Length), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
               Apis.WriteProcessMemory(proc, rems, dll, tama, IntPtr.Zero);
               Apis.CreateRemoteThread(proc, IntPtr.Zero, 0, nLoad, rems, 0, 0);
               Apis.CloseHandle(proc);
               return 1;
           }
 
           catch
           {
               Console.WriteLine("\nUn error ha ocurrido.");
               Console.ReadLine();
               return 0;
           }
 
 
 
       }
   }
}

Le e añadido algo de poca importancia, y es que en el codigo de MazarD te pedia el pid y en este te pide el nombre (sin la extension) del ejecutable donde se tiene que inyectar, que es algo mas comodo.

Un Saludo y espero que le sirva a alguien.  ;)


Nota al moderador:
Si se tiene que mover a Troyenos y Virus se meuve....no lo e posteado hay porque precisamente esta el de MazarD en C... :-\ :-\

BADBYTE-K:
no hay problema, esta bien como referencia aca
 :D
gracias,
Saludos

Navegación

[0] Índice de Mensajes