本地hook API MessageBoxA的masm32源代码[07-10更新]

本文提供了一个使用MASM实现的本地hook MessageBoxA函数的例子。该示例展示了如何通过读取和更改目标进程内存来实现hook操作,适用于Windows XP + SP1环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;FileName:msgbox2.asm
;Function:DemohowtohookMessageBoxAlocally
;Author:PurpleEndurer
;
;log
;--------------------------------------------------
;2006-07-10Optimizedcode
;2006-07-08Created,successunderWindowsXP+SP1
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.586p
.modelflat,stdcall
optioncasemap:none

include/masm32/include/windows.inc

include/masm32/include/kernel32.inc
include/masm32/include/user32.inc

includelib/masm32/lib/kernel32.lib
includelib/masm32/lib/user32.lib

m_m2mMACROd1,d2
pushd2
popd1
ENDM


MEMORY_BASIC_INFORMATION_SIZEEQU28

.data
g_szUser32dllDB"user32.dll",0
g_szMsgBoxDB"MessageBoxA",0
g_szHookedOKdb"hasbeenhookedOK!",0

.data?
g_dwOld_protectDD?
g_lpfnMessagBoxdword?
g_dbOldCodedb10dup(?)
g_dwReadeddword?
g_hCurProcHANDLE?

.code

start:
do_hook:
invokeGetModuleHandle,ADDRg_szUser32dll
invokeGetProcAddress,eax,ADDRg_szMsgBox
movedi,eax;finallygotMessageBoxAaddress
movg_lpfnMessagBox,eax

push0
pushOFFSETg_szMsgBox
pushOFFSETg_szMsgBox
push0
callg_lpfnMessagBox;确认得到MessageBoxA的地址

invokeGetCurrentProcess
movg_hCurProc,eax

;BOOLReadProcessMemory(
;HANDLEhProcess,//handleoftheprocesswhosememoryisread
;LPCVOIDlpBaseAddress,//addresstostartreading
;LPVOIDlpBuffer,//addressofbuffertoplacereaddata
;DWORDnSize,//numberofbytestoread
;LPDWORDlpNumberOfBytesRead//addressofnumberofbytesread
;);
invokeReadProcessMemory,eax,g_lpfnMessagBox,ADDRg_dbOldCode,10,ADDRg_dwReaded

testeax,eax
jz@FinalMsgBox

invokeVirtualAlloc,0,MEMORY_BASIC_INFORMATION_SIZE,MEM_COMMIT,PAGE_READWRITE

testeax,eax
jz@FinalMsgBox

movesi,eax;allocationforMBI
invokeVirtualQuery,edi,esi,MEMORY_BASIC_INFORMATION_SIZE

;typedefstruct_MEMORY_BASIC_INFORMATION{//mbi
;PVOIDBaseAddress;//baseaddressofregion
;PVOIDAllocationBase;//allocationbaseaddress
;DWORDAllocationProtect;//initialaccessprotection
;DWORDRegionSize;//size,inbytes,ofregion
;DWORDState;//committed,reserved,free
;DWORDProtect;//currentaccessprotection
;DWORDType;//typeofpages
;}MEMORY_BASIC_INFORMATION;

testeax,eax
jz@free_mem

invokeFlushInstructionCache,g_hCurProc,edi,5;justtobesure

leaeax,[esi+014h]
pusheax
pushPAGE_EXECUTE_READWRITE
leaeax,[esi+0Ch]
push[eax]
push[esi]
callVirtualProtect
;wewillchangeprotectionforamoment,sowewillbeabletowritethere

testeax,eax
jz@free_mem

movbyteptr[edi],0E9h;写入jmp跳转指令
moveax,OFFSET@newMsgBox;计算跳转地址
subeax,edi
subeax,5
incedi
stosd;传送32位跳转地址

pushOFFSETg_dwOld_protect
leaeax,[esi+014h]
push[eax]
leaeax,[esi+0Ch]
push[eax]
push[esi]
callVirtualProtect;returnbacktheprotectionofpage

@free_mem:
pushMEM_RELEASE
push0
pushesi
callVirtualFree;freememory

@FinalMsgBox:
invokeMessageBoxA,0,ADDRg_szMsgBox,ADDRg_szMsgBox,0
invokeExitProcess,0

@newMsgBox:;004010CD
;mov[esp+16],MB_ICONINFORMATION;修改信息ICON
m_m2m[esp+16],MB_ICONINFORMATION
;mov[esp+12],OFFSETg_szHookedOK;修改标题
moveax,[esp+8];修改信息内容
invokelstrcat,eax,ADDRg_szHookedOK

;BOOLWriteProcessMemory(
;HANDLEhProcess,//handletoprocesswhosememoryiswrittento
;LPVOIDlpBaseAddress,//addresstostartwritingto
;LPVOIDlpBuffer,//pointertobuffertowritedatato
;DWORDnSize,//numberofbytestowrite
;LPDWORDlpNumberOfBytesWritten//actualnumberofbyteswritten
;);
invokeWriteProcessMemory,g_hCurProc,g_lpfnMessagBox,ADDRg_dbOldCode,10,ADDRg_dwReaded
jmpg_lpfnMessagBox;pushg_lpfnMessagBox
;ret;10H

endstart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值