From 8f939279cae28c163ec0d15bb55bdc3c7c162865 Mon Sep 17 00:00:00 2001 From: dmiller Date: Wed, 25 Feb 2015 05:06:03 +0000 Subject: [PATCH] Let charset arg to stdnse.generate_random_string be a string The charset argument was intended to be a table, since Lua doesn't allow indexing strings with []. Unfortunately, irc-brute and tftp-enum passed a string instead, which meant that the output was always the empty string. This change allows both formats, and fixes those scripts. irc-brute and tftp-enum had been broken since r33632 (no released version of Nmap) --- nselib/stdnse.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index c154e2319..b98c04c40 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -252,7 +252,7 @@ end -- You can either provide your own charset or the function will use -- a default one which is [A-Z]. -- @param len Length of the string we want to generate. --- @param charset Charset that will be used to generate the string. +-- @param charset Charset that will be used to generate the string. String or table -- @return A random string of length len consisting of -- characters from charset if one was provided, otherwise -- charset defaults to [A-Z] letters. @@ -261,8 +261,15 @@ function generate_random_string(len, charset) local ascii_A = 65 local ascii_Z = 90 if charset then - for i=1,len do - t[i]=charset[random(#charset)] + if type(charset) == "string" then + for i=1,len do + local r = random(#charset) + t[i] = sub(charset, r, r) + end + else + for i=1,len do + t[i]=charset[random(#charset)] + end end else for i=1,len do