首发:变态VB之HookAPI(陈辉作品)
(2012-08-01 20:00:52)
标签:
it |
分类: VB编程笔记 |
(声明:魏滔序原创,转贴请注明出处。)
HookAPI核心部分
项目文件:HookInfo.vbp
Type=Exe
Module=modHookMain; modHookMain.bas
Reference=*/G{00020430-0000-0000-C000-000000000046}#2.0#0#../../../../WINDOWS/system32/STDOLE2.TLB#OLE
Automation
Form=frmMain.frm
Module=modHookInfo; modHookInfo.bas
Startup="Sub Main"
HelpFile=""
Title="HookInfo"
ExeName32="HookInfo.exe"
Command32=""
Name="HookInfo"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="Chenhui530"
CompilationType=-1
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
[MS Transaction Server]
AutoRefresh=1
窗体文件:frmMain.frm (本窗体无任何操作,为了进程的生命期而存在。)
VERSION 5.00
Begin VB.Form frmMain
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
模块文件:modHookInfo.bas
Attribute VB_Name = "modHookInfo"
Private Declare Function MessageBoxA Lib "user32" (ByVal hwnd
As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal
wType As Long) As Long
Private Declare Function MessageBoxW Lib "user32" (ByVal hwnd
As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal
wType As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32"
(ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any,
ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private 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 Long
Private Declare Function LoadLibrary Lib "kernel32" Alias
"LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal
hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" ()
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal
hObject As Long) As Long
Private Const PROCESS_QUERY_INFORMATION As Long =
(&H400)
Private Const STANDARD_RIGHTS_REQUIRED As Long =
&HF0000
Private Const SYNCHRONIZE As Long =
&H100000
Private Const PROCESS_ALL_ACCESS As Long =
(STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or
&HFFF)
Private mbytOldCode(5) As Byte
Private mbytNewCode(5) As Byte
Private mlngFunAddr As Long
Private mhProcess As Long
Public Function HookApi(ByVal strDllName As String, ByVal
strFunName As String, ByVal lngFunAddr As Long, ByVal hProcess As
Long) As Boolean
End Function
Public Function HookStatus(ByVal blnIsHook As Boolean) As
Boolean
End Function
Private Sub Class_Initialize()
' mhProcess =
OpenProcess(PROCESS_ALL_ACCESS, 0, GetCurrentProcessId)
End Sub
Private Sub Class_Terminate()
' CloseHandle
mhProcess
End Sub
模块文件:modHookMain.bas
Attribute VB_Name = "modHookMain"
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal
dwProcessId As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal
hProcess As Long, ByVal lpAddress As Long, ByVal dwnSize As Long,
ByVal flAllocationType As Long, ByVal flProtect As Long) As
Long
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal
hProcess As Long, lpAddress As Any, ByVal dwnSize As Long, ByVal
dwFreeType As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32"
(ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal
hhMod As Long, ByVal lpProcName As String) As Long
Private Declare Function FindWindow Lib "user32" Alias
"FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any)
As Long
Private Declare Function CreateRemoteThread Lib "kernel32"
(ByVal hProcess As Long, lpThreadAttributes As Long, ByVal
dwStacknSize As Long, lpStartAddress As Long, lpParameter As Any,
ByVal dwCreationFlags As Long, lpthreadid As Long) As Long
Private Declare Function GetModuleHandleA Lib "kernel32"
(ByVal ModName As Any) As Long
Private Declare Function WriteProcessMemory Lib "kernel32"
(ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any,
ByVal nnSize As Long, lpNumberOflngBytesWritten As Long) As
Long
Private Declare Function ReadProcessMemory Lib "kernel32"
(ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any,
ByVal nnSize As Long, lpNumberOflngBytesWritten As Long) As
Long
Private Declare Function LoadLibrary Lib "kernel32" Alias
"LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function CallWindowProc Lib "user32" Alias
"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long,
ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As
Long
Private Declare Function MessageBox Lib "user32" Alias
"MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal
lpCaption As String, ByVal wType As Long) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias
"DeleteFileA" (ByVal lpFileName As String) As Long
Private Declare Function GetModuleFileName Lib "kernel32"
Alias "GetModuleFileNameA" (ByVal hhMod As Integer, ByVal
lpFileName As String, ByVal nnSize As Integer) As Integer
Private Declare Function GetCurrentProcessId Lib "kernel32" ()
As Long
Private Declare Sub ExitProcess Lib "kernel32" (ByVal
dwExitCode As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias
"RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As
Long)
Private Const MEM_COMMIT = &H1000
Private Const MEM_RESERVE = &H2000
Private Const MEM_RELEASE = &H8000
Private Const PAGE_EXECUTE_READWRITE =
&H40&
Private Const IMAGE_NUMBEROF_DIRECTIRY_ENRIES = 16
Private Const STANDARD_RIGHTS_REQUIRED =
&HF0000
Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED
Or SYNCHRONIZE Or &HFFF)
Private Type IMAGE_DATA_DIRECTORY
End Type
Private Type IMAGE_FILE_HEADER
End Type
Private Type IMAGE_OPTIONAL_HEADER32
End Type
Private Type IMAGE_DOS_HEADER
End Type
Private Const szTarget As String = "ProgMan"
Private szFileName As String * 261
Private Type OBJECT_ATTRIBUTES
End Type
Private Type CLIENT_ID
End Type
Private Declare Function NtOpenProcess Lib "NTDLL.DLL" (ByRef
hProcess As Long, ByVal AccessMask As Long, ByRef ObjectAttributes
As OBJECT_ATTRIBUTES, ByRef CliendwThreadId As CLIENT_ID) As
Long
Private Declare Function lstrlen Lib "kernel32" Alias
"lstrlenA" (ByVal lpString As String) As Long
Private Declare Function lstrcpyn Lib "kernel32" Alias
"lstrcpynA" (ByVal lpString1 As String, ByVal lpString2 As String,
ByVal iMaxLength As Long) As Long
Private mProcess As Long
Private mlnghWnd As Long
Private mdwProcessId As Long
Private Type MYTYPE
End Type
Private pInfo As MYTYPE
Private Sub Main()
' Dim hVBMod As Long,
lngVBnSize As Long, hVBNewMod As Long
' hVBMod =
GetModuleHandleA("msvbvm60.dll")
' If hVBMod = 0 Then
End
' CopyMemory
objPImageDosHeader, ByVal hVBMod, Len(objPImageDosHeader)
' CopyMemory
objPImageOptionalHeader, ByVal (hVBMod +
objPImageDosHeader.e_lfanew + 4 + Len(objTImageFileHeader)),
Len(objPImageOptionalHeader)
' lngVBnSize =
objPImageOptionalHeader.SizeOfImage
' hVBNewMod =
VirtualAllocEx(hProcess, hVBMod, lngVBnSize, MEM_RESERVE Or
MEM_COMMIT, PAGE_EXECUTE_READWRITE)
' If hVBNewMod = 0 Then
End
' If
WriteProcessMemory(hProcess, ByVal hVBNewMod, ByVal hVBMod,
lngVBnSize, lngBytesWritten) = 0 Then End
End Sub
Public Function GetFunAddr(ByVal lngEntrypoint As Long) As
Long
End Function
Public Function RemoteFunAdd(ByVal hAddr As Long) As Long '
Code that will run in the hijacked program - CANNOT USE MOST VB
INTRISTIC FUNCTIONS -
End Function
Private Function HookMain(ByVal lngAgs As Long) As Long
' MessageBox 0,
mInfo.hWnd, szExename, 0
'
GetWindowThreadProcessId mInfo.hWnd,
mdwProcessId
End Function
Private Function NtOpenProcessCallback(ByRef hProcess As Long,
ByVal AccessMask As Long, ByRef ObjectAttributes As
OBJECT_ATTRIBUTES, ByRef ClientId As CLIENT_ID) As Long
End Function
测试部分
项目文件:Test.vbp
Type=Exe
Form=frmMain.frm
Reference=*/G{00020430-0000-0000-C000-000000000046}#2.0#0#../../../../../WINDOWS/system32/STDOLE2.TLB#OLE
Automation
Startup="frmMain"
ExeName32="Test.exe"
Command32=""
Name="Test"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="eabax"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
[MS Transaction Server]
AutoRefresh=1
窗体文件:frmMain.frm
VERSION 5.00
Begin VB.Form frmMain
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function GetWindowThreadProcessId Lib "user32"
(ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal
dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32"
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As
String) As Long
Private Sub cmdKill_Click()
End Sub
两个项目编译后,先打开Test.exe,然后再打开HookInfo.exe。
点击Test中窗体的按钮即可看到效果,祝你好运。