clsHookInfo.clsVERSION 1.0 CLASSBEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObjectENDAttribute VB_Name = "clsHookInfo"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = TrueAttribute VB_PredeclaredId = FalseAttribute VB_Exposed = FalsePrivate Declare Function MessageBoxA Lib "user32" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As LongPrivate Declare Function MessageBoxW Lib "user32" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As LongPrivate Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As LongPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPrivate Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As LongPrivate Declare Function GetCurrentProcessId Lib "kernel32" () As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Const PROCESS_QUERY_INFORMATION As Long = (&H400)Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000Private Const SYNCHRONIZE As Long = &H100000Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)Private mbytOldCode(5) As BytePrivate mbytNewCode(5) As BytePrivate mlngFunAddr As LongPrivate mhProcess As LongPublic Function HookApi(ByVal strDllName As String, ByVal strFunName As String, ByVal lngFunAddr As Long, ByVal hProcess As Long) As Boolean Dim hModule As Long, dwJmpAddr As Long mhProcess = hProcess hModule = LoadLibrary(strDllName) If hModule = 0 Then HookApi = False Exit Function End If mlngFunAddr = GetProcAddress(hModule, strFunName) If mlngFunAddr = 0 Then HookApi = False Exit Function End If CopyMemory mbytOldCode(0), ByVal mlngFunAddr, 6 Debug.Print mbytOldCode(0); mbytOldCode(1); mbytOldCode(2); mbytOldCode(3); mbytOldCode(4) mbytNewCode(0) = &HE9 dwJmpAddr = lngFunAddr - mlngFunAddr - 5 CopyMemory mbytNewCode(1), dwJmpAddr, 4 Debug.Print mbytNewCode(0); mbytNewCode(1); mbytNewCode(2); mbytNewCode(3); mbytNewCode(4) HookStatus True HookApi = TrueEnd FunctionPublic Function HookStatus(ByVal blnIsHook As Boolean) As Boolean If blnIsHook Then If WriteProcessMemory(mhProcess, ByVal mlngFunAddr, mbytNewCode(0), 5, 0) <> 0 Then HookStatus = False '拦截 Else If WriteProcessMemory(mhProcess, ByVal mlngFunAddr, mbytOldCode(0), 5, 0) <> 0 Then HookStatus = False '恢复 End IfEnd FunctionPrivate Sub Class_Initialize()' mhProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, GetCurrentProcessId)End SubPrivate Sub Class_Terminate() HookStatus False' CloseHandle mhProcessEnd SubfrmMain.frmVERSION 5.00Begin VB.Form frmMain BorderStyle = 1 'Fixed Single Caption = "创建系统进程" ClientHeight = 3090 ClientLeft = 45 ClientTop = 435 ClientWidth = 4680 LinkTopic = "Form1" MaxButton = 0 'False MinButton = 0 'False ScaleHeight = 3090 ScaleWidth = 4680 StartUpPosition = 3 '窗口缺省 Begin VB.CommandButton cmdExit Caption = "退出" Default = -1 'True Height = 375 Left = 3510 TabIndex = 3 Top = 2010 Width = 945 End Begin VB.CommandButton cmdRun Caption = "启动" Height = 375 Left = 2190 TabIndex = 2 Top = 2010 Width = 945 End Begin VB.TextBox txtPath Height = 255 Left = 960 TabIndex = 1 Text = "notepad" Top = 1020 Width = 3525 End Begin VB.Label lblNote AutoSize = -1 'True Caption = "文件路径:" Height = 180 Left = 90 TabIndex = 0 Top = 1050 Width = 810 EndEndAttribute VB_Name = "frmMain"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalsePrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function GetCurrentProcessId Lib "kernel32" () As LongPrivate Const PROCESS_QUERY_INFORMATION As Long = (&H400)Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000Private Const SYNCHRONIZE As Long = &H100000Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadId As LongEnd TypePrivate Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Integer cbReserved2 As Integer lpReserved2 As Byte hStdInput As Long hStdOutput As Long hStdError As LongEnd TypePrivate Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As LongPrivate Sub cmdExit_Click() Unload MeEnd SubPrivate Sub cmdRun_Click() Dim lp As PROCESS_INFORMATION Dim si As STARTUPINFO si.cb = Len(si) CreateProcess vbNullString, txtPath.Text, ByVal 0&, ByVal 0&, 0, 0, ByVal 0&, vbNullString, si, lpEnd SubPrivate Sub Form_Load() EnablePrivilege '注意这里不能马上把句柄关闭掉 glngSystemHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, GetSystemProcessId) If glngSystemHandle = 0 Then MsgBox "获取系统进程句柄出错!!", vbCritical, "错误" Exit Sub End If Set gclsHookNtCreateProcess = New clsHookInfo Set gclsHookNtCreateProcessEx = New clsHookInfo glngProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, GetCurrentProcessId) gclsHookNtCreateProcessEx.HookApi "ntdll.dll", "NtCreateProcessEx", GetFunAddr(AddressOf NtCreateProcessExCallback), glngProcess gclsHookNtCreateProcess.HookApi "ntdll.dll", "NtCreateProcess", GetFunAddr(AddressOf NtCreateProcessCallback), glngProcessEnd SubPrivate Sub Form_Unload(Cancel As Integer) Set gclsHookNtCreateProcess = Nothing Set gclsHookNtCreateProcessEx = Nothing CloseHandle glngSystemHandle CloseHandle glngProcessEnd SubmodEnablePrivilege.basAttribute VB_Name = "modEnablePrivilege"Option ExplicitPrivate Const STANDARD_RIGHTS_REQUIRED = &HF0000Private Const TOKEN_ASSIGN_PRIMARY = &H1Private Const TOKEN_DUPLICATE = (&H2)Private Const TOKEN_IMPERSONATE = (&H4)Private Const TOKEN_QUERY = (&H8)Private Const TOKEN_QUERY_SOURCE = (&H10)Private Const TOKEN_ADJUST_PRIVILEGES = (&H20)Private Const TOKEN_ADJUST_GROUPS = (&H40)Private Const TOKEN_ALL_ACCESS = 983551Private Const SE_PRIVILEGE_ENABLED = &H2Private Const ANYSIZE_ARRAY = 1Private Const SE_DEBUG_NAME = "SeDebugPrivilege"Private Type LUID lowpart As Long highpart As LongEnd TypePrivate Type LUID_AND_ATTRIBUTES pLuid As LUID Attributes As LongEnd TypePrivate Type TOKEN_PRIVILEGES PrivilegeCount As Long Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTESEnd TypePrivate Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongPrivate Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPriv As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long 'Used to adjust your program's security privileges, can't restore without it!Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, ByVal lpName As String, lpLuid As LUID) As LongPrivate Declare Function GetCurrentProcess Lib "kernel32" () As Long '获取当前进程句柄 Public Function EnablePrivilege() As Boolean Dim hdlProcessHandle As Long Dim hdlTokenHandle As Long Dim tmpLuid As LUID Dim tkp As TOKEN_PRIVILEGES Dim tkpNewButIgnored As TOKEN_PRIVILEGES Dim lBufferNeeded As Long Dim lp As Long hdlProcessHandle = GetCurrentProcess() lp = OpenProcessToken(hdlProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hdlTokenHandle) lp = LookupPrivilegeValue(vbNullString, "SeDebugPrivilege", tmpLuid) tkp.PrivilegeCount = 1 tkp.Privileges(0).pLuid = tmpLuid tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED EnablePrivilege = AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)End Function modHook.basAttribute VB_Name = "modHook"Private Declare Function NtCreateProcessEx Lib "NTDLL.DLL" (ByRef ProcessHandle As Long, ByVal AccessMask As Long, ByVal ObjectAttributes As Long, ByVal hParentProcess As Long, ByVal InheritHandles As Long, ByVal hSection As Long, ByVal hDebugPort As Long, ByVal hExceptionPort As Long, ByVal reserv As Long) As LongPrivate Declare Function NtCreateProcess Lib "NTDLL.DLL" (ByRef ProcessHandle As Long, ByVal AccessMask As Long, ByVal ObjectAttributes As Long, ByVal hParentProcess As Long, ByVal InheritHandles As Long, ByVal hSection As Long, ByVal hDebugPort As Long, ByVal hExceptionPort As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function GetCurrentProcessId Lib "kernel32" () As LongPrivate Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As LongPrivate Const PROCESS_QUERY_INFORMATION As Long = (&H400)Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000Private Const SYNCHRONIZE As Long = &H100000Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)Private Type OBJECT_ATTRIBUTES Length As Long RootDirectory As Long ObjectName As Long Attributes As Long SecurityDescriptor As Long SecurityQualityOfService As LongEnd TypePublic gclsHookNtCreateProcessEx As clsHookInfoPublic gclsHookNtCreateProcess As clsHookInfoPublic glngProcess As LongPublic glngSystemHandle As LongPublic Function NtCreateProcessExCallback(ByRef ProcessHandle As Long, ByVal AccessMask As Long, ByVal ObjectAttributes As Long, ByVal hParentProcess As Long, ByVal InheritHandles As Long, ByVal hSection As Long, ByVal hDebugPort As Long, ByVal hExceptionPort As Long, ByVal reserv As Long) As Long Dim hReturn As Long gclsHookNtCreateProcessEx.HookStatus False hReturn = NtCreateProcessEx(ProcessHandle, AccessMask, ObjectAttributes, glngSystemHandle, InheritHandles, hSection, hDebugPort, hExceptionPort, reserv) gclsHookNtCreateProcessEx.HookStatus True NtCreateProcessExCallback = hReturnEnd FunctionPublic Function NtCreateProcessCallback(ByRef ProcessHandle As Long, ByVal AccessMask As Long, ByVal ObjectAttributes As Long, ByVal hParentProcess As Long, ByVal InheritHandles As Long, ByVal hSection As Long, ByVal hDebugPort As Long, ByVal hExceptionPort As Long) As Long Dim hReturn As Long gclsHookNtCreateProcess.HookStatus False hReturn = NtCreateProcess(ProcessHandle, AccessMask, ObjectAttributes, glngSystemHandle, InheritHandles, hSection, hDebugPort, hExceptionPort) gclsHookNtCreateProcess.HookStatus True NtCreateProcessCallback = hReturnEnd FunctionPublic Function GetFunAddr(lngFunAddr As Long) As Long GetFunAddr = lngFunAddrEnd FunctionmodProcess.basAttribute VB_Name = "modProcess"Option ExplicitPrivate Declare Function CloseHandle Lib "kernel32.dll" (ByVal Handle As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As LongPrivate Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As LongPrivate Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As LongPrivate Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As LongPrivate Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As LongPrivate Declare Function GetCurrentProcess Lib "kernel32" () As Long '获取当前进程句柄Private Declare Function GetLastError Lib "kernel32" () As LongPrivate Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpBaseName As String, ByVal nSize As Long) As LongPrivate Const PROCESS_QUERY_INFORMATION = &H400Private Const PROCESS_VM_READ = &H10'Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000Private Const SYNCHRONIZE As Long = &H100000Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)Public Function GetSystemProcessId() As Long Dim lngCbNeeded As Long Dim lngNumElements As Long Dim lngProcessIDArray() As Long Dim lngCbNeeded2 As Long Dim lngNumElements2 As Long Dim Modules(0 To 1023) As Long Dim lngRet As Long Dim lngSize As Long Dim hProcess As Long Dim i As Long, strModuleName As String Dim lngModules As Long, hLen As Long ReDim lngProcessIDArray(1024) lngRet = EnumProcesses(lngProcessIDArray(0), 4 * 1024, lngCbNeeded) lngNumElements = lngCbNeeded / 4 ReDim Preserve lngProcessIDArray(lngNumElements - 1) On Error Resume Next For i = 0 To lngNumElements - 1 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, False, lngProcessIDArray(i)) If hProcess <> 0 And lngProcessIDArray(i) <> 4 Then lngRet = EnumProcessModules(hProcess, Modules(0), 1024, lngCbNeeded2) If lngRet <> 0 Then strModuleName = String(260, "*") lngRet = GetModuleFileNameExA(hProcess, Modules(0), strModuleName, 260) strModuleName = Left(strModuleName, lngRet) End If If InStr(LCase(strModuleName), "system32/smss.exe") Then 'If InStr(LCase(strModuleName), "system32/winlogon.exe") Then GetSystemProcessId = lngProcessIDArray(i) lngRet = CloseHandle(hProcess) Exit Function End If End If lngRet = CloseHandle(hProcess) NextEnd Function