1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-10 09:49:05 +00:00

Fix endianness in unpacking IP addresses, since ipOps.fromdword is fixed. Fixes #750

This commit is contained in:
dmiller
2017-03-13 14:58:57 +00:00
parent 3e3f600b8a
commit 2091ce3199
17 changed files with 42 additions and 44 deletions

View File

@@ -68,7 +68,7 @@ NetworkAddress = {
local na = NetworkAddress:new() local na = NetworkAddress:new()
local _ local _
_, na.service, na.ipv6_prefix, na.host, na.port = bin.unpack("<LH12I>S", data) _, na.service, na.ipv6_prefix, na.host, na.port = bin.unpack("<LH12>IS", data)
na.host = ipOps.fromdword(na.host) na.host = ipOps.fromdword(na.host)
return na return na
end, end,

View File

@@ -89,7 +89,7 @@ Decoders = {
pos, sender.mac, pos, sender.mac,
sender.ip, sender.ip,
target.mac, target.mac,
target.ip = bin.unpack("<H" .. hwsize .. "IH" .. hwsize .. "I", data, pos) target.ip = bin.unpack(">H" .. hwsize .. "IH" .. hwsize .. "I", data, pos)
if ( not(self.results) ) then if ( not(self.results) ) then
self.results = tab.new(3) self.results = tab.new(3)
@@ -131,7 +131,7 @@ Decoders = {
if ( addr_proto == 'CC' ) then if ( addr_proto == 'CC' ) then
-- IPv4 address, extract it -- IPv4 address, extract it
pos, addr_len = bin.unpack(">S", data, pos) pos, addr_len = bin.unpack(">S", data, pos)
pos, dev_addr = bin.unpack("<I", data, pos) pos, dev_addr = bin.unpack(">I", data, pos)
addr_list = addr_list .. ' ' .. ipOps.fromdword(dev_addr) addr_list = addr_list .. ' ' .. ipOps.fromdword(dev_addr)
end end
-- Add code here for IPv6, others -- Add code here for IPv6, others
@@ -483,7 +483,7 @@ udp = {
local data = layer3:sub(p.udp_offset + 9) local data = layer3:sub(p.udp_offset + 9)
local pos, ip, _, src, dst = 5 local pos, ip, _, src, dst = 5
pos, ip, _, _, _, src, dst = bin.unpack("<ISSSA34A34", data, pos) pos, ip, _, _, _, src, dst = bin.unpack(">ISSSA34A34", data, pos)
ip = ipOps.fromdword(ip) ip = ipOps.fromdword(ip)
src = netbios.name_decode(src) src = netbios.name_decode(src)
@@ -662,7 +662,7 @@ udp = {
if ( version ~= 0 ) then return end if ( version ~= 0 ) then return end
pos = pos + ( 7 - #secret ) pos = pos + ( 7 - #secret )
local virtip local virtip
pos, virtip = bin.unpack("<I", data, pos) pos, virtip = bin.unpack(">I", data, pos)
if ( not(self.dups[p.ip_src]) ) then if ( not(self.dups[p.ip_src]) ) then
if ( not(self.results) ) then if ( not(self.results) ) then

View File

@@ -66,7 +66,7 @@ local function read_ip(data, pos, length)
local results = {} local results = {}
for i=1, length, 4 do for i=1, length, 4 do
local value local value
pos, value = bin.unpack("<I", data, pos) pos, value = bin.unpack(">I", data, pos)
table.insert(results, ipOps.fromdword(value)) table.insert(results, ipOps.fromdword(value))
end end
@@ -74,7 +74,7 @@ local function read_ip(data, pos, length)
end end
else else
local value local value
pos, value = bin.unpack("<I", data, pos) pos, value = bin.unpack(">I", data, pos)
return pos, ipOps.fromdword(value) return pos, ipOps.fromdword(value)
end end
@@ -485,10 +485,10 @@ function dhcp_parse(data, transaction_id)
-- Unpack the secs, flags, addresses, sname, and file -- Unpack the secs, flags, addresses, sname, and file
pos, result['secs'], result['flags'] = bin.unpack(">SS", data, pos) pos, result['secs'], result['flags'] = bin.unpack(">SS", data, pos)
pos, result['ciaddr'] = bin.unpack("<I", data, pos) pos, result['ciaddr'] = bin.unpack(">I", data, pos)
pos, result['yiaddr'] = bin.unpack("<I", data, pos) pos, result['yiaddr'] = bin.unpack(">I", data, pos)
pos, result['siaddr'] = bin.unpack("<I", data, pos) pos, result['siaddr'] = bin.unpack(">I", data, pos)
pos, result['giaddr'] = bin.unpack("<I", data, pos) pos, result['giaddr'] = bin.unpack(">I", data, pos)
pos, result['chaddr'] = bin.unpack("A16", data, pos) pos, result['chaddr'] = bin.unpack("A16", data, pos)
pos, result['sname'] = bin.unpack("A64", data, pos) pos, result['sname'] = bin.unpack("A64", data, pos)
pos, result['file'] = bin.unpack("A128", data, pos) pos, result['file'] = bin.unpack("A128", data, pos)

View File

@@ -138,8 +138,8 @@ EIGRP = {
elseif tlv.type == TLV.SEQ then elseif tlv.type == TLV.SEQ then
-- Sequence -- Sequence
index, tlv.addlen = bin.unpack(">S", eigrp_raw, index) index, tlv.addlen = bin.unpack(">S", eigrp_raw, index)
index, tlv.address = bin.unpack("<C".. tlv.addlen, eigrp_raw, index) index, tlv.address = bin.unpack("A".. tlv.addlen, eigrp_raw, index)
tlv.address = ipOps.fromdword(tlv.address) tlv.address = ipOps.str_to_ip(tlv.address)
index = index + tlv.length - 7 index = index + tlv.length - 7
elseif tlv.type == TLV.SWVER then elseif tlv.type == TLV.SWVER then
-- Software version -- Software version
@@ -170,7 +170,7 @@ EIGRP = {
index = index + tlv.length - 4 index = index + tlv.length - 4
elseif tlv.type == TLV.INT then elseif tlv.type == TLV.INT then
-- Internal Route -- Internal Route
index, tlv.nexth = bin.unpack("<I", eigrp_raw, index) index, tlv.nexth = bin.unpack(">I", eigrp_raw, index)
tlv.nexth = ipOps.fromdword(tlv.nexth) tlv.nexth = ipOps.fromdword(tlv.nexth)
index, tlv.mask = bin.unpack(">S", eigrp_raw, index + 15) index, tlv.mask = bin.unpack(">S", eigrp_raw, index + 15)
-- Destination varies in length -- Destination varies in length
@@ -189,9 +189,9 @@ EIGRP = {
tlv.dst = dst[1] .. '.' .. dst[2] .. '.' .. dst[3] .. '.' .. dst[4] tlv.dst = dst[1] .. '.' .. dst[2] .. '.' .. dst[3] .. '.' .. dst[4]
elseif tlv.type == TLV.EXT then elseif tlv.type == TLV.EXT then
-- External Route -- External Route
index, tlv.nexth = bin.unpack("<I", eigrp_raw, index) index, tlv.nexth = bin.unpack(">I", eigrp_raw, index)
tlv.nexth = ipOps.fromdword(tlv.nexth) tlv.nexth = ipOps.fromdword(tlv.nexth)
index, tlv.orouterid = bin.unpack("<I", eigrp_raw, index) index, tlv.orouterid = bin.unpack(">I", eigrp_raw, index)
tlv.orouterid = ipOps.fromdword(tlv.orouterid) tlv.orouterid = ipOps.fromdword(tlv.orouterid)
index, tlv.oas = bin.unpack(">I", eigrp_raw, index) index, tlv.oas = bin.unpack(">I", eigrp_raw, index)
index, tlv.tag = bin.unpack(">I", eigrp_raw, index) index, tlv.tag = bin.unpack(">I", eigrp_raw, index)

View File

@@ -95,13 +95,13 @@ Response = {
end end
local pos local pos
pos, self.version, self.op, self.rescode = bin.unpack("<CCS", self.data) pos, self.version, self.op, self.rescode = bin.unpack(">CCS", self.data)
if ( self.rescode ~= ResultCode.SUCCESS or self.op ~= 128 ) then if ( self.rescode ~= ResultCode.SUCCESS or self.op ~= 128 ) then
return return
end end
pos, self.time, self.ip = bin.unpack("<II", self.data, pos) pos, self.time, self.ip = bin.unpack(">II", self.data, pos)
self.ip = ipOps.fromdword(self.ip) self.ip = ipOps.fromdword(self.ip)
self.time = stdnse.format_timestamp(self.time) self.time = stdnse.format_timestamp(self.time)
return true return true
@@ -126,7 +126,7 @@ Response = {
end end
local pos local pos
pos, self.version, self.op, self.rescode = bin.unpack("<CCS", self.data) pos, self.version, self.op, self.rescode = bin.unpack(">CCS", self.data)
if ( self.rescode ~= ResultCode.SUCCESS ) then if ( self.rescode ~= ResultCode.SUCCESS ) then
return return

View File

@@ -341,7 +341,7 @@ ResponseParser = {
local function DecodeAddress(data, pos) local function DecodeAddress(data, pos)
local COMM_TYPES = { [5] = "udp", [6] = "tcp" } local COMM_TYPES = { [5] = "udp", [6] = "tcp" }
local comm_type, port, ip, _ local comm_type, port, ip, _
pos, comm_type, _, _, _, port, ip = bin.unpack(">CCISS<I", data, pos) pos, comm_type, _, _, _, port, ip = bin.unpack(">CCISSI", data, pos)
return pos, { port = port, ip = ipOps.fromdword(ip), return pos, { port = port, ip = ipOps.fromdword(ip),
proto = COMM_TYPES[comm_type] or "unknown" } proto = COMM_TYPES[comm_type] or "unknown" }

View File

@@ -55,7 +55,7 @@ OSPF = {
assert( header.ver == 2, "Invalid OSPF version detected") assert( header.ver == 2, "Invalid OSPF version detected")
pos, header.router_id, header.area_id, header.chksum, header.auth_type pos, header.router_id, header.area_id, header.chksum, header.auth_type
= bin.unpack("<I>ISS", data, pos) = bin.unpack(">IISS", data, pos)
-- No authentication -- No authentication
if header.auth_type == 0x00 then if header.auth_type == 0x00 then
@@ -183,7 +183,7 @@ OSPF = {
assert( #data >= hello.header.length, "OSPF packet too short") assert( #data >= hello.header.length, "OSPF packet too short")
pos, hello.netmask, hello.interval, hello.options, hello.prio, pos, hello.netmask, hello.interval, hello.options, hello.prio,
hello.router_dead_interval, hello.DR, hello.router_dead_interval, hello.DR,
hello.BDR = bin.unpack("<ISCCIII", data, pos) hello.BDR = bin.unpack(">ISCCIII", data, pos)
hello.netmask = ipOps.fromdword(hello.netmask) hello.netmask = ipOps.fromdword(hello.netmask)
hello.DR = ipOps.fromdword(hello.DR) hello.DR = ipOps.fromdword(hello.DR)
@@ -199,7 +199,7 @@ OSPF = {
hello.neighbors = {} hello.neighbors = {}
for i=1, neighbor_count do for i=1, neighbor_count do
pos, neighbor = bin.unpack("<I", data, pos) pos, neighbor = bin.unpack(">I", data, pos)
neighbor = ipOps.fromdword(neighbor) neighbor = ipOps.fromdword(neighbor)
table.insert(hello.neighbors, neighbor) table.insert(hello.neighbors, neighbor)
end end

View File

@@ -139,7 +139,7 @@ Attribute = {
local function parseAddress(data, pos) local function parseAddress(data, pos)
local _, addr = nil, {} local _, addr = nil, {}
pos, _, addr.family, addr.port, addr.ip = bin.unpack("<CCSI", data, pos) pos, _, addr.family, addr.port, addr.ip = bin.unpack(">CCSI", data, pos)
if ( addr.ip ) then if ( addr.ip ) then
addr.ip = ipOps.fromdword(addr.ip) addr.ip = ipOps.fromdword(addr.ip)
end end

View File

@@ -161,7 +161,7 @@ Response = {
local pos, addr_len = bin.unpack("C", self.data) local pos, addr_len = bin.unpack("C", self.data)
if ( addr_len == 4 ) then if ( addr_len == 4 ) then
self.length = 4 + 2 + 1 self.length = 4 + 2 + 1
pos, self.ip = bin.unpack("<I", self.data, pos) pos, self.ip = bin.unpack(">I", self.data, pos)
self.ip = ipOps.fromdword(self.ip) self.ip = ipOps.fromdword(self.ip)
elseif( addr_len == 16 ) then elseif( addr_len == 16 ) then
self.length = 16 + 2 + 1 self.length = 16 + 2 + 1
@@ -289,7 +289,7 @@ Response = {
pos, contact.type, contact.proto_version, addr_len = bin.unpack("CCC", self.data, pos) pos, contact.type, contact.proto_version, addr_len = bin.unpack("CCC", self.data, pos)
if ( addr_len == 4 ) then if ( addr_len == 4 ) then
pos, address = bin.unpack("<I", self.data, pos) pos, address = bin.unpack(">I", self.data, pos)
contact.address = ipOps.fromdword(address) contact.address = ipOps.fromdword(address)
elseif ( addr_len == 16 ) then elseif ( addr_len == 16 ) then
pos, contact.address = bin.unpack("H16", self.data, pos) pos, contact.address = bin.unpack("H16", self.data, pos)

View File

@@ -120,7 +120,7 @@ local igmpParse = function(data)
-- Checksum -- Checksum
index, response.checksum = bin.unpack(">S", data, index) index, response.checksum = bin.unpack(">S", data, index)
-- Multicast group -- Multicast group
index, response.group = bin.unpack("<I", data, index) index, response.group = bin.unpack(">I", data, index)
response.group = ipOps.fromdword(response.group) response.group = ipOps.fromdword(response.group)
return response return response
elseif response.type == 0x22 and #data >= 12 then elseif response.type == 0x22 and #data >= 12 then
@@ -141,12 +141,12 @@ local igmpParse = function(data)
index, group.auxdlen = bin.unpack(">C", data, index) index, group.auxdlen = bin.unpack(">C", data, index)
-- Number of source addresses -- Number of source addresses
index, group.nsrc = bin.unpack(">S", data, index) index, group.nsrc = bin.unpack(">S", data, index)
index, group.address = bin.unpack("<I", data, index) index, group.address = bin.unpack(">I", data, index)
group.address = ipOps.fromdword(group.address) group.address = ipOps.fromdword(group.address)
group.src = {} group.src = {}
if group.nsrc > 0 then if group.nsrc > 0 then
for i=1,group.nsrc do for i=1,group.nsrc do
index, source = bin.unpack("<I", data, index) index, source = bin.unpack(">I", data, index)
table.insert(group.src, ipOps.fromdword(source)) table.insert(group.src, ipOps.fromdword(source))
end end
end end

View File

@@ -55,7 +55,7 @@ function action()
for _, attr in ipairs(attribs) do for _, attr in ipairs(attribs) do
local addr = attr:match("^%d*%-%d*%-%d*%-(........)") local addr = attr:match("^%d*%-%d*%-%d*%-(........)")
if ( addr ) then if ( addr ) then
local pos, dw_addr = bin.unpack( "<I", bin.pack("H", addr) ) local pos, dw_addr = bin.unpack( ">I", bin.pack("H", addr) )
local ip = ipOps.fromdword(dw_addr) local ip = ipOps.fromdword(dw_addr)
if ( not(ips[ip]) ) then if ( not(ips[ip]) ) then

View File

@@ -124,7 +124,7 @@ RIPv2 = {
while( #data - pos >= 20 ) do while( #data - pos >= 20 ) do
local family, address, metric, _, netmask, nexthop local family, address, metric, _, netmask, nexthop
pos, family, _, address, netmask, nexthop, pos, family, _, address, netmask, nexthop,
metric = bin.unpack(">SS<III>I", data, pos) metric = bin.unpack(">SSIIII", data, pos)
if ( family == RIPv2.AddressFamily.IP ) then if ( family == RIPv2.AddressFamily.IP ) then
local ip = ipOps.fromdword(address) local ip = ipOps.fromdword(address)

View File

@@ -317,8 +317,6 @@ local get_addresses = function(address, mask, domain, nameserver, port)
-- DNS library expects -- DNS library expects
if ( "number" == type(address) ) then if ( "number" == type(address) ) then
address = ipOps.fromdword(address) address = ipOps.fromdword(address)
local a, b, c, d = address:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")
address = ("%d.%d.%d.%d"):format(d,c,b,a)
end end
local subnet = { family = nmap.address_family(), address = address, mask = mask } local subnet = { family = nmap.address_family(), address = address, mask = mask }

View File

@@ -1429,7 +1429,7 @@ action = function(host,port)
output["Revision"] = char1 .. "." .. char2 output["Revision"] = char1 .. "." .. char2
-- Device IP, this could be the same, as the IP scanning, or may be actual IP behind NAT -- Device IP, this could be the same, as the IP scanning, or may be actual IP behind NAT
local dword local dword
pos, dword = bin.unpack("<I", response, 37) pos, dword = bin.unpack(">I", response, 37)
output["Device IP"] = ipOps.fromdword(dword) output["Device IP"] = ipOps.fromdword(dword)
-- set Nmap output -- set Nmap output
set_nmap(host, port) set_nmap(host, port)

View File

@@ -127,7 +127,7 @@ local llmnrListen = function(interface, timeout, result)
-- skip null byte, type, class, ttl, dlen -- skip null byte, type, class, ttl, dlen
index = index + 1 + 2 + 2 + 4 + 2 index = index + 1 + 2 + 2 + 4 + 2
index, response.address = bin.unpack("<I", llmnr, index) index, response.address = bin.unpack(">I", llmnr, index)
response.address = ipOps.fromdword(response.address) response.address = ipOps.fromdword(response.address)
table.insert(result, response) table.insert(result, response)
else else

View File

@@ -107,7 +107,7 @@ local mrinfoParse = function(data)
if data:byte(index) == 0x00 then break end if data:byte(index) == 0x00 then break end
address = {} address = {}
-- Local address -- Local address
index, address.ip = bin.unpack("<I", data, index) index, address.ip = bin.unpack(">I", data, index)
address.ip = ipOps.fromdword(address.ip) address.ip = ipOps.fromdword(address.ip)
-- Link metric -- Link metric
index, address.metric = bin.unpack(">C", data, index) index, address.metric = bin.unpack(">C", data, index)
@@ -121,7 +121,7 @@ local mrinfoParse = function(data)
address.neighbors = {} address.neighbors = {}
-- Iterate over neighbors -- Iterate over neighbors
for i = 1, address.ncount do for i = 1, address.ncount do
index, neighbor = bin.unpack("<I", data, index) index, neighbor = bin.unpack(">I", data, index)
table.insert(address.neighbors, ipOps.fromdword(neighbor)) table.insert(address.neighbors, ipOps.fromdword(neighbor))
end end
table.insert(response.addresses, address) table.insert(response.addresses, address)

View File

@@ -186,19 +186,19 @@ local traceParse = function(data)
index, response.checksum = bin.unpack(">S", data, index) index, response.checksum = bin.unpack(">S", data, index)
-- Group -- Group
index, response.group = bin.unpack("<I", data, index) index, response.group = bin.unpack(">I", data, index)
response.group = ipOps.fromdword(response.group) response.group = ipOps.fromdword(response.group)
-- Source address -- Source address
index, response.source = bin.unpack("<I", data, index) index, response.source = bin.unpack(">I", data, index)
response.source = ipOps.fromdword(response.source) response.source = ipOps.fromdword(response.source)
-- Destination address -- Destination address
index, response.destination = bin.unpack("<I", data, index) index, response.destination = bin.unpack(">I", data, index)
response.receiver = ipOps.fromdword(response.destination) response.receiver = ipOps.fromdword(response.destination)
-- Response address -- Response address
index, response.response = bin.unpack("<I", data, index) index, response.response = bin.unpack(">I", data, index)
response.response = ipOps.fromdword(response.response) response.response = ipOps.fromdword(response.response)
-- Response TTL -- Response TTL
@@ -225,15 +225,15 @@ local traceParse = function(data)
index, block.query = bin.unpack(">I", data, index) index, block.query = bin.unpack(">I", data, index)
-- In itf address -- In itf address
index, block.inaddr = bin.unpack("<I", data, index) index, block.inaddr = bin.unpack(">I", data, index)
block.inaddr = ipOps.fromdword(block.inaddr) block.inaddr = ipOps.fromdword(block.inaddr)
-- Out itf address -- Out itf address
index, block.outaddr = bin.unpack("<I", data, index) index, block.outaddr = bin.unpack(">I", data, index)
block.outaddr = ipOps.fromdword(block.outaddr) block.outaddr = ipOps.fromdword(block.outaddr)
-- Previous rtr address -- Previous rtr address
index, block.prevaddr = bin.unpack("<I", data, index) index, block.prevaddr = bin.unpack(">I", data, index)
block.prevaddr = ipOps.fromdword(block.prevaddr) block.prevaddr = ipOps.fromdword(block.prevaddr)
-- In packets -- In packets