library ProtectMe;
uses
SysUtils,
windows,
PsApi;
function GetSystemPath():String;stdcall;
var
SysDir:array[0..255] of char;
begin
GetSystemDirectory(@SysDir,255);
GetSystemPath:=String(SysDir);
end;
procedure ProtectProcess();
const
n = 512;
var
pHandle:Cardinal;
IDArr: array[0..n-1] of DWORD;
size,i: DWORD;
buf: array[0..MAX_PATH] of Char;
isLive:Boolean;
label BeginScan;
begin
BeginScan:FillChar(buf, n, #0); {这样可避免乱码}
EnumProcesses(@IDArr, n, size);
for i := 0 to size div SizeOf(DWORD) - 1 do
begin
pHandle := OpenProcess(PROCESS_ALL_ACCESS, False, IDArr[i]);
GetModuleFileNameEx(pHandle, 0, buf, Length(buf)*SizeOf(buf[0]));
CloseHandle(pHandle);
if SameText(String(buf),String('C:\ProtectMe.exe'))=True then
begin
isLive:=True;
Break;
end
else
begin
isLive:=False;
end;
end;
if isLive=False then Begin
WinExec(PChar('C:\ProtectMe.exe'),5);
end;
Sleep(100);
Goto BeginScan;
end;
procedure DllMain(Reason: Integer);
var
hThread:Cardinal;
hThreadID:Cardinal;
begin
if reason=DLL_PROCESS_ATTACH then begin
hThread:= CreateThread(nil,0,@ProtectProcess, nil, 0, hThreadID);
end;
if reason=DLL_PROCESS_DETACH then begin
TerminateThread(hThread,0);
CloseHandle (hThread);
end;
end;
exports
DllMain;
begin
DLLProc := @DllMain;
DllMain(DLL_PROCESS_ATTACH);
end.
EXE
MOD:
Option Explicit
Public Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, _
ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, _
ByVal flProtect As Long) As Long
Public Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, _
ByVal lpAddress As Long, ByVal dwSize As Long, _
ByVal dwFreeType As Long) As Long
Public Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, _
ByVal lpProcName As String) As Long
Public Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" _
(ByVal lpModuleName As String) As Long
Public Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib _
"kernel32" (ByVal hObject As Long) As Long
Public Declare Function WriteProcessMemory Lib "kernel32" _
(ByVal hProcess As Long, ByVal lpBaseAddress As Long, lpBuffer As Any, _
ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Public Declare Function CreateRemoteThread Lib "kernel32" _
(ByVal hProcess As Long, ByVal lpThreadAttributes As Long, _
ByVal dwStackSize As Long, ByVal lpStartAddress As Long, _
ByVal lpParameter As Long, ByVal dwCreationFlags As Long, _
lpThreadId As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function GetExitCodeThread Lib "kernel32" _
(ByVal hThread As Long, lpExitCode As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib _
"kernel32" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function Process32First Lib "kernel32" _
(ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Public Declare Function RtlAdjustPrivilege Lib _
"ntdll.dll" (ByVal Privilege As Long, _
ByVal Enable As Boolean, ByVal Client As Boolean, _
WasEnabled As Long) As Long
Public Const MEM_COMMIT = 4096
Public Const PAGE_READWRITE = 4
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 260
End Type
Public Function Inject(ByVal ProcessHandle As String, ByVal MyDllFileName As String) As Boolean
Dim MyDllFileLength As Long 'dll文件名长度
Dim MyDllFileBuffer As Long '写入dll文件名的内存地址
Dim MyReturn As Long
Dim MyResult As Long
Dim MyAddr As Long '执行远程线程代码的起始地址。这里等于LoadLibraryA的地址
'好了,现在用CreateRemoteThread在目标进程创建一个线程,线程起始地址指向LoadLibraryA, _
参数就是MyDllFileBuffer中保存的dll路径?
If MyResult = 0 Then
Inject = False
Else
Inject = True
End If
'接下来你可以使用WaitForSingleObject等待线程执行完毕。 _
并用GetExitCodeThread得到线程的退出代码,用来判断时候正确执行了dll中的代码。
CloseHandle MyResult
CloseHandle ProcessHandle
'扫地工作
End Function
Public Function GetProcessID(ByVal sProcess As String) As Long
Dim i As Long
Dim Proc As PROCESSENTRY32
Dim snap As Long
Dim theloop As Long
snap = CreateToolhelp32Snapshot(&H2&, 0)
Proc.dwSize = Len(Proc)
theloop = Process32First(snap, Proc)
i = 0
While theloop <> 0
If LCase$(sProcess) = LCase$(Left(Proc.szExeFile, InStr(1, Proc.szExeFile, Chr(0)) - 1)) Then
GetProcessID = Proc.th32ProcessID
End If
DoEvents
i = i + 1
theloop = Process32Next(snap, Proc)
Wend
CloseHandle snap
End Function
FORM
Option Explicit
Private Sub Command1_Click()
Call Inject(OpenProcess(&H1F0FFF, False, GetProcessID("Explorer.exe")), App.Path & "\ProtectMe.dll")
End Sub
---------------------------------------------------