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

Remove bin.lua from a few more scripts

This commit is contained in:
dmiller
2018-09-03 22:14:16 +00:00
parent 69dd30727d
commit 6d8e307ac8
5 changed files with 57 additions and 67 deletions

View File

@@ -1,4 +1,3 @@
local bin = require "bin"
local dns = require "dns" local dns = require "dns"
local ipOps = require "ipOps" local ipOps = require "ipOps"
local nmap = require "nmap" local nmap = require "nmap"
@@ -69,10 +68,8 @@ local QTYPE_STRINGS = {
} }
local function build_ni_query(src, dst, qtype) local function build_ni_query(src, dst, qtype)
local payload, p, flags local flags
local nonce local nonce = openssl.rand_pseudo_bytes(8)
nonce = openssl.rand_pseudo_bytes(8)
if qtype == QTYPE_NODENAME then if qtype == QTYPE_NODENAME then
flags = 0x0000 flags = 0x0000
elseif qtype == QTYPE_NODEADDRESSES then elseif qtype == QTYPE_NODEADDRESSES then
@@ -84,8 +81,8 @@ local function build_ni_query(src, dst, qtype)
else else
error("Unknown qtype " .. qtype) error("Unknown qtype " .. qtype)
end end
payload = bin.pack(">SSAA", qtype, flags, nonce, dst) local payload = string.pack(">I2 I2", qtype, flags) .. nonce .. dst
p = packet.Packet:new() local p = packet.Packet:new()
p:build_icmpv6_header(ICMPv6_NODEINFOQUERY, ICMPv6_NODEINFOQUERY_IPv6ADDR, payload, src, dst) p:build_icmpv6_header(ICMPv6_NODEINFOQUERY, ICMPv6_NODEINFOQUERY_IPv6ADDR, payload, src, dst)
p:build_ipv6_packet(src, dst, packet.IPPROTO_ICMPV6) p:build_ipv6_packet(src, dst, packet.IPPROTO_ICMPV6)
@@ -129,11 +126,9 @@ end
-- a list of DNS names. In case of a parsing error, returns false and the -- a list of DNS names. In case of a parsing error, returns false and the
-- partial list of names that were parsed prior to the error. -- partial list of names that were parsed prior to the error.
local function try_decode_nodenames(data) local function try_decode_nodenames(data)
local ttl
local names = {} local names = {}
local pos = nil
pos, ttl = bin.unpack(">I", data, pos) local ttl, pos = string.unpack(">I4", data)
if not ttl then if not ttl then
return false, names return false, names
end end
@@ -186,7 +181,7 @@ local function stringify_nodeaddresses(flags, data)
local pos = nil local pos = nil
while true do while true do
pos, ttl, binaddr = bin.unpack(">IA16", data, pos) ttl, binaddr, pos = string.unpack(">I4 c16", data, pos)
if not ttl then if not ttl then
break break
end end
@@ -228,7 +223,7 @@ local function stringify_nodeipv4addresses(flags, data)
-- Okay, looks like it's really IP addresses. -- Okay, looks like it's really IP addresses.
while true do while true do
pos, ttl, binaddr = bin.unpack(">IA4", data, pos) ttl, binaddr, pos = string.unpack(">I4 c4", data, pos)
if not ttl then if not ttl then
break break
end end

View File

@@ -1,4 +1,3 @@
local bin = require "bin"
local os = require "os" local os = require "os"
local datetime = require "datetime" local datetime = require "datetime"
local mssql = require "mssql" local mssql = require "mssql"
@@ -77,12 +76,12 @@ action = function(host, port)
local recvtime = os.time() local recvtime = os.time()
tdsstream:Disconnect() tdsstream:Disconnect()
local pos, ttype = bin.unpack("C", response) local ttype, pos = string.unpack("B", response)
if ttype ~= mssql.TokenType.NTLMSSP_CHALLENGE then if ttype ~= mssql.TokenType.NTLMSSP_CHALLENGE then
return nil return nil
end end
local pos, data = bin.unpack("<P", response, pos) local data, pos = string.unpack("<s2", response, pos)
if not string.match(data, "^NTLMSSP") then if not string.match(data, "^NTLMSSP") then
return nil return nil
end end

View File

