Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - caricare una dll in un processo vb.net
Forum - C# / VB.NET - caricare una dll in un processo vb.net

Avatar
gianluca (Normal User)
Pro


Messaggi: 103
Iscritto: 23/06/2008

Segnala al moderatore
Postato alle 14:37
Venerdì, 19/06/2009
Ciao a tutti! :D Sto tentando di fare 1 exe in vb.net che mi carichi 1 dll in un altro processo! Ho trovato 1 esempio(mesi fa) fatto in vb6(funzionante) ma solo ora ho avuto tempo di provare a tradurlo in .net! ho fatto anke 1 dll(messa nella stessa cartella dell'exe .net) ke se viene caricata fa apre 1 msgbox per capire ke è funzionato!! è tutta la mattina che voglio vedere quella msgbox ma nn viene fuori! ufffff! :(
chi mi da 1 manooo???


Grazie 1000 in anticipo

Gianluca


gianluca ha allegato un file: Injector.zip (261497 bytes)
Clicca qui per scaricare il file

Ultima modifica effettuata da gianluca il 19/06/2009 alle 17:07
PM Quote
Avatar
gianluca (Normal User)
Pro


Messaggi: 103
Iscritto: 23/06/2008

Segnala al moderatore
Postato alle 17:29
Venerdì, 19/06/2009
Ah, giusto! ho messo anche 2 Label che se finisce tutto con successo mi scrive che è stata caricata! bene, le label mi scrivono che la dll è stata caricata ma in realta non è vero!
xke quello in vb6 funziona e questo in vb.net no?

ps, questo è il modulo in vb6

Codice sorgente - presumibilmente VB.NET

  1. 'VB DLL injector
  2. 'By Reckless Youth
  3.  
  4. 'All the shit it takes to make VB to inject dlls...
  5. Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  6. Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
  7. Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  8. Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal fAllocType As Long, FlProtect As Long) As Long
  9. Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
  10. Private Declare Function CreateRemoteThread Lib "kernel32" (ByVal ProcessHandle As Long, lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Any, ByVal lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
  11. Public ProsH As Long
  12.  
  13. 'The Injection Function
  14. Public Function InjectDll(DllPath As String, ProsH As Long)
  15. Dim DLLVirtLoc As Long, DllLength, Inject As Long, LibAddress As Long
  16. Dim CreateThread As Long, ThreadID As Long
  17.  
  18. 'STEP 1 -  The easy part...Putting the bitch in the process' memory
  19. Form1.Label1.Caption = "Injecting......"
  20. 'Find a nice spot for your DLL to chill using VirtualAllocEx
  21. DllLength = Len(DllPath)
  22. DLLVirtLoc = VirtualAllocEx(ProsH, ByVal 0, DllLength, &H1000, ByVal &H4)
  23. If DLLVirtLoc = 0 Then Form1.Label1.Caption = "VirtualAllocEx API failed!" & Form1.Timer1.Enabled = True: Exit Function
  24. 'Inject the Dll into that spot
  25. Inject = WriteProcessMemory(ProsH, DLLVirtLoc, ByVal DllPath, DllLength, vbNull)
  26. If Inject = 0 Then Form1.Label1.Caption = "Failed to Write DLL to Process!"
  27. Form1.Label1.Caption = "Dll Injected...Creating Thread....."
  28.  
  29.  
  30. 'STEP 2 - Loading it in the process
  31. 'This is where it gets a little interesting....
  32. 'Just throwing our Dll into the process isnt going to do shit unless you
  33. 'Load it into the precess address using LoadLibrary.  The LoadLibrary function
  34. 'maps the specified executable module into the address space of the
  35. 'calling process.  You call LoadLibrary by using CreateRemoteThread to
  36. 'create a thread(no shit) that runs in the address space of another process.
  37. 'First we find the LoadLibrary API function and store it
  38. LibAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA")
  39. If LibAddress = 0 Then Form1.Label1.Caption = "Can't find LoadLibrary API from kernel32.dll": Exit Function
  40. 'Next, the part the took me damn near 2 hours to figure out - using CreateRemoteThread
  41. 'We set a pointer to LoadLibrary(LibAddress) in our process, LoadLibrary then puts
  42. 'our Dll(DLLVirtLoc) into the process address.  Easy enough right?
  43. CreateThread = CreateRemoteThread(ProsH, vbNull, 0, LibAddress, DLLVirtLoc, 0, ThreadID)
  44. If CreateThread = 0 Then
  45. Form1.Label1.Caption = "Failed to Create Thead!"
  46. Form1.Timer1.Enabled = True
  47. Else
  48. Form1.Label1.Caption = "Dll Injection Successful!"
  49. End
  50. End If
  51. End Function


PM Quote
Avatar
gianluca (Normal User)
Pro


Messaggi: 103
Iscritto: 23/06/2008

Segnala al moderatore
Postato alle 13:43
Domenica, 21/06/2009
Ho fatto un po di passi avanti! anzi, direi un bel po! Mi sono bloccato sul createRemoteTread! Questa funzione dovrebbe ritornare il numero del tread ke ha creato, bene, mi torna 0!!! vuol dire ke non ha creato un bel niente difatti non mi esce la msgbox della dll!

Mi sa aiutare qualcuno??? Ho sbagliato a chamare l'api?

Vi ringrazio per l'ascolto! :D

Gianluca

PM Quote
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 13:51
Domenica, 21/06/2009
A me restituisce un risultato diverso da 0. Penso che il tuo errore dipenda dal fatto che la funzione LoadLibrary richieda un entry point nella dll, ossia una dllmain che contenga il codice da eseguire quando la dll viene caricata. Negli assembly .net non c'è modo di creare librerie con entry point.

PM Quote
Avatar
gianluca (Normal User)
Pro


Messaggi: 103
Iscritto: 23/06/2008

Segnala al moderatore
Postato alle 18:39
Lunedì, 22/06/2009
a me va a momenti in verita! l'ho notato ora, cmq la dll è fatta in c++ e con vb6 funziona tutto bene! xo dato ke l'applicazione devo farla in vb.net volevo fare anke l'injector... cmq anke nei momenti ke mi da CreateThread != 0 non mi esce la msgbox e attaccandomi con ollydbg al processo non la vedo iniettata!


Grazie intanto per l'interessamento!

Gianluca

PM Quote
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 9:32
Martedì, 23/06/2009
Ah, se la dll è in c++ allora non può essere l'entry point. Se usi Vista e la classe process per ottenere l'handle del processo è possibile che si verifichi un errore non segnalato (Win32Exception). Prova a controllare nella finestra di debug, lì dovrebbe venire fuori la scritta.

PM Quote
Avatar
gianluca (Normal User)
Pro


Messaggi: 103
Iscritto: 23/06/2008

Segnala al moderatore
Postato alle 15:21
Martedì, 23/06/2009
Se la finestra di debug che mi dici è quella che si chiama Output(View->Other Windows->Output) allora non mi dice niente... xo ho provato con l'api GetLastError() è mi da l'errore N°87... puo avere qualche utilita? cmq se fosse Win32Exception come potrei risolvere?

ps, uso vista e la classe process per ottenere l'handle!

Grazie

Gianluca

PM Quote