Delphi Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
델파이 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
FreePascal/Lazarus
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
델마당
볼랜드포럼 광고 모집

델파이 팁&트릭
Delphi Programming Tip&Tricks
[272] Shutdown 후킹
civilian [civilian] 6644 읽음    2010-01-26 13:06
library ShutdownHooks;

{$IMAGEBASE $42800000}

uses
  Windows,
  madCodeHook,
  sysutils;

// ***************************************************************

var
  ExitWindowsExNext             : function (flags, reserved: dword) : bool; stdcall;
  InitiateSystemShutdownWNext   : function (pc, msg: pwideChar; timeOut: dword; force, reboot: bool) : bool; stdcall;
  InitiateSystemShutdownExWNext : function (pc, msg: pwideChar; timeOut: dword; force, reboot, reason: bool) : bool; stdcall;

procedure log(s:string);
var f:textfile;
begin
  assignfile(f,'c:\hooking.log');
  try
    append(f);
  except
    rewrite(f);
  end;
  writeln(f,s);
  closefile(f);
end;

function IsShutdownAllowed(flags: dword) : boolean;
var b1 : boolean;
begin
  log('IsShutdownAllowed');
  b1 := false;
  if SendIpcMessage('ShutdownIpcQueue', @flags, 4, @b1, 1, 5000, false) and (not b1) then begin
    result := false;
    SetLastError(ERROR_ACCESS_DENIED);
  end else
    result := true;
end;

function ExitWindowsExCallback(flags, reserved: dword) : bool; stdcall;
begin
  log('ExitWindowsExCallback');
  result := IsShutdownAllowed(flags) and
            ExitWindowsExNext(flags, reserved);
end;

function GetShutdownFlags(force, reboot: boolean) : dword;
begin
  log('GetShutdownFlags');
  if reboot then
       result := EWX_REBOOT
  else result := EWX_SHUTDOWN;
  if force then
    result := result or EWX_FORCE;
end;

function InitiateSystemShutdownWCallback(pc, msg: pwideChar; timeOut: dword; force, reboot: bool) : bool; stdcall;
begin
  log('InitiateSystemShutdownWCallback');
  result := IsShutdownAllowed(GetShutdownFlags(force, reboot)) and
            InitiateSystemShutdownWNext(pc, msg, timeOut, force, reboot);
end;

function InitiateSystemShutdownExWCallback(pc, msg: pwideChar; timeOut: dword; force, reboot, reason: bool) : bool; stdcall;
begin
  log('InitiateSystemShutdownExWCallback');
  result := IsShutdownAllowed(GetShutdownFlags(force, reboot)) and
            InitiateSystemShutdownExWNext(pc, msg, timeOut, force, reboot, reason);
end;

// ***************************************************************

begin
  log('hook1 ok? '+booltostr(HookAPI(  user32,             'ExitWindowsEx',             @ExitWindowsExCallback,             @ExitWindowsExNext),true));
  log('hook2 ok? '+booltostr(HookAPI(advapi32,   'InitiateSystemShutdownW',   @InitiateSystemShutdownWCallback,   @InitiateSystemShutdownWNext),true));
  log('hook3 ok? '+booltostr(HookAPI(advapi32, 'InitiateSystemShutdownExW', @InitiateSystemShutdownExWCallback, @InitiateSystemShutdownExWNext),true));
end.
---------end lib

--------------program:
program prog;

uses windows, madcodehook, sysutils;

var MayShutdown:boolean=false;

procedure log(s:string);
var f:textfile;
begin
  assignfile(f,'c:\hooking.log');
  try
    append(f);
  except
    rewrite(f);
  end;
  writeln(f,s);
  closefile(f);
end;

procedure ShutdownIpcQueue(name       : pchar;
                           messageBuf : pointer;
                           messageLen : dword;
                           answerBuf  : pointer;
                           answerLen  : dword); stdcall;
var s1 : string;
begin
  boolean(answerBuf^) := MayShutdown;
  if not MayShutdown then begin
    if      dword(messageBuf^) and EWX_LOGOFF <> 0 then s1 := 'You''re not allowed to log off.'
    else if dword(messageBuf^) and EWX_REBOOT <> 0 then s1 := 'You''re not allowed to restart Windows.'
    else                                                s1 := 'You''re not allowed to shutdown Windows.';
    log('ShutdownIpcQueue: '+s1);
  end;
end;

begin
  CreateIpcQueue('ShutdownIpcQueue', ShutdownIpcQueue);
  log('inject ok? ' + booltostr(InjectLibrary(ALL_SESSIONS or SYSTEM_PROCESSES, 'ShutdownHooks.dll'),true));
end.
하두고 [hadugo]   2012-11-29 13:15 X
이번에도 civilian님의 도움을 받아야 할것 같습니다.
madCodeHook이라는 라이브러리가 상용이라 전 사용할 수 없네요.
방법이 없을까요?

+ -

관련 글 리스트
272 Shutdown 후킹 civilian 6644 2010/01/26
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.