1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Replace IP address parsing with functions from ipOps

This commit is contained in:
dmiller
2017-03-14 18:59:12 +00:00
parent cc644955c4
commit f89d7610b0
5 changed files with 27 additions and 49 deletions

View File

@@ -113,6 +113,7 @@
local bin = require "bin"
local bit = require "bit"
local ipOps = require "ipOps"
local nmap = require "nmap"
local os = require "os"
local stdnse = require "stdnse"
@@ -706,15 +707,14 @@ Proto = {
-- return an error? maybe not, lets just ignore this
elseif tag == 0x01 then
-- four byte ip
local octet = {}
pos, octet[1], octet[2], octet[3], octet[4] = bin.unpack("CCCC", packet.data, pos)
table.insert(result.network_addresses, string.format("%d.%d.%d.%d", octet[1], octet[2], octet[3], octet[4]))
local ip
ip, pos = string.unpack("c4", packet.data, pos)
table.insert(result.network_addresses, ipOps.str_to_ip(ip))
elseif tag == 0x02 then
-- four byte ip and two byte port
local octet = {}
local port
pos, octet[1], octet[2], octet[3], octet[4], port = bin.unpack(">CCCCS", packet.data, pos)
table.insert(result.network_addresses, string.format("%d.%d.%d.%d:%d", octet[1], octet[2], octet[3], octet[4], port))
local ip, port
ip, port, pos = string.unpack("c4 >I2", packet.data, pos)
table.insert(result.network_addresses, string.format("%s:%d", ipOps.str_to_ip(ip), port))
elseif tag == 0x03 then
-- ddp address (two byte network, one byte
-- node, one byte socket) not tested, anyone
@@ -735,37 +735,24 @@ Proto = {
-- four byte ip and two byte port, client
-- should use ssh. not tested, should work as it
-- is the same as tag 0x02
local octet = {}
local port
pos, octet[1], octet[2], octet[3], octet[4], port = bin.unpack(">CCCCS", packet.data, pos)
table.insert(result.network_addresses, string.format("ssh://%d.%d.%d.%d:%d", octet[1], octet[2], octet[3], octet[4], port))
local ip, port
ip, port, pos = string.unpack("c4 >I2", packet.data, pos)
table.insert(result.network_addresses, string.format("ssh://%s:%d", ipOps.str_to_ip(ip), port))
elseif tag == 0x06 then
-- 16 byte ipv6
-- not tested, but should work (next tag is
-- tested)
local octet = {}
local ip
ip, pos = string.unpack("c16", packet.data, pos)
for j = 1, 8 do
local o
pos, o = bin.unpack(">S", packet.data, pos)
octet[j] = string.format("%04x", o)
end
table.insert(result.network_addresses, table.concat(octet, ':'))
table.insert(result.network_addresses, ipOps.str_to_ip(ip))
elseif tag == 0x07 then
-- 16 byte ipv6 and two byte port
local octet = {}
local port
for j = 1, 8 do
local o
pos, o = bin.unpack(">S", packet.data, pos)
octet[j] = string.format("%04x", o)
end
pos, port = bin.unpack(">S", packet.data, pos)
local ip, port
ip, port, pos = string.unpack(">c16 I2", packet.data, pos)
table.insert(result.network_addresses,
string.format("[%s]:%d", table.concat(octet, ':'), port))
string.format("[%s]:%d", ipOps.str_to_ip(ip), port))
end
end

View File

@@ -52,6 +52,7 @@
local bin = require "bin"
local bit = require "bit"
local ipOps = require "ipOps"
local math = require "math"
local msrpctypes = require "msrpctypes"
local netbios = require "netbios"
@@ -1266,9 +1267,9 @@ function epmapper_lookup(smbstate,handle)
elseif address_type == 0x08 then
pos,lookup_response.udp_port = bin.unpack(">S",data,pos)
elseif address_type == 0x09 then
local i1,i2,i3,i4
pos,i1,i2,i3,i4 = bin.unpack("CCCC",data,pos)
lookup_response.ip_addr = string.format("%d.%d.%d.%d",i1,i2,i3,i4)
local ip
ip, pos = string.unpack("c4", data, pos)
lookup_response.ip_addr = ipOps.str_to_ip(ip)
elseif address_type == 0x0f then
lookup_response.ncacn_np = string.sub(data,pos,pos+address_len-2)
floor_len = floor_len + address_len - 2

View File

@@ -182,7 +182,7 @@ action = function()
local transaction_id = bin.pack("<I", math.random(0, 0x7FFFFFFF))
local request_type = dhcp.request_types["DHCPDISCOVER"]
local ip_address = bin.pack(">I", ipOps.todword("0.0.0.0"))
local ip_address = ipOps.ip_to_str("0.0.0.0")
-- we need to set the flags to broadcast
local request_options, overrides, lease_time = nil, { flags = 0x8000 }, nil

View File

@@ -7,6 +7,7 @@ local string = require "string"
local table = require "table"
local target = require "target"
local unicode = require "unicode"
local ipOps = require "ipOps"
local openssl = stdnse.silent_require "openssl"
@@ -135,15 +136,10 @@ local parseHello = function(data)
-- Host ID (MAC Address)
mac = get_mac_addr(v:sub(1,6))
elseif t == 0x08 then
ipv6 = string.format(
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
v:byte(1), v:byte(2), v:byte(3), v:byte(4),
v:byte(5), v:byte(6), v:byte(7), v:byte(8),
v:byte(9), v:byte(10), v:byte(11), v:byte(12),
v:byte(13), v:byte(14), v:byte(15), v:byte(16))
ipv6 = ipOps.str_to_ip(v:sub(1,16))
elseif t == 0x07 then
-- IPv4 address
ipv4 = string.format("%d.%d.%d.%d",v:byte(1),v:byte(2),v:byte(3),v:byte(4)), mac
ipv4 = ipOps.str_to_ip(v:sub(1,4))
-- Machine Name (Hostname)
elseif t == 0x0f then

View File

@@ -1,4 +1,5 @@
local bin = require "bin"
local ipOps = require "ipOps"
local nmap = require "nmap"
local shortport = require "shortport"
local stdnse = require "stdnse"
@@ -117,15 +118,8 @@ local function getservers(host, port, q3protocol)
local servers = {}
for _, value in ipairs(pieces) do
local parts = {bin.unpack("CCCC>S", value)}
if #parts > 5 then
local o1 = parts[2]
local o2 = parts[3]
local o3 = parts[4]
local o4 = parts[5]
local p = parts[6]
table.insert(servers, {string.format("%d.%d.%d.%d", o1, o2, o3, o4), p})
end
local ip, port = string.unpack("c4 >I2", value)
table.insert(servers, {ipOps.str_to_ip(ip), port})
end
socket:close()
return servers