10:229
7 3ˆÑ�þ]iénQ8ëq‘â¼@?Ð
„ï2ïÏa–äY>” ”¾R( Å ‚vã!S�ÿÎ ?÷ 3---
-- Utility functions for manipulating and comparing IP addresses.
--
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
local math = require "math"
local stdnse = require "stdnse"
local string = require "string"
local stringaux = require "stringaux"
local table = require "table"
local type = type
local ipairs = ipairs
local tonumber = tonumber
local unittest = require "unittest"
_ENV = stdnse.module("ipOps", stdnse.seeall)
local pack = string.pack
local unpack = string.unpack
---
-- Checks to see if the supplied IP address is part of a non-routable
-- address space.
--
-- The non-Internet-routable address spaces known to this function are
-- * IPv4 Loopback (RFC3330)
-- * IPv4 Private Use (RFC1918)
-- * IPv4 Link Local (RFC3330)
-- * IPv4 IETF Protocol Assignments (RFC 5736)
-- * IPv4 TEST-NET-1, TEST-NET-2, TEST-NET-3 (RFC 5737)
-- * IPv4 Network Interconnect Device Benchmark Testing (RFC 2544)
-- * IPv4 Reserved for Future Use (RFC 1112, Section 4)
-- * IPv4 Multicast Local Network Control Block (RFC 3171, Section 3)
-- * IPv6 Unspecified and Loopback (RFC3513)
-- * IPv6 Site-Local (RFC3513, deprecated in RFC3879)
-- * IPv6 Unique Local Unicast (RFC4193)
-- * IPv6 Link Local Unicast (RFC4291)
-- @param ip String representing an IPv4 or IPv6 address. Shortened notation
-- is permitted.
-- @usage
-- local is_private = ipOps.isPrivate( "192.168.1.1" )
-- @return True or false (or nil in case of an error).
-- @return String error message in case of an error or
-- String non-routable address containing the supplied IP address.
isPrivate = function( ip )
local err
ip, err = expand_ip( ip )
if err then return nil, err end
if ip:match( ":" ) then
local is_private
local ipv6_private = { "::/127", "FC00::/7", "FE80::/10", "FEC0::/10" }
for _, range in ipairs( ipv6_private ) do
is_private, err = ip_in_range( ip, range )
if is_private == true then
return true, range
end
if err then
return nil, err
end
end
elseif ip:sub(1,3) == '10.' then
return true, '10/8'
elseif ip:sub(1,4) == '127.' then
return true, '127/8'
elseif ip:sub(1,8) == '169.254.' then
return true, '169.254/16'
elseif ip:sub(1,4) == '172.' then
local p, e = ip_in_range(ip, '172.16/12')
if p == true then
return true, '172.16/12'
else
return p, e
end
elseif ip:sub(1,4) == '192.' then
if ip:sub(5,8) == '168.' then
return true, '192.168/16'
elseif ip:match('^192%.[0][0]?[0]?%.[0][0]?[0]?%.') then
return true, '192.0.0/24'
elseif ip:match('^192%.[0][0]?[0]?%.[0]?[0]?2') then
return true, '192.0.2/24'
end
elseif ip:sub(1,4) == '198.' then
if ip:match('^198%.[0]?18%.') or ip:match('^198%.[0]?19%.') then
return true, '198.18/15'
elseif ip:match('^198%.[0]?51%.100%.') then
return true, '198.51.100/24'
end
elseif ip:match('^203%.[0][0]?[0]?%.113%.') then
return true, '203.0.113/24'
elseif ip:match('^224%.[0][0]?[0]?%.[0][0]?[0]?%.') then
return true, '224.0.0/24'
elseif ip:match('^24[0-9]%.') or ip:match('^25[0-5]%.') then
return true, '240.0.0/4'
end
return false, nil
end
---
-- Converts the supplied IPv4 address into a DWORD value.
--
-- For example, the address a.b.c.d becomes (((a*256+b)*256+c)*256+d).
--
-- Note: IPv6 addresses are not supported. Currently, numbers in NSE are
-- limited to 10^14, and consequently not all IPv6 addresses can be
-- represented. Consider using ip_to_str for IPv6 addresses.
-- @param ip String representing an IPv4 address. Shortened notation is
-- permitted.
-- @usage
-- local dword = ipOps.todword( "73.150.2.210" )
-- @return Number corresponding to the supplied IP address (or nil
-- in case of an error).
-- @return String error message in case of an error.
todword = function( ip )
if type( ip ) ~= "string" or ip:match( ":" ) then
return nil, "Error