elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Guía actualizada para evitar que un ransomware ataque tu empresa


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  [Source] Inyeccion Dll Microsoft Visual C# (Sharp) 2008
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Source] Inyeccion Dll Microsoft Visual C# (Sharp) 2008  (Leído 2,942 veces)
XSaMuXPH *-* Traigo uno encima! =D!

Desconectado Desconectado

Mensajes: 17


Ver Perfil
[Source] Inyeccion Dll Microsoft Visual C# (Sharp) 2008
« en: 3 Octubre 2009, 02:26 am »

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
  1.        private void InjectDLL(IntPtr hProcess, String strDLLName)
  2.        {
  3.            IntPtr bytesout;
  4.            Int32 LenWrite = strDLLName.Length + 1;
  5.            IntPtr AllocMem = (IntPtr)APIS.VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
  6.            APIS.WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
  7.            UIntPtr Injector = (UIntPtr)APIS.GetProcAddress(APIS.GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  8.  
  9.            if (Injector == null)
  10.            {
  11.                 // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
  12.                return;
  13.            }
  14.  
  15.            IntPtr hThread = (IntPtr)APIS.CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
  16.  
  17.            if (hThread == null)
  18.            {
  19.                 // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
  20.                return;
  21.            }
  22.  
  23.            int Result = APIS.WaitForSingleObject(hThread, 10 * 1000);
  24.            if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFF)
  25.            {
  26.                 // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
  27.  
  28.                if (hThread != null)
  29.                {
  30.                    APIS.CloseHandle(hThread);
  31.                }
  32.                return;
  33.            }
  34.  
  35.            Thread.Sleep(1000);
  36.            APIS.VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
  37.  
  38.            if (hThread != null)
  39.            {
  40.                APIS.CloseHandle(hThread);
  41.            }
  42.  
  43.            return;
  44.        }

Codigo clase APIS:

Código
  1.        private struct APIS
  2.        {
  3.            [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  4.            public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
  5.  
  6.            [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
  7.            public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
  8.  
  9.            [DllImport("kernel32.dll")]
  10.            public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten);
  11.  
  12.            [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  13.            public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
  14.  
  15.            [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  16.            public static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
  17.  
  18.            [DllImport("kernel32")]
  19.            public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId);
  20.  
  21.            [DllImport("kernel32.dll")]
  22.            public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, Int32 dwProcessId);
  23.  
  24.            [DllImport("kernel32.dll")]
  25.            public static extern Int32 CloseHandle(IntPtr hObject);
  26.  
  27.            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  28.            public static extern IntPtr GetModuleHandle(string lpModuleName);
  29.        }

Bien, ahora en using:

Código
  1. using System;
  2. using System.IO;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Drawing;
  7. using System.Threading;
  8. using System.Diagnostics;
  9. using System.Windows.Forms;
  10. using System.ComponentModel;
  11. using System.Collections.Generic;
  12. using System.Runtime.InteropServices;

Codigo para los procesos :P:

Código
  1.        private Int32 GetProcessID(String proc)
  2.        {
  3.            Process[] ProcList;
  4.            ProcList = Process.GetProcessesByName(proc);
  5.            return ProcList[0].Id;
  6.        }

Ahora Inyectemos la dll :xD:

Código
  1.        private void button1_Click(object sender, EventArgs e)
  2.        {
  3.  
  4.         string DllStrng = "C:\\Ladll.Dll";
  5.  
  6.         Int32 ProcID = GetProcessID("Explorer"); //Coloquen aqui el nombre del proceso :P sin la extensión ".exe".
  7.            if (ProcID >= 0)
  8.            {
  9.                IntPtr hProcess = (IntPtr)APIS.OpenProcess(0x1F0FFF, 1, ProcID);
  10.                if (hProcess == null)
  11.                {
  12.                 // Agreguen aqui un messagebox o lo que deseen para mostrar el error :P.
  13.                    return;
  14.                }
  15.                else
  16.                {
  17.                    InjectDLL(hProcess, DllStrng);
  18.                }
  19.            }
  20.        }

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!


« Última modificación: 3 Octubre 2009, 02:34 am por WhatsHappen? » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[DUDA]Programar en C una especie de LOGIN [C SHARP, visual c]
.NET (C#, VB.NET, ASP)
TopoJunior 2 2,097 Último mensaje 14 Septiembre 2012, 03:41 am
por Mr.Blue
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines