library injComFunctions;
uses
windows, uallHook, SysUtils;
const WM_COPYDATA = $004A;
type Tmydata =
packed record
datacount: integer;
ind: boolean;
end;
var
nextCreateFile, oldCreateFile :
function(lpFileName: PChar; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle;
stdcall;
nextCreateFileA, oldCreateFileA :
function(lpFileName: PAnsiChar; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle;
stdcall;
nextCreateFileW, oldCreateFileW :
function(lpFileName: PWideChar; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle;
stdcall;
myData : Tmydata;
CDS : TCopyDataStruct;
winh : integer;
procedure sendapp(len: integer; indata: boolean);
begin
MessageBoxA(0,PChar('
Function sendapp: ' + IntToStr(len)),'
Msg',0);
mydata.datacount := len;
mydata.ind := indata;
SendMessageA(winh,WM_COPYDATA,0,cardinal(@CDS));
end;
function myCreateFile(lpFileName: PChar; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle;
stdcall;
begin
sendapp(11, true);
Result := nextCreateFile(lpFileName, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
hTemplateFile);
end;
function myCreateFileA(lpFileName: PAnsiChar; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle;
stdcall;
begin
sendapp(22, true);
Result := nextCreateFileA(lpFileName, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
hTemplateFile);
end;
function myCreateFileW(lpFileName: PWideChar; dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle;
stdcall;
begin
sendapp(33, true);
Result := nextCreateFileW(lpFileName, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
hTemplateFile);
end;
procedure InjectMain;
var kernelHandle : Integer;
begin
@oldCreateFile :=
nil;
@oldCreateFileA :=
nil;
@oldCreateFileW :=
nil;
CDS.dwData := 0;
CDS.cbData := sizeof(TMyData);
CDS.lpData := @mydata;
winh := FindWindowA(
nil,'
CondorComTest');
MessageBoxA(0,PChar('
Handle winh: ' + IntToStr(winh)),'
Msg',0);
if winh <> 0
then sendapp(10, true);
kernelHandle := GetModuleHandle('
kernel32.dll');
if kernelHandle > 0
then begin
@oldCreateFile := GetProcAddress(kernelHandle,'
CreateFile');
if @oldCreateFile <>
nil then HookCode(@oldCreateFile, @myCreateFile, @nextCreateFile);
@oldCreateFileA := GetProcAddress(kernelHandle,'
CreateFileA');
if @oldCreateFileA <>
nil then HookCode(@oldCreateFileA, @myCreateFileA, @nextCreateFileA);
@oldCreateFileW := GetProcAddress(kernelHandle,'
CreateFileW');
if @oldCreateFileW <>
nil then HookCode(@oldCreateFileW, @myCreateFileW, @nextCreateFileW);
end;
end;
procedure UnInjectMain;
begin
if @oldCreateFile <>
nil then UnhookCode(@nextCreateFile);
if @oldCreateFileA <>
nil then UnhookCode(@nextCreateFileA);
if @oldCreateFileW <>
nil then UnhookCode(@nextCreateFileW);
end;
procedure DllMain(dwReason: DWord);
begin
case dwReason
of
DLL_PROCESS_ATTACH:
begin
InjectMain;
MessageBoxA(0,PChar('
Loaded :'+Paramstr(0)),'
Msg',0);
end;
DLL_PROCESS_DETACH:
begin
UnInjectMain;
//MessageBoxA(0,PChar('Unloaded :'+Paramstr(0)),'Msg',0);
end;
end;
end;
begin
DllProc := @DllMain;
DllMain(DLL_PROCESS_ATTACH);
end.