From cf406cb5e02aa2e4fe2af8bf56b0b6da118d2563 Mon Sep 17 00:00:00 2001 From: nnposter Date: Sat, 23 Sep 2017 23:57:53 +0000 Subject: [PATCH] Strips off ANSI terminal escape sequences from the telnet session stream to simplify pattern matching --- scripts/telnet-brute.nse | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/scripts/telnet-brute.nse b/scripts/telnet-brute.nse index 8053529d9..ef7c6b0c5 100644 --- a/scripts/telnet-brute.nse +++ b/scripts/telnet-brute.nse @@ -97,8 +97,7 @@ local is_login_success = function (str) local lcstr = str:lower() return lcstr:find("[/>%%%$#]%s*$") -- general prompt or lcstr:find("^last login%s*:") -- linux telnetd - or lcstr:find("main%smenu%f[%W]") -- Netgear RM356 - or lcstr:find("main\x1B%[%d+;%d+hmenu%f[%W]") -- Netgear RM356 + or lcstr:find("%f[%w]main%smenu%f[%W]") -- Netgear RM356 or lcstr:find("^enter terminal emulation:%s*$") -- Hummingbird telnetd or lcstr:find("%f[%w]select an option%f[%W]") -- Zebra PrintServer end @@ -120,6 +119,20 @@ local is_login_failure = function (str) end +--- +-- Strip off ANSI escape sequences (terminal codes) that start with [ +-- and replace them with white space, namely the VT character (0x0B). +-- This way their new representation can be naturally matched with pattern %s. +-- +-- @param str The string that needs to be strained +-- @return The same string without the escape sequences +local remove_termcodes = function (str) + local mark = '\x0B' + return str:gsub('\x1B%[%??%d*%a', mark) + :gsub('\x1B%[%??%d*;%d*%a', mark) +end + + --- -- Simple class to encapsulate connection operations local Connection = { methods = {} } @@ -269,7 +282,7 @@ Connection.methods.get_line = function (self) self:fill_buffer(data) end - return self.buffer:match('^[^\r\n]*') + return remove_termcodes(self.buffer:match('^[^\r\n]*')) end