Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Hendrix en 2 Junio 2007, 22:42 pm



Título: [Source] Inyector de Dll's en C#
Publicado por: Hendrix en 2 Junio 2007, 22:42 pm
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
  1. /* Inyección de Dll escrito por Hendrix
  2.  * en base al codigo en C de MazarD por
  3.  * CreateRemoteThread.
  4.  *
  5.  * Fecha: 2-6-2007
  6.  *
  7.  * Dll echa en Vc++
  8.  */
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.Runtime.InteropServices;
  14. using System.Diagnostics;
  15. using System.ComponentModel;
  16. using System.IO;
  17.  
  18. namespace ConsoleApplication1
  19. {
  20.    class Apis
  21.    {
  22.        [DllImport("kernel32.dll", EntryPoint = "OpenProcess")]
  23.        public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
  24.  
  25.        [DllImport("kernel32", CharSet = CharSet.Ansi)]
  26.        public extern static int GetProcAddress(int hwnd, string procedureName);
  27.  
  28.        [DllImport("kernel32.dll", EntryPoint = "GetModuleHandle")]
  29.        public static extern int GetModuleHandle(string lpModuleName);
  30.  
  31.        [DllImport("kernel32.dll", EntryPoint = "VirtualAllocEx")]
  32.        public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
  33.  
  34.        [DllImport("kernel32")]
  35.        [return: MarshalAs(UnmanagedType.Bool)]
  36.        public static extern bool CloseHandle(IntPtr hObject);
  37.  
  38.        [DllImport("kernel32", EntryPoint = "CreateRemoteThread")]
  39.        public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, uint lpThreadId);
  40.  
  41.        [DllImport("kernel32.dll",EntryPoint = "WriteProcessMemory")]
  42.        public static extern IntPtr WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, IntPtr lpNumberOfBytesWritten);
  43.  
  44.    }
  45.  
  46.  
  47.  
  48.    class Program
  49.    {
  50.        static int Main(string[] args)
  51.        {
  52.            uint pid;
  53.            int load;
  54.            uint tama;
  55.            string ladll;
  56.            string nom;
  57.  
  58.            const uint PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
  59.            const uint MEM_COMMIT = 0x1000;
  60.            const uint MEM_RESERVE = 0x2000;
  61.            const uint PAGE_READWRITE = 0x40;
  62.  
  63.            try
  64.            {
  65.                Console.Write("Introduce el nombre del proceso (sin la extension): ");
  66.                nom = Console.ReadLine();
  67.                Console.Write("\nIntroduce la ruta de la dll a inyectar: ");
  68.                ladll = Console.ReadLine();
  69.  
  70.                if (File.Exists(ladll) == false)
  71.                {
  72.                    Console.WriteLine("La ruta de la Dll no existe");
  73.                    Console.Read();
  74.                    return 0;
  75.                }
  76.  
  77.                Byte[] dll = Encoding.ASCII.GetBytes(ladll);
  78.                tama = Convert.ToUInt32(ladll.Length);
  79.  
  80.                Process[] nombre = Process.GetProcessesByName(nom);
  81.                if (nombre.Length == 0)
  82.                {
  83.                    Console.WriteLine("El proceso no existe");
  84.                    Console.Read();
  85.                    return 0;
  86.                }
  87.                pid = Convert.ToUInt32(nombre[0].Id);
  88.  
  89.                IntPtr proc = Apis.OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  90.                load = Apis.GetProcAddress(Apis.GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  91.                IntPtr nLoad = new IntPtr(load);
  92.                IntPtr rems = Apis.VirtualAllocEx(proc, IntPtr.Zero, Convert.ToUInt32(ladll.Length), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  93.                Apis.WriteProcessMemory(proc, rems, dll, tama, IntPtr.Zero);
  94.                Apis.CreateRemoteThread(proc, IntPtr.Zero, 0, nLoad, rems, 0, 0);
  95.                Apis.CloseHandle(proc);
  96.                return 1;
  97.            }
  98.  
  99.            catch
  100.            {
  101.                Console.WriteLine("\nUn error ha ocurrido.");
  102.                Console.ReadLine();
  103.                return 0;
  104.            }
  105.  
  106.  
  107.  
  108.        }
  109.    }
  110. }

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... :-\ :-\



Título: Re: [Source] Inyector de Dll's en C#
Publicado por: BADBYTE-K en 3 Junio 2007, 17:23 pm
no hay problema, esta bien como referencia aca
 :D
gracias,
Saludos