-----------------------------------------------------------------
-- Name: DNSBLCheckLuaV0.1
-- Author: Mark MacDonald
-- Required Host: spamblock.outblaze.com (205.158.62.116)
-- Version: 1.0.1
-- Date: 2009-07-29
-- Description:
-- Script takes an IP and searches spamblock.outblaze.com's
-- DNSBL to see if it is on one of the four lists
-- Maximum run time is 1m
-- Minimum run time is 4s
-- Needs host spamblock.outblaze.com (205.158.62.116)
-- Arguments:
-- 1) IP checking
-- Sites Checked
-- 1)Outblaze spamblock.outblaze.com (also includes Spamhaus)
-----------------------------------------------------------------

function OnConfigure()
--Setting Config
Config = LuaScriptConfigurator()
Config:SetAuthor("Mark MacDonald")
Config:SetDescription("Connects to outblaze and searches DNSBL for IP Address"
.. " Given. Returns False if not found, True if found, " ..
"and any errors that occur as well.")

Config:SetMinBuildVersion(0)
Config:SetScriptVersion(1,0.1)
Config:AddArgument("IP Address","IP address you are searching the lists for.",
LuaScriptConfigurator.CHECK_NOT_EMPTY)

Config:SetEntryPoint("main")
return Config
end

function fQuery(sIP)
--Pull Webpage then searches for pattern returns a table of booleans
--If any errors occur ExitStatus is set and function ends returning -1
tResults = {}

for i=1, 4 do
tResults[i] = false
end

socket = TLuaSocket()
sSource = ""
sSource2 = ""
sExit = ""
iTimerMax = 15
sCommand = "GET http://spamblock.outblaze.com/" ..sIP.. " HTTP/1.0\r\n\r\n"
iReadSize = 10240
sPattern = {}
sPattern[1] = "is not blocked by Outblaze"
sPattern[2] = "is on the Spamhaus Block List."
sPattern[3] = "Spamhaus Policy Block List"
sPattern[4] = "Spamhaus Exploits Block List"
sPattern[5] = "is on the Outblaze Spammers List"
sPattern[6] = "If you."
sPattern[7] = "400 Bad Request"
iTimer = 0
iRet = socket:OpenTCP(80)

--Reading from Webpage
if iRet==0 then

sExit = "Cannot open port 80. Error code:" .. GetLastError()
SetExitStatus(sExit, false)
tResults[1] = -1
return tResults

else

iRes = socket:Write(sCommand,string.len(sCommand))

if iRes == 0 then

sExit = "Cannot write to socket. Error code:" .. GetLastError()
SetExitStatus(sExit, false)
tResults[1] = -1
socket:Close()
return tResults

else

repeat

sSource = socket:Read(iReadSize)
Wait(2000)
iTimer = iTimer + 1
until sSource ~= nil or iTimer == iTimerMax

if iTimer == iTimerMax then

SetExitStatus("No response from spamblock.outblaze.com.", false)
tResults[1] = -1
socket:Close()
return tResults

end

repeat

repeat

sSource2 = socket:Read(iReadSize)
Wait(2000)
iTimer = iTimer + 1

until sSource2 ~= nil or iTimer == iTimerMax

if iTimer == (2*iTimerMax) then

SetExitStatus("No response from spamblock.outblaze.com.", false)
tResults[1] = -1
socket:Close()
return tResults

end

sSource = sSource .. sSource2

until sSource2 == ""

if nil == string.find(sSource, "</html>") and
nil == string.find(sSource, "</HTML>") then

SetExitStatus("Failed to obtain complete copy of html code.", false)
tResults[1] = -1
socket:Close()
return tResults

end

end

end

socket:Close()

--Possible Results from OutBlaze
if nil ~= string.find(sSource, sPattern[1]) then

return tResults

end
--Searching for pattern matches
for i=1, 4 do

if nil ~= string.find(sSource, sPattern[i+1]) then

tResults[i] = true

end

end
--Checking for query fails
if nil ~= string.find(sSource, sPattern[6]) or
nil ~= string.find(sSource, sPattern[7]) then

SetExitStatus("Query was given invalid IP.", false)
tResults[1] = -1

end

return tResults

end

function main()

sIPAddress = GetArgument(GetArgumentCount()-1)
tBlackListed = {}
strExit = sIPAddress .. " is on the following lists: "
tListNames = {}
tListNames[1] = "SBL"
tListNames[2] = "PBL"
tListNames[3] = "XBL"
tListNames[4] = "Outblaze Spammers List"

tBlackListed = fQuery(sIPAddress)

--Formats and returns results listing what lists IP is on
if tBlackListed[1] ~= -1 then

for i = 1, 4 do

if true == tBlackListed[i] then

strExit = strExit .. tListNames[i] .. ", "

end

end

if nil ~= string.find(string.sub(strExit,(string.len(strExit)-2),-1),":") then

strExit = sIPAddress .. " is not on a block list."
SetExitStatus(strExit, true)

else

strExit = string.sub(strExit, 0, -3)
SetExitStatus(strExit, false)

end

end

end --End Main