2 * sshdump is extcap tool used to capture data using a remote ssh host
4 * Copyright 2015, Dario Lombardo
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
14 #define WS_LOG_DOMAIN "sshdump"
16 #include <extcap/extcap-base.h>
17 #include <extcap/ssh-base.h>
18 #include <wsutil/interface.h>
19 #include <wsutil/file_util.h>
20 #include <wsutil/strtoi.h>
21 #include <wsutil/filesystem.h>
22 #include <wsutil/privileges.h>
23 #include <wsutil/please_report_bug.h>
24 #include <wsutil/wslog.h>
32 static char* sshdump_extcap_interface
;
33 #define DEFAULT_SSHDUMP_EXTCAP_INTERFACE "sshdump"
35 #define SSHDUMP_VERSION_MAJOR "1"
36 #define SSHDUMP_VERSION_MINOR "2"
37 #define SSHDUMP_VERSION_RELEASE "0"
39 #define SSH_READ_BLOCK_SIZE 256
42 EXTCAP_BASE_OPTIONS_ENUM
,
50 OPT_REMOTE_CAPTURE_COMMAND_SELECT
,
51 OPT_REMOTE_CAPTURE_COMMAND
,
54 OPT_SSHKEY_PASSPHRASE
,
58 OPT_REMOTE_SUDO
, // Deprecated
64 static const struct ws_option longopts
[] = {
66 { "help", ws_no_argument
, NULL
, OPT_HELP
},
67 { "version", ws_no_argument
, NULL
, OPT_VERSION
},
68 SSH_BASE_PACKET_OPTIONS
,
69 { "remote-capture-command-select", ws_required_argument
, NULL
, OPT_REMOTE_CAPTURE_COMMAND_SELECT
},
70 { "remote-capture-command", ws_required_argument
, NULL
, OPT_REMOTE_CAPTURE_COMMAND
},
71 { "remote-sudo", ws_no_argument
, NULL
, OPT_REMOTE_SUDO
}, // Deprecated
72 { "remote-priv", ws_required_argument
, NULL
, OPT_REMOTE_PRIV
},
73 { "remote-priv-user", ws_required_argument
, NULL
, OPT_REMOTE_PRIV_USER
},
74 { "remote-noprom", ws_no_argument
, NULL
, OPT_REMOTE_NOPROM
},
78 static char* interfaces_list_to_filter(GSList
* if_list
, unsigned int remote_port
);
80 static int ssh_loop_read(ssh_channel channel
, FILE* fp
)
83 int ret
= EXIT_SUCCESS
;
84 char buffer
[SSH_READ_BLOCK_SIZE
];
86 /* read from stdin until data are available */
87 while (ssh_channel_is_open(channel
) && !ssh_channel_is_eof(channel
)) {
88 nbytes
= ssh_channel_read(channel
, buffer
, SSH_READ_BLOCK_SIZE
, 0);
90 ws_warning("Error reading from channel");
96 if (fwrite(buffer
, 1, nbytes
, fp
) != (unsigned)nbytes
) {
97 ws_warning("Error writing to fifo");
104 /* read loop finished... maybe something wrong happened. Read from stderr */
105 while (ssh_channel_is_open(channel
) && !ssh_channel_is_eof(channel
)) {
106 nbytes
= ssh_channel_read(channel
, buffer
, SSH_READ_BLOCK_SIZE
, 1);
108 ws_warning("Error reading from channel");
111 if (fwrite(buffer
, 1, nbytes
, stderr
) != (unsigned)nbytes
) {
112 ws_warning("Error writing to stderr");
118 if (ssh_channel_send_eof(channel
) != SSH_OK
) {
119 ws_warning("Error sending EOF in ssh channel");
125 static char* local_interfaces_to_filter(const uint16_t remote_port
)
127 GSList
* interfaces
= local_interfaces_to_list();
128 char* filter
= interfaces_list_to_filter(interfaces
, remote_port
);
129 g_slist_free_full(interfaces
, g_free
);
133 static ssh_channel
run_ssh_command(ssh_session sshs
, const char* capture_command_select
,
134 const char* capture_command
, const char* privilege
, bool noprom
,
135 const char* iface
, const char* cfilter
, const uint32_t count
)
137 char* cmdline
= NULL
;
139 char** ifaces_array
= NULL
;
140 int ifaces_array_num
= 0;
141 GString
*ifaces_string
;
143 char* quoted_iface
= NULL
;
144 char* quoted_filter
= NULL
;
145 char* count_str
= NULL
;
146 unsigned int remote_port
= 22;
148 channel
= ssh_channel_new(sshs
);
150 ws_warning("Can't create channel");
154 if (ssh_channel_open_session(channel
) != SSH_OK
) {
155 ws_warning("Can't open session");
156 ssh_channel_free(channel
);
160 ssh_options_get_port(sshs
, &remote_port
);
162 if (capture_command_select
== NULL
|| !g_strcmp0(capture_command_select
, "other")) {
163 if (capture_command
&& *capture_command
) {
164 cmdline
= g_strdup(capture_command
);
165 ws_debug("Remote capture command has disabled other options");
167 capture_command_select
= "tcpdump";
171 /* escape parameters to go save with the shell */
172 if (!g_strcmp0(capture_command_select
, "tcpdump")) {
173 quoted_iface
= iface
? g_shell_quote(iface
) : NULL
;
174 quoted_filter
= g_shell_quote(cfilter
? cfilter
: "");
176 count_str
= ws_strdup_printf("-c %u", count
);
178 cmdline
= ws_strdup_printf("%s tcpdump -U %s%s %s -w - %s %s",
180 quoted_iface
? "-i " : "",
181 quoted_iface
? quoted_iface
: "",
183 count_str
? count_str
: "",
185 } else if (!g_strcmp0(capture_command_select
, "dumpcap")) {
187 ifaces_array
= g_strsplit(iface
, " ", -1);
188 ifaces_string
= g_string_new(NULL
);
189 while (ifaces_array
[ifaces_array_num
])
191 quoted_iface
= g_shell_quote(ifaces_array
[ifaces_array_num
]);
192 g_string_append_printf(ifaces_string
, "-i %s ", quoted_iface
);
195 ifaces
= g_string_free(ifaces_string
, FALSE
);
197 quoted_filter
= g_shell_quote(cfilter
? cfilter
: "");
199 count_str
= ws_strdup_printf("-c %u", count
);
201 cmdline
= ws_strdup_printf("%s dumpcap %s %s -w - %s -f %s",
204 ifaces
? ifaces
: "",
205 count_str
? count_str
: "",
209 g_strfreev(ifaces_array
);
212 ws_debug("Running: %s", cmdline
);
213 if (ssh_channel_request_exec(channel
, cmdline
) != SSH_OK
) {
214 ws_warning("Can't request exec");
215 ssh_channel_close(channel
);
216 ssh_channel_free(channel
);
220 g_free(quoted_iface
);
221 g_free(quoted_filter
);
228 static int ssh_open_remote_connection(const ssh_params_t
* params
, const char* iface
, const char* cfilter
,
229 const char* capture_command_select
, const char* capture_command
, const char* privilege
,
230 bool noprom
, const uint32_t count
, const char* fifo
)
232 ssh_session sshs
= NULL
;
233 ssh_channel channel
= NULL
;
235 int ret
= EXIT_FAILURE
;
236 char* err_info
= NULL
;
238 if (g_strcmp0(fifo
, "-")) {
239 /* Open or create the output file */
240 fp
= fopen(fifo
, "wb");
242 ws_warning("Error creating output file: %s (%s)", fifo
, g_strerror(errno
));
247 sshs
= create_ssh_connection(params
, &err_info
);
250 ws_warning("Error creating connection.");
254 channel
= run_ssh_command(sshs
, capture_command_select
, capture_command
, privilege
, noprom
, iface
, cfilter
, count
);
257 ws_warning("Can't run ssh command.");
261 /* read from channel and write into fp */
262 if (ssh_loop_read(channel
, fp
) != EXIT_SUCCESS
) {
263 ws_warning("Error in read loop.");
271 ws_warning("%s", err_info
);
274 /* clean up and exit */
275 ssh_cleanup(&sshs
, &channel
);
277 if (g_strcmp0(fifo
, "-"))
282 static char* interfaces_list_to_filter(GSList
* interfaces
, unsigned int remote_port
)
284 GString
* filter
= g_string_new(NULL
);
287 // If no port is given, assume the default one. This might not be
288 // correct if the port is looked up from the ssh config file, but it is
289 // better than nothing.
290 if (remote_port
== 0) {
295 g_string_append_printf(filter
, "not port %u", remote_port
);
297 g_string_append_printf(filter
, "not ((host %s", (char*)interfaces
->data
);
298 cur
= g_slist_next(interfaces
);
300 g_string_append_printf(filter
, " or host %s", (char*)cur
->data
);
301 cur
= g_slist_next(cur
);
303 g_string_append_printf(filter
, ") and port %u)", remote_port
);
305 return g_string_free(filter
, FALSE
);
308 static int list_config(char *interface
, unsigned int remote_port
)
314 ws_warning("ERROR: No interface specified.");
318 if (g_strcmp0(interface
, sshdump_extcap_interface
)) {
319 ws_warning("ERROR: interface must be %s", sshdump_extcap_interface
);
323 ipfilter
= local_interfaces_to_filter(remote_port
);
325 printf("arg {number=%u}{call=--remote-host}{display=Remote SSH server address}"
326 "{type=string}{tooltip=The remote SSH host. It can be both "
327 "an IP address or a hostname}{required=true}{group=Server}\n", inc
++);
328 printf("arg {number=%u}{call=--remote-port}{display=Remote SSH server port}"
329 "{type=unsigned}{default=22}{tooltip=The remote SSH host port (1-65535)}"
330 "{range=1,65535}{group=Server}\n", inc
++);
331 printf("arg {number=%u}{call=--remote-username}{display=Remote SSH server username}"
332 "{type=string}{tooltip=The remote SSH username. If not provided, "
333 "the current user will be used}{group=Authentication}\n", inc
++);
334 printf("arg {number=%u}{call=--remote-password}{display=Remote SSH server password}"
335 "{type=password}{tooltip=The SSH password, used when other methods (SSH agent "
336 "or key files) are unavailable.}{group=Authentication}\n", inc
++);
337 printf("arg {number=%u}{call=--sshkey}{display=Path to SSH private key}"
338 "{type=fileselect}{tooltip=The path on the local filesystem of the private SSH key (OpenSSH format)}"
339 "{mustexist=true}{group=Authentication}\n", inc
++);
340 printf("arg {number=%u}{call=--sshkey-passphrase}{display=SSH key passphrase}"
341 "{type=password}{tooltip=Passphrase to unlock the SSH private key}{group=Authentication}\n",
343 printf("arg {number=%u}{call=--proxycommand}{display=ProxyCommand}"
344 "{type=string}{tooltip=The command to use as proxy for the SSH connection}"
345 "{group=Authentication}\n", inc
++);
346 printf("arg {number=%u}{call=--ssh-sha1}{display=Support SHA-1 keys (deprecated)}"
347 "{type=boolflag}{tooltip=Support keys and key exchange algorithms using SHA-1 (deprecated)}{group=Authentication}"
349 printf("arg {number=%u}{call=--remote-interface}{display=Remote interface}"
350 "{type=string}{tooltip=The remote network interface used for capture"
351 "}{group=Capture}\n", inc
++);
352 printf("arg {number=%u}{call=--remote-capture-command-select}{display=Remote capture command selection}"
353 "{type=radio}{tooltip=The remote capture command to build a command line for}{group=Capture}\n", inc
);
354 printf("value {arg=%u}{value=dumpcap}{display=dumpcap}\n", inc
);
355 printf("value {arg=%u}{value=tcpdump}{display=tcpdump}{default=true}\n", inc
);
356 printf("value {arg=%u}{value=other}{display=Other:}\n", inc
++);
357 printf("arg {number=%u}{call=--remote-capture-command}{display=Remote capture command}"
358 "{type=string}{tooltip=The remote command used to capture}{group=Capture}\n", inc
++);
360 //printf("arg {number=%u}{call=--remote-sudo}{display=Use sudo on the remote machine}"
361 // "{type=boolflag}{tooltip=Prepend the capture command with sudo on the remote machine}"
362 // "{group=Capture}\n", inc++);
363 printf("arg {number=%u}{call=--remote-priv}{display=Gain capture privilege on the remote machine}"
364 "{type=radio}{tooltip=Optionally prepend the capture command with sudo or doas on the remote machine}"
365 "{group=Capture}\n", inc
);
366 printf("value {arg=%u}{value=none}{display=none}{default=true}\n", inc
);
367 printf("value {arg=%u}{value=sudo}{display=sudo}\n", inc
);
368 printf("value {arg=%u}{value=doas -n}{display=doas}\n", inc
++);
369 printf("arg {number=%u}{call=--remote-priv-user}{display=Privileged user name for sudo or doas}"
370 "{type=string}{tooltip=User name of privileged user to execute the capture command on the remote machine}"
371 "{group=Capture}\n", inc
++);
372 printf("arg {number=%u}{call=--remote-noprom}{display=No promiscuous mode}"
373 "{type=boolflag}{tooltip=Don't use promiscuous mode on the remote machine}{group=Capture}"
375 printf("arg {number=%u}{call=--remote-filter}{display=Remote capture filter}{type=string}"
376 "{tooltip=The remote capture filter}", inc
++);
378 printf("{default=%s}", ipfilter
);
379 printf("{group=Capture}\n");
380 printf("arg {number=%u}{call=--remote-count}{display=Packets to capture}"
381 "{type=unsigned}{default=0}{tooltip=The number of remote packets to capture. (Default: inf)}"
382 "{group=Capture}\n", inc
++);
384 extcap_config_debug(&inc
);
391 static char* concat_filters(const char* extcap_filter
, const char* remote_filter
)
393 if (!extcap_filter
&& remote_filter
)
394 return g_strdup(remote_filter
);
396 if (!remote_filter
&& extcap_filter
)
397 return g_strdup(extcap_filter
);
399 if (!remote_filter
&& !extcap_filter
)
402 return ws_strdup_printf("(%s) and (%s)", extcap_filter
, remote_filter
);
405 int main(int argc
, char *argv
[])
410 ssh_params_t
* ssh_params
= ssh_params_new();
411 char* remote_interface
= NULL
;
412 char* remote_capture_command_select
= NULL
;
413 char* remote_capture_command
= NULL
;
414 char* remote_filter
= NULL
;
416 int ret
= EXIT_FAILURE
;
417 extcap_parameters
* extcap_conf
= g_new0(extcap_parameters
, 1);
419 char* help_header
= NULL
;
421 char* priv_user
= NULL
;
423 char* interface_description
= g_strdup("SSH remote capture");
425 /* Set the program name. */
426 g_set_prgname("sshdump");
428 /* Initialize log handler early so we can have proper logging during startup. */
431 sshdump_extcap_interface
= g_path_get_basename(argv
[0]);
432 if (g_str_has_suffix(sshdump_extcap_interface
, ".exe")) {
433 sshdump_extcap_interface
[strlen(sshdump_extcap_interface
) - 4] = '\0';
437 * Get credential information for later use.
439 init_process_policies();
442 * Attempt to get the pathname of the directory containing the
445 err_msg
= configuration_init(argv
[0]);
446 if (err_msg
!= NULL
) {
447 ws_warning("Can't get pathname of directory containing the extcap program: %s.",
452 help_url
= data_file_url("sshdump.html");
453 extcap_base_set_util_info(extcap_conf
, argv
[0], SSHDUMP_VERSION_MAJOR
, SSHDUMP_VERSION_MINOR
,
454 SSHDUMP_VERSION_RELEASE
, help_url
);
456 add_libssh_info(extcap_conf
);
457 if (g_strcmp0(sshdump_extcap_interface
, DEFAULT_SSHDUMP_EXTCAP_INTERFACE
)) {
458 char* temp
= interface_description
;
459 interface_description
= ws_strdup_printf("%s, custom version", interface_description
);
462 extcap_base_register_interface(extcap_conf
, sshdump_extcap_interface
, interface_description
, 147, "Remote capture dependent DLT");
463 g_free(interface_description
);
465 help_header
= ws_strdup_printf(
466 " %s --extcap-interfaces\n"
467 " %s --extcap-interface=%s --extcap-dlts\n"
468 " %s --extcap-interface=%s --extcap-config\n"
469 " %s --extcap-interface=%s --remote-host myhost --remote-port 22222 "
470 "--remote-username myuser --remote-interface eth2 --remote-capture-command 'tcpdump -U -i eth0 -w -' "
471 "--fifo=FILENAME --capture\n", argv
[0], argv
[0], sshdump_extcap_interface
, argv
[0],
472 sshdump_extcap_interface
, argv
[0], sshdump_extcap_interface
);
473 extcap_help_add_header(extcap_conf
, help_header
);
475 extcap_help_add_option(extcap_conf
, "--help", "print this help");
476 extcap_help_add_option(extcap_conf
, "--version", "print the version");
477 extcap_help_add_option(extcap_conf
, "--remote-host <host>", "the remote SSH host");
478 extcap_help_add_option(extcap_conf
, "--remote-port <port>", "the remote SSH port");
479 extcap_help_add_option(extcap_conf
, "--remote-username <username>", "the remote SSH username");
480 extcap_help_add_option(extcap_conf
, "--remote-password <password>", "the remote SSH password. If not specified, ssh-agent and ssh-key are used");
481 extcap_help_add_option(extcap_conf
, "--sshkey <private key path>", "the path of the SSH key (OpenSSH format)");
482 extcap_help_add_option(extcap_conf
, "--sshkey-passphrase <private key passphrase>", "the passphrase to unlock private SSH key");
483 extcap_help_add_option(extcap_conf
, "--proxycommand <proxy command>", "the command to use as proxy for the SSH connection");
484 extcap_help_add_option(extcap_conf
, "--ssh-sha1", "support keys and key exchange using SHA-1 (deprecated)");
485 extcap_help_add_option(extcap_conf
, "--remote-interface <iface>", "the remote capture interface");
486 extcap_help_add_option(extcap_conf
, "--remote-capture-command-select <selection>", "dumpcap, tcpdump or other remote capture command");
487 extcap_help_add_option(extcap_conf
, "--remote-capture-command <capture command>", "the remote capture command");
488 //extcap_help_add_option(extcap_conf, "--remote-sudo", "use sudo on the remote machine to capture"); // Deprecated
489 extcap_help_add_option(extcap_conf
, "--remote-priv <selection>", "none, sudo or doas");
490 extcap_help_add_option(extcap_conf
, "--remote-priv-user <username>", "privileged user name");
491 extcap_help_add_option(extcap_conf
, "--remote-noprom", "don't use promiscuous mode on the remote machine");
492 extcap_help_add_option(extcap_conf
, "--remote-filter <filter>", "a filter for remote capture (default: don't listen on local interfaces IPs)");
493 extcap_help_add_option(extcap_conf
, "--remote-count <count>", "the number of packets to capture");
499 extcap_help_print(extcap_conf
);
503 while ((result
= ws_getopt_long(argc
, argv
, ":", longopts
, &option_idx
)) != -1) {
508 extcap_help_print(extcap_conf
);
513 extcap_version_print(extcap_conf
);
517 case OPT_REMOTE_HOST
:
518 g_free(ssh_params
->host
);
519 ssh_params
->host
= g_strdup(ws_optarg
);
522 case OPT_REMOTE_PORT
:
523 if (!ws_strtou16(ws_optarg
, NULL
, &ssh_params
->port
) || ssh_params
->port
== 0) {
524 ws_warning("Invalid port: %s", ws_optarg
);
529 case OPT_REMOTE_USERNAME
:
530 g_free(ssh_params
->username
);
531 ssh_params
->username
= g_strdup(ws_optarg
);
534 case OPT_REMOTE_PASSWORD
:
535 g_free(ssh_params
->password
);
536 ssh_params
->password
= g_strdup(ws_optarg
);
537 memset(ws_optarg
, 'X', strlen(ws_optarg
));
541 g_free(ssh_params
->sshkey_path
);
542 ssh_params
->sshkey_path
= g_strdup(ws_optarg
);
545 case OPT_SSHKEY_PASSPHRASE
:
546 g_free(ssh_params
->sshkey_passphrase
);
547 ssh_params
->sshkey_passphrase
= g_strdup(ws_optarg
);
548 memset(ws_optarg
, 'X', strlen(ws_optarg
));
551 case OPT_PROXYCOMMAND
:
552 g_free(ssh_params
->proxycommand
);
553 ssh_params
->proxycommand
= g_strdup(ws_optarg
);
557 ssh_params
->ssh_sha1
= true;
560 case OPT_REMOTE_INTERFACE
:
561 g_free(remote_interface
);
562 remote_interface
= g_strdup(ws_optarg
);
565 case OPT_REMOTE_CAPTURE_COMMAND_SELECT
:
566 g_free(remote_capture_command_select
);
567 remote_capture_command_select
= g_strdup(ws_optarg
);
570 case OPT_REMOTE_CAPTURE_COMMAND
:
571 g_free(remote_capture_command
);
572 remote_capture_command
= g_strdup(ws_optarg
);
575 case OPT_REMOTE_SUDO
:
578 priv
= g_strdup("sudo");
581 case OPT_REMOTE_PRIV
:
583 priv
= g_strdup(ws_optarg
);
586 case OPT_REMOTE_PRIV_USER
:
588 priv_user
= g_strdup(ws_optarg
);
591 case OPT_REMOTE_FILTER
:
592 g_free(remote_filter
);
593 remote_filter
= g_strdup(ws_optarg
);
596 case OPT_REMOTE_COUNT
:
597 if (!ws_strtou32(ws_optarg
, NULL
, &count
)) {
598 ws_warning("Invalid value for count: %s", ws_optarg
);
603 case OPT_REMOTE_NOPROM
:
608 /* missing option argument */
609 ws_warning("Option '%s' requires an argument", argv
[ws_optind
- 1]);
613 if (!extcap_base_parse_options(extcap_conf
, result
- EXTCAP_OPT_LIST_INTERFACES
, ws_optarg
)) {
614 ws_warning("Invalid option: %s", argv
[ws_optind
- 1]);
620 extcap_cmdline_debug(argv
, argc
);
622 if (extcap_base_handle_interface(extcap_conf
)) {
627 if (extcap_conf
->show_config
) {
628 ret
= list_config(extcap_conf
->interface
, ssh_params
->port
);
632 err_msg
= ws_init_sockets();
633 if (err_msg
!= NULL
) {
634 ws_warning("ERROR: %s", err_msg
);
636 ws_warning("%s", please_report_bug());
640 if (extcap_conf
->capture
) {
644 if (!ssh_params
->host
) {
645 ws_warning("Missing parameter: --remote-host");
649 if ((priv
) && g_strcmp0(priv
, "none") && strlen(g_strstrip(priv
))) {
650 if ((priv_user
) && strlen(g_strstrip(priv_user
)))
651 /* Both sudo and doas use the same command line option */
652 privilege
= g_strconcat(priv
, " -u ", priv_user
, NULL
);
654 privilege
= g_strdup(priv
);
656 privilege
= g_strdup("");
659 // This may result in the use of a different port number than was given in
660 // the default filter string, as presented in the config dialog. The default
661 // given is always using the default SSH port since there's no remote SSH port
662 // given on the command line to get the extcap arguments.
663 // However the remote SSH port used here is the one given on the command line
664 // when the capture us started, which is the intended one.
665 // And this is only happening when no remote filter is specified on the command
666 // line to start the capture.
667 if (remote_filter
== NULL
)
668 remote_filter
= local_interfaces_to_filter(ssh_params
->port
);
669 filter
= concat_filters(extcap_conf
->capture_filter
, remote_filter
);
670 ssh_params_set_log_level(ssh_params
, extcap_conf
->debug
);
671 ret
= ssh_open_remote_connection(ssh_params
, remote_interface
,
672 filter
, remote_capture_command_select
, remote_capture_command
,
673 privilege
, noprom
, count
, extcap_conf
->fifo
);
677 ws_debug("You should not come here... maybe some parameter missing?");
683 ssh_params_free(ssh_params
);
684 g_free(remote_capture_command_select
);
685 g_free(remote_capture_command
);
686 g_free(remote_interface
);
687 g_free(remote_filter
);
690 extcap_base_cleanup(&extcap_conf
);
695 * Editor modelines - https://www.wireshark.org/tools/modelines.html
700 * indent-tabs-mode: t
703 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
704 * :indentSize=8:tabSize=8:noTabs=false: