使用VB在特定端口丢弃数据包的代码

时间:2010-04-02 04:40:37

标签: firewall

这段代码应该在VB中使用。端口号和IP地址可用。如何防止数据包进入是众所周知的。它类似于FireWall的工作。

1 个答案:

答案 0 :(得分:0)

Ur拥有Personal FIREwall !!


以下是一些示例VB6代码,以帮助您入门。

它使用winsock控件打开并连接到端口本身。

因此,它会自动拒绝其他进程对端口的访问。

Public Type MIB_TCPROW
dwState As Long
dwLocalAddr As Long
dwLocalPort As Long
dwRemoteAddr As Long
dwRemotePort As Long
End Type

Public Type MIB_TCPTABLE
dwNumEntries As Long
table(100) As MIB_TCPROW
End Type

Public MIB_TCPTABLE As MIB_TCPTABLE

Public Declare Function GetTcpTable Lib "iphlpapi.dll" (ByRef pTcpTable As
MIB_TCPTABLE, ByRef pdwSize As Long, ByVal bOrder As Long) As Long
Public Declare Function SetTcpEntry Lib "IPhlpAPI" (pTcpRow As MIB_TCPROW)
As Long
Public Declare Function ntohs Lib "WSOCK32.DLL" (ByVal netshort As Long) As
Long


Public Sub BlockPort


Dim LTmp As Long
Dim x As Integer, i As Integer, n As Integer
Dim RemP As String
Dim tcpt As MIB_TCPTABLE

LTmp = Len(MIB_TCPTABLE)
GetTcpTable tcpt, LTmp, 0
x = tcpt.dwNumEntries

For i = 0 To tcpt.dwNumEntries - 1

RemP = ntohs(tcpt.table(i).dwRemotePort)

If RemP = "8080" And tcpt.table(i).dwState <> 2 Then
tcpt.table(i).dwState = 12
SetTcpEntry tcpt.table(i)
End If

Next i

End Sub

如果您正在寻找一种更简单的方法来阻止单个端口,那么:

  • 在VB表单中使用Winsock控件。

  • localport属性设置为您要阻止的端口号

  

完整参考Winsock for VB6 here

......那就是它!您自己的个人防火墙已经启动!!

古德勒克!!
- CVS