本帖最后由 pusofalse 于 2011-5-5 16:20 编辑
回复 14# auto
先运行帮助文件中TCPRecv、TCPSend函数中的两个例子,然后运行以下这段代码,截取server端接收到的数据。
这段代码假设已经知道 占用指定端口的进程。SCITE中运行。
只是段演示代码,不确保同样能够截取其他服务器程序的数据。
只截取在阻塞模式下接收的数据,对于使用重叠模式的socket,还要挂钩关联到socket的窗口过程,或检测关联到socket句柄的事件(Event)的状态。
#include <RTApiHook32.au3>
Const $PORT_NUMBER = 33891
Const $sFormat = "Remote IP: %s\nData: %s\n\n"
Local $iPid = _GetOpenedPortProcess() ; 假设已经知道占用指定端口的进程。
If ($iPid = 0) Then Exit 1
TCPStartup()
Local $hProcess = _RTOpenProcess($iPid)
Local $hCallBack = DllCallBackRegister("_MyWSARecv", "long", "handle;ptr")
Local $pCallBack = DllCallBackGetPtr($hCallBack)
Local $pWSARecv = _RTGetProcAddress("Ws2_32.dll", "WSARecv")
Local $tWSARecv = _RTApiHookEx($hProcess, $pWSARecv, 7, $pCallBack, $APIHOOK_Flags_2)
OnAutoItExitRegister("_RestoreHook")
While 1
Sleep(100)
WEnd
Func _RestoreHook()
_RTApiUnhook($hProcess, $pWSARecv, DllStructGetData($tWSARecv, "Entrypoint"))
Exit
EndFunc ;==>_RestoreHook
Func _MyWSARecv($hProcess, $pCallInfo)
Local $hSocket, $hDupSocket, $pBuffers, $pBuffer, $iBuffer, $pBytesRead, $sData
$hSocket = _RTApiHookReadParam($hProcess, $pCallInfo, 1, "handle")
$hDupSocket = _RTDuplicateHandle($hProcess, $hSocket, -1)
If _GetSocketPort($hDupSocket) <> $PORT_NUMBER Then
Return _RTCloseHandle($hDupSocket)
EndIf
$pBuffers = _RTApiHookReadParam($hProcess, $pCallInfo, 2)
$pBytesRead = _RTApiHookReadParam($hProcess, $pCallInfo, 4)
$iBuffer = _RTReadProcessMemory($hProcess, $pBytesRead, 0, 4, "long*")
$pBuffer = _RTReadProcessMemory($hProcess, $pBuffers + 4, 0, 4, "ptr*")
$sData = BinaryToString(_RTReadProcessMemory($hProcess, $pBuffer, 0, $iBuffer, "binary"))
ConsoleWrite(StringFormat($sFormat, _GetRemoteIP($hDupSocket), $sData))
_RTCloseHandle($hDupSocket)
EndFunc ;==>_MyWSARecv
Func _GetRemoteIP($hSocket)
Local $tSockAddr, $iResult
$tSockAddr = DllStructCreate("word Family;word Port;long IPAddress;byte Zero[8]")
$iResult = DllCall("Ws2_32.dll", "long", "getpeername", "handle", $hSocket, _
"ptr", DllStructGetPtr($tSockAddr), "long*", 16)
If $iResult[0] Then Return 0
$iResult = DllCall("Ws2_32.dll", "str", "inet_ntoa", "long", DllStructGetData($tSockAddr, "IPAddress"))
Return $iResult[0]
EndFunc ;==>_GetRemoteIP
Func _GetSocketPort($hSocket)
Local $tSockAddr, $iResult
$tSockAddr = DllStructCreate("word Family;word Port;long IPAddress;byte Zero[8]")
$iResult = DllCall("Ws2_32.dll", "long", "getsockname", "handle", $hSocket, _
"ptr", DllStructGetPtr($tSockAddr), "long*", 16)
If $iResult[0] Then Return 0
$iResult = DllCall("Ws2_32.dll", "word", "htons", "word", DllStructGetData($tSockAddr, "Port"))
Return $iResult[0]
EndFunc ;==>_GetSocketPort
Func _GetOpenedPortProcess()
Local $hWnd = WinGetHandle("My Server (IP: " & @IPAddress1 & ")")
Return _RTGetWindowThreadProcessId($hWnd)
EndFunc ;==>_GetOpenedPortProcess
|