1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-18 05:29:02 +00:00

String concat cleanup grab-bag

Mostly just eliminating concatenation-reassignments by chaining
concatenations, reordering assignments to allow better use of bin.pack,
and using tables to store intermediate results before concatenating
them. Used strbuf as a quick fix in dhcp.lua. Eliminated some unused
string variables in vulns.lua.
This commit is contained in:
dmiller
2015-03-02 14:39:29 +00:00
parent c1b2429efd
commit 03110e7e89
13 changed files with 57 additions and 82 deletions

View File

@@ -583,9 +583,9 @@ Proto = {
local new_name = new_name or "" local new_name = new_name or ""
data = bin.pack(">CCSISI", COMMAND.FPCopyFile, pad, src_vol, src_did, dst_vol, dst_did ) data = bin.pack(">CCSISI", COMMAND.FPCopyFile, pad, src_vol, src_did, dst_vol, dst_did )
data = data .. bin.pack(">CIP", unicode_names, unicode_hint, src_path ) .. bin.pack(">CIP", unicode_names, unicode_hint, src_path )
data = data .. bin.pack(">CIP", unicode_names, unicode_hint, dst_path ) .. bin.pack(">CIP", unicode_names, unicode_hint, dst_path )
data = data .. bin.pack(">CIP", unicode_names, unicode_hint, new_name ) .. bin.pack(">CIP", unicode_names, unicode_hint, new_name )
packet = self:create_fp_packet( REQUEST.Command, data_offset, data ) packet = self:create_fp_packet( REQUEST.Command, data_offset, data )
self:send_fp_packet( packet ) self:send_fp_packet( packet )
@@ -744,39 +744,28 @@ Proto = {
-- not tested, but should work (next tag is -- not tested, but should work (next tag is
-- tested) -- tested)
local octet = {} local octet = {}
local j
local addr
for j = 1, 8 do for j = 1, 8 do
pos, octet[j] = bin.unpack(">S", packet.data, pos) local o
pos, o = bin.unpack(">S", packet.data, pos)
octet[j] = string.format("%04x", o)
end end
for j = 1, 7 do table.insert(result.network_addresses, table.concat(octet, ':'))
addr = addr .. string.format("%04x:", octet[j])
end
addr = addr .. string.format("%04x", octet[8])
table.insert(result.network_addresses, addr)
elseif tag == 0x07 then elseif tag == 0x07 then
-- 16 byte ipv6 and two byte port -- 16 byte ipv6 and two byte port
local octet = {} local octet = {}
local port local port
local j
local addr
for j = 1, 8 do for j = 1, 8 do
pos, octet[j] = bin.unpack(">S", packet.data, pos) local o
pos, o = bin.unpack(">S", packet.data, pos)
octet[j] = string.format("%04x", o)
end end
pos, port = bin.unpack(">S", packet.data, pos) pos, port = bin.unpack(">S", packet.data, pos)
addr = "[" table.insert(result.network_addresses,
string.format("[%s]:%d", table.concat(octet, ':'), port))
for j = 1, 7 do
addr = addr .. string.format("%04x:", octet[j])
end
addr = addr .. string.format("%04x]:%d", octet[8], port)
table.insert(result.network_addresses, addr)
end end
end end

View File

@@ -20,6 +20,7 @@ local math = require "math"
local nmap = require "nmap" local nmap = require "nmap"
local stdnse = require "stdnse" local stdnse = require "stdnse"
local string = require "string" local string = require "string"
local strbuf = require "strbuf"
local table = require "table" local table = require "table"
_ENV = stdnse.module("dhcp", stdnse.seeall) _ENV = stdnse.module("dhcp", stdnse.seeall)
@@ -400,7 +401,7 @@ end
--@return status (true or false) --@return status (true or false)
--@return The parsed response, as a table. --@return The parsed response, as a table.
function dhcp_build(request_type, ip_address, mac_address, options, request_options, overrides, lease_time, transaction_id) function dhcp_build(request_type, ip_address, mac_address, options, request_options, overrides, lease_time, transaction_id)
local packet = '' local packet = strbuf.new()
-- Set up the default overrides -- Set up the default overrides
if(overrides == nil) then if(overrides == nil) then
@@ -451,7 +452,7 @@ function dhcp_build(request_type, ip_address, mac_address, options, request_opti
packet = packet .. bin.pack(">C", 0xFF) -- Termination packet = packet .. bin.pack(">C", 0xFF) -- Termination
return true, packet return true, strbuf.dump(packet)
end end
---Parse a DHCP packet (either a request or a response) and return the results ---Parse a DHCP packet (either a request or a response) and return the results

View File

@@ -306,11 +306,7 @@ DRDAParameter = {
-- --
-- @return data string containing the DRDA Parameter -- @return data string containing the DRDA Parameter
__tostring = function( self ) __tostring = function( self )
local data = bin.pack(">SS", self.Length, self.CodePoint ) return bin.pack(">SSA", self.Length, self.CodePoint, self.Data or "" )
if ( self.Data ) then
data = data .. bin.pack("A", self.Data)
end
return data
end, end,
--- Builds a DRDA Parameter from a string --- Builds a DRDA Parameter from a string

View File

@@ -8,6 +8,7 @@
local bin = require "bin" local bin = require "bin"
local table = require "table" local table = require "table"
local stdnse = require "stdnse" local stdnse = require "stdnse"
local strbuf = require "strbuf"
local ipOps = require "ipOps" local ipOps = require "ipOps"
local packet = require "packet" local packet = require "packet"
_ENV = stdnse.module("eigrp", stdnse.seeall) _ENV = stdnse.module("eigrp", stdnse.seeall)
@@ -308,7 +309,8 @@ EIGRP = {
--- Converts the request to a string suitable to be sent over a socket. --- Converts the request to a string suitable to be sent over a socket.
-- @return data string containing the complete request to send over the socket -- @return data string containing the complete request to send over the socket
__tostring = function(self) __tostring = function(self)
local data = bin.pack(">C", self.ver) -- Version 2 local data = strbuf.new()
data = data .. bin.pack(">C", self.ver) -- Version 2
data = data .. bin.pack(">C", self.opcode) -- Opcode: Hello data = data .. bin.pack(">C", self.opcode) -- Opcode: Hello
-- If checksum not manually. -- If checksum not manually.
@@ -378,6 +380,7 @@ EIGRP = {
stdnse.debug1("eigrp.lua: TLV type %d unknown.", tlv.type) stdnse.debug1("eigrp.lua: TLV type %d unknown.", tlv.type)
end end
end end
data = strbuf.dump(data)
-- In the end, correct the checksum if not manually set -- In the end, correct the checksum if not manually set
if not self.checksum then if not self.checksum then
data = data:sub(1,2) .. bin.pack(">S", packet.in_cksum(data)) .. data:sub(5) data = data:sub(1,2) .. bin.pack(">S", packet.in_cksum(data)) .. data:sub(5)

View File

@@ -722,8 +722,9 @@ Packet.SQ_INFO =
end end
end end
data = bin.pack(">SSSSS", Constants.Message.SQ_INFO, 0x0006, #params + 6, 0x000c, 0x0004 ) data = bin.pack(">SSSSSASSS", Constants.Message.SQ_INFO, 0x0006,
data = data .. params .. bin.pack(">SSS", 0x0000, 0x0000, Constants.Message.SQ_EOT) #params + 6, 0x000c, 0x0004, params, 0x0000, 0x0000,
Constants.Message.SQ_EOT)
return data return data
end end
} }

View File

@@ -130,20 +130,14 @@ JDWPCommandPacket = {
-- Packs command packet as a string od bytes, ready to be sent -- Packs command packet as a string od bytes, ready to be sent
-- to the target debuggee. -- to the target debuggee.
pack = function(self) pack = function(self)
local packed_packet local data = self.data or ""
if self.data == nil then return bin.pack(">IICCC",
packed_packet = bin.pack(">I",11) -- length - minimal header is 11 bytes 11 + #data, -- length - minimal header is 11 bytes
else self.id,
packed_packet = bin.pack(">I",11 + #self.data) -- length with data 0, -- flag
end self.command_set,
packed_packet = packed_packet .. bin.pack(">I",self.id) self.command,
packed_packet = packed_packet .. bin.pack(">C",0) -- flag data)
packed_packet = packed_packet .. bin.pack(">C",self.command_set)
packed_packet = packed_packet .. bin.pack(">C",self.command)
if self.data then
packed_packet = packed_packet .. self.data
end
return packed_packet
end end
} }

View File

@@ -403,8 +403,7 @@ function unbindRequest( socket )
encoder:registerTagEncoders(tagEncoder) encoder:registerTagEncoders(tagEncoder)
ldapMessageId = ldapMessageId +1 ldapMessageId = ldapMessageId +1
ldapMsg = encode( ldapMessageId ) ldapMsg = encode( ldapMessageId ) .. encodeLDAPOp( APPNO.UnbindRequest, false, nil)
ldapMsg = ldapMsg .. encodeLDAPOp( APPNO.UnbindRequest, false, nil)
packet = encoder:encodeSeq( ldapMsg ) packet = encoder:encodeSeq( ldapMsg )
try( socket:send( packet ) ) try( socket:send( packet ) )
return true, "" return true, ""

View File

@@ -299,9 +299,7 @@ function do_nbstat(host)
0, -- Answers 0, -- Answers
0, -- Authority 0, -- Authority
0 -- Extra 0 -- Extra
) ) .. bin.pack(">zSS",
query = query .. bin.pack(">zSS",
encoded_name, -- Encoded name encoded_name, -- Encoded name
0x0021, -- Query type (0x21 = NBSTAT) 0x0021, -- Query type (0x21 = NBSTAT)
0x0001 -- Class = IN 0x0001 -- Class = IN

View File

@@ -150,8 +150,7 @@ Helper = {
status, data = DominoPacket:new():read( self.domsock ) status, data = DominoPacket:new():read( self.domsock )
id_data = id_data:sub(33) id_data = id_data:sub(33) .. data:sub(11, total_len - #id_data + 11)
id_data = id_data .. data:sub(11, total_len - #id_data + 11)
return true, id_data return true, id_data
end, end,

View File

@@ -68,15 +68,19 @@ Packet = {
end, end,
__tostring = function(self) __tostring = function(self)
local len = (self.code ~= 0xF0 and #self.data + 1 or 2) local len, eot
local data = bin.pack("CC", if self.code == 0xF0 then
len, eot = "\x80"
self.code or 0 len = 2
) else
eot = ""
if ( self.code == 0xF0 ) then len = #self.data + 1
data = data .. "\x80" -- EOT
end end
local data = bin.pack("CCA",
len,
self.code or 0,
eot
)
return data .. self.data return data .. self.data
end, end,

View File

@@ -553,12 +553,11 @@ JavaField = {
getValue = function( self ) return self.value end, getValue = function( self ) return self.value end,
__tostring = function( self ) __tostring = function( self )
local data = tostring(self.type) .. " " .. tostring(self.name)
if self.value ~= nil then if self.value ~= nil then
data = data .." = " .. tostring(self.value) return string.format("%s %s = %s", self.type, self.name, self.value)
else
return string.format("%s %s", self.type, self.name)
end end
return data
end, end,
toTable = function(self) toTable = function(self)
local data = {tostring(self.type) .. " " .. tostring(self.name)} local data = {tostring(self.type) .. " " .. tostring(self.name)}
@@ -571,8 +570,7 @@ JavaField = {
table.insert(data, self.value) table.insert(data, self.value)
end end
else else
--TODO: FIXME This is illegal, but I don't know what the intent was: table.insert(data, self.value)
data = data .." = " .. tostring(self.value) --FIXME
end end
end end
return data return data

View File

@@ -242,19 +242,19 @@ local function processConnection( host, port, data )
end end
end end
local filecontent = "" local filecontent = {}
-- Make sure we received all the blocks needed to proceed -- Make sure we received all the blocks needed to proceed
for i=1, #blocks do for i=1, #blocks do
if ( not(blocks[i]) ) then if ( not(blocks[i]) ) then
return false, ("Block #%d was missing in transfer") return false, ("Block #%d was missing in transfer")
end end
filecontent = filecontent .. blocks[i] filecontent[#filecontent+1] = blocks[i]
end end
stdnse.debug1("Finished receiving file \"%s\"", filename) stdnse.debug1("Finished receiving file \"%s\"", filename)
-- Add anew file to the global infiles table -- Add anew file to the global infiles table
table.insert( infiles, File:new(filename, filecontent, host) ) table.insert( infiles, File:new(filename, table.concat(filecontent), host) )
local condvar = nmap.condvar(infiles) local condvar = nmap.condvar(infiles)
condvar "broadcast" condvar "broadcast"

View File

@@ -689,9 +689,8 @@ local l_update_id = function(fid_table, id_type, id, vuln_table)
local push_table = fid_table[id_type][id]['ENTRIES'] local push_table = fid_table[id_type][id]['ENTRIES']
if vuln_table.host and next(vuln_table.host) then if vuln_table.host and next(vuln_table.host) then
local host_info = string_format(" (host:%s", vuln_table.host.ip)
local target_key = l_get_host_port_key(vuln_table) local target_key = l_get_host_port_key(vuln_table)
host_info = host_info..string_format(" %s)", target_key) local host_info = string_format(" (host:%s %s)", vuln_table.host.ip, target_key)
debug(5, debug(5,
"vulns.lua: Updating VULNS.FILTERS_IDS{} with '%s' ID:%s:%s %s", "vulns.lua: Updating VULNS.FILTERS_IDS{} with '%s' ID:%s:%s %s",
@@ -1006,10 +1005,8 @@ local l_add = function(vulndb, vuln_table)
local host_info, target_key = "", "" local host_info, target_key = "", ""
if vuln_table.host and next(vuln_table.host) then if vuln_table.host and next(vuln_table.host) then
host_info = string_format(" (host:%s", vuln_table.host.ip)
target_key = l_get_host_port_key(vuln_table) target_key = l_get_host_port_key(vuln_table)
host_info = host_info..string_format(" %s)", target_key) host_info = string_format(" (host:%s %s)", vuln_table.host.ip, target_key)
end end
-- Search the Filters IDS for the vulnerability -- Search the Filters IDS for the vulnerability
@@ -1817,18 +1814,16 @@ local format_vuln_base = function(vuln_table, showall)
string_format(" State: %s", STATE_MSG[vuln_table.state])) string_format(" State: %s", STATE_MSG[vuln_table.state]))
if vuln_table.IDS and next(vuln_table.IDS) then if vuln_table.IDS and next(vuln_table.IDS) then
local ids_str = ""
local ids_t = {} local ids_t = {}
for id_type, id in pairs(vuln_table.IDS) do for id_type, id in pairs(vuln_table.IDS) do
-- ignore internal NMAP IDs -- ignore internal NMAP IDs
if id_type ~= 'NMAP_ID' then if id_type ~= 'NMAP_ID' then
ids_str = ids_str .. string_format(" %s:%s", id_type, id)
table.insert(ids_t, string_format("%s:%s", id_type, id)) table.insert(ids_t, string_format("%s:%s", id_type, id))
end end
end end
if ids_str:len() > 0 then if next(ids_t) then
insert(out, string_format(" IDs:%s", ids_str)) insert(out, string_format(" IDs: %s", table.concat(ids_t, " ")))
output_table.ids = ids_t output_table.ids = ids_t
end end
end end
@@ -1852,10 +1847,8 @@ local format_vuln_base = function(vuln_table, showall)
if vuln_table.description then if vuln_table.description then
local desc = format_vuln_special_fields(vuln_table.description) local desc = format_vuln_special_fields(vuln_table.description)
if desc then if desc then
local desc_str = ""
for _, line in ipairs(desc) do for _, line in ipairs(desc) do
insert(out, string_format(" %s", line)) insert(out, string_format(" %s", line))
desc_str = desc_str .. line
end end
output_table.description = vuln_table.description output_table.description = vuln_table.description
end end