From dfdf8a5752e10f1cfad7b255237ee10a55e81a4c Mon Sep 17 00:00:00 2001 From: david Date: Sat, 6 Sep 2008 03:45:37 +0000 Subject: [PATCH] Move the new version of dns.reverse that does IPv6 reverse lookups out of ASN.nse and into the dns library. --- nselib/dns.lua | 29 ++++++++++++++++++++++++++--- scripts/ASN.nse | 45 +-------------------------------------------- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/nselib/dns.lua b/nselib/dns.lua index aba9c3a00..0eed5e523 100644 --- a/nselib/dns.lua +++ b/nselib/dns.lua @@ -3,6 +3,7 @@ module(... or "dns", package.seeall) -- simple DNS library -- packet creation, encoding, decoding, querying +require("ipOps") require("stdnse") get_servers = nmap.get_dns_servers @@ -224,15 +225,37 @@ end --- -- Formats IP for reverse lookup --@param ip IP address string ---@return "Domain" style representation of IP as subdomain of in-addr.arpa +--@return "Domain" style representation of IP as subdomain of in-addr.arpa or ip6.arpa function reverse(ip) + ip = ipOps.expand_ip(ip) if type(ip) ~= "string" then return nil end - local ipParts = stdnse.strsplit("%.", ip) + local delim = "%." + local arpa = ".in-addr.arpa" + if ip:match(":") then + delim = ":" + arpa = ".ip6.arpa" + end + local ipParts = stdnse.strsplit(delim, ip) + if #ipParts == 8 then + -- padding + local mask = "0000" + for i, part in ipairs(ipParts) do + ipParts[i] = mask:sub(1, string.len(mask) - string.len(part)) .. part + end + -- 32 parts from 8 + local temp = {} + for i, hdt in ipairs(ipParts) do + for part in hdt:gmatch("%x") do + temp[#temp+1] = part + end + end + ipParts = temp + end local ipReverse = {} for i = #ipParts, 1, -1 do table.insert(ipReverse, ipParts[i]) end - return table.concat(ipReverse, ".") .. ".in-addr.arpa" + return table.concat(ipReverse, ".") .. arpa end --- diff --git a/scripts/ASN.nse b/scripts/ASN.nse index 9c6d51434..7efb0c460 100644 --- a/scripts/ASN.nse +++ b/scripts/ASN.nse @@ -100,7 +100,7 @@ action = function( host ) end -- name to query for - local dname = reverse( host.ip ) + local dname = dns.reverse( host.ip ) -- perform queries for each applicable zone for asn_type, zone in pairs( cymru[IPv] ) do @@ -281,49 +281,6 @@ end --- *** UTILITY FUNCTIONS *** --- remove when these functions are available in libraries - - ---- --- Formats IP for reverse lookup. --- @param ip String IP address. --- @return "Domain" style representation of IP as subdomain of in-addr.arpa or ip6.arpa - -function reverse(ip) - ip = ipOps.expand_ip(ip) - if type(ip) ~= "string" then return nil end - local delim = "%." - local arpa = ".in-addr.arpa" - if ip:match(":") then - delim = ":" - arpa = ".ip6.arpa" - end - local ipParts = stdnse.strsplit(delim, ip) - if #ipParts == 8 then - -- padding - local mask = "0000" - for i, part in ipairs(ipParts) do - ipParts[i] = mask:sub(1, string.len(mask) - string.len(part)) .. part - end - -- 32 parts from 8 - local temp = {} - for i, hdt in ipairs(ipParts) do - for part in hdt:gmatch("%x") do - temp[#temp+1] = part - end - end - ipParts = temp - end - local ipReverse = {} - for i = #ipParts, 1, -1 do - table.insert(ipReverse, ipParts[i]) - end - return table.concat(ipReverse, ".") .. arpa -end - - - --- -- Calculates the prefix length for the given IP address range. -- @param range String representing an IP address range