Replace concat-loop padding with string.rep

This commit is contained in:
dmiller 2015-02-27 14:55:42 +00:00
parent e275a96c72
commit 4f0518bd93
10 changed files with 16 additions and 63 deletions

View file

@ -1846,15 +1846,7 @@ Util =
-- @param len number containing the length of the new string
-- @return str string containing the new string
ZeroPad = function( str, len )
if len < str:len() then
return
end
for i=1, len - str:len() do
str = str .. string.char(0)
end
return str
return str .. string.rep('\0', len - str:len())
end,
--- Splits a path into two pieces, directory and file

View file

@ -775,13 +775,7 @@ StringUtil =
-- @param len the total length of the finished string
-- @return str string containing the padded string
padWithChar = function( str, chr, len )
if ( len < #str ) then
return str
end
for i=1, (len - #str) do
str = str .. chr
end
return str
return str .. string.rep(chr, len - #str)
end,
}

View file

@ -126,8 +126,6 @@ Packet = {
local pad = 4 - ((#kvps + 48) % 4)
pad = ( pad == 4 ) and 0 or pad
for i=1, pad do kvps = kvps .. "\0" end
local len = bit.lshift( self.total_ahs_len, 24 ) + self.data_seg_len
local flags = bit.lshift( ( self.flags.transit or 0 ), 7 )
flags = flags + bit.lshift( ( self.flags.continue or 0 ), 6)
@ -136,12 +134,12 @@ Packet = {
local opcode = self.opcode + bit.lshift((self.immediate or 0), 6)
local data = bin.pack(">CCCCICSCSSISSIILLA", opcode,
local data = bin.pack(">CCCCICSCSSISSIILLAA", opcode,
flags, self.ver_max, self.ver_min, len,
bit.lshift( self.isid.t, 6 ) + bit.band( self.isid.a, 0x3f),
self.isid.b, self.isid.c, self.isid.d, self.tsih,
self.initiator_task_tag, self.cid, reserved, self.cmdsn,
self.expstatsn, reserved, reserved, kvps )
self.expstatsn, reserved, reserved, kvps, string.rep('\0', pad) )
return data
end
@ -271,7 +269,7 @@ Packet = {
flags = flags + bit.lshift( (self.flags.continue or 0), 6 )
local kvps = tostring(self.kvp)
for i=1, (#kvps % 2) do kvps = kvps .. "\0" end
kvps = kvps .. string.rep('\0', #kvps % 2)
self.data_seg_len = #kvps
local len = bit.lshift( self.total_ahs_len, 24 ) + self.data_seg_len

View file

@ -589,7 +589,6 @@ end
function marshall_ascii(str, max_length)
local buffer_length
local result
local padding = ""
buffer_length = #str + 1
@ -597,9 +596,7 @@ function marshall_ascii(str, max_length)
max_length = buffer_length
end
while((#(str .. string.char(0 .. padding)) % 4) ~= 0) do
padding = padding .. string.char(0)
end
local padding = string.rep('\0', (4 - (buffer_length % 4)) % 4)
result = bin.pack("<IIIzA",
max_length,

View file

@ -1108,9 +1108,7 @@ Util =
-- @param len number containing the length of the new string
-- @return str string containing the new string
ZeroPad = function( str, len )
if len < str:len() then return end
for i=1, len - str:len() do str = str .. string.char(0) end
return str
return str .. string.rep('\0', len - #str)
end,
-- Removes trailing nulls

View file

@ -51,9 +51,7 @@ function name_encode(name, scope)
padding = "\0"
end
repeat
name = name .. padding
until #name == 16
name = name .. string.rep(padding, 16 - #name)
end
-- Convert to uppercase

View file

@ -53,15 +53,7 @@ v2 =
-- @param len number containing the wanted length
-- @return string containing the padded string value
zeroPad = function(str, len)
local padding = len - str:len()
if ( padding < 0 ) then
return str
end
for i=1,padding do
str = str .. string.char(0x00)
end
return str
return str .. string.rep('\0', len - #str)
end,
messageDecoder = {

View file

@ -2381,10 +2381,7 @@ function send_transaction_waitnamedpipe(smb, priority, pipe, overrides)
priority -- Handle to open file
)
while(((#pipe + 1 + #padding) % 4) ~= 0) do
padding = padding .. string.char(0)
end
data = bin.pack("<zA", pipe, padding);
data = bin.pack("zA", pipe, string.rep('\0', (4 - ((#pipe+1) % 4)) % 4))
-- Send the transaction request
stdnse.debug2("SMB: Sending SMB_COM_TRANSACTION (WaitNamedPipe)")
@ -2763,17 +2760,12 @@ function find_files(smbstate, fname, options)
local loi = 260 -- Level of interest, return SMB_FIND_FILE_BOTH_DIRECTORY_INFO
local storage_type = 0 -- despite the documentation of having to be either 0x01 or 0x40, wireshark reports 0
local function_parameters = bin.pack("<SSSSIA", nattrs, srch_count, flags, loi, storage_type, fname)
-- SMB header: 32
-- trans2 header: 36
-- FIND_FIRST2 parameters: #function_parameters
local pad = ( 32 + 36 + #function_parameters ) % 4
if ( pad > 0 ) then
for i=1, ( 4-pad ) do
function_parameters = function_parameters .. "\0"
end
end
-- FIND_FIRST2 parameters: 12 + #fname
local pad = ( 32 + 36 + 12 + #fname ) % 4
local function_parameters = bin.pack("<SSSSIAA",
nattrs, srch_count, flags, loi, storage_type, fname, string.rep('\0', (4 - pad) % 4))
local function next_item()

View file

@ -1456,11 +1456,7 @@ Crypt = {
local key = bin.pack("H", "0123456789abcdef")
-- do padding
if ( #uspw % 8 > 0 ) then
for i=1,(8-(#uspw % 8)) do
uspw = uspw .. "\0"
end
end
uspw = uspw .. string.rep('\0', (8 - (#uspw % 8)) % 8)
local iv2 = openssl.encrypt( "DES-CBC", key, nil, uspw, false ):sub(-8)
local enc = openssl.encrypt( "DES-CBC", iv2, nil, uspw, false ):sub(-8)

View file

@ -196,11 +196,7 @@ VNC = {
-- @param password string containing the password to process
-- @return password string containing the processed password
createVNCDESKey = function( self, password )
if ( #password < 8 ) then
for i=1, (8 - #password) do
password = password .. string.char(0x00)
end
end
password = password .. string.rep('\0', 8 - #password)
local newpass = ""
for i=1, 8 do