procedure hook(target, newfunc:pointer);
var
jmpto:dword;
OldProtect: Cardinal; // old protect in memory
begin
jmpto:=dword(newfunc)-dword(target)-5;
VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, @OldProtect);
pbyte(target)^:=$e9;
pdword(dword(target)+1)^:=jmpto;
end;
procedure myLdrLoadDll(PathToFile:PAnsiChar; Flags:variant; ModuleFileName:PAnsiChar; var ModuleHandle:THandle);
begin
MessageBox(0, 'I have blocked your attempt to inject a dll file!!', 'WARNING!', MB_OK);
ModuleHandle:=0;
end;
procedure Main;
begin
Hook(GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'), @myLdrLoadDll);
end;
begin
end.
This probably one of the methods that allows to avoid injection.
There is a mistake implementing of function myLdrLoadDll. There you have to check the name of the dll that is being injected, and if this dll is not on the list of "trusted" dlls then return a zero handle.
procedure myLdrLoadDll(PathToFile:PAnsiChar; Flags:variant; ModuleFileName:PAnsiChar; var ModuleHandle:THandle);
const
ALLOWED_MODULES : array [0..2] of string = ('kernel32.dll', 'user32.dll', 'ntdll.dll');
var
s : string;
found : boolean;
begin
s := LowerCase(ExtractFileName(String(PathToFile)));
found := false;
for i := 0 to length(ALLOWED_MODULES) do
begin
if s = ALLOWED_MODULES[i] then
begin
found := true;
break;
end;
end;
if not found then
begin
MessageBox(0, 'I have blocked your attempt to inject a dll file!!', 'WARNING!', MB_OK);
ModuleHandle:=0;
end;
end;
Also, to avoid another method of injection, you have to disable callback of function CreateRemoteThread, that is starting to run in RtlRemoteCall. I.e. get an address of the function RtlRemoteCall and write a byte $C3 to this address.
Pls wrote:Hello Enigmahad any progress with this plugin?
As I already said, this is impossible to avoid any dll injection. There are many methods that can inject dll, and avoid all of them - impossible.
It is better and more useful to analyze what injected dll is really doing and detect it by it's work. For example, if injected dll modifies some code inside the process, we can make a plugin for Enigma that will check if this memory and, for example, terminate processes if memory is modified.