@@ -1,4 +1,3 @@
local bin = require "bin"
local comm = require "comm" local comm = require "comm"
local datetime = require "datetime" local datetime = require "datetime"
local os = require "os" local os = require "os"
@@ -104,7 +103,7 @@ action = function(host, port)
if status then if status then
local recvtime = os.time() local recvtime = os.time()
local _, sec, frac = bin.unpack(">II", buftres, 33) local sec, frac = string.unpack(">I4I4", buftres, 33)
-- The NTP epoch is 1900-01-01, so subtract 70 years to bring the date into -- The NTP epoch is 1900-01-01, so subtract 70 years to bring the date into
-- the range Lua expects. The number of seconds at 1970-01-01 is taken from -- the range Lua expects. The number of seconds at 1970-01-01 is taken from
-- the NTP4 reference above. -- the NTP4 reference above.
@@ -120,11 +119,10 @@ action = function(host, port)
if status then if status then
-- This only looks at the first fragment of what can possibly be several -- This only looks at the first fragment of what can possibly be several
-- fragments in the response. -- fragments in the response.
local _, data, k, q, v
-- Skip the first 10 bytes of the header, then get the data which is -- Skip the first 10 bytes of the header, then get the data which is
-- preceded by a 2-byte length. -- preceded by a 2-byte length.
_, data = bin.unpack(">P", bufrlres, 11) local data = string.unpack(">s2", bufrlres, 11)
-- loop over capture pairs which represent (key, value) -- loop over capture pairs which represent (key, value)
local function accumulate_output (...) local function accumulate_output (...)

View File

