random padding bytes for strings

This commit is contained in:
Denis Knauf 2015-07-03 15:35:20 +02:00
parent a2b6bde057
commit 11d0f09c6d
2 changed files with 9 additions and 6 deletions

View file

@ -67,15 +67,18 @@ module NSCA
end
# Builds a null terminated, null padded string of length maxlen
def str2cstr( str, maxlen = nil)
def str2cstr str, maxlen = nil
str = str.to_s
str = str.to_s[0..(maxlen-2)] if maxlen
"#{str}\x00"
end
def str2nstr( str, maxlen = nil)
str = str.to_s.gsub( ' ', "\x00")
def rand_padding( str, maxlen) str + SecureRandom.random_bytes( maxlen - str.length) end
def str2cstr_rand_padding( str, maxlen = nil) rand_padding str2cstr( str, maxlen), maxlen end
def str2nstr str, maxlen = nil
str = str.to_s.gsub ' ', "\x00"
"#{str} "
end
def str2nstr_rand_padding( str, maxlen = nil) rand_padding str2nstr( str, maxlen), maxlen end
def cstr2str( str, maxlen = nil) str[ 0, str.index( ?\0) || ((maxlen||str.length+1)-1)] end
def nstr2str( str, maxlen = nil) str[ 0, str.index( ' ') || ((maxlen||str.length+1)-1)].gsub( "\x00", ' ') end
end

View file

@ -60,9 +60,9 @@ module NSCA
0, # crc32 (unknown yet)
(timestamp || Time.now).to_i,
return_code.to_i,
NSCA::str2cstr( hostname || `hostname -f`, cl::HOSTNAME_LENGTH),
NSCA::str2cstr( service, cl::SERVICE_LENGTH),
NSCA::str2cstr( status, cl::PLUGIN_OUTPUT_LENGTH) # incl perfdata
NSCA::str2cstr_rand_padding( hostname || `hostname -f`, cl::HOSTNAME_LENGTH),
NSCA::str2cstr_rand_padding( service, cl::SERVICE_LENGTH),
NSCA::str2cstr_rand_padding( status, cl::PLUGIN_OUTPUT_LENGTH) # incl perfdata
]
# generate crc32 and put it at entry[2...6]
entry[1] = NSCA::crc32 entry.pack( cl::PACK_STRING)