DLL question

Just starting out? Need help? Post your questions and find answers here.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

DLL question

Post by Inf0Byt3 »

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)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

FreeLibrary_(Handle)?
thefool
Always Here
Always Here
Posts: 5881
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

@trond: i think he would like to uninject the dll from another process
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Yes, the dll has to uninject itself when i send a command to it...

[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)
thefool
Always Here
Always Here
Posts: 5881
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

i meant that you need to make the process it is injected to call the freelibrary command.

oh i meant that you mean this: the DLL should have an uninject procedure that it can call. So when its loaded into a process it should be able to remove itself again
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

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)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Inf0Byt3 wrote:The only code I found was this:
Which, sure enough, calls FreeLibrary_() with a handle.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

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:

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
Thanks!
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
thefool
Always Here
Always Here
Posts: 5881
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Trond wrote:
Inf0Byt3 wrote:The only code I found was this:
Which, sure enough, calls FreeLibrary_() with a handle.
yep
We did try that but we forgot the terminate thread :D
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

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)
Post Reply