Blocking IP addresses
Question posted by: printedgoods
(Newbie)
on
March 28th, 2008 04:21 PM
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
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
April 1st, 2008 11:01 PM
# 2
|
Re: Blocking IP addresses
Code: ( text )
response.write request.serverVariables("remote_addr") 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:
Code: ( text )
if left(request.serverVariables("remote_addr"), 3) = "255" then response,.write "Welcome to my site" else response.write "see you later, bozo." end if
|
|
April 3rd, 2008 08:42 PM
# 3
|
Re: Blocking IP addresses
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: ( text )
'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.ScriptFullName, 1, InStrRev(Wscript.ScriptFullName, "\")) sFileIn=sAppPath & "iptocountry.csv" sFileOut=sAppPath & "CountryIPs.csv" call TrimJunk(sFileIn, sFileOut) sub TrimJunk(from_name, to_name) Dim sTemp, arTemp, LineNo Dim fFrom, fTo Dim fso set fso = CreateObject("Scripting.FileSystemObject") Set fFrom = fso.OpenTextFile(from_name, ForReading) Set fTo = fso.CreateTextFile(to_name, True) fTo.WriteLine "StartIP" & vbtab & "EndIP" & vbTab & "CountryCode" Do Until fFrom.AtEndOfStream sTemp = replace(fFrom.ReadLine,"""","") LineNo=LineNo+1 If sTemp <> empty and left(sTemp, 1) <> "#" Then ' write line to combined csv file arTemp=split(stemp,",") if ubound(arTemp) <> 6 then wscript.echo "Error parsing line (" & LineNo & ") too many commas." else if IsGoodCountry(arTemp(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(sCC) Dim arGoodCCs, iCC arGoodCCs=split(Countries2Keep,",") for iCC = 0 to ubound(arGoodCCs) if arGoodCCs(iCC) = sCC then IsGoodCountry=True exit for end if next 'iCC end function
Import the "CountryIPs.csv" output file created by the script into your database.
Use this ASP page to test.
Code: ( text )
<% 'IPCheck.asp 'ASP File (need to add your own DB connection string) Dim rs Dim cn 'Create database connection object set cn = server.CreateObject("adodb.connection") 'Create recordset object set rs = server.CreateObject("adodb.recordset") 'Open database connection cn.Open strCon 'use your DB connection string here call WritePage 'End sub WritePage dim VisitorIP VisitorIP=Request.ServerVariables("Remote_Addr") strSQL="Select StartIP, EndIP, CountryCode from CountryIPs.dbo.IPs WHERE " & IPToNum(VisitorIP) & " BETWEEN StartIP AND EndIP" rs.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly if rs.EOF then 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." else Response.Write "Access Granted: " & VisitorIP & " is between " & Num2IP(rs("startip")) & " and " & Num2IP(rs("endip")) & " 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(numOctetsArray) <> 3 Then 'oops = wrong number of octets IPToNum = -1 Exit Function End If For i = 0 to 3 If Not IsNumeric(numOctetsArray(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 %>
You will want to update your database occasionally.
 |
Not the answer you were looking for? Post your question . . .
173,515 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Top ASP Forum Contributors
|