Ncat: Add --ignore-sigint option to ignore Ctrl+C. Fixes #2752

When Ncat is used as an SSH ProxyCommand, pressing Ctrl+C in an interactive
SSH session sends SIGINT to the foreground process group. While SSH handles
the signal to send an interrupt packet to the remote session, Ncat terminates
under the default signal handler, closing the proxy connection.

Add the --ignore-sigint option so Ncat ignores SIGINT (and Windows Console
Ctrl-C/Ctrl-Break events), allowing interactive sessions through Ncat proxies
to handle Ctrl+C without breaking the tunnel.
This commit is contained in:
amitpadhan525 2026-10-05 23:52:29 +05:30
parent 527ef01bf3
commit fe7fcae2ab
7 changed files with 41 additions and 1 deletions

View file

@ -79,6 +79,7 @@ Options taking a time assume seconds\&. Append \*(Aqms\*(Aq for milliseconds,
\-\-send\-only Only send data, ignoring received; quit on EOF
\-\-recv\-only Only receive data, never send anything
\-\-no\-shutdown Continue half\-duplex when receiving EOF on stdin
\-\-ignore\-sigint Do not terminate upon receiving SIGINT/Ctrl+C
\-q <time> After EOF on stdin, wait <time> then quit\&.
\-\-allow Allow only given hosts to connect to Ncat
\-\-allowfile A file of hosts allowed to connect to Ncat
@ -541,6 +542,11 @@ If this option is passed, then Ncat will only send data and will ignore anything
If this option is passed, Ncat will not invoke shutdown on a socket after seeing EOF on stdin\&. This is provided for backward\-compatibility with OpenBSD netcat, which exhibits this behavior when executed with its \*(Aq\-d\*(Aq option\&.
.RE
.PP
\fB\-\-ignore\-sigint\fR (Ignore SIGINT / Ctrl+C)
.RS 4
If this option is passed, Ncat will ignore the SIGINT signal (Ctrl+C on terminal or console)\&. This is particularly useful when Ncat is used as an SSH \fBProxyCommand\fR, preventing Ctrl+C during remote interactive sessions from terminating the proxy connection\&.
.RE
.PP
\fB\-n\fR, \fB\-\-nodns\fR (Do not resolve hostnames)
.RS 4
Completely disable hostname resolution across all Ncat options, such as the destination, source address, source routing hops, and the proxy\&. All addresses must be specified numerically\&. (Note that resolution of proxy destinations is controlled separately via option

View file

@ -141,6 +141,7 @@ void options_init(void)
o.proxyaddr = NULL;
o.proxydns = PROXYDNS_REMOTE;
o.zerobyte = 0;
o.ignore_sigint = 0;
#ifdef HAVE_OPENSSL
o.ssl = 0;

View file

@ -158,6 +158,7 @@ struct options {
char* sslservername;
char *sslalpn;
int zerobyte;
int ignore_sigint;
};
extern struct options o;
@ -240,6 +241,10 @@ extern int ncat_hostaccess(char *matchaddr, char *filename, char *remoteip);
Defined in ncat_posix.c and ncat_win.c. */
extern void set_lf_mode(void);
/* Ignore SIGINT/Ctrl+C signals if requested by the user.
Defined in ncat_posix.c and ncat_win.c. */
extern void set_sigint_handler(void);
extern int getaddrfamily(const char *addr);
extern int setenv_portable(const char *name, const char *value);
extern void setup_environment(struct fdinfo *fdinfo);

View file

@ -272,7 +272,8 @@ static void install_signal_handlers(void)
sa.sa_handler = signal_propagate_exit;
sa.sa_flags = SA_RESTART | SA_RESETHAND;
sigaddset(&sa.sa_mask, SIGCHLD);
sigaction(SIGINT, &sa, NULL);
if (!o.ignore_sigint)
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);

View file

@ -262,6 +262,7 @@ int main(int argc, char *argv[])
{"source", required_argument, NULL, 's'},
{"send-only", no_argument, &o.sendonly, 1},
{"no-shutdown", no_argument, &o.noshutdown,1},
{"ignore-sigint", no_argument, &o.ignore_sigint, 1},
{"broker", no_argument, NULL, 0},
{"chat", no_argument, NULL, 0},
{"talk", no_argument, NULL, 0},
@ -621,6 +622,7 @@ int main(int argc, char *argv[])
" --send-only Only send data, ignoring received; quit on EOF\n"
" --recv-only Only receive data, never send anything\n"
" --no-shutdown Continue half-duplex when receiving EOF on stdin\n"
" --ignore-sigint Do not terminate upon receiving SIGINT/Ctrl+C\n"
" -q <time> After EOF on stdin, wait <time> then quit.\n"
" --allow Allow only given hosts to connect to Ncat\n"
" --allowfile A file of hosts allowed to connect to Ncat\n"
@ -992,6 +994,7 @@ int main(int argc, char *argv[])
/* Do whatever is necessary to receive \n for line endings on input from
the console. A no-op on Unix. */
set_lf_mode();
set_sigint_handler();
#ifdef HAVE_LUA
if (o.execmode == EXEC_LUA)

View file

@ -438,6 +438,13 @@ void set_lf_mode(void)
/* Nothing needed. */
}
void set_sigint_handler(void)
{
if (o.ignore_sigint) {
Signal(SIGINT, SIG_IGN);
}
}
#ifdef HAVE_OPENSSL
#define NCAT_CA_CERTS_PATH (NCAT_DATADIR "/" NCAT_CA_CERTS_FILE)

View file

@ -82,6 +82,23 @@ void set_lf_mode(void)
_setmode(STDOUT_FILENO, _O_BINARY);
}
static BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
if (ctrl_type == CTRL_C_EVENT || ctrl_type == CTRL_BREAK_EVENT) {
if (o.debug)
logdebug("Ignored Console Ctrl Event %lu\n", (unsigned long) ctrl_type);
return TRUE;
}
return FALSE;
}
void set_sigint_handler(void)
{
if (o.ignore_sigint) {
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
}
}
#ifdef HAVE_OPENSSL
int ssl_load_default_ca_certs(SSL_CTX *ctx)