From d37d285b9c61d605a5f685a1d677fd037c82fe33 Mon Sep 17 00:00:00 2001 From: Sparsh Agarwal Date: Wed, 11 Nov 2026 20:29:54 +0530 Subject: [PATCH] Fix terminal control characters in NSE logging --- nse_nmaplib.cc | 9 ++++++++- output.cc | 2 +- output.h | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nse_nmaplib.cc b/nse_nmaplib.cc index 67f05c62d..ea0aef1a2 100644 --- a/nse_nmaplib.cc +++ b/nse_nmaplib.cc @@ -4,6 +4,7 @@ #include #include "nmap.h" +#include "output.h" #include "nmap_error.h" #include "NmapOps.h" #include "FingerPrintResults.h" @@ -618,7 +619,13 @@ static int l_log_write (lua_State *L) static const char * const ops[] = {"stdout", "stderr", NULL}; static const int logs[] = {LOG_STDOUT, LOG_STDERR}; int log = logs[luaL_checkoption(L, 1, NULL, ops)]; - log_write(log, "%s: %s\n", SCRIPT_ENGINE, luaL_checkstring(L, 2)); + + size_t message_len; + const char *message = luaL_checklstring(L, 2, &message_len); + std::string escaped_message = + escape_for_screen(std::string(message, message_len)); + + log_write(log, "%s: %s\n", SCRIPT_ENGINE, escaped_message.c_str()); log_flush(log); return 0; } diff --git a/output.cc b/output.cc index 995949f92..932dbed56 100644 --- a/output.cc +++ b/output.cc @@ -388,7 +388,7 @@ int print_iflist(void) { #ifndef NOLUA /* Escape control characters to make a string safe to display on a terminal. */ -static std::string escape_for_screen(const std::string s) { +std::string escape_for_screen(const std::string s) { std::string r; for (unsigned int i = 0; i < s.size(); i++) { diff --git a/output.h b/output.h index 99862a858..a8581398e 100644 --- a/output.h +++ b/output.h @@ -112,6 +112,11 @@ class Target; #include #include +#ifndef NOLUA +/* Escape control characters to make a string safe to display on a terminal. */ +std::string escape_for_screen(const std::string s); +#endif + #if HAVE_SYS_TIME_H #include #endif