@@ -1,4 +1,3 @@
local bin = require "bin"
local ipOps = require "ipOps" local ipOps = require "ipOps"
local math = require "math" local math = require "math"
local nmap = require "nmap" local nmap = require "nmap"
@@ -270,22 +269,17 @@ end
--@param data The data to create a checksum for. --@param data The data to create a checksum for.
--@return An integer representing the checksum. --@return An integer representing the checksum.
local function p2p_checksum(data) local function p2p_checksum(data)
local pos, i
local hash = #data local hash = #data
stdnse.debug2("Conficker: Calculating checksum for %d-byte buffer", #data) stdnse.debug2("Conficker: Calculating checksum for %d-byte buffer", #data)
-- Get the first character data:sub(".", function(i)
pos, i = bin.unpack("<C", data) local h = hash ~ string.byte(i)
while i ~= nil do
local h = hash ~ i
-- Incorporate the current character into the checksum -- Incorporate the current character into the checksum
hash = (h + h) | (h >> 31) hash = (h + h) | (h >> 31)
hash = hash & 0xFFFFFFFF hash = hash & 0xFFFFFFFF
-- Get the next character
pos, i = bin.unpack("<C", data, pos)
end end
)
return hash return hash
end end
@@ -338,59 +332,64 @@ function p2p_parse(packet)
local data = {} local data = {}
-- Get the key -- Get the key
pos, data['key1'], data['key2'] = bin.unpack("<II", packet, pos) if #packet < 8 then
if(data['key2'] == nil) then
return false, "Packet was too short [1]" return false, "Packet was too short [1]"
end end
data['key1'], data['key2'], pos = string.unpack("<I4 I4", packet, pos)
-- Decrypt the second half of the packet using the key -- Decrypt the second half of the packet using the key
packet = string.sub(packet, 1, pos - 1) .. p2p_cipher(string.sub(packet, pos), data['key1'], data['key2']) packet = string.sub(packet, 1, pos - 1) .. p2p_cipher(string.sub(packet, pos), data['key1'], data['key2'])
-- Parse the flags -- Parse the flags
pos, data['flags'] = bin.unpack("<S", packet, pos) if #packet - pos + 1 < 2 then
if(data['flags'] == nil) then
return false, "Packet was too short [2]" return false, "Packet was too short [2]"
end end
data['flags'], pos = string.unpack("<I2", packet, pos)
-- Get the IP, if it's present -- Get the IP, if it's present
if(data['flags'] & mode_flags.FLAG_IP_INCLUDED) ~= 0 then if(data['flags'] & mode_flags.FLAG_IP_INCLUDED) ~= 0 then
pos, data['ip'], data['port'] = bin.unpack("<IS", packet, pos) if #packet - pos + 1 < 6 then
if(data['ip'] == nil) then
return false, "Packet was too short [3]" return false, "Packet was too short [3]"
end end
data['ip'], data['port'], pos = string.unpack("<I4 I2", packet, pos)
end end
-- Read the first unknown value, if present -- Read the first unknown value, if present
if(data['flags'] & mode_flags.FLAG_UNKNOWN0_INCLUDED) ~= 0 then if(data['flags'] & mode_flags.FLAG_UNKNOWN0_INCLUDED) ~= 0 then
pos, data['unknown0'] = bin.unpack("<I", packet, pos) if #packet - pos + 1 < 4 then
if(data['unknown0'] == nil) then
return false, "Packet was too short [3]" return false, "Packet was too short [3]"
end end
data['unknown0'], pos = string.unpack("<I4", packet, pos)
end end
-- Read the second unknown value, if present -- Read the second unknown value, if present
if(data['flags'] & mode_flags.FLAG_UNKNOWN1_INCLUDED) ~= 0 then if(data['flags'] & mode_flags.FLAG_UNKNOWN1_INCLUDED) ~= 0 then
pos, data['unknown1'] = bin.unpack("<I", packet, pos) if #packet - pos + 1 < 4 then
if(data['unknown1'] == nil) then
return false, "Packet was too short [4]" return false, "Packet was too short [4]"
end end
data['unknown1'], pos = string.unpack("<I4", packet, pos)
end end
-- Read the data, if present -- Read the data, if present
if(data['flags'] & mode_flags.FLAG_DATA_INCLUDED) ~= 0 then if(data['flags'] & mode_flags.FLAG_DATA_INCLUDED) ~= 0 then
pos, data['data_flags'], data['data_length'] = bin.unpack("<CS", packet, pos) if #packet - pos + 1 < 3 then
if(data['data_length'] == nil) then
return false, "Packet was too short [5]" return false, "Packet was too short [5]"
end end
pos, data['data'] = bin.unpack(string.format("A%d", data['data_length']), packet, pos) data['data_flags'], data['data_length'], pos = string.unpack("<B I2", packet, pos)
if(data['data'] == nil) then if #packet - pos + 1 < data.data_length then
return false, "Packet was too short [6]" return false, "Packet was too short [6]"
end end
data['data'], pos = string.unpack(("c%d"):format(data['data_length']), packet, pos)
end end
-- Read the sysinfo, if present -- Read the sysinfo, if present
if(data['flags'] & mode_flags.FLAG_SYSINFO_INCLUDED) ~= 0 then if(data['flags'] & mode_flags.FLAG_SYSINFO_INCLUDED) ~= 0 then
pos, data['sysinfo_systemtestflags'], local sysinfo_format = "<I2 BBI2 BB I2 I4 I2I2I4I2I2"
if #packet - pos + 1 < string.packsize(sysinfo_format) then
return false, "Packet was too short [7]"
end
data['sysinfo_systemtestflags'],
data['sysinfo_os_major'], data['sysinfo_os_major'],
data['sysinfo_os_minor'], data['sysinfo_os_minor'],
data['sysinfo_os_build'], data['sysinfo_os_build'],
@@ -402,20 +401,17 @@ function p2p_parse(packet)
data['sysinfo_unknown1'], data['sysinfo_unknown1'],
data['sysinfo_unknown2'], data['sysinfo_unknown2'],
data['sysinfo_unknown3'], data['sysinfo_unknown3'],
data['sysinfo_unknown4'] = bin.unpack("<SCCSCCSISSISS", packet, pos) data['sysinfo_unknown4'], pos = string.unpack(sysinfo_format, packet, pos)
if(data['sysinfo_unknown4'] == nil) then
return false, "Packet was too short [7]"
end
end end
-- Pull out the data that's used in the hash -- Pull out the data that's used in the hash
data['hash_data'] = string.sub(packet, 1, pos - 1) data['hash_data'] = string.sub(packet, 1, pos - 1)
-- Read the hash -- Read the hash
pos, data['hash'] = bin.unpack("<I", packet, pos) if #packet - pos + 1 < 4 then
if(data['hash'] == nil) then
return false, "Packet was too short [8]" return false, "Packet was too short [8]"
end end
data['hash'], pos = string.unpack("<I4", packet, pos)
-- Record the noise -- Record the noise
data['noise'] = string.sub(packet, pos) data['noise'] = string.sub(packet, pos)
@@ -456,18 +452,18 @@ local function p2p_create_packet(protocol, do_encryption)
end end
-- Add the key and flags that are always present (and skip over the boring stuff) -- Add the key and flags that are always present (and skip over the boring stuff)
local packet = bin.pack("<IIS", key1, key2, flags) local packet = string.pack("<I4 I4 I2", key1, key2, flags)
-- Generate the checksum for the packet -- Generate the checksum for the packet
local hash = p2p_checksum(packet) local hash = p2p_checksum(packet)
packet = packet .. bin.pack("<I", hash) packet = packet .. string.pack("<I4", hash)
-- Encrypt the full packet, except for the key and optional length -- Encrypt the full packet, except for the key and optional length
packet = string.sub(packet, 1, 8) .. p2p_cipher(string.sub(packet, 9), key1, key2) packet = string.sub(packet, 1, 8) .. p2p_cipher(string.sub(packet, 9), key1, key2)
-- Add the length in front if it's TCP -- Add the length in front if it's TCP
if(protocol == "tcp") then if(protocol == "tcp") then
packet = bin.pack("<P", packet) packet = string.pack("<s2", packet)
end end
return true, packet return true, packet
@@ -512,16 +508,20 @@ local function conficker_check(ip, port, protocol)
return false, "Timeout" return false, "Timeout"
elseif(response == "EOF") then elseif(response == "EOF") then
return false, "Couldn't connect" return false, "Couldn't connect"
elseif #response < 2 then
return false, "Data too short"
end end
-- If it's TCP, get the length and make sure we have the full packet -- If it's TCP, get the length and make sure we have the full packet
if(protocol == "tcp") then if(protocol == "tcp") then
local _, length = bin.unpack("<S", response, 1) local length = string.unpack("<I2", response)
while length > (#response - 2) do -- Only try for 2 timeouts to get the whole packet
local response2 local tries = 2
while length > (#response - 2) and tries > 0 do
tries = tries - 1
status, response2 = socket:receive_bytes(2) local status, response2 = socket:receive_bytes(length - (#response - 2))
if(status == false) then if(status == false) then
return false, "Couldn't receive bytes: " .. response2 return false, "Couldn't receive bytes: " .. response2
elseif(response2 == "ERROR") then elseif(response2 == "ERROR") then
@@ -593,9 +593,8 @@ action = function(host)
-- Reverse the IP's endianness -- Reverse the IP's endianness
ip = ipOps.todword(ip) ip = ipOps.todword(ip)
ip = bin.pack(">I", ip) ip = string.pack(">I4", ip)
local _ ip = string.unpack("<I4", ip)
_, ip = bin.unpack("<I", ip)
-- Generate the ports -- Generate the ports
local generated_ports = prng_generate_ports(ip, seed) local generated_ports = prng_generate_ports(ip, seed)

View File

@@ -1,4 +1,3 @@
local bin = require "bin"
local ipOps = require "ipOps" local ipOps = require "ipOps"
local nmap = require "nmap" local nmap = require "nmap"
local shortport = require "shortport" local shortport = require "shortport"
@@ -78,7 +77,7 @@ local function getservers(host, port, q3protocol)
if not status then if not status then
return {} return {}
end end
local probe = bin.pack("CCCCA", 0xff, 0xff, 0xff, 0xff, string.format("getservers %s empty full\n", q3protocol)) local probe = string.format("\xff\xff\xff\xffgetservers %s empty full\n", q3protocol)
socket:send(probe) socket:send(probe)
local data local data
@@ -88,7 +87,7 @@ local function getservers(host, port, q3protocol)
end end
nmap.set_port_state(host, port, "open") nmap.set_port_state(host, port, "open")
local magic = bin.pack("CCCCA", 0xff, 0xff, 0xff, 0xff, "getserversResponse") local magic = "\xff\xff\xff\xffgetserversResponse"
local tmp local tmp
while #data < #magic do -- get header while #data < #magic do -- get header
status, tmp = socket:receive() status, tmp = socket:receive()
@@ -103,7 +102,7 @@ local function getservers(host, port, q3protocol)
port.version.name = "quake3-master" port.version.name = "quake3-master"
nmap.set_port_version(host, port) nmap.set_port_version(host, port)
local EOT = bin.pack("ACCC", "EOT", 0, 0, 0) local EOT = "EOT\0\0\0"
local pieces = stdnse.strsplit("\\", data) local pieces = stdnse.strsplit("\\", data)
while pieces[#pieces] ~= EOT do -- get all data while pieces[#pieces] ~= EOT do -- get all data
status, tmp = socket:receive() status, tmp = socket:receive()