Foro de elhacker.net

Seguridad Informática => Análisis y Diseño de Malware => Mensaje iniciado por: fary en 8 Octubre 2010, 20:44 pm



Título: [ASM+C] Inyeccion Dll
Publicado por: fary en 8 Octubre 2010, 20:44 pm
Bueno trasteandole a la inyeccion dll y a la vez ensayando c y asm consegui esto:

Codigo inyector  en c:

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <string.h>
  5.  
  6. void inyectar(char * rutadll);
  7.  
  8. int main()
  9. {
  10.   inyectar("c:\\windows\\system32\\fary.dll");
  11.    return 0;
  12. }
  13.  
  14. void inyectar(char * rutadll)
  15. {
  16.    DWORD id;
  17.    DWORD mangoproc;
  18.    HANDLE idproc;
  19.    LPVOID espacio;
  20.    LPVOID carga;
  21.  
  22.    printf("Inyeccion Dll by Drinky94\n");
  23.  
  24.    ShellExecute(0,0,"c:\\windows\\system32\\calc.exe",0,0,0);
  25.    Sleep(2000); //  Esperamos que se ejecute la calculadora...
  26.    id = FindWindow(NULL,"Calculadora");
  27.    GetWindowThreadProcessId(id,&mangoproc);
  28.  
  29.    idproc = OpenProcess(PROCESS_ALL_ACCESS,0,mangoproc);
  30.  
  31.    carga = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  32.    espacio = (LPVOID)VirtualAllocEx(idproc,0,strlen(rutadll),MEM_COMMIT, PAGE_READWRITE);
  33.    WriteProcessMemory(idproc,(LPVOID)espacio,rutadll,strlen(rutadll),0);
  34.    CreateRemoteThread(idproc, 0, 0,(LPTHREAD_START_ROUTINE)carga,(LPVOID)espacio, 0, 0);
  35.  
  36.    CloseHandle(idproc);
  37.    CloseHandle(espacio);
  38.  
  39.    system("PAUSE");      
  40. }

Dll creada en Fasm:

Código
  1. format PE GUI 4.0 DLL
  2. entry DllEntryPoint
  3.  
  4. include 'c:\include\win32ax.inc'
  5.  
  6. section '.code' code readable executable
  7.  
  8. proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
  9.        cmp [fdwReason],1
  10.        JE mensage
  11.        mensage:
  12.        invoke MessageBox,0,mensajito,titulo,MB_OK
  13.        ret
  14. endp
  15.  
  16. ; VOID ShowErrorMessage(HWND hWnd,DWORD dwError);
  17.  
  18. proc ShowErrorMessage hWnd,dwError
  19.  local lpBuffer:DWORD
  20.        lea     eax,[lpBuffer]
  21.        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
  22.        invoke  MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
  23.        invoke  LocalFree,[lpBuffer]
  24.        ret
  25. endp
  26.  
  27. ; VOID ShowLastError(HWND hWnd);
  28.  
  29. proc ShowLastError hWnd
  30.        invoke  GetLastError
  31.        stdcall ShowErrorMessage,[hWnd],eax
  32.        ret
  33. endp
  34. section '.data' data readable writeable
  35.        mensajito db 'Dll Inyectada con exito',0
  36.        titulo db ' Exito!!',0
  37.  
  38.  
  39. section '.idata' import data readable writeable
  40.  
  41.  library kernel,'KERNEL32.DLL',\
  42.          user,'USER32.DLL'
  43.  
  44.  import kernel,\
  45.         GetLastError,'GetLastError',\
  46.         SetLastError,'SetLastError',\
  47.         FormatMessage,'FormatMessageA',\
  48.         LocalFree,'LocalFree'
  49.  
  50.  import user,\
  51.         MessageBox,'MessageBoxA'
  52.  
  53. section '.edata' export data readable
  54.  
  55.  export 'ERRORMSG.DLL',\
  56.         ShowErrorMessage,'ShowErrorMessage',\
  57.         ShowLastError,'ShowLastError'
  58.  
  59. section '.reloc' fixups data discardable
  60.  



Una Capturita:

(http://img708.imageshack.us/img708/2905/inyectada.png)

proximamente mas y mejor  que ya le estoi tomando el gusto  ;D

PD: Agradecimiento a [Zero] por su incansable ayuda en todo lo que le pregunto.

salu2!


Título: Re: [ASM+C] Inyeccion Dll
Publicado por: [Zero] en 8 Octubre 2010, 21:39 pm
Que bien que lo conseguiste  ;D. Sigue por ese camino, ahora que sabes inyectar una DLL puedes trastear con API Hooking, MazarD tiene unos excelentes textos sobre eso.

Saludos


Título: Re: [ASM+C] Inyeccion Dll
Publicado por: bizco en 9 Octubre 2010, 00:28 am
en el entry de la dll la comparacion no tiene efecto.


Título: Re: [ASM+C] Inyeccion Dll
Publicado por: fary en 9 Octubre 2010, 00:31 am
en el entry de la dll la comparacion no tiene efecto.

cierto XDD pero asi me quedo mas claro el DllMain callback.

salu2!


Título: Re: [ASM+C] Inyeccion Dll
Publicado por: prat en 28 Octubre 2010, 11:32 am
Yo tenia algo parecido, pero todo con Delphi y me funcionaba en WinXp pero con Win7 no me salia el dichoso mensajito de "Inyectado con exito"....  :-[

Segun me parecio entender, en Win7 calc.exe no se ejecutaba con el permiso seDebugPrivilege y en consecuencia CreateRemoteThread devolvia el error ERROR_ACCESS_DENIED.


¿Has probado tu codigo en Win7?


Correccion:

Ahora me he fijado mejor y si que funcionaba en mi Win7 64 siempre que no intente inyectar la dll dentro de un proceso de 64 bits. Imagino que en mi Windows, el calc.exe sea de 64. De ahi la confusion..