記錄外殼的活動
記錄外殼活動有很多好處
一個實現了IShellExecuteHook接口的COM對象可以截獲所有對ShellExecute和ShellExecuteEx函數的調用
(
(
(
(
Windows外殼大量調用ShellExecute和ShellExecuteEx函數來執行幾乎是所有的資源管理器的操作
編寫外殼活動記錄器
首先需要創建一個進程內COM對象
IShellExecuteHook的接口定義在shlobj
unit ShellExecuteHookObj;
interface
uses
Windows
type
TTShellExecuteHook = class (TComObject
protected
function Execute(var ShellExecuteInfo: TShellExecuteInfo): HResult; stdcall;
end;
const
Class_TShellExecuteHook: TGUID =
下面就是用來截獲並記錄外殼操作的實現部分
function TTShellExecuteHook
var ShellExecuteInfo: TShellExecuteInfo): HResult;
Execute方法會從外殼獲得一個類型為TshellExecuteInfo的參數
_SHELLEXECUTEINFOA = record
cbSize: DWORD;
fMask: ULONG;
Wnd: HWND;
lpVerb: PAnsiChar;
lpFile: PAnsiChar;
lpParameters: PAnsiChar;
lpDirectory: PAnsiChar;
nShow: Integer;
hInstApp: HINST;
{ Optional fields }
lpIDList: Pointer;
lpClass: PAnsiChar;
hkeyClass: HKEY;
dwHotKey: DWORD;
hIcon: THandle;
hProcess: THandle;
end;
這個記錄結構中的lpFile包含了要運行的文件名
TShellExecuteInfo結構中還記錄了要運行程序的很多信息
如果Execute的返回值為S_FALSE
function TTShellExecuteHook
var ShellExecuteInfo: TShellExecuteInfo): HResult;
var
FileName: String;
begin
Result := S_FALSE;
with ShellExecuteInfo do
begin
FileName := UpperCase(ExtractFileName(lpFile));
if Pos(
begin
Result := S_OK;
hInstApp :=
MessageBox(Wnd
end;
end;
end;
進一步
有一點要注意的是
對於外殼動作記錄器來說
function TTShellExecuteHook
var ShellExecuteInfo: TShellExecuteInfo): HResult;
var
FileStream: TFileStream;
a:TStringList;
S:string;
begin
Result := S_FALSE;
with ShellExecuteInfo do
begin
FileStream:=TFileStream
S:=string(lpVerb)+
FileStream
FileStream
FileStream
end;
end;
注冊ShellExecuteHook
要想使COM對象被外殼加載
HKEY_LOCAL_MACHINE
SOFTWARE
Microsoft
Windows
CurrentVersion
Explorer
ShellExecuteHooks
{CLSID}=
修改注冊表可以通過重載COM的類工廠的UpdateRegistry方法來實現
implementation
uses ComServ
resourcestring
sCreateRegKeyError =
type
TShellExComObjectFactory = class(TComObjectFactory)
public
procedure UpdateRegistry(Register: Boolean); override;
end;
{ TShellExComObjectFactory }
procedure TShellExComObjectFactory
const
hellExecuteHooksKey=
SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks
var
Handle: HKey;
Status
ClassID: String;
begin
ClassID := GUIDToString(Class_TShellExecuteHook);
if Register then
begin
Status := RegCreateKeyEx(HKEY_LOCAL_MACHINE
ShellExecuteHooksKey)
KEY_READ or KEY_WRITE
if Status =
begin
Status := RegSetValueEx(Handle
PChar(Description)
RegCloseKey(Handle);
end;
end else
begin
Status := RegOpenKeyEx(HKEY_LOCAL_MACHINE
KEY_READ or KEY_WRITE
if Status =
begin
Status := RegDeleteValue(Handle
RegCloseKey(Handle);
end;
end;
if Status <>
inherited UpdateRegistry(Register);
end;
initialization
TShellExComObjectFactory
ComServer
end
如果系統中有多個ShellExecuteHook的話
記住ShellExecuteHook並不是一個完善的用於監視系統運行的擴展
ShellExecute(nil
這說明外殼並不使用ShellExecute函數顯示屬性對話框
From:http://tw.wingwit.com/Article/program/Delphi/201311/24787.html