Blocking IP addresses

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • printedgoods
    New Member
    • Nov 2007
    • 9

    Blocking IP addresses

    I currently have an IP address checking system in place to block IP's that abuse my querystrings. Most of these are from outside the US.

    My question is:

    How can I block everyone but US IP addresses? I don't know if i can use somthing like this "215.*.*.*" or "215*"' and it work.

    Thanks
    Jason
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    [code=asp]response.write request.serverV ariables("remot e_addr")
    response.write request.serverV ariables("remot e_host")[/code]I can't remember what happens when you try to connect thru a proxy, and I've never bothered to check which IP addresses belong to which areas, but I understand that isn't hard to look up. So you could do something like this: [code=asp]if left(request.se rverVariables(" remote_addr"), 3) = "255" then
    response,.write "Welcome to my site"
    else
    response.write "see you later, bozo."
    end if[/code]

    Comment

    • danp129
      Recognized Expert Contributor
      • Jul 2006
      • 323

      #3
      Download IP2Country.zip from here and extract it.

      Make this vbs script and edit it to allow only US country code or whatever country codes you wish to allow. Then execute the file.

      [CODE=vb]
      'RemoveJunk.vbs
      Const Countries2Keep = "US,CA,AU" 'Country codes to allow access
      Const ForReading = 1
      Const ForWriting = 2

      Dim sAppPath, sFileIn, sFileOut
      sAppPath = Mid(Wscript.Scr iptFullName, 1, InStrRev(Wscrip t.ScriptFullNam e, "\"))
      sFileIn=sAppPat h & "iptocountry.cs v"
      sFileOut=sAppPa th & "CountryIPs.csv "

      call TrimJunk(sFileI n, sFileOut)

      sub TrimJunk(from_n ame, to_name)
      Dim sTemp, arTemp, LineNo
      Dim fFrom, fTo
      Dim fso
      set fso = CreateObject("S cripting.FileSy stemObject")
      Set fFrom = fso.OpenTextFil e(from_name, ForReading)
      Set fTo = fso.CreateTextF ile(to_name, True)

      fTo.WriteLine "StartIP" & vbtab & "EndIP" & vbTab & "CountryCod e"

      Do Until fFrom.AtEndOfSt ream
      sTemp = replace(fFrom.R eadLine,"""","" )
      LineNo=LineNo+1
      If sTemp <> empty and left(sTemp, 1) <> "#" Then
      ' write line to combined csv file
      arTemp=split(st emp,",")
      if ubound(arTemp) <> 6 then
      wscript.echo "Error parsing line (" & LineNo & ") too many commas."
      else
      if IsGoodCountry(a rTemp(4)) then
      fTo.WriteLine arTemp(0) & vbtab & _
      arTemp(1) & vbtab & _
      arTemp(4)
      end if
      end if
      End If
      Loop

      fFrom.close
      fTo.close
      End sub

      function IsGoodCountry(s CC)
      Dim arGoodCCs, iCC
      arGoodCCs=split (Countries2Keep ,",")
      for iCC = 0 to ubound(arGoodCC s)
      if arGoodCCs(iCC) = sCC then
      IsGoodCountry=T rue
      exit for
      end if
      next 'iCC
      end function[/CODE]

      Import the "CountryIPs.csv " output file created by the script into your database.

      Use this ASP page to test.

      [CODE=asp]

      <%
      'IPCheck.asp
      'ASP File (need to add your own DB connection string)

      Dim rs
      Dim cn
      'Create database connection object
      set cn = server.CreateOb ject("adodb.con nection")
      'Create recordset object
      set rs = server.CreateOb ject("adodb.rec ordset")
      'Open database connection
      cn.Open strCon 'use your DB connection string here

      call WritePage
      'End


      sub WritePage
      dim VisitorIP
      VisitorIP=Reque st.ServerVariab les("Remote_Add r")

      strSQL="Select StartIP, EndIP, CountryCode from CountryIPs.dbo. IPs WHERE " & IPToNum(Visitor IP) & " BETWEEN StartIP AND EndIP"

      rs.Open strSQL, cn, adOpenForwardOn ly, adLockReadOnly
      if rs.EOF then
      Response.Write "Access Denied, If you are in the United States please <a href=""mailto:W ebmaster@mydoma in.com"">Let us know</a> you are having this error."
      else
      Response.Write "Access Granted: " & VisitorIP & " is between " & Num2IP(rs("star tip")) & " and " & Num2IP(rs("endi p")) & " assigned to " & rs("CountryCode ")
      end if
      end sub



      'IPToNum() function - turns a textual IP address into a 32-bit number
      Function IPToNum(strIP)
      Dim numOctetsArray
      Dim i
      numOctetsArray = Split(strIP,"." )

      'sanity checks
      If UBound(numOctet sArray) <> 3 Then
      'oops = wrong number of octets
      IPToNum = -1
      Exit Function
      End If

      For i = 0 to 3
      If Not IsNumeric(numOc tetsArray(i)) Then
      'oops - not an IP address
      IPToNum = -2
      Exit Function
      End If

      If numOctetsArray( i) > 254 Then
      'oops - octet out of range
      IPToNum = -3
      Exit Function
      End If
      Next

      'now compile a number
      IPToNum = numOctetsArray( 0) * (2^24)
      IPToNum = IPToNum + numOctetsArray( 1) * (2^16)
      IPToNum = IPToNum + numOctetsArray( 2) * (2^8)
      IPToNum = IPToNum + numOctetsArray( 3)
      End Function

      Function Num2Ip(ByVal Num)
      'Presets the return of function
      Num2Ip = Null
      Num=clng(num)
      'Evaluates the parameter
      If Len(Num) = 0 Then Exit Function
      If Not IsNumeric(Num) Then Exit Function
      Num = CDbl(Num)
      If Num < 0 Or Num > 4294967295 Then Exit Function

      'Starts the calc
      Num = Num / 16777216
      Num2Ip = Fix(Num) & "."
      Num = ((Num - Fix(Num)) * 16777216) / 65536
      Num2Ip = Num2Ip & Fix(Num) & "."
      Num = ((Num - Fix(Num)) * 65536) / 256
      Num2Ip = Num2Ip & Fix(Num) & "."
      Num = (Num - Fix(Num)) * 256

      'Returns the sum
      Num2Ip = Num2Ip & Fix(Num)
      End Function

      %>[/CODE]

      You will want to update your database occasionally.

      Comment

      Working...