DLL question
DLL question
I have a DLL that is injected in a program and I'd like to make it unload itself when i send some data to it on the network. However, I have no idea how to make it uninject itself... Anybody knows how to do this?
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Yes, the dll has to uninject itself when i send a command to it...
[edit]
The only code I found was this:
[edit]
The only code I found was this:
Code: Select all
//Prototype
void __declspec(noreturn) UninjectSelf(HMODULE);
//Function
void __declspec(naked) __declspec(noreturn) UninjectSelf(HMODULE Module)
{
__asm
{
push -2
push 0
push Module
mov eax, TerminateThread
push eax
mov eax, FreeLibrary
jmp eax
}
}
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Well it works like this (pseudocode)
Code: Select all
;The main code:
InjectToHost("ProgramName")
;The Dll
Repeat
Do stuff here
Until ReceivedQuitSignal()
;Remove hooks and clean all stuff
UninjectSelf()
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
I have no idea why, but when I see assembler code, I inhibate... It works now, you were right, that are simple calls... I wonder why did they put them is ASM?
Here's what i've done:
Thanks!
Here's what i've done:
Code: Select all
Global Mod.l
Declare Bla(a.l)
ProcedureDLL AttachProcess(Hmodule.l)
Mod = Hmodule
OpenConsole()
CreateThread(@bla(),0)
EndProcedure
ProcedureDLL DetachProcess(Hmodule.l)
;This never gets executed :/
MessageRequester("Exiting","Haha")
EndProcedure
Procedure bla(a.l)
For x = 1 To 5
PrintN("Running!")
Delay(1000)
Next
CloseConsole()
TerminateThread_(GetCurrentThread_(),0)
FreeLibrary_(Mod)
EndProcedure
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Well that works but does not release the dll... But as allways, I found the cure hehe. Micro$oft is not that stupid afterall, they made my life easyer: Kernel32.dll exports "FreeLibraryAndExitThread" so I just called that and it works. Here is the final dll:
Code: Select all
Global Mod.l
Global mythread.l
Declare Bla(a.l)
ProcedureDLL AttachProcess(Hmodule.l)
Mod = Hmodule
OpenConsole()
mythread = CreateThread(@bla(),0)
EndProcedure
ProcedureDLL DetachProcess(Hmodule.l)
MessageRequester("Exiting","Haha")
EndProcedure
Procedure bla(a.l)
For t = 1 To 5
PrintN("Running!")
Delay(1000)
Next
FreeLibraryAndExitThread_(Mod,0)
EndProcedure
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)