Prevent your DLL from being unloaded

30 06 2008

Here’s the scenario, you’ve just found a neat little way to inject your dll into another process but for one reason or another your dll is being unloaded instantly right after DllMain() is called. Damn, you think! It must be checking the dll somehow and calling FreeLibrary() if it doesn’t match specific requirements, how do I bypass this?

I had no idea either until I read this article.

Essentially what you do is call LoadLibrary() again from inside your DllMain() function so the count for your dll is incremented, the host app calls FreeLibrary() and you stay resident (every one is happy).

Here’s the source for you lazy types….


BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
LockLibraryIntoProcessMem(hModule, &g_Self);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
int LockLibraryIntoProcessMem(HMODULE DllHandle, HMODULE *LocalDllHandle)
{
if(NULL == LocalDllHandle)
return ERROR_INVALID_PARAMETER;
*LocalDllHandle = NULL;
TCHAR moduleName[1024];
if(0 == GetModuleFileName(DllHandle, moduleName, sizeof(moduleName)/ sizeof(TCHAR)))
return GetLastError();
*LocalDllHandle = LoadLibrary(moduleName);
if(NULL == *LocalDllHandle)
return GetLastError();
return NO_ERROR;
}

Also I apologise for the shitbox code paste, the <code> tags on wordpress seem to go crazy whenever you insert a newline.





Does anyone else?

29 06 2008

Have drunken coding sessions? I know we all have caffeine induced ones but does anyone else apart from myself do this? Well then if you never have then I present:

“Xavier’s Guide to drunken coding”

Step 1:

Acquire your favourite alcoholic beverage. My personal favourite is beer but you can use vodka or any other spirit if you so desire.

Step 2:

Start drinking and remove any distracting media from your computer and lock it away somewhere (hold on to your music, you need something to drunkenly sing to) and make sure your mobile phone is turned off.

Step 3:

Once you’ve read something interesting enough to warrant your interest then fire up your compiler/ide and start coding (By this point you should have had about 2 or so beers).

Step 4:

This is probably the hardest part. You need to reach the level of drunkeness where you have tons of energy but can still say focused (it is very easy to fall past this), and you need to maintain this level for as long as possible.

Helpful tips:

  • You should know what your limits are, try pre-planning how many drinks you need to reach the level of drunkeness you need.
  • Resist the urge to “Get on a roll” or have a session, the goal is to do some coding damn it! (I found only buying a certain amount of alcohol fixes this problem)
  • Depending on who your friends are a drunken coding group can be a rewarding experience, or it can completely sabotage what you set out to do.
  • Make sure you have enough cash on you to order pizza, who can be fucked cooking when you’ve got coding to do right?




How to be a british sex bomb…

29 06 2008

Please make more ;)