1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-18 13:39: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 ""
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 )
data = data .. 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, src_path )
.. bin.pack(">CIP", unicode_names, unicode_hint, dst_path )
.. bin.pack(">CIP", unicode_names, unicode_hint, new_name )
packet = self:create_fp_packet( REQUEST.Command, data_offset, data )
self:send_fp_packet( packet )
@@ -744,39 +744,28 @@ Proto = {
-- not tested, but should work (next tag is
-- tested)
local octet = {}
local j
local addr
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
for j = 1, 7 do
addr = addr .. string.format("%04x:", octet[j])
end
addr = addr .. string.format("%04x", octet[8])
table.insert(result.network_addresses, addr)
table.insert(result.network_addresses, table.concat(octet, ':'))
elseif tag == 0x07 then
-- 16 byte ipv6 and two byte port
local octet = {}
local port
local j
local addr
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
pos, port = bin.unpack(">S", packet.data, pos)
addr = "["
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)
table.insert(result.network_addresses,
string.format("[%s]:%d", table.concat(octet, ':'), port))
end
end

View File

@@ -20,6 +20,7 @@ local math = require "math"
local nmap = require "nmap"
local stdnse = require "stdnse"
local string = require "string"
local strbuf = require "strbuf"
local table = require "table"
_ENV = stdnse.module("dhcp", stdnse.seeall)
@@ -400,7 +401,7 @@ end
--@return status (true or false)
--@return The parsed response, as a table.
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
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
return true, packet
return true, strbuf.dump(packet)
end
---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
__tostring = function( self )
local data = bin.pack(">SS", self.Length, self.CodePoint )
if ( self.Data ) then
data = data .. bin.pack("A", self.Data)
end
return data
return bin.pack(">SSA", self.Length, self.CodePoint, self.Data or "" )
end,
--- Builds a DRDA Parameter from a string

View File

@@ -8,6 +8,7 @@
local bin = require "bin"
local table = require "table"
local stdnse = require "stdnse"
local strbuf = require "strbuf"
local ipOps = require "ipOps"
local packet = require "packet"
_ENV = stdnse.module("eigrp", stdnse.seeall)
@@ -308,7 +309,8 @@ EIGRP = {
--- 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
__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
-- If checksum not manually.
@@ -378,6 +380,7 @@ EIGRP = {
stdnse.debug1("eigrp.lua: TLV type %d unknown.", tlv.type)
end
end
data = strbuf.dump(data)
-- In the end, correct the checksum if not manually set
if not self.checksum then
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
data = bin.pack(">SSSSS", Constants.Message.SQ_INFO, 0x0006, #params + 6, 0x000c, 0x0004 )
data = data .. params .. bin.pack(">SSS", 0x0000, 0x0000, Constants.Message.SQ_EOT)
data = bin.pack(">SSSSSASSS", Constants.Message.SQ_INFO, 0x0006,
#params + 6, 0x000c, 0x0004, params, 0x0000, 0x0000,
Constants.Message.SQ_EOT)
return data
end
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -553,12 +553,11 @@ JavaField = {
getValue = function( self ) return self.value end,
__tostring = function( self )
local data = tostring(self.type) .. " " .. tostring(self.name)
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
return data
end,
toTable = function(self)
local data = {tostring(self.type) .. " " .. tostring(self.name)}
@@ -571,8 +570,7 @@ JavaField = {
table.insert(data, self.value)
end
else
--TODO: FIXME This is illegal, but I don't know what the intent was:
data = data .." = " .. tostring(self.value) --FIXME
table.insert(data, self.value)
end
end
return data

View File

@@ -242,19 +242,19 @@ local function processConnection( host, port, data )
end
end
local filecontent = ""
local filecontent = {}
-- Make sure we received all the blocks needed to proceed
for i=1, #blocks do
if ( not(blocks[i]) ) then
return false, ("Block #%d was missing in transfer")
end
filecontent = filecontent .. blocks[i]
filecontent[#filecontent+1] = blocks[i]
end
stdnse.debug1("Finished receiving file \"%s\"", filename)
-- 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)
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']
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)
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,
"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 = "", ""
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)
host_info = host_info..string_format(" %s)", target_key)
host_info = string_format(" (host:%s %s)", vuln_table.host.ip, target_key)
end
-- 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]))
if vuln_table.IDS and next(vuln_table.IDS) then
local ids_str = ""
local ids_t = {}
for id_type, id in pairs(vuln_table.IDS) do
-- ignore internal NMAP IDs
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))
end
end
if ids_str:len() > 0 then
insert(out, string_format(" IDs:%s", ids_str))
if next(ids_t) then
insert(out, string_format(" IDs: %s", table.concat(ids_t, " ")))
output_table.ids = ids_t
end
end
@@ -1852,10 +1847,8 @@ local format_vuln_base = function(vuln_table, showall)
if vuln_table.description then
local desc = format_vuln_special_fields(vuln_table.description)
if desc then
local desc_str = ""
for _, line in ipairs(desc) do
insert(out, string_format(" %s", line))
desc_str = desc_str .. line
end
output_table.description = vuln_table.description
end