Skip to content

Instantly share code, notes, and snippets.

@qwerty12
Created November 3, 2011 18:58
Show Gist options
  • Save qwerty12/1337443 to your computer and use it in GitHub Desktop.
Save qwerty12/1337443 to your computer and use it in GitHub Desktop.
Basic hook (thanks to Mhook)
/*
* SleepLock: Simplest DLL ever to lock when resuming from sleep only. Designed to be injected into processes that call SetSuspendState.
*
* I feel dirty for attaching a license to something as simple as this. Nevertheless, the BSD licence is pretty much a free pass.
*
* Copyright (C) 2011 Faheem Pervez <trippin1@gmail.com>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied.
*
*/
// SleepLock.cpp : Entry point for the DLL
#include "stdafx.h"
#include "mhook/mhook-lib/mhook.h"
//////////////////////////////////////////////////////////////////////////
// Defines and typedefs
typedef BOOLEAN (WINAPI *_SLSetSuspendState)(
__in BOOLEAN Hibernate,
__in BOOLEAN ForceCritical,
__in BOOLEAN DisableWakeEvent
);
//////////////////////////////////////////////////////////////////////////
// Original function
_SLSetSuspendState RealSetSuspendState = (_SLSetSuspendState) GetProcAddress(GetModuleHandle(TEXT("powrprof")), "SetSuspendState");
//////////////////////////////////////////////////////////////////////////
// Hooked function
BOOLEAN WINAPI HookedSetSuspendState(
__in BOOLEAN Hibernate,
__in BOOLEAN ForceCritical,
__in BOOLEAN DisableWakeEvent
)
{
if (!Hibernate) // Run if we're going into sleep mode
LockWorkStation();
// We're done; call the OS' proper SetSuspendState function
return RealSetSuspendState(Hibernate, ForceCritical, DisableWakeEvent);
}
//////////////////////////////////////////////////////////////////////////
// Entry point
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (RealSetSuspendState) //Check to see if GetProcAddress did actually give us a pointer to the original SetSuspendState
(void) Mhook_SetHook((PVOID*)&RealSetSuspendState, HookedSetSuspendState);
break;
case DLL_PROCESS_DETACH:
Mhook_Unhook((PVOID*)&RealSetSuspendState);
break;
default:
break;
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment