Author Topic: (Snippit) How To Make a Dll Unload Its Self  (Read 3558 times)

0 Members and 1 Guest are viewing this topic.

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
(Snippit) How To Make a Dll Unload Its Self
« on: October 03, 2012, 12:21:19 am »
i was looking for code to make a dll unload itself, and foud some broken code
problem was the coder was pushing a handle to the exe not the dll

here is the fixed code 

Code: (c) [Select]
void UnloadSelf(HMODULE hdl)
{
   LPVOID FP_ExitThread = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "ExitThread");
   __asm
   {
      push hdl
      push FP_ExitThread
      jmp dword ptr [FreeLibrary]
   }
}

to get the handle to the loaded dll you can just grab it from main

example:
Code: (c) [Select]
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            MessageBox(0,"hello","world",0);
            UnloadSelf(hinstDLL);
            break;
        case DLL_PROCESS_DETACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
            break;
    }
    return TRUE;
}

beats injecting code to call FreeLibrary
« Last Edit: October 03, 2012, 12:23:40 am by bubzuru »
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: (Snippit) How To Make a Dll Unload Its Self
« Reply #1 on: October 03, 2012, 07:22:42 am »
Why can't the WinAPI be more simple..
Satan911
Evilzone Network Administrator

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: (Snippit) How To Make a Dll Unload Its Self
« Reply #2 on: October 03, 2012, 07:39:09 pm »
Because it's WinAPI?  Have a look at intermixing MFC with custom WinAPI class wrappers for use w/ DirectX.  It's atrocious.  Oh, how simple & elegant is the world of FOSS.
« Last Edit: October 03, 2012, 07:39:30 pm by Xires »
-Xires

Offline TETYYS

  • /dev/null
  • *
  • Posts: 16
  • Cookies: -12
    • View Profile
Re: (Snippit) How To Make a Dll Unload Its Self
« Reply #3 on: April 26, 2013, 08:33:11 pm »
Sorry for reviving thread, but this method causes application to crash, or unlass FileZilla to crash. I used this and it worked.
Code: (C++) [Select]
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)FreeLibrary, &__ImageBase, 0, NULL);
« Last Edit: April 26, 2013, 08:33:39 pm by TETYYS »

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: (Snippit) How To Make a Dll Unload Its Self
« Reply #4 on: April 28, 2013, 07:06:57 pm »
No need to feel sorry when adding important/useful information.
-Xires