Page 1 of 2 12 LastLast
Results 1 to 40 of 51

Thread: [RESOLVED] Block IP

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] Block IP

    Hey guys I was wondering by using VB is there a way to block a certain IP address from accessing my computer?

    Thanks

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Block IP

    In what way? You can use sockets to listen on a certain port and deny the access..

    RDP port is 3389..

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Block IP

    Do you have your computer, or folders on your computer, set up as Shared?

  4. #4
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Block IP

    You can't really block an IP address with VB like firewalls do. The closest you'll probably get with VB, is disconnecting already established connections with the GetTcpTable and SetTcpEntry API's.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Well you know the program Peer Guardian that blocks IP I would prefer to have a specific IP blocked,

    Has anyone heard of it?

    @ Hack, yes I do.

  6. #6
    Fanatic Member
    Join Date
    May 2007
    Location
    Merced
    Posts
    868

    Re: Block IP

    This is what you can do. If can have the Ip load into a textbox or a label that is not visable and have it check with the following code:

    Code:
    If Text1.Caption = "Ip Address" Then
    'Do The Following
    Else
    'Do This
    End if

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Block IP

    that code doesnt do anything.

    Post #4 is what the solution would be. Just execute it on a timer checking every couple of seconds or so as not to take up too many system resources.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    lol I was about to say, so I can use GetTcpTable to stop connections. I never heard of it before.

    So if I have a textbox with a certain Ip in it, GetTcpTable would check to see if it was connected and then stop it?

    How do I do that?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Block IP

    You have to use the SetTcpEntry to close the port.

    GetTcpTable:
    http://msdn2.microsoft.com/en-us/library/aa366026.aspx

    SetTcpEntry:
    http://msdn2.microsoft.com/en-us/library/aa366378.aspx


    If you use the structure's data you can set the state of the port to closed (MIB_TCP_STATE_CLOSED).
    http://msdn2.microsoft.com/en-us/lib...09(VS.85).aspx
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Ok, so this code with print the state of each connection.

    VB Code:
    1. #include <winsock2.h>
    2. #include <ws2tcpip.h>
    3. #include <iphlpapi.h>
    4. #include <stdio.h>
    5.  
    6. // Need to link with Iphlpapi.lib and Ws2_32.lib
    7.  
    8.  
    9. #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
    10. #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
    11. /* Note: could also use malloc() and free() */
    12.  
    13. int main()
    14. {
    15.  
    16.     // Declare and initialize variables
    17.     PMIB_TCPTABLE pTcpTable;
    18.     DWORD dwSize = 0;
    19.     DWORD dwRetVal = 0;
    20.  
    21.     char szLocalAddr[128];
    22.     char szRemoteAddr[128];
    23.  
    24.     struct in_addr IpAddr;
    25.  
    26.     int i;
    27.  
    28.     pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
    29.     if (pTcpTable == NULL) {
    30.         printf("Error allocating memory\n");
    31.         return 1;
    32.     }
    33.  
    34.     dwSize = sizeof (MIB_TCPTABLE);
    35. // Make an initial call to GetTcpTable to
    36. // get the necessary size into the dwSize variable
    37.     if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) ==
    38.         ERROR_INSUFFICIENT_BUFFER) {
    39.         FREE(pTcpTable);
    40.         pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);
    41.         if (pTcpTable == NULL) {
    42.             printf("Error allocating memory\n");
    43.             return 1;
    44.         }
    45.     }
    46. // Make a second call to GetTcpTable to get
    47. // the actual data we require
    48.     if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
    49.         printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
    50.         for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
    51.             IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
    52.             strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
    53.             IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
    54.             strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
    55.  
    56.             printf("\n\tTCP[%d] State: %ld - ", i,
    57.                    pTcpTable->table[i].dwState);
    58.             switch (pTcpTable->table[i].dwState) {
    59.             case MIB_TCP_STATE_CLOSED:
    60.                 printf("CLOSED\n");
    61.                 break;
    62.             case MIB_TCP_STATE_LISTEN:
    63.                 printf("LISTEN\n");
    64.                 break;
    65.             case MIB_TCP_STATE_SYN_SENT:
    66.                 printf("SYN-SENT\n");
    67.                 break;
    68.             case MIB_TCP_STATE_SYN_RCVD:
    69.                 printf("SYN-RECEIVED\n");
    70.                 break;
    71.             case MIB_TCP_STATE_ESTAB:
    72.                 printf("ESTABLISHED\n");
    73.                 break;
    74.             case MIB_TCP_STATE_FIN_WAIT1:
    75.                 printf("FIN-WAIT-1\n");
    76.                 break;
    77.             case MIB_TCP_STATE_FIN_WAIT2:
    78.                 printf("FIN-WAIT-2 \n");
    79.                 break;
    80.             case MIB_TCP_STATE_CLOSE_WAIT:
    81.                 printf("CLOSE-WAIT\n");
    82.                 break;
    83.             case MIB_TCP_STATE_CLOSING:
    84.                 printf("CLOSING\n");
    85.                 break;
    86.             case MIB_TCP_STATE_LAST_ACK:
    87.                 printf("LAST-ACK\n");
    88.                 break;
    89.             case MIB_TCP_STATE_TIME_WAIT:
    90.                 printf("TIME-WAIT\n");
    91.                 break;
    92.             case MIB_TCP_STATE_DELETE_TCB:
    93.                 printf("DELETE-TCB\n");
    94.                 break;
    95.             default:
    96.                 printf("UNKNOWN dwState value\n");
    97.                 break;
    98.             }
    99.             printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
    100.             printf("\tTCP[%d] Local Port: %d \n", i,
    101.                    ntohs((u_short)pTcpTable->table[i].dwLocalPort));
    102.             printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);
    103.             printf("\tTCP[%d] Remote Port: %d\n", i,
    104.                    ntohs((u_short)pTcpTable->table[i].dwRemotePort));
    105.         }
    106.     } else {
    107.         printf("\tGetTcpTable failed with %d\n", dwRetVal);
    108.         FREE(pTcpTable);
    109.         return 1;
    110.     }
    111.  
    112.     return 0;    
    113. }

    but where would it print to?

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Block IP

    I dont know C++ so couldnt say for sure but looks like it would go to a console sessions screen?

    http://en.wikipedia.org/wiki/Printf
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    So this would work in VB 6 right?

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Block IP

    you would have to convert it to or complete write it in VB 6 and then it would work.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Block IP

    Actually I created a little tool to disconnect IP's a few months ago. The TCP table checking code is in a Timer with an interval of 1 millisecond (although it will probably only fire once every 15 milliseconds or so). It loads IP's from a text file. It can easily handle 100.000 ranges with more than 100 million IP's and it barely uses any resources.

    Make sure there is an empty line at the end of the IP list or else there's an error at the end when loading the list. I haven't looked into that, because I've stopped working on it.
    Attached Files Attached Files
    Last edited by Chris001; Jan 10th, 2008 at 10:57 AM.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    I know this is pretty old, but I with the thing Christ001 posted, I can't figure out how to open the file with all the IPs so it can be updated.

  16. #16
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Block IP

    Chris's code does not include the facility to edit the list of IPs. You could always use Notepad. (or write your own Editor)

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Ok I have gotten a bit lost, Chris's code will disconnect certain Ips?

    How would he know which Ip's should be disconnecteD?

  18. #18
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Block IP

    I suspect he spent some time doing some research on 'rogue' IP Address usage. Presumably, as you asked the question, you know of some IP Addresses that you don't want to connect to your machine (or if they do, you want to disconnect tham).

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Ok so what I can do is make a database of these IP addresses I do not want connected.

    So I have created my own simple listview where there are 3 columns, how may I load the database file to the corresponding columns in the listview and ahve the program search for the ip adresses in the first column and if theey find them connected, disconnect.

    Or maybe I am going about this wrong, maybe i could have a list1 listview hat shows the ips trying to connect to the comp, and if there is a match between the list1 listbox and the first column of the listview then it disconnects it.

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Another question I had is I have a listbox called list1, how would I be able to have all the IP addresses trying to connect to my PC be shown in that? All the ones that use TCP protocol?

  21. #21

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Wait I got an idea, Can I have a listbox that shows all incoming IP's and have a another listbox of blacklisted IPs, where if there was a match between an incoming Ip and a black list IP it would get disconnected? How could I do this?

  22. #22
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Block IP

    Well, using Chris's code as a sort of template, I knocked up this. It will read a series of IP Addresses from a text file (c:\Blacklist.txt - One IP Address per line) and populate a ListBox (lstBlacklist). It then grabs the TCP Table and extracts all the Remote Host IP Addresses, which are refreshed every second, and puts them into lstRemote. The two lists are compared, and if anything in lstBlacklist connects, or is connected, it is disconnected and a line is written to txtAction (a multiline TextBox)
    Code:
    Option Explicit
    
    Private Declare Function GetTcpTable Lib "iphlpapi.dll" _
                    (ByRef pTcpTable As Any, _
                     ByRef pdwSize As Long, _
                     ByVal bOrder As Long) As Long
                     
    Private Declare Function SetTcpEntry Lib "iphlpapi.dll" _
                    (pTcpTableEx As MIB_TCPROW) As Long
                     
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
                    (ByRef pDest As Any, _
                     ByRef pSource As Any, _
                     ByVal Length As Long)
    
    Private Declare Function RtlIpv4AddressToString Lib "ntdll.dll" Alias "RtlIpv4AddressToStringA" _
                    (ByRef lngAddr As Long, ByVal strAddr As String) As Long
                    
    Private Const ERROR_SUCCESS = 0&
    Private Const ERROR_INSUFFICIENT_BUFFER = 122
    
    Private Type MIB_TCPROW
        dwState As Long
        dwLocalAddr As Long
        dwLocalPort As Long
        dwRemoteAddr As Long
        dwRemotePort As Long
    End Type
    
    Private udtTCP() As MIB_TCPROW
    
    Private Function GetIPInfo() As Boolean
    Dim bytBuffer() As Byte
    Dim lngSize As Long
    Dim lngEntries As Long
    Dim lngReturn As Long
    Dim lngState As Long
    Dim intI As Integer
    Dim intOffSet As Integer
    Dim strRemote As String
    Dim strState As String
    '
    ' Obtain the length of the TCP Table
    '
    lngReturn = GetTcpTable(lngSize, lngSize, 0)
    If lngReturn = ERROR_INSUFFICIENT_BUFFER Then
        ReDim bytBuffer(lngSize - 1)
        '
        ' Get the TCP Table
        ' and populate the MIB_TCPROWs
        '
        lngReturn = GetTcpTable(bytBuffer(0), lngSize, 0)
        If lngReturn = 0 Then
            CopyMemory lngEntries, bytBuffer(0), 4
            ReDim udtTCP(lngEntries - 1)
            For intI = 0 To lngEntries - 1
                strRemote = Space(16)
                intOffSet = (intI * Len(udtTCP(intI))) + 4
                CopyMemory udtTCP(intI), bytBuffer(intOffSet), Len(udtTCP(intI))
                '
                ' Remote Addresses of zero are of no interest
                ' otherwise convert the address to dotted form
                ' and add it to the ListBox
                '
                If udtTCP(intI).dwRemoteAddr <> 0 Then
                    lngReturn = RtlIpv4AddressToString(udtTCP(intI).dwRemoteAddr, strRemote)
                    lstRemote.AddItem Trim$(Left$(strRemote, InStr(strRemote, Chr(0)) - 1))
                End If
            Next intI
        Else
            MsgBox "Unable to Access TCP Table Entries"
        End If
    Else
        MsgBox "Unable to Access TCP Table"
    End If
    End Function
    
    Private Sub CheckIP()
    Dim intI As Integer
    Dim intJ As Integer
    Dim lngReturn As Long
    Dim strRemote As String
    For intI = LBound(udtTCP) To UBound(udtTCP)
        strRemote = Space(15)
        lngReturn = RtlIpv4AddressToString(udtTCP(intI).dwRemoteAddr, strRemote)
        strRemote = Trim$(Left$(strRemote, InStr(strRemote, Chr(0)) - 1))
        For intJ = 0 To lstBlacklist.ListCount - 1
            If strRemote = Trim(lstBlacklist.List(intJ)) Then
                udtTCP(intI).dwState = 12
                lngReturn = SetTcpEntry(udtTCP(intI))
                txtAction.Text = txtAction.Text & lstBlacklist.List(intJ) & _
                        " has been Disconnected at " & Format(Now, "dd/mm/yyyy hh:mm:ss") & vbCrLf
            End If
        Next intJ
    Next intI
    End Sub
    
    Private Sub LoadBlacklist()
    Dim intFile As Integer
    Dim strLine As String
    intFile = FreeFile
    Open "C:\Blacklist.txt" For Input As intFile
    Do Until EOF(intFile)
        Line Input #intFile, strLine
        lstBlacklist.AddItem strLine
    Loop
    Close intFile
    End Sub
    
    Private Sub Form_Load()
    lstBlacklist.Clear
    Call LoadBlacklist
    Call GetIPInfo
    Timer1.Interval = 1000
    Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
    lstRemote.Clear
    Call GetIPInfo
    Call CheckIP
    End Sub
    That might get you sarted.
    Last edited by Doogle; Feb 10th, 2008 at 06:21 AM. Reason: Corrected a couple of errors

  23. #23
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Block IP

    Loading the IP's that should be disconnected in a listview or listbox only works when you have a handful of IP's or else it takes up too much resources.

    The example I created loads all IP ranges into memory, which is much faster than continuously reading the IP's from the listbox/listview and comparing them with the IP's of the established connections.

  24. #24

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Quote Originally Posted by Doogle
    Well, using Chris's code as a sort of template, I knocked up this. It will read a series of IP Addresses from a text file (c:\Blacklist.txt - One IP Address per line) and populate a ListBox (lstBlacklist). It then grabs the TCP Table and extracts all the Remote Host IP Addresses, which are refreshed every second, and puts them into lstRemote. The two lists are compared, and if anything in lstBlacklist connects, or is connected, it is disconnected and a line is written to txtAction (a multiline TextBox)
    Code:
    Option Explicit
    
    Private Declare Function GetTcpTable Lib "iphlpapi.dll" _
                    (ByRef pTcpTable As Any, _
                     ByRef pdwSize As Long, _
                     ByVal bOrder As Long) As Long
                     
    Private Declare Function SetTcpEntry Lib "iphlpapi.dll" _
                    (pTcpTableEx As MIB_TCPROW) As Long
                     
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
                    (ByRef pDest As Any, _
                     ByRef pSource As Any, _
                     ByVal Length As Long)
    
    Private Declare Function RtlIpv4AddressToString Lib "ntdll.dll" Alias "RtlIpv4AddressToStringA" _
                    (ByRef lngAddr As Long, ByVal strAddr As String) As Long
                    
    Private Const ERROR_SUCCESS = 0&
    Private Const ERROR_INSUFFICIENT_BUFFER = 122
    
    Private Type MIB_TCPROW
        dwState As Long
        dwLocalAddr As Long
        dwLocalPort As Long
        dwRemoteAddr As Long
        dwRemotePort As Long
    End Type
    
    Private udtTCP() As MIB_TCPROW
    
    Private Function GetIPInfo() As Boolean
    Dim bytBuffer() As Byte
    Dim lngSize As Long
    Dim lngEntries As Long
    Dim lngReturn As Long
    Dim lngState As Long
    Dim intI As Integer
    Dim intOffSet As Integer
    Dim strRemote As String
    Dim strState As String
    '
    ' Obtain the length of the TCP Table
    '
    lngReturn = GetTcpTable(lngSize, lngSize, 0)
    If lngReturn = ERROR_INSUFFICIENT_BUFFER Then
        ReDim bytBuffer(lngSize - 1)
        '
        ' Get the TCP Table
        ' and populate the MIB_TCPROWs
        '
        lngReturn = GetTcpTable(bytBuffer(0), lngSize, 0)
        If lngReturn = 0 Then
            CopyMemory lngEntries, bytBuffer(0), 4
            ReDim udtTCP(lngEntries - 1)
            For intI = 0 To lngEntries - 1
                strRemote = Space(16)
                intOffSet = (intI * Len(udtTCP(intI))) + 4
                CopyMemory udtTCP(intI), bytBuffer(intOffSet), Len(udtTCP(intI))
                '
                ' Remote Addresses of zero are of no interest
                ' otherwise convert the address to dotted form
                ' and add it to the ListBox
                '
                If udtTCP(intI).dwRemoteAddr <> 0 Then
                    lngReturn = RtlIpv4AddressToString(udtTCP(intI).dwRemoteAddr, strRemote)
                    lstRemote.AddItem Trim$(Left$(strRemote, InStr(strRemote, Chr(0)) - 1))
                End If
            Next intI
        Else
            MsgBox "Unable to Access TCP Table Entries"
        End If
    Else
        MsgBox "Unable to Access TCP Table"
    End If
    End Function
    
    Private Sub CheckIP()
    Dim intI As Integer
    Dim intJ As Integer
    Dim lngReturn As Long
    Dim strRemote As String
    For intI = LBound(udtTCP) To UBound(udtTCP)
        strRemote = Space(15)
        lngReturn = RtlIpv4AddressToString(udtTCP(intI).dwRemoteAddr, strRemote)
        strRemote = Trim$(Left$(strRemote, InStr(strRemote, Chr(0)) - 1))
        For intJ = 0 To lstBlacklist.ListCount - 1
            If strRemote = Trim(lstBlacklist.List(intJ)) Then
                udtTCP(intI).dwState = 12
                lngReturn = SetTcpEntry(udtTCP(intI))
                txtAction.Text = txtAction.Text & lstBlacklist.List(intJ) & _
                        " has been Disconnected at " & Format(Now, "dd/mm/yyyy hh:mm:ss") & vbCrLf
            End If
        Next intJ
    Next intI
    End Sub
    
    Private Sub LoadBlacklist()
    Dim intFile As Integer
    Dim strLine As String
    intFile = FreeFile
    Open "C:\Blacklist.txt" For Input As intFile
    Do Until EOF(intFile)
        Line Input #intFile, strLine
        lstBlacklist.AddItem strLine
    Loop
    Close intFile
    End Sub
    
    Private Sub Form_Load()
    lstBlacklist.Clear
    Call LoadBlacklist
    Call GetIPInfo
    Timer1.Interval = 1000
    Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
    lstRemote.Clear
    Call GetIPInfo
    Call CheckIP
    End Sub
    That might get you sarted.

    Ok so I have the text box, and the list boxes made but when I run the program I get the error invalid procedure call or argument and it highlights this line of code strRemote = Trim$(Left$(strRemote, InStr(strRemote, Chr(0)) - 1))

  25. #25
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Block IP

    I had exactly that problem but thought I'd fixed it ! (It works ok for me!) I edited the post about 2 hours after I posted the original to correct the errors.

    Is there a chance that you have the un-updated version?

    The problem was that when the Long Address is converted to a string, the API terminates it with a Hex 00 (Chr(0)) so where I set strRemote as 15 characters it should have been 16. This was causing InStr to return 0 and caused the exception. Hence I changed strRemote = Space(15) to strRemote = Space(16) which is the maximum length of a dotted IP address + 1. I'd also got the brackets in a muddle and corrected that.

    Could you run it again, please and when it breaks, hover the mouse over strRemote and see what the contents are.

    EDIT: Thre's also a slight performance improvement that can be made to CheckIP. Ignore remoteIP addresses of 0
    Code:
    Private Sub CheckIP()
    Dim intI As Integer
    Dim intJ As Integer
    Dim lngReturn As Long
    Dim strRemote As String * 16
    For intI = LBound(udtTCP) To UBound(udtTCP)
        If udtTCP(intI).dwRemoteAddr <> 0 Then
            strRemote = Space(16)
            lngReturn = RtlIpv4AddressToString(udtTCP(intI).dwRemoteAddr, strRemote)
            strRemote = Trim$(Left$(strRemote, InStr(strRemote, Chr(0)) - 1))
            For intJ = 0 To lstBlacklist.ListCount - 1
                If strRemote = Trim(lstBlacklist.List(intJ)) Then
                    udtTCP(intI).dwState = 12
                    lngReturn = SetTcpEntry(udtTCP(intI))
                    txtAction.Text = txtAction.Text & lstBlacklist.List(intJ) & _
                        " has been Disconnected at " & Format(Now, "dd/mm/yyyy hh:mm:ss") & vbCrLf
                End If
            Next intJ
        End If
    Next intI
    End Sub
    Last edited by Doogle; Feb 11th, 2008 at 12:42 AM.

  26. #26

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    I just added the new code, and I no longer get any error cheers!

    However I was wondering, the lstRemote text box, it contains IPs trying to connect to me? Or the ones connected to me?

  27. #27
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Block IP

    The lstRemote ListBox displays the remotes actually connected. t's updated every second so you should see the contents changng as and when others connect or disconnect.

  28. #28

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Block IP

    Excellent, thanks for your help. I was wondering though how do I know which IPs could be bad ?lol

  29. #29
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] Block IP

    Personally, I haven't a clue

    However a google search with "known bad Ip addresses" came up with, for example, this http://www.unixhub.com/block.html

    Perhaps Chris will pop by and tell you how he created his list.

  30. #30
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] Block IP

    Well, what do you consider a "bad IP"?
    If you want to block "a certain IP address" from accessing your computer, like you said in your first post, then you should already know who/what you want to block.

    My list are all known IP ranges used in the US. Many websites are hosted in the US, so that list can easily be used for testing purposes.

    Here's an updated list with the IP ranges used in most countries in the world. It's a CSV file, so it needs to be converted first to a format your app can read.

    http://ip-to-country.webhosting.info/node/view/6
    Last edited by Chris001; Feb 11th, 2008 at 10:11 AM.

  31. #31

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] Block IP

    Well originally I would like to block a certain anti p2p ip, but I would like to block p2p in general, gov websites, etc.

  32. #32
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] Block IP

    Then you can for example download the eMule list here: http://www.bluetack.co.uk/forums/ind...ewcat&cat_id=4

    But those are more than 200.000 IP ranges (almost a billion IP addresses) and you will never be able to load them all into a Listbox and compare each established connection with the IP's ranges in the Listbox.

  33. #33

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] Block IP

    You mean one single list would have that many?

    Besides the downloads are in gz format, how would VB be able to read that?

  34. #34
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] Block IP

    Yes, one list would have that many.

    GZ is a compression format. Extract the contents from the GZ file with Winrar.

    The extracted .dat file contains dotted IP ranges like this:

    Code:
    000.000.000.001 - 003.255.255.255 , 000 , Bogon,
    004.000.025.146 - 004.000.025.148 , 000 , s0-0.c
    004.000.026.014 - 004.000.029.024 , 000 , p1-0.c
    004.001.075.131 - 004.001.075.156 , 000 , s0.med
    Then make your app read the .dat file or convert the dotted IP's to some other format, which is easier to read.

  35. #35

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] Block IP

    Ok so 000.000.000.001 - 003.255.255.255 for ex cover a whole net range or just two ips? whats with the names like Bogon?

  36. #36
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] Block IP

    It's a whole range.

    Bogon and the other ones are the names the IP ranges belong to. In this case "Bogon" (Bogus) are IP's not allocated to any Internet Service Provider.

  37. #37

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] Block IP

    Could I put it into an acess database file?

    PS: Since this program currently blocks one single IP how would It block a netrange?

    If i put a listview one where lstBlacklist is, how would I load a database to the corresponding columns if it had a net range column and a name for that range?
    Last edited by Justin M; Feb 11th, 2008 at 10:54 PM.

  38. #38
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] Block IP

    I'm sure you can put the ranges in a database, but that still requires continuous reading from a file.

    I posted a sample project above that shows you how to disconnect a range (reply #14).

  39. #39

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: [RESOLVED] Block IP

    Well if the database of black lists was loaded to a listview, wouldn't it just read through the listview over and over?

  40. #40
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: [RESOLVED] Block IP

    Yes, but comparing an IP address with 200.000 IP ranges over and over in a Listview is very SLOW.

    If you want to handle so many ranges, you'll have to load the entire list in memory and do a quicksort based on the start IP range. Then do a binary search to see if there's a match.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width