diff --git a/nselib/packet.lua b/nselib/packet.lua index a5a03c649..27b37c926 100644 --- a/nselib/packet.lua +++ b/nselib/packet.lua @@ -5,6 +5,7 @@ -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html local bit = require "bit" +local ipOps = require "ipOps" local nmap = require "nmap" local stdnse = require "stdnse" local string = require "string" @@ -480,82 +481,6 @@ end -- Helpers ---- Convert a dotted-quad IP address string (like "1.2.3.4") to a --- raw string four bytes long. --- @param str IP address string. --- @return Four-byte string. -function iptobin(str) - local ret = "" - for c in string.gmatch(str, "[0-9]+") do - ret = ret .. string.char(c+0) -- automatic conversion to int - end - return ret -end ---- Convert an IPv6 address string (like "fe80:21::1") to a raw --- string 16 bytes long (big-endian). --- @param str IPv6 address string. --- @return 16-byte string. -function ip6tobin(str) - if not str then - return nil - end - -- Handle IPv4-compatible IPv6 address. - local ipv6_size = 8 -- An IPv6 address is 8*16bits long. But for IPv4-compatible address, the IPv6-style part is 6*16bits long. - local ip4_bin = "" - local dot_count = stdnse.strsplit("%.", str) - if #dot_count == 4 then -- It might be IPv4-compatible IPv6 address. - local ip64 = stdnse.strsplit(":", str) - local ip4_str = ip64[#ip64] -- Get the embedded IPv4 address string. - ip4_bin = iptobin(ip4_str) - if not ip4_bin then - return nil - end - ipv6_size = 6 - str = string.sub(str, 1, -#ip4_str-1) - elseif #dot_count ~= 1 then - return nil - end - -- Handle the left IPv6-style part. - local sides = stdnse.strsplit("::", str) - if #sides > 2 then - return nil - end - local head = stdnse.strsplit(":", sides[1]) - if #sides > 1 then - local tail = stdnse.strsplit(":", sides[2]) - if tail[#tail] == "" then - table.remove(tail, #tail) - end - local missing = ipv6_size - #head - #tail - while missing > 0 do - table.insert(head, "0") - missing = missing - 1 - end - for _, e in ipairs(tail) do - table.insert(head, e) - end - end - if #head ~= ipv6_size then - return nil - end - -- Transfer the 16-bit units to raw string. - local unit16 - local addr_hex = "" - for _, unit16 in ipairs(head) do - local h8 = string.sub(unit16,-4,-3) - local l8 = string.sub(unit16,-2,-1) - local unit8 - for _,unit8 in pairs({h8,l8}) do - if (unit8 == "") then - addr_hex = addr_hex .. string.char(0x00) - else - addr_hex = addr_hex .. string.char("0x"..unit8) - end - end - end - - return addr_hex .. ip4_bin -end --- Convert a MAC address string (like "00:23:ae:5d:3b:10") to -- a raw six-byte long. -- @param str MAC address string. @@ -564,37 +489,11 @@ function mactobin(str) if not str then return nil, "MAC was not specified." end - local unit8 - local addr_hex = "" - for unit8 in string.gmatch(str,"%x+") do - addr_hex = addr_hex .. string.char("0x"..unit8) - end - return addr_hex + return (str:gsub("(%x%x)[^%x]?", function (x) + return string.char(tonumber(x, 16)) + end)) end ---- Convert a four-byte raw string to a dotted-quad IP address string. --- @param raw_ip_addr Four-byte string. --- @return IP address string. -function toip(raw_ip_addr) - if not raw_ip_addr then - return "?.?.?.?" - end - return string.format("%i.%i.%i.%i", string.byte(raw_ip_addr,1,4)) -end ---- Convert a 16-byte raw string to an IPv6 address string. --- @param raw_ipv6_addr 16-byte string. --- @return IPv6 address string. -function toipv6(raw_ipv6_addr) - local long_addr_str - local status, addrs - if not raw_ipv6_addr then - return nil, "IPv6 address was not specified." - end - long_addr_str = stdnse.tohex(raw_ipv6_addr, {separator=":", group=4}) - status, addrs = nmap.resolve(long_addr_str, "inet6") - - return (status and addrs[1]) or long_addr_str -end --- Generate the link-local IPv6 address from the MAC address. -- @param mac MAC address string. -- @return Link-local IPv6 address string. @@ -603,7 +502,7 @@ function mac_to_lladdr(mac) return nil, "MAC was not specified." end local interfier = string.char(bit.bor(string.byte(mac,1),0x02))..string.sub(mac,2,3)..string.char(0xff,0xfe)..string.sub(mac,4,6) - local ll_prefix = ip6tobin("fe80::") + local ll_prefix = ipOps.ip_to_str("fe80::") return string.sub(ll_prefix,1,8)..interfier end --- Get an 8-bit integer at a 0-based byte offset in the packet. @@ -690,8 +589,8 @@ function Packet:ip_parse(force_continue) self.ip_sum = self:u16(self.ip_offset + 10) self.ip_bin_src = self:raw(self.ip_offset + 12,4) -- raw 4-bytes string self.ip_bin_dst = self:raw(self.ip_offset + 16,4) - self.ip_src = toip(self.ip_bin_src) -- formatted string - self.ip_dst = toip(self.ip_bin_dst) + self.ip_src = ipOps.str_to_ip(self.ip_bin_src) -- formatted string + self.ip_dst = ipOps.str_to_ip(self.ip_bin_dst) self.ip_opt_offset = self.ip_offset + 20 self.ip_options = self:parse_options(self.ip_opt_offset, ((self.ip_hl*4)-20)) self.ip_data_offset = self.ip_offset + self.ip_hl*4 @@ -717,8 +616,8 @@ function Packet:ip6_parse(force_continue) self.ip6_hlimt = self:u8(self.ip6_offset + 7) self.ip_bin_src = self:raw(self.ip6_offset + 8, 16) self.ip_bin_dst = self:raw(self.ip6_offset + 24, 16) - self.ip_src = toipv6(self.ip_bin_src) - self.ip_dst = toipv6(self.ip_bin_dst) + self.ip_src = ipOps.str_to_ip(self.ip_bin_src) + self.ip_dst = ipOps.str_to_ip(self.ip_bin_dst) self.ip6_data_offset = 40 return true end diff --git a/scripts/broadcast-ping.nse b/scripts/broadcast-ping.nse index e5fe8215b..2580e53fd 100644 --- a/scripts/broadcast-ping.nse +++ b/scripts/broadcast-ping.nse @@ -1,5 +1,6 @@ local bin = require "bin" local coroutine = require "coroutine" +local ipOps = require "ipOps" local nmap = require "nmap" local packet = require "packet" local stdnse = require "stdnse" @@ -124,8 +125,8 @@ local icmp_packet = function(srcIP, dstIP, ttl, data_length, mtu, seqNo, icmp_id local icmp = packet.Packet:new(icmp_bin,#icmp_bin) assert(icmp,"Mistake during ICMP packet parsing") - icmp:ip_set_bin_src(packet.iptobin(srcIP)) - icmp:ip_set_bin_dst(packet.iptobin(dstIP)) + icmp:ip_set_bin_src(ipOps.ip_to_str(srcIP)) + icmp:ip_set_bin_dst(ipOps.ip_to_str(dstIP)) icmp:ip_count_checksum() return icmp diff --git a/scripts/dns-zone-transfer.nse b/scripts/dns-zone-transfer.nse index 5fd507927..a76f04bb5 100644 --- a/scripts/dns-zone-transfer.nse +++ b/scripts/dns-zone-transfer.nse @@ -11,7 +11,6 @@ local string = require "string" local tab = require "tab" local table = require "table" local target = require "target" -local packet = require "packet" description = [[ Requests a zone transfer (AXFR) from a DNS server. @@ -258,7 +257,7 @@ end --- Retrieve type specific data (rdata) from dns packets local RD = { A = function(data, offset) - return offset+4, packet.toip(data:sub(offset, offset+3)) + return offset+4, ipOps.str_to_ip(data:sub(offset, offset+3)) end, NS = parse_domain, MD = parse_domain, -- obsolete per rfc1035, use MX @@ -284,7 +283,7 @@ local RD = { WKS = function(data, offset) local len, ip, proto, svcs len = bto16(data, offset-2) - 5 -- length of bit field - ip = packet.toip(data:sub(offset, offset+3)) + ip = ipOps.str_to_ip(data:sub(offset, offset+3)) proto = string.byte(data, offset+4) offset = offset + 5 svcs = {} @@ -356,7 +355,7 @@ local RD = { return offset, string.format("%s %s %s", lat, long, alt) end, AAAA = function(data, offset) - return offset+16, packet.toipv6(data:sub(offset, offset+15)) + return offset+16, ipOps.str_to_ip(data:sub(offset, offset+15)) end, LOC = function(data, offset) local version, siz, hp, vp, lat, lon, alt @@ -419,7 +418,7 @@ local RD = { local prefix, addr, name prefix = string.byte(data, offset) local pbytes = bit.rshift(prefix,3) - addr = packet.toipv6(string.rep("\000", pbytes) .. data:sub(offset+1, 16-pbytes)) + addr = ipOps.str_to_ip(string.rep("\000", pbytes) .. data:sub(offset+1, 16-pbytes)) offset, name = parse_domain(data, offset + 17 - pbytes) return offset, string.format("%d %s %s", prefix, addr, name) end, diff --git a/scripts/firewalk.nse b/scripts/firewalk.nse index 36cafe64d..9b752ffb8 100644 --- a/scripts/firewalk.nse +++ b/scripts/firewalk.nse @@ -1,4 +1,5 @@ local bin = require "bin" +local ipOps = require "ipOps" local math = require "math" local nmap = require "nmap" local packet = require "packet" @@ -128,19 +129,6 @@ local proto_vtable = {} local Firewalk = {} ---- Printable representation of a v4 or v6 IP address. --- @param addr Binary representation of the address --- @return the printable representation of the address, as a string. -local function toip(addr) - -- XXX Beware this function uses nmap.address_family() to format the result. - - if nmap.address_family() == "inet" then - return packet.toip(addr) - else - return packet.toipv6(addr) - end -end - --- lookup for TTL of a given gateway in a traceroute results table -- @param traceroute a host traceroute results table -- @param gw the IP address of the gateway (as a decimal-dotted string) @@ -444,7 +432,7 @@ local Firewalk_v4 = { --- IPv4 initialization function. Open injection and reception sockets. -- @param scanner the scanner handle init = function(scanner) - local saddr = packet.toip(scanner.target.bin_ip_src) + local saddr = ipOps.str_to_ip(scanner.target.bin_ip_src) scanner.sock = nmap.new_dnet() scanner.pcap = nmap.new_socket() @@ -510,7 +498,7 @@ local Firewalk_v6 = { --- IPv6 initialization function. Open injection and reception sockets. -- @param scanner the scanner handle init = function(scanner) - local saddr = packet.toipv6(scanner.target.bin_ip_src) + local saddr = ipOps.str_to_ip(scanner.target.bin_ip_src) scanner.sock = nmap.new_dnet() scanner.pcap = nmap.new_socket() @@ -821,7 +809,7 @@ local function report(scanner) -- duplicate traceroute results and add localhost at the beginning local path = { -- XXX 'localhost' might be a better choice? - {ip = toip(scanner.target.bin_ip_src)} + {ip = ipOps.str_to_ip(scanner.target.bin_ip_src)} } for _, v in pairs(scanner.target.traceroute) do diff --git a/scripts/ip-forwarding.nse b/scripts/ip-forwarding.nse index eae68d82a..6ca79c483 100644 --- a/scripts/ip-forwarding.nse +++ b/scripts/ip-forwarding.nse @@ -56,8 +56,8 @@ icmpEchoRequest = function(ifname, host, addr) local probe = packet.Frame:new() probe.mac_src = iface.mac probe.mac_dst = host.mac_addr - probe.ip_bin_src = packet.iptobin(iface.address) - probe.ip_bin_dst = packet.iptobin(addr) + probe.ip_bin_src = ipOps.ip_to_str(iface.address) + probe.ip_bin_dst = ipOps.ip_to_str(addr) probe.echo_id = 0x1234 probe.echo_seq = 6 probe.echo_data = "Nmap host discovery." diff --git a/scripts/ipidseq.nse b/scripts/ipidseq.nse index 04db03c13..d1148eba0 100644 --- a/scripts/ipidseq.nse +++ b/scripts/ipidseq.nse @@ -1,4 +1,5 @@ local bin = require "bin" +local ipOps = require "ipOps" local math = require "math" local nmap = require "nmap" local packet = require "packet" @@ -206,8 +207,8 @@ action = function(host) local ipids = {} local sock = nmap.new_dnet() local pcap = nmap.new_socket() - local saddr = packet.toip(host.bin_ip_src) - local daddr = packet.toip(host.bin_ip) + local saddr = ipOps.str_to_ip(host.bin_ip_src) + local daddr = ipOps.str_to_ip(host.bin_ip) local try = nmap.new_try() try(sock:ip_open()) diff --git a/scripts/ipv6-node-info.nse b/scripts/ipv6-node-info.nse index 622f37bb0..b12202015 100644 --- a/scripts/ipv6-node-info.nse +++ b/scripts/ipv6-node-info.nse @@ -1,6 +1,7 @@ local bin = require "bin" local bit = require "bit" local dns = require "dns" +local ipOps = require "ipOps" local nmap = require "nmap" local packet = require "packet" local stdnse = require "stdnse" @@ -190,7 +191,7 @@ local function stringify_nodeaddresses(flags, data) if not ttl then break end - addrs[#addrs + 1] = packet.toipv6(binaddr) + addrs[#addrs + 1] = ipOps.str_to_ip(binaddr) end if empty(addrs) then return @@ -232,7 +233,7 @@ local function stringify_nodeipv4addresses(flags, data) if not ttl then break end - addrs[#addrs + 1] = packet.toip(binaddr) + addrs[#addrs + 1] = ipOps.str_to_ip(binaddr) end if empty(addrs) then return diff --git a/scripts/ipv6-ra-flood.nse b/scripts/ipv6-ra-flood.nse index b1672b214..1e6aaa9d2 100644 --- a/scripts/ipv6-ra-flood.nse +++ b/scripts/ipv6-ra-flood.nse @@ -1,3 +1,4 @@ +local ipOps = require "ipOps" local nmap = require "nmap" local packet = require "packet" local stdnse = require "stdnse" @@ -66,7 +67,7 @@ local function get_interface() local if_table = nmap.get_interface_info(arg_interface) - if if_table and packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if if_table and ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then return if_table.device else stdnse.debug1("Interface %s not supported or not properly configured, exiting...", arg_interface) @@ -139,7 +140,7 @@ local function broadcast_on_interface(iface) try(dnet:ethernet_open(iface)) local dst_mac = packet.mactobin("33:33:00:00:00:01") - local dst_ip6_addr = packet.ip6tobin("ff02::1") + local dst_ip6_addr = ipOps.ip_to_str("ff02::1") local prefix_len = 64 @@ -156,7 +157,7 @@ local function broadcast_on_interface(iface) local src_mac = packet.mactobin(random_mac()) local src_ip6_addr = packet.mac_to_lladdr(src_mac) - local prefix = packet.ip6tobin(get_random_prefix()) + local prefix = ipOps.ip_to_str(get_random_prefix()) local packet = packet.Frame:new() diff --git a/scripts/ntp-monlist.nse b/scripts/ntp-monlist.nse index b219f7297..a39c2b034 100644 --- a/scripts/ntp-monlist.nse +++ b/scripts/ntp-monlist.nse @@ -570,8 +570,8 @@ function parse_monlist_1(pkt, recs) -- src and dst addresses -- IPv4 if impl == 2 or v6 flag is not set if isize == 32 or pkt:u8(pos+32) ~= 1 then -- IPv4 - local saddr = packet.toip(pkt:raw(pos+16, 4)) - local daddr = packet.toip(pkt:raw(pos+20, 4)) + local saddr = ipOps.str_to_ip(pkt:raw(pos+16, 4)) + local daddr = ipOps.str_to_ip(pkt:raw(pos+20, 4)) t.saddr = saddr t.daddr = daddr else -- IPv6 @@ -638,7 +638,7 @@ function parse_peerlist(pkt, recs) -- src address -- IPv4 if impl == 2 or v6 flag is not set if isize == 8 or pkt:u8(pos+8) ~= 1 then - local saddr = packet.toip(pkt:raw(pos, 4)) + local saddr = ipOps.str_to_ip(pkt:raw(pos, 4)) t.saddr = saddr else -- IPv6 local saddr = {} diff --git a/scripts/path-mtu.nse b/scripts/path-mtu.nse index cae9e600f..c679e4992 100644 --- a/scripts/path-mtu.nse +++ b/scripts/path-mtu.nse @@ -1,4 +1,5 @@ local bin = require "bin" +local ipOps = require "ipOps" local math = require "math" local nmap = require "nmap" local packet = require "packet" @@ -300,8 +301,8 @@ action = function(host) local pcap = nmap.new_socket() local proto = host.registry['pathmtuprobe']['proto'] local port = host.registry['pathmtuprobe']['port'] - local saddr = packet.toip(host.bin_ip_src) - local daddr = packet.toip(host.bin_ip) + local saddr = ipOps.str_to_ip(host.bin_ip_src) + local daddr = ipOps.str_to_ip(host.bin_ip) local try = nmap.new_try() local status, pkt, ip diff --git a/scripts/qscan.nse b/scripts/qscan.nse index 6955c0bf1..fb2e1e1a3 100644 --- a/scripts/qscan.nse +++ b/scripts/qscan.nse @@ -1,4 +1,5 @@ local bin = require "bin" +local ipOps = require "ipOps" local math = require "math" local nmap = require "nmap" local packet = require "packet" @@ -402,8 +403,8 @@ end action = function(host) local sock = nmap.new_dnet() local pcap = nmap.new_socket() - local saddr = packet.toip(host.bin_ip_src) - local daddr = packet.toip(host.bin_ip) + local saddr = ipOps.str_to_ip(host.bin_ip_src) + local daddr = ipOps.str_to_ip(host.bin_ip) local start local rtt local stats = {} diff --git a/scripts/targets-ipv6-multicast-echo.nse b/scripts/targets-ipv6-multicast-echo.nse index 6af0c8a69..640b79cee 100644 --- a/scripts/targets-ipv6-multicast-echo.nse +++ b/scripts/targets-ipv6-multicast-echo.nse @@ -1,4 +1,5 @@ local coroutine = require "coroutine" +local ipOps = require "ipOps" local nmap = require "nmap" local packet = require "packet" local stdnse = require "stdnse" @@ -43,14 +44,14 @@ local function get_interfaces() if interface_name then -- single interface defined local if_table = nmap.get_interface_info(interface_name) - if if_table and packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if if_table and ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then interfaces[#interfaces + 1] = if_table else stdnse.debug1("Interface not supported or not properly configured.") end else for _, if_table in ipairs(nmap.list_interfaces()) do - if packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then table.insert(interfaces, if_table) end end @@ -64,9 +65,9 @@ local function single_interface_broadcast(if_nfo, results) local condvar = nmap.condvar(results) local src_mac = if_nfo.mac - local src_ip6 = packet.ip6tobin(if_nfo.address) + local src_ip6 = ipOps.ip_to_str(if_nfo.address) local dst_mac = packet.mactobin("33:33:00:00:00:01") - local dst_ip6 = packet.ip6tobin("ff02::1") + local dst_ip6 = ipOps.ip_to_str("ff02::1") ---------------------------------------------------------------------------- --Multicast echo ping probe diff --git a/scripts/targets-ipv6-multicast-invalid-dst.nse b/scripts/targets-ipv6-multicast-invalid-dst.nse index acfbb994d..403ea8f62 100644 --- a/scripts/targets-ipv6-multicast-invalid-dst.nse +++ b/scripts/targets-ipv6-multicast-invalid-dst.nse @@ -1,4 +1,5 @@ local coroutine = require "coroutine" +local ipOps = require "ipOps" local nmap = require "nmap" local packet = require "packet" local stdnse = require "stdnse" @@ -58,14 +59,14 @@ local function get_interfaces() if interface_name then -- single interface defined local if_table = nmap.get_interface_info(interface_name) - if if_table and packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if if_table and ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then interfaces[#interfaces + 1] = if_table else stdnse.debug1("Interface not supported or not properly configured.") end else for _, if_table in ipairs(nmap.list_interfaces()) do - if packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then table.insert(interfaces, if_table) end end @@ -79,9 +80,9 @@ local function single_interface_broadcast(if_nfo, results) local condvar = nmap.condvar(results) local src_mac = if_nfo.mac - local src_ip6 = packet.ip6tobin(if_nfo.address) + local src_ip6 = ipOps.ip_to_str(if_nfo.address) local dst_mac = packet.mactobin("33:33:00:00:00:01") - local dst_ip6 = packet.ip6tobin("ff02::1") + local dst_ip6 = ipOps.ip_to_str("ff02::1") ---------------------------------------------------------------------------- --Multicast invalid destination exheader probe diff --git a/scripts/targets-ipv6-multicast-mld.nse b/scripts/targets-ipv6-multicast-mld.nse index fdd146db9..8ecccda9a 100644 --- a/scripts/targets-ipv6-multicast-mld.nse +++ b/scripts/targets-ipv6-multicast-mld.nse @@ -1,4 +1,5 @@ local bin = require "bin" +local ipOps = require "ipOps" local coroutine = require "coroutine" local nmap = require "nmap" local packet = require "packet" @@ -52,14 +53,14 @@ local function get_interfaces() if interface_name then -- single interface defined local if_table = nmap.get_interface_info(interface_name) - if if_table and packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if if_table and ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then interfaces[#interfaces + 1] = if_table else stdnse.debug1("Interface not supported or not properly configured.") end else for _, if_table in ipairs(nmap.list_interfaces()) do - if packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then table.insert(interfaces, if_table) end end @@ -72,10 +73,10 @@ local function single_interface_broadcast(if_nfo, results) stdnse.debug2("Starting " .. SCRIPT_NAME .. " on " .. if_nfo.device) local condvar = nmap.condvar(results) local src_mac = if_nfo.mac - local src_ip6 = packet.ip6tobin(if_nfo.address) + local src_ip6 = ipOps.ip_to_str(if_nfo.address) local dst_mac = packet.mactobin("33:33:00:00:00:01") - local dst_ip6 = packet.ip6tobin("ff02::1") - local gen_qry = packet.ip6tobin("::") + local dst_ip6 = ipOps.ip_to_str("ff02::1") + local gen_qry = ipOps.ip_to_str("::") local dnet = nmap.new_dnet() local pcap = nmap.new_socket() diff --git a/scripts/targets-ipv6-multicast-slaac.nse b/scripts/targets-ipv6-multicast-slaac.nse index 797189795..13e7cd576 100644 --- a/scripts/targets-ipv6-multicast-slaac.nse +++ b/scripts/targets-ipv6-multicast-slaac.nse @@ -64,9 +64,9 @@ local function get_random_ula_prefix(local_scope) local global_id = string.char(math.random(256)-1,math.random(256)-1,math.random(256)-1,math.random(256)-1,math.random(256)-1) if local_scope then - ula_prefix = packet.ip6tobin("fd00::") + ula_prefix = ipOps.ip_to_str("fd00::") else - ula_prefix = packet.ip6tobin("fc00::") + ula_prefix = ipOps.ip_to_str("fc00::") end ula_prefix = string.sub(ula_prefix,1,1) .. global_id .. string.sub(ula_prefix,7,-1) return ula_prefix,64 @@ -104,14 +104,14 @@ local function get_interfaces() if interface_name then -- single interface defined local if_table = nmap.get_interface_info(interface_name) - if if_table and packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if if_table and ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then interfaces[#interfaces + 1] = if_table else stdnse.debug1("Interface not supported or not properly configured.") end else for _, if_table in ipairs(nmap.list_interfaces()) do - if packet.ip6tobin(if_table.address) and if_table.link == "ethernet" then + if ipOps.ip_to_str(if_table.address) and if_table.link == "ethernet" then table.insert(interfaces, if_table) end end @@ -125,9 +125,9 @@ local function single_interface_broadcast(if_nfo, results) local condvar = nmap.condvar(results) local src_mac = if_nfo.mac - local src_ip6 = packet.ip6tobin(if_nfo.address) + local src_ip6 = ipOps.ip_to_str(if_nfo.address) local dst_mac = packet.mactobin("33:33:00:00:00:01") - local dst_ip6 = packet.ip6tobin("ff02::1") + local dst_ip6 = ipOps.ip_to_str("ff02::1") ---------------------------------------------------------------------------- --SLAAC-based host discovery probe @@ -168,8 +168,8 @@ local function single_interface_broadcast(if_nfo, results) try(dnet:ethernet_send(probe.frame_buf)) local expected_mac_dst_prefix = packet.mactobin("33:33:ff:00:00:00") - local expected_ip6_src = packet.ip6tobin("::") - local expected_ip6_dst_prefix = packet.ip6tobin("ff02::1:0:0") + local expected_ip6_src = ipOps.ip_to_str("::") + local expected_ip6_dst_prefix = ipOps.ip_to_str("ff02::1:0:0") pcap:set_timeout(1000) local pcap_timeout_count = 0 @@ -188,11 +188,11 @@ local function single_interface_broadcast(if_nfo, results) local reply = packet.Packet:new(layer3) if reply.ip_bin_src == expected_ip6_src and string.sub(expected_ip6_dst_prefix,1,12) == string.sub(reply.ip_bin_dst,1,12) then - local ula_target_addr_str = packet.toipv6(reply.ns_target) + local ula_target_addr_str = ipOps.str_to_ip(reply.ns_target) local identifier = get_identifier(reply.ns_target) --Filter out the reduplicative identifiers. --A host will send several NS packets with the same interface identifier if it receives several RA packets with different prefix during the discovery phase. - local actual_addr_str = packet.toipv6(actual_prefix .. identifier) + local actual_addr_str = ipOps.str_to_ip(actual_prefix .. identifier) if not results[actual_addr_str] then if target.ALLOW_NEW_TARGETS then target.add(actual_addr_str) diff --git a/scripts/targets-sniffer.nse b/scripts/targets-sniffer.nse index f16548f33..51311b0ae 100644 --- a/scripts/targets-sniffer.nse +++ b/scripts/targets-sniffer.nse @@ -1,3 +1,4 @@ +local ipOps = require "ipOps" local nmap = require "nmap" local packet = require "packet" local stdnse = require "stdnse" @@ -61,11 +62,7 @@ end -- Returns an array of address strings. local function get_ip_addresses(layer3) local ip = packet.Packet:new(layer3, layer3:len()) - if ip.ip_v == 4 then - return { packet.toip(ip.ip_bin_src), packet.toip(ip.ip_bin_dst) } - elseif ip.ip_v == 6 then - return { packet.toipv6(ip.ip_bin_src), packet.toipv6(ip.ip_bin_dst) } - end + return { ipOps.str_to_ip(ip.ip_bin_src), ipOps.str_to_ip(ip.ip_bin_dst) } end prerule = function()