473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Blocking IP addresses

printedgoods
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
Mar 28 '08 #1
2 4014
jhardman
3,406 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. response.write request.serverVariables("remote_addr")
  2. response.write request.serverVariables("remote_host")
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:
Expand|Select|Wrap|Line Numbers
  1. if left(request.serverVariables("remote_addr"), 3) = "255" then
  2.    response,.write "Welcome to my site"
  3. else
  4.    response.write "see you later, bozo."
  5. end if
Apr 1 '08 #2
danp129
323 Expert 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. 'RemoveJunk.vbs
  2. Const Countries2Keep = "US,CA,AU"    'Country codes to allow access
  3. Const ForReading = 1
  4. Const ForWriting = 2
  5.  
  6. Dim sAppPath, sFileIn, sFileOut
  7. sAppPath = Mid(Wscript.ScriptFullName, 1, InStrRev(Wscript.ScriptFullName, "\"))
  8. sFileIn=sAppPath & "iptocountry.csv"
  9. sFileOut=sAppPath & "CountryIPs.csv"
  10.  
  11. call TrimJunk(sFileIn, sFileOut)
  12.  
  13. sub TrimJunk(from_name, to_name)
  14.     Dim sTemp, arTemp, LineNo
  15.     Dim fFrom, fTo
  16.     Dim fso
  17.     set fso = CreateObject("Scripting.FileSystemObject")
  18.     Set fFrom = fso.OpenTextFile(from_name, ForReading)
  19.     Set fTo = fso.CreateTextFile(to_name, True)
  20.  
  21.     fTo.WriteLine "StartIP" & vbtab & "EndIP" & vbTab & "CountryCode"
  22.  
  23.     Do Until fFrom.AtEndOfStream
  24.         sTemp = replace(fFrom.ReadLine,"""","")
  25.         LineNo=LineNo+1
  26.         If sTemp <> empty and left(sTemp, 1) <> "#" Then
  27.             ' write line to combined csv file
  28.             arTemp=split(stemp,",")
  29.             if ubound(arTemp) <> 6 then
  30.                 wscript.echo "Error parsing line (" & LineNo & ") too many commas."
  31.             else
  32.                 if IsGoodCountry(arTemp(4)) then
  33.                     fTo.WriteLine arTemp(0) & vbtab & _
  34.                                   arTemp(1) & vbtab & _
  35.                                   arTemp(4)
  36.                 end if
  37.             end if
  38.         End If
  39.     Loop
  40.  
  41.     fFrom.close
  42.     fTo.close
  43. End sub
  44.  
  45. function IsGoodCountry(sCC)
  46.     Dim arGoodCCs, iCC
  47.     arGoodCCs=split(Countries2Keep,",")
  48.     for iCC = 0 to ubound(arGoodCCs)
  49.         if arGoodCCs(iCC) = sCC then
  50.             IsGoodCountry=True
  51.             exit for
  52.         end if
  53.     next 'iCC
  54. end function
Import the "CountryIPs.csv" output file created by the script into your database.

Use this ASP page to test.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%
  3. 'IPCheck.asp
  4. 'ASP File (need to add your own DB connection string)
  5.  
  6. Dim rs
  7. Dim cn
  8. 'Create database connection object
  9. set cn = server.CreateObject("adodb.connection")
  10. 'Create recordset object
  11. set rs = server.CreateObject("adodb.recordset")
  12. 'Open database connection
  13. cn.Open strCon 'use your DB connection string here
  14.  
  15. call WritePage
  16. 'End
  17.  
  18.  
  19. sub WritePage
  20.     dim VisitorIP
  21.     VisitorIP=Request.ServerVariables("Remote_Addr")
  22.  
  23.     strSQL="Select StartIP, EndIP, CountryCode from CountryIPs.dbo.IPs WHERE " & IPToNum(VisitorIP) & " BETWEEN StartIP AND EndIP"
  24.  
  25.     rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly
  26.     if rs.EOF then
  27.         Response.Write "Access Denied, If you are in the United States please <a href=""mailto:Webmaster@mydomain.com"">Let us know</a> you are having this error."
  28.     else
  29.         Response.Write "Access Granted: " & VisitorIP & " is between " & Num2IP(rs("startip")) & " and " & Num2IP(rs("endip")) & " assigned to " & rs("CountryCode")
  30.     end if
  31. end sub
  32.  
  33.  
  34.  
  35. 'IPToNum() function - turns a textual IP address into a 32-bit number
  36. Function IPToNum(strIP)
  37.     Dim numOctetsArray
  38.     Dim i
  39.     numOctetsArray = Split(strIP,".")
  40.  
  41.     'sanity checks
  42.     If UBound(numOctetsArray) <> 3 Then
  43.         'oops = wrong number of octets
  44.         IPToNum = -1
  45.         Exit Function
  46.     End If
  47.  
  48.     For i = 0 to 3
  49.         If Not IsNumeric(numOctetsArray(i)) Then
  50.             'oops - not an IP address
  51.             IPToNum = -2
  52.             Exit Function
  53.         End If
  54.  
  55.         If numOctetsArray(i) > 254 Then
  56.             'oops - octet out of range
  57.             IPToNum = -3
  58.             Exit Function
  59.         End If
  60.     Next
  61.  
  62.     'now compile a number
  63.     IPToNum = numOctetsArray(0) * (2^24)
  64.     IPToNum = IPToNum + numOctetsArray(1) * (2^16)
  65.     IPToNum = IPToNum + numOctetsArray(2) * (2^8)
  66.     IPToNum = IPToNum + numOctetsArray(3)
  67. End Function
  68.  
  69. Function Num2Ip(ByVal Num)
  70.     'Presets the return of function
  71.     Num2Ip = Null
  72.     Num=clng(num)
  73.     'Evaluates the parameter
  74.     If Len(Num) = 0 Then Exit Function
  75.     If Not IsNumeric(Num) Then Exit Function
  76.     Num = CDbl(Num)
  77.     If Num < 0 Or Num > 4294967295 Then Exit Function
  78.  
  79.     'Starts the calc
  80.     Num = Num / 16777216
  81.     Num2Ip = Fix(Num) & "."
  82.     Num = ((Num - Fix(Num)) * 16777216) / 65536
  83.     Num2Ip = Num2Ip & Fix(Num) & "."
  84.     Num = ((Num - Fix(Num)) * 65536) / 256
  85.     Num2Ip = Num2Ip & Fix(Num) & "."
  86.     Num = (Num - Fix(Num)) * 256
  87.  
  88.     'Returns the sum
  89.     Num2Ip = Num2Ip & Fix(Num)
  90. End Function
  91.  
  92. %>
You will want to update your database occasionally.
Apr 3 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jtd | last post by:
Hi all, I'm using asyncore to download a large list of web pages, and I've noticed dispatcher.connect blocks for some hosts. I was under the impression that non-blocking sockets do not block on...
2
by: F Magnell | last post by:
How do I block referring sites in ASP using VBScript? Like blocking referrer's from: 192.025.215.001 and altavista.com
1
by: Hal | last post by:
I am experiencing blocking problems on SQL Server 2000, SP3a. I have read the posts and set up a job SQL agent to report on these occurences I save the results to a table before executing an sp to...
3
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers...
6
by: him | last post by:
All the world aside, Iran alone and on itself has blocked half of the internet, including many "moral" and useful websites, mainly because of its medieval policies. The level of filtering has...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
7
by: Michi Henning | last post by:
Hi, I'm using a non-blocking connect to connect to a server. Works fine -- the server gets and accepts the connection. However, once the connection is established, I cannot retrieve either the...
10
by: =?ISO-8859-1?Q?Fran=E7oise_Debat?= | last post by:
Hello, I wonder if anybody can help. I have an IP blocking script which displays a blank screen if an IP is detected from a list in an external file. The problem is, the script only reads the...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.