VB design IP packet interception program in Win2000

Source: Internet
Author: User
The following is the source code of the TCP/IP package in WIN2000 intercepted in VB. It passes the test in VB6.0 and win2000. Note that: 1. must be a local Nic, 2. each time data is obtained, there must be a delay. 3. Put the data in the Buff array. 4. Put the following code in a module.

'----------------------------- Code start --------------------------------------------------
Declare Function bind Lib "ws2_32.dll" (ByVal s As Long, addr As SOCK_ADDR, ByVal namelen As Long) As Long
Declare Function closesocket Lib "ws2_32.dll" (ByVal s As Long) As Long
Declare Function connect Lib "ws2_32.dll" (ByVal s As Long, name As SOCK_ADDR, ByVal namelen As Integer) As Long
Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long
Declare Function htons Lib "ws2_32.dll" (ByVal hostshort As Integer) As Integer
Declare Function recv Lib "ws2_32.dll" (ByVal s As Long, buffer As Any, ByVal length As Long, ByVal flags As Long) As Long
Declare function send lib "ws2_32.dll" (byval s as long, buffer as any, byval length as long, byval flags as long) as long
Declare function shutdown lib "ws2_32.dll" (byval s as long, byval how as long) as long
Declare function ioctlsocket lib "ws2_32.dll" (byval s as long, byval V as long, ut as long) as long
Declare function socket lib "ws2_32.dll" (byval AF as long, byval type_specification as long, byval protocol as long) as long
Declare function wsacancelblockingcall lib "ws2_32.dll" () as long
Declare function wsacleanup lib "ws2_32.dll" () as long
Declare function wsagetlasterror lib "ws2_32.dll" () as long
Declare function wsastartup lib "ws2_32.dll" (byval wversionrequired as integer, wsdata as wsa_data) as long
Declare Function WSASocketA Lib "ws2_32.dll" (ByVal af As Long, ByVal type1 As Long, ByVal protocol As Long, lpProtocolInfo As Long, g As Long, ByVal dwFlags As Long)
Declare Function WSAIoctl Lib "ws2_32.dll" (ByVal s As Long, ByVal dwIoControlCode As Long, lpvInBuffer As Long, ByVal cbInBuffer As Long, lpvOutBuffer As Long, ByVal cbOutBuffer As Long, stored As Long, lpOverlapped As Long, lpCompletionRoutine As Long) As Long

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public constwsadescription_len = 256
Public Const WSASYS_STATUS_LEN = 128

Type WSA_DATA
WVersion As Integer
WHighVersion As Integer
StrDescription (WSADESCRIPTION_LEN + 1) As Byte
StrSystemStatus (WSASYS_STATUS_LEN + 1) As Byte
IMaxSockets As Integer
IMaxUdpDg As Integer
LpVendorInfo As Long
End Type

Type IN_ADDR
S_addr As Long
End Type

Type SOCK_ADDR
Sin_family As Integer
Sin_port As Integer
Sin_addr As IN_ADDR
Sin_zero (0 To 7) As Byte
End Type

Type IPHeader
Lenver As Byte
Tos As Byte
Len As Integer
Ident As Integer
Flags As Integer
Ttl As Byte
Proto As Byte
Checksum As Integer
SourceIP As Long
DestIP As Long
End Type

Const AF_INET = 2
Const SOCK_RAW = 3
Const IPPROTO_IP = 0
Const ipproto_tcp = 6
Const ipproto_udp = 17
Const max_pack_len= 65535
Const socket_error =-1 &

Private mwsadata as wsa_data
Private m_hsocket as long

Private msalocaladdr as sock_addr
Private msaremoteaddr as sock_addr

Sub main ()
Dim nresult as long

Nresult = wsastartup (& H202, mwsadata)
If nresult <> wsanoerror then
Msgbox "error en wsastartup"
Exit sub
End if

M_hsocket = socket (af_inet, sock_raw, ipproto_ip)
If (m_hsocket = invalid_socket) then
Msgbox "error in socket"
Exit sub
End if

Msalocaladdr. sin_family = af_inet
Msalocaladdr. sin_port = 0
MsaLocalAddr. sin_addr.S_addr = inet_addr ("192.168.1.125") 'the IP address of your Nic is required.

NResult = bind (m_hSocket, msaLocalAddr, Len (msaLocalAddr ))
If (nResult = SOCKET_ERROR) Then
MsgBox "Error in bind"
Exit Sub
End If

Dim InParamBuffer As Long
Dim BytesRet As Long
BytesRet = 0
InParamBuffer = 1

NResult = ioctlsocket (m_hSocket, & H98000001, 1)

If nResult <> 0 Then
MsgBox "ioctlsocket"
Exit Sub
End If

Dim strData As String
Dim nReceived As Long
 
'Put the intercepted data in the BUFF.
Dim Buff (0 To MAX_PACK_LEN) As Byte
Dim IPH As IPHeader

Do Until false' is always obtained
DoEvents
NResult = recv (m_hSocket, Buff (0), MAX_PACK_LEN, 0)
If nResult = SOCKET_ERROR Then
MsgBox "Error in RecvData: recv"
Exit Do
End If
CopyMemory IPH, Buff (0), Len (IPH) 'for convenient access
Select Case IPH. proto
Case IPPROTO_TCP
'Frmhooktcpip. Text1.SelText = HexIp2DotIp (IPH. sourceIP)
'Frmhooktcpip. Text1.SelText = "----->"
'Frmhooktcpip. Text1.SelText = HexIp2DotIp (IPH. destIP)
'Frmhooktcpip. Text1.SelText = vbCrLf
Debug. Print HexIp2DotIp (IPH. sourceIP) & "----->" & HexIp2DotIp (IPH. destIP)
End Select
Loop

NResult = shutdown (m_hSocket, 2)
NResult = closesocket (m_hSocket)
NResult = WSACancelBlockingCall
NResult = WSACleanup
End Sub

Function HexIp2DotIp (ByVal ip As Long) As String
Dim s As String, p1 As String, p2 As String, p3 As String, p4 As String
S = Right ("00000000" & Hex (ip), 8)
P1 = Val ("& h" & Mid (s, 1, 2 ))
P2 = Val ("& h" & Mid (s, 3, 2 ))
P3 = Val ("& h" & Mid (s, 5, 2 ))
P4 = Val ("& h" & Mid (s, 7, 2 ))
HexIp2DotIp = p4 & "." & p3 & "." & p2 & "." & p1
End Function
'----------------------------- Code end --------------------------------------------------

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.