1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-28 02:19:04 +00:00

Final re-indent for scripts.

This commit is contained in:
dmiller
2014-02-02 15:33:39 +00:00
parent d309fecd12
commit 31a2c432e1
43 changed files with 10426 additions and 10426 deletions

View File

@@ -33,48 +33,48 @@ categories = {"discovery","external","safe"}
hostrule = function(host)
local is_private, err = ipOps.isPrivate( host.ip )
if is_private == nil then
stdnse.print_debug( "%s Error in Hostrule: %s.", SCRIPT_NAME, err )
return false
end
return not is_private
local is_private, err = ipOps.isPrivate( host.ip )
if is_private == nil then
stdnse.print_debug( "%s Error in Hostrule: %s.", SCRIPT_NAME, err )
return false
end
return not is_private
end
local MaxmindDef = {
-- Database structure constants
COUNTRY_BEGIN = 16776960,
STATE_BEGIN_REV0 = 16700000,
STATE_BEGIN_REV1 = 16000000,
-- Database structure constants
COUNTRY_BEGIN = 16776960,
STATE_BEGIN_REV0 = 16700000,
STATE_BEGIN_REV1 = 16000000,
STRUCTURE_INFO_MAX_SIZE = 20,
DATABASE_INFO_MAX_SIZE = 100,
STRUCTURE_INFO_MAX_SIZE = 20,
DATABASE_INFO_MAX_SIZE = 100,
-- Database editions,
COUNTRY_EDITION = 1,
REGION_EDITION_REV0 = 7,
REGION_EDITION_REV1 = 3,
CITY_EDITION_REV0 = 6,
CITY_EDITION_REV1 = 2,
ORG_EDITION = 5,
ISP_EDITION = 4,
PROXY_EDITION = 8,
ASNUM_EDITION = 9,
NETSPEED_EDITION = 11,
COUNTRY_EDITION_V6 = 12,
-- Database editions,
COUNTRY_EDITION = 1,
REGION_EDITION_REV0 = 7,
REGION_EDITION_REV1 = 3,
CITY_EDITION_REV0 = 6,
CITY_EDITION_REV1 = 2,
ORG_EDITION = 5,
ISP_EDITION = 4,
PROXY_EDITION = 8,
ASNUM_EDITION = 9,
NETSPEED_EDITION = 11,
COUNTRY_EDITION_V6 = 12,
SEGMENT_RECORD_LENGTH = 3,
STANDARD_RECORD_LENGTH = 3,
ORG_RECORD_LENGTH = 4,
MAX_RECORD_LENGTH = 4,
MAX_ORG_RECORD_LENGTH = 300,
FULL_RECORD_LENGTH = 50,
SEGMENT_RECORD_LENGTH = 3,
STANDARD_RECORD_LENGTH = 3,
ORG_RECORD_LENGTH = 4,
MAX_RECORD_LENGTH = 4,
MAX_ORG_RECORD_LENGTH = 300,
FULL_RECORD_LENGTH = 50,
US_OFFSET = 1,
CANADA_OFFSET = 677,
WORLD_OFFSET = 1353,
FIPS_RANGE = 360,
DMA_MAP = {
US_OFFSET = 1,
CANADA_OFFSET = 677,
WORLD_OFFSET = 1353,
FIPS_RANGE = 360,
DMA_MAP = {
[500] = 'Portland-Auburn, ME',
[501] = 'New York, NY',
[502] = 'Binghamton, NY',
@@ -287,8 +287,8 @@ local MaxmindDef = {
[866] = 'Fresno, CA',
[868] = 'Chico-Redding, CA',
[881] = 'Spokane, WA'
},
COUNTRY_CODES = {
},
COUNTRY_CODES = {
'', 'AP', 'EU', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ',
'AR', 'AS', 'AT', 'AU', 'AW', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH',
'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA',
@@ -309,8 +309,8 @@ local MaxmindDef = {
'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN',
'VU', 'WF', 'WS', 'YE', 'YT', 'RS', 'ZA', 'ZM', 'ME', 'ZW', 'A1', 'A2', 'O1',
'AX', 'GG', 'IM', 'JE', 'BL', 'MF'
},
COUNTRY_CODES3 = {
},
COUNTRY_CODES3 = {
'','AP','EU','AND','ARE','AFG','ATG','AIA','ALB','ARM','ANT','AGO','AQ','ARG',
'ASM','AUT','AUS','ABW','AZE','BIH','BRB','BGD','BEL','BFA','BGR','BHR','BDI',
'BEN','BMU','BRN','BOL','BRA','BHS','BTN','BV','BWA','BLR','BLZ','CAN','CC',
@@ -331,8 +331,8 @@ local MaxmindDef = {
'UKR','UGA','UM','USA','URY','UZB','VAT','VCT','VEN','VGB','VIR','VNM','VUT',
'WLF','WSM','YEM','YT','SRB','ZAF','ZMB','MNE','ZWE','A1','A2','O1',
'ALA','GGY','IMN','JEY','BLM','MAF'
},
COUNTRY_NAMES = {
},
COUNTRY_NAMES = {
"", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates",
"Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
"Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa",
@@ -386,226 +386,226 @@ local MaxmindDef = {
"Serbia", "South Africa", "Zambia", "Montenegro", "Zimbabwe",
"Anonymous Proxy","Satellite Provider","Other",
"Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin"
}
}
}
local ip2long=function(ip_str)
local ip = stdnse.strsplit('%.',ip_str)
local ip_num = (tonumber(ip[1])*16777216 + tonumber(ip[2])*65536
+ tonumber(ip[3])*256 + tonumber(ip[4]))
return ip_num
local ip = stdnse.strsplit('%.',ip_str)
local ip_num = (tonumber(ip[1])*16777216 + tonumber(ip[2])*65536
+ tonumber(ip[3])*256 + tonumber(ip[4]))
return ip_num
end
local GeoIP = {
new = function(self, filename)
local o = {}
setmetatable(o, self)
self.__index = self
o._filename=filename
local err
o._filehandle= assert(io.open(filename,'rb'))
o._databaseType = MaxmindDef.COUNTRY_EDITION
o._recordLength = MaxmindDef.STANDARD_RECORD_LENGTH
new = function(self, filename)
local o = {}
setmetatable(o, self)
self.__index = self
o._filename=filename
local err
o._filehandle= assert(io.open(filename,'rb'))
o._databaseType = MaxmindDef.COUNTRY_EDITION
o._recordLength = MaxmindDef.STANDARD_RECORD_LENGTH
local filepos = o._filehandle:seek()
o._filehandle:seek("end",-3)
local filepos = o._filehandle:seek()
o._filehandle:seek("end",-3)
for i=1,MaxmindDef.STRUCTURE_INFO_MAX_SIZE do
local delim = o._filehandle:read(3)
for i=1,MaxmindDef.STRUCTURE_INFO_MAX_SIZE do
local delim = o._filehandle:read(3)
if delim == '\255\255\255' then
o._databaseType = o._filehandle:read(1):byte()
-- backward compatibility with databases from April 2003 and earlier
if (o._databaseType >= 106) then
o._databaseType = o._databaseType - 105
end
if delim == '\255\255\255' then
o._databaseType = o._filehandle:read(1):byte()
-- backward compatibility with databases from April 2003 and earlier
if (o._databaseType >= 106) then
o._databaseType = o._databaseType - 105
end
local fast_combo1={[MaxmindDef.CITY_EDITION_REV0]=true,
[MaxmindDef.CITY_EDITION_REV1]=true,
[MaxmindDef.ORG_EDITION]=true,
[MaxmindDef.ISP_EDITION]=true,
[MaxmindDef.ASNUM_EDITION]=true}
local fast_combo1={[MaxmindDef.CITY_EDITION_REV0]=true,
[MaxmindDef.CITY_EDITION_REV1]=true,
[MaxmindDef.ORG_EDITION]=true,
[MaxmindDef.ISP_EDITION]=true,
[MaxmindDef.ASNUM_EDITION]=true}
if o._databaseType == MaxmindDef.REGION_EDITION_REV0 then
o._databaseSegments = MaxmindDef.STATE_BEGIN_REV0
elseif o._databaseType == MaxmindDef.REGION_EDITION_REV1 then
o._databaseSegments = MaxmindDef.STATE_BEGIN_REV1
elseif fast_combo1[o._databaseType] then
o._databaseSegments = 0
local buf = o._filehandle:read(MaxmindDef.SEGMENT_RECORD_LENGTH)
if o._databaseType == MaxmindDef.REGION_EDITION_REV0 then
o._databaseSegments = MaxmindDef.STATE_BEGIN_REV0
elseif o._databaseType == MaxmindDef.REGION_EDITION_REV1 then
o._databaseSegments = MaxmindDef.STATE_BEGIN_REV1
elseif fast_combo1[o._databaseType] then
o._databaseSegments = 0
local buf = o._filehandle:read(MaxmindDef.SEGMENT_RECORD_LENGTH)
-- the original representation in the MaxMind API is ANSI C integer
-- which should not overflow the greatest value Lua can offer ;)
for j=0,(MaxmindDef.SEGMENT_RECORD_LENGTH-1) do
o._databaseSegments = o._databaseSegments + bit.lshift( buf:byte(j+1), j*8)
end
-- the original representation in the MaxMind API is ANSI C integer
-- which should not overflow the greatest value Lua can offer ;)
for j=0,(MaxmindDef.SEGMENT_RECORD_LENGTH-1) do
o._databaseSegments = o._databaseSegments + bit.lshift( buf:byte(j+1), j*8)
end
if o._databaseType == MaxmindDef.ORG_EDITION or o._databaseType == MaxmindDef.ISP_EDITION then
o._recordLength = MaxmindDef.ORG_RECORD_LENGTH
end
end
break
else
o._filehandle:seek("cur",-4)
end
end
if o._databaseType == MaxmindDef.ORG_EDITION or o._databaseType == MaxmindDef.ISP_EDITION then
o._recordLength = MaxmindDef.ORG_RECORD_LENGTH
end
end
break
else
o._filehandle:seek("cur",-4)
end
end
if o._databaseType == MaxmindDef.COUNTRY_EDITION then
o._databaseSegments = MaxmindDef.COUNTRY_BEGIN
end
o._filehandle:seek("set",filepos)
if o._databaseType == MaxmindDef.COUNTRY_EDITION then
o._databaseSegments = MaxmindDef.COUNTRY_BEGIN
end
o._filehandle:seek("set",filepos)
return o
end,
return o
end,
output_record_by_addr = function(self,addr)
local loc = self:record_by_addr(addr)
if not loc then return nil end
output_record_by_addr = function(self,addr)
local loc = self:record_by_addr(addr)
if not loc then return nil end
local output = {}
--output.name = "Maxmind database"
table.insert(output, "coordinates (lat,lon): " .. loc.latitude .. "," .. loc.longitude)
local output = {}
--output.name = "Maxmind database"
table.insert(output, "coordinates (lat,lon): " .. loc.latitude .. "," .. loc.longitude)
local str = ""
if loc.city then
str = str.."city: "..loc.city
end
if loc.metro_code then
str = str .. ", "..loc.metro_code
end
if loc.country_name then
str = str .. ", "..loc.country_name
end
table.insert(output,str)
local str = ""
if loc.city then
str = str.."city: "..loc.city
end
if loc.metro_code then
str = str .. ", "..loc.metro_code
end
if loc.country_name then
str = str .. ", "..loc.country_name
end
table.insert(output,str)
return output
end,
return output
end,
record_by_addr=function(self,addr)
local ipnum = ip2long(addr)
return self:_get_record(ipnum)
end,
record_by_addr=function(self,addr)
local ipnum = ip2long(addr)
return self:_get_record(ipnum)
end,
_get_record=function(self,ipnum)
local seek_country = self:_seek_country(ipnum)
if seek_country == self._databaseSegments then
return nil
end
local record_pointer = seek_country + (2 * self._recordLength - 1) * self._databaseSegments
_get_record=function(self,ipnum)
local seek_country = self:_seek_country(ipnum)
if seek_country == self._databaseSegments then
return nil
end
local record_pointer = seek_country + (2 * self._recordLength - 1) * self._databaseSegments
self._filehandle:seek("set",record_pointer)
local record_buf = self._filehandle:read(MaxmindDef.FULL_RECORD_LENGTH)
self._filehandle:seek("set",record_pointer)
local record_buf = self._filehandle:read(MaxmindDef.FULL_RECORD_LENGTH)
local record = {}
local start_pos = 1
local char = record_buf:byte(start_pos)
char=char+1
record.country_code = MaxmindDef.COUNTRY_CODES[char]
record.country_code3 = MaxmindDef.COUNTRY_CODES3[char]
record.country_name = MaxmindDef.COUNTRY_NAMES[char]
start_pos = start_pos + 1
local end_pos = 0
local record = {}
local start_pos = 1
local char = record_buf:byte(start_pos)
char=char+1
record.country_code = MaxmindDef.COUNTRY_CODES[char]
record.country_code3 = MaxmindDef.COUNTRY_CODES3[char]
record.country_name = MaxmindDef.COUNTRY_NAMES[char]
start_pos = start_pos + 1
local end_pos = 0
end_pos = record_buf:find("\0",start_pos)
if start_pos ~= end_pos then
record.region_name = record_buf:sub(start_pos, end_pos-1)
end
start_pos = end_pos + 1
end_pos = record_buf:find("\0",start_pos)
if start_pos ~= end_pos then
record.region_name = record_buf:sub(start_pos, end_pos-1)
end
start_pos = end_pos + 1
end_pos = record_buf:find("\0",start_pos)
if start_pos ~= end_pos then
record.city = record_buf:sub(start_pos, end_pos-1)
end
start_pos = end_pos + 1
end_pos = record_buf:find("\0",start_pos)
if start_pos ~= end_pos then
record.city = record_buf:sub(start_pos, end_pos-1)
end
start_pos = end_pos + 1
end_pos = record_buf:find("\0",start_pos)
if start_pos ~= end_pos then
record.postal_code = record_buf:sub(start_pos, end_pos-1)
end
start_pos = end_pos + 1
end_pos = record_buf:find("\0",start_pos)
if start_pos ~= end_pos then
record.postal_code = record_buf:sub(start_pos, end_pos-1)
end
start_pos = end_pos + 1
local c1,c2,c3=record_buf:byte(start_pos,start_pos+3)
record.latitude = (( bit.lshift(c1,0*8) + bit.lshift(c2,1*8) + bit.lshift(c3,2*8) )/10000) - 180
start_pos = start_pos +3
local c1,c2,c3=record_buf:byte(start_pos,start_pos+3)
record.latitude = (( bit.lshift(c1,0*8) + bit.lshift(c2,1*8) + bit.lshift(c3,2*8) )/10000) - 180
start_pos = start_pos +3
c1,c2,c3=record_buf:byte(start_pos,start_pos+3)
record.longitude = (( bit.lshift(c1,0*8) + bit.lshift(c2,1*8) + bit.lshift(c3,2*8) )/10000) - 180
start_pos = start_pos +3
c1,c2,c3=record_buf:byte(start_pos,start_pos+3)
record.longitude = (( bit.lshift(c1,0*8) + bit.lshift(c2,1*8) + bit.lshift(c3,2*8) )/10000) - 180
start_pos = start_pos +3
if self._databaseType == MaxmindDef.CITY_EDITION_REV1 and record.country_code=='US' then
c1,c2,c3=record_buf:byte(start_pos,start_pos+3)
local dmaarea_combo= bit.lshift(c1,0*8) + bit.lshift(c2,1*8) + bit.lshift(c3,2*8)
record.dma_code = math.floor(dmaarea_combo/1000)
record.area_code = dmaarea_combo % 1000
else
record.dma_code = nil
record.area_code = nil
end
if self._databaseType == MaxmindDef.CITY_EDITION_REV1 and record.country_code=='US' then
c1,c2,c3=record_buf:byte(start_pos,start_pos+3)
local dmaarea_combo= bit.lshift(c1,0*8) + bit.lshift(c2,1*8) + bit.lshift(c3,2*8)
record.dma_code = math.floor(dmaarea_combo/1000)
record.area_code = dmaarea_combo % 1000
else
record.dma_code = nil
record.area_code = nil
end
if record.dma_code and MaxmindDef.DMA_MAP[record.dma_code] then
record.metro_code = MaxmindDef.DMA_MAP[record.dma_code]
else
record.metro_code = nil
end
if record.dma_code and MaxmindDef.DMA_MAP[record.dma_code] then
record.metro_code = MaxmindDef.DMA_MAP[record.dma_code]
else
record.metro_code = nil
end
return record
end,
return record
end,
_seek_country=function(self,ipnum)
local offset = 0
for depth=31,0,-1 do
self._filehandle:seek("set", 2 * self._recordLength * offset)
local buf = self._filehandle:read(2*self._recordLength)
_seek_country=function(self,ipnum)
local offset = 0
for depth=31,0,-1 do
self._filehandle:seek("set", 2 * self._recordLength * offset)
local buf = self._filehandle:read(2*self._recordLength)
local x = {}
x[0],x[1] = 0,0
local x = {}
x[0],x[1] = 0,0
for i=0,1 do
for j=0,(self._recordLength-1) do
x[i] = x[i] + bit.lshift(buf:byte((self._recordLength * i + j) +1 ), j*8)
end
end
-- Gotta test this out thorougly because of the ipnum
if bit.band(ipnum, bit.lshift(1,depth)) ~= 0 then
if x[1] >= self._databaseSegments then
return x[1]
end
offset = x[1]
else
if x[0] >= self._databaseSegments then
return x[0]
end
offset = x[0]
end
end
stdnse.print_debug('Error traversing database - perhaps it is corrupt?')
return nil
end,
for i=0,1 do
for j=0,(self._recordLength-1) do
x[i] = x[i] + bit.lshift(buf:byte((self._recordLength * i + j) +1 ), j*8)
end
end
-- Gotta test this out thorougly because of the ipnum
if bit.band(ipnum, bit.lshift(1,depth)) ~= 0 then
if x[1] >= self._databaseSegments then
return x[1]
end
offset = x[1]
else
if x[0] >= self._databaseSegments then
return x[0]
end
offset = x[0]
end
end
stdnse.print_debug('Error traversing database - perhaps it is corrupt?')
return nil
end,
}
action = function(host,port)
local f_maxmind = stdnse.get_script_args(SCRIPT_NAME .. ".maxmind_db")
local f_maxmind = stdnse.get_script_args(SCRIPT_NAME .. ".maxmind_db")
local output = {}
local output = {}
--if f_maxmind is a string, it should specify the filename of the database
if f_maxmind then
local gi = assert( GeoIP:new(f_maxmind), "Wrong file specified for a Maxmind database")
local out = gi:output_record_by_addr(host.ip)
output = out
else
local gi = assert( GeoIP:new(nmap.fetchfile("nselib/data/GeoLiteCity.dat")), "Cannot read Maxmind database file in 'nselib/data/'.")
local out = gi:output_record_by_addr(host.ip)
output = out
end
--if f_maxmind is a string, it should specify the filename of the database
if f_maxmind then
local gi = assert( GeoIP:new(f_maxmind), "Wrong file specified for a Maxmind database")
local out = gi:output_record_by_addr(host.ip)
output = out
else
local gi = assert( GeoIP:new(nmap.fetchfile("nselib/data/GeoLiteCity.dat")), "Cannot read Maxmind database file in 'nselib/data/'.")
local out = gi:output_record_by_addr(host.ip)
output = out
end
if(#output~=0) then
output.name = host.ip
if host.targetname then
output.name = output.name.." ("..host.targetname..")"
end
end
if(#output~=0) then
output.name = host.ip
if host.targetname then
output.name = output.name.." ("..host.targetname..")"
end
end
return stdnse.format_output(true,output)
return stdnse.format_output(true,output)
end