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
;
34 #define DEFAULT_SSHDUMP_EXTCAP_INTERFACE "sshdump.exe"
36 #define DEFAULT_SSHDUMP_EXTCAP_INTERFACE "sshdump"
39 #define SSHDUMP_VERSION_MAJOR "1"
40 #define SSHDUMP_VERSION_MINOR "2"
41 #define SSHDUMP_VERSION_RELEASE "0"
43 #define SSH_READ_BLOCK_SIZE 256
46 EXTCAP_BASE_OPTIONS_ENUM
,
54 OPT_REMOTE_CAPTURE_COMMAND_SELECT
,
55 OPT_REMOTE_CAPTURE_COMMAND
,
58 OPT_SSHKEY_PASSPHRASE
,
62 OPT_REMOTE_SUDO
, // Deprecated
68 static const struct ws_option longopts
[] = {
70 { "help", ws_no_argument
, NULL
, OPT_HELP
},
71 { "version", ws_no_argument
, NULL
, OPT_VERSION
},
73 { "remote-capture-command-select", ws_required_argument
, NULL
, OPT_REMOTE_CAPTURE_COMMAND_SELECT
},
74 { "remote-capture-command", ws_required_argument
, NULL
, OPT_REMOTE_CAPTURE_COMMAND
},
75 { "remote-sudo", ws_no_argument
, NULL
, OPT_REMOTE_SUDO
}, // Deprecated
76 { "remote-priv", ws_required_argument
, NULL
, OPT_REMOTE_PRIV
},
77 { "remote-priv-user", ws_required_argument
, NULL
, OPT_REMOTE_PRIV_USER
},
78 { "remote-noprom", ws_no_argument
, NULL
, OPT_REMOTE_NOPROM
},
82 static char* interfaces_list_to_filter(GSList
* if_list
, unsigned int remote_port
);
84 static int ssh_loop_read(ssh_channel channel
, FILE* fp
)
87 int ret
= EXIT_SUCCESS
;
88 char buffer
[SSH_READ_BLOCK_SIZE
];
90 /* read from stdin until data are available */
91 while (ssh_channel_is_open(channel
) && !ssh_channel_is_eof(channel
)) {
92 nbytes
= ssh_channel_read(channel
, buffer
, SSH_READ_BLOCK_SIZE
, 0);
94 ws_warning("Error reading from channel");
100 if (fwrite(buffer
, 1, nbytes
, fp
) != (unsigned)nbytes
) {
101 ws_warning("Error writing to fifo");
108 /* read loop finished... maybe something wrong happened. Read from stderr */
109 while (ssh_channel_is_open(channel
) && !ssh_channel_is_eof(channel
)) {
110 nbytes
= ssh_channel_read(channel
, buffer
, SSH_READ_BLOCK_SIZE
, 1);
112 ws_warning("Error reading from channel");
115 if (fwrite(buffer
, 1, nbytes
, stderr
) != (unsigned)nbytes
) {
116 ws_warning("Error writing to stderr");
122 if (ssh_channel_send_eof(channel
) != SSH_OK
) {
123 ws_warning("Error sending EOF in ssh channel");
129 static char* local_interfaces_to_filter(const uint16_t remote_port
)
131 GSList
* interfaces
= local_interfaces_to_list();
132 char* filter
= interfaces_list_to_filter(interfaces
, remote_port
);
133 g_slist_free_full(interfaces
, g_free
);
137 static ssh_channel
run_ssh_command(ssh_session sshs
, const char* capture_command_select
,
138 const char* capture_command
, const char* privilege
, bool noprom
,
139 const char* iface
, const char* cfilter
, const uint32_t count
)
141 char* cmdline
= NULL
;
143 char** ifaces_array
= NULL
;
144 int ifaces_array_num
= 0;
145 GString
*ifaces_string
;
147 char* quoted_iface
= NULL
;
148 char* quoted_filter
= NULL
;
149 char* count_str
= NULL
;
150 unsigned int remote_port
= 22;
152 channel
= ssh_channel_new(sshs
);
154 ws_warning("Can't create channel");
158 if (ssh_channel_open_session(channel
) != SSH_OK
) {
159 ws_warning("Can't open session");
160 ssh_channel_free(channel
);
164 ssh_options_get_port(sshs
, &remote_port
);
166 if (capture_command_select
== NULL
|| !g_strcmp0(capture_command_select
, "other")) {
167 if (capture_command
&& *capture_command
) {
168 cmdline
= g_strdup(capture_command
);
169 ws_debug("Remote capture command has disabled other options");
171 capture_command_select
= "tcpdump";
175 /* escape parameters to go save with the shell */
176 if (!g_strcmp0(capture_command_select
, "tcpdump")) {
177 quoted_iface
= iface
? g_shell_quote(iface
) : NULL
;
178 quoted_filter
= g_shell_quote(cfilter
? cfilter
: "");
180 count_str
= ws_strdup_printf("-c %u", count
);
182 cmdline
= ws_strdup_printf("%s tcpdump -U %s%s %s -w - %s %s",
184 quoted_iface
? "-i " : "",
185 quoted_iface
? quoted_iface
: "",
187 count_str
? count_str
: "",
189 } else if (!g_strcmp0(capture_command_select
, "dumpcap")) {
191 ifaces_array
= g_strsplit(iface
, " ", -1);
192 ifaces_string
= g_string_new(NULL
);
193 while (ifaces_array
[ifaces_array_num
])
195 quoted_iface
= g_shell_quote(ifaces_array
[ifaces_array_num
]);
196 g_string_append_printf(ifaces_string
, "-i %s ", quoted_iface
);
199 ifaces
= g_string_free(ifaces_string
, FALSE
);
201 quoted_filter
= g_shell_quote(cfilter
? cfilter
: "");
203 count_str
= ws_strdup_printf("-c %u", count
);
205 cmdline
= ws_strdup_printf("%s dumpcap %s %s -w - %s -f %s",
208 ifaces
? ifaces
: "",
209 count_str
? count_str
: "",
213 g_strfreev(ifaces_array
);
216 ws_debug("Running: %s", cmdline
);
217 if (ssh_channel_request_exec(channel
, cmdline
) != SSH_OK
) {
218 ws_warning("Can't request exec");
219 ssh_channel_close(channel
);
220 ssh_channel_free(channel
);
224 g_free(quoted_iface
);
225 g_free(quoted_filter
);
232 static int ssh_open_remote_connection(const ssh_params_t
* params
, const char* iface
, const char* cfilter
,
233 const char* capture_command_select
, const char* capture_command
, const char* privilege
,
234 bool noprom
, const uint32_t count
, const char* fifo
)
236 ssh_session sshs
= NULL
;
237 ssh_channel channel
= NULL
;
239 int ret
= EXIT_FAILURE
;
240 char* err_info
= NULL
;
242 if (g_strcmp0(fifo
, "-")) {
243 /* Open or create the output file */
244 fp
= fopen(fifo
, "wb");
246 ws_warning("Error creating output file: %s (%s)", fifo
, g_strerror(errno
));
251 sshs
= create_ssh_connection(params
, &err_info
);
254 ws_warning("Error creating connection.");
258 channel
= run_ssh_command(sshs
, capture_command_select
, capture_command
, privilege
, noprom
, iface
, cfilter
, count
);
261 ws_warning("Can't run ssh command.");
265 /* read from channel and write into fp */
266 if (ssh_loop_read(channel
, fp
) != EXIT_SUCCESS
) {
267 ws_warning("Error in read loop.");
275 ws_warning("%s", err_info
);
278 /* clean up and exit */
279 ssh_cleanup(&sshs
, &channel
);
281 if (g_strcmp0(fifo
, "-"))
286 static char* interfaces_list_to_filter(GSList
* interfaces
, unsigned int remote_port
)
288 GString
* filter
= g_string_new(NULL
);
291 // If no port is given, assume the default one. This might not be
292 // correct if the port is looked up from the ssh config file, but it is
293 // better than nothing.
294 if (remote_port
== 0) {
299 g_string_append_printf(filter
, "not port %u", remote_port
);
301 g_string_append_printf(filter
, "not ((host %s", (char*)interfaces
->data
);
302 cur
= g_slist_next(interfaces
);
304 g_string_append_printf(filter
, " or host %s", (char*)cur
->data
);
305 cur
= g_slist_next(cur
);
307 g_string_append_printf(filter
, ") and port %u)", remote_port
);
309 return g_string_free(filter
, FALSE
);
312 static int list_config(char *interface
, unsigned int remote_port
)
318 ws_warning("ERROR: No interface specified.");
322 if (g_strcmp0(interface
, sshdump_extcap_interface
)) {
323 ws_warning("ERROR: interface must be %s", sshdump_extcap_interface
);
327 ipfilter
= local_interfaces_to_filter(remote_port
);
329 printf("arg {number=%u}{call=--remote-host}{display=Remote SSH server address}"
330 "{type=string}{tooltip=The remote SSH host. It can be both "
331 "an IP address or a hostname}{required=true}{group=Server}\n", inc
++);
332 printf("arg {number=%u}{call=--remote-port}{display=Remote SSH server port}"
333 "{type=unsigned}{default=22}{tooltip=The remote SSH host port (1-65535)}"
334 "{range=1,65535}{group=Server}\n", inc
++);
335 printf("arg {number=%u}{call=--remote-username}{display=Remote SSH server username}"
336 "{type=string}{tooltip=The remote SSH username. If not provided, "
337 "the current user will be used}{group=Authentication}\n", inc
++);
338 printf("arg {number=%u}{call=--remote-password}{display=Remote SSH server password}"
339 "{type=password}{tooltip=The SSH password, used when other methods (SSH agent "
340 "or key files) are unavailable.}{group=Authentication}\n", inc
++);
341 printf("arg {number=%u}{call=--sshkey}{display=Path to SSH private key}"
342 "{type=fileselect}{tooltip=The path on the local filesystem of the private SSH key (OpenSSH format)}"
343 "{mustexist=true}{group=Authentication}\n", inc
++);
344 printf("arg {number=%u}{call=--sshkey-passphrase}{display=SSH key passphrase}"
345 "{type=password}{tooltip=Passphrase to unlock the SSH private key}{group=Authentication}\n",
347 printf("arg {number=%u}{call=--proxycommand}{display=ProxyCommand}"
348 "{type=string}{tooltip=The command to use as proxy for the SSH connection}"
349 "{group=Authentication}\n", inc
++);
350 printf("arg {number=%u}{call=--ssh-sha1}{display=Support SHA-1 keys (deprecated)}"
351 "{type=boolflag}{tooltip=Support keys and key exchange algorithms using SHA-1 (deprecated)}{group=Authentication}"
353 printf("arg {number=%u}{call=--remote-interface}{display=Remote interface}"
354 "{type=string}{tooltip=The remote network interface used for capture"
355 "}{group=Capture}\n", inc
++);
356 printf("arg {number=%u}{call=--remote-capture-command-select}{display=Remote capture command selection}"
357 "{type=radio}{tooltip=The remote capture command to build a command line for}{group=Capture}\n", inc
);
358 printf("value {arg=%u}{value=dumpcap}{display=dumpcap}\n", inc
);
359 printf("value {arg=%u}{value=tcpdump}{display=tcpdump}{default=true}\n", inc
);
360 printf("value {arg=%u}{value=other}{display=Other:}\n", inc
++);
361 printf("arg {number=%u}{call=--remote-capture-command}{display=Remote capture command}"
362 "{type=string}{tooltip=The remote command used to capture}{group=Capture}\n", inc
++);
364 //printf("arg {number=%u}{call=--remote-sudo}{display=Use sudo on the remote machine}"
365 // "{type=boolflag}{tooltip=Prepend the capture command with sudo on the remote machine}"
366 // "{group=Capture}\n", inc++);
367 printf("arg {number=%u}{call=--remote-priv}{display=Gain capture privilege on the remote machine}"
368 "{type=radio}{tooltip=Optionally prepend the capture command with sudo or doas on the remote machine}"
369 "{group=Capture}\n", inc
);
370 printf("value {arg=%u}{value=none}{display=none}{default=true}\n", inc
);
371 printf("value {arg=%u}{value=sudo}{display=sudo}\n", inc
);
372 printf("value {arg=%u}{value=doas -n}{display=doas}\n", inc
++);
373 printf("arg {number=%u}{call=--remote-priv-user}{display=Privileged user name for sudo or doas}"
374 "{type=string}{tooltip=User name of privileged user to execute the capture command on the remote machine}"
375 "{group=Capture}\n", inc
++);
376 printf("arg {number=%u}{call=--remote-noprom}{display=No promiscuous mode}"
377 "{type=boolflag}{tooltip=Don't use promiscuous mode on the remote machine}{group=Capture}"
379 printf("arg {number=%u}{call=--remote-filter}{display=Remote capture filter}{type=string}"
380 "{tooltip=The remote capture filter}", inc
++);
382 printf("{default=%s}", ipfilter
);
383 printf("{group=Capture}\n");
384 printf("arg {number=%u}{call=--remote-count}{display=Packets to capture}"
385 "{type=unsigned}{default=0}{tooltip=The number of remote packets to capture. (Default: inf)}"
386 "{group=Capture}\n", inc
++);
388 extcap_config_debug(&inc
);
395 static char* concat_filters(const char* extcap_filter
, const char* remote_filter
)
397 if (!extcap_filter
&& remote_filter
)
398 return g_strdup(remote_filter
);
400 if (!remote_filter
&& extcap_filter
)
401 return g_strdup(extcap_filter
);
403 if (!remote_filter
&& !extcap_filter
)
406 return ws_strdup_printf("(%s) and (%s)", extcap_filter
, remote_filter
);
409 int main(int argc
, char *argv
[])
414 ssh_params_t
* ssh_params
= ssh_params_new();
415 char* remote_interface
= NULL
;
416 char* remote_capture_command_select
= NULL
;
417 char* remote_capture_command
= NULL
;
418 char* remote_filter
= NULL
;
420 int ret
= EXIT_FAILURE
;
421 extcap_parameters
* extcap_conf
= g_new0(extcap_parameters
, 1);
423 char* help_header
= NULL
;
425 char* priv_user
= NULL
;
427 char* interface_description
= g_strdup("SSH remote capture");
429 /* Initialize log handler early so we can have proper logging during startup. */
430 extcap_log_init("sshdump");
432 sshdump_extcap_interface
= g_path_get_basename(argv
[0]);
435 * Get credential information for later use.
437 init_process_policies();
440 * Attempt to get the pathname of the directory containing the
443 err_msg
= configuration_init(argv
[0], NULL
);
444 if (err_msg
!= NULL
) {
445 ws_warning("Can't get pathname of directory containing the extcap program: %s.",
450 help_url
= data_file_url("sshdump.html");
451 extcap_base_set_util_info(extcap_conf
, argv
[0], SSHDUMP_VERSION_MAJOR
, SSHDUMP_VERSION_MINOR
,
452 SSHDUMP_VERSION_RELEASE
, help_url
);
454 add_libssh_info(extcap_conf
);
455 if (g_strcmp0(sshdump_extcap_interface
, DEFAULT_SSHDUMP_EXTCAP_INTERFACE
)) {
456 char* temp
= interface_description
;
457 interface_description
= ws_strdup_printf("%s, custom version", interface_description
);
460 extcap_base_register_interface(extcap_conf
, sshdump_extcap_interface
, interface_description
, 147, "Remote capture dependent DLT");
461 g_free(interface_description
);
463 help_header
= ws_strdup_printf(
464 " %s --extcap-interfaces\n"
465 " %s --extcap-interface=%s --extcap-dlts\n"
466 " %s --extcap-interface=%s --extcap-config\n"
467 " %s --extcap-interface=%s --remote-host myhost --remote-port 22222 "
468 "--remote-username myuser --remote-interface eth2 --remote-capture-command 'tcpdump -U -i eth0 -w -' "
469 "--fifo=FILENAME --capture\n", argv
[0], argv
[0], sshdump_extcap_interface
, argv
[0],
470 sshdump_extcap_interface
, argv
[0], sshdump_extcap_interface
);
471 extcap_help_add_header(extcap_conf
, help_header
);
473 extcap_help_add_option(extcap_conf
, "--help", "print this help");
474 extcap_help_add_option(extcap_conf
, "--version", "print the version");
475 extcap_help_add_option(extcap_conf
, "--remote-host <host>", "the remote SSH host");
476 extcap_help_add_option(extcap_conf
, "--remote-port <port>", "the remote SSH port");
477 extcap_help_add_option(extcap_conf
, "--remote-username <username>", "the remote SSH username");
478 extcap_help_add_option(extcap_conf
, "--remote-password <password>", "the remote SSH password. If not specified, ssh-agent and ssh-key are used");
479 extcap_help_add_option(extcap_conf
, "--sshkey <private key path>", "the path of the SSH key (OpenSSH format)");
480 extcap_help_add_option(extcap_conf
, "--sshkey-passphrase <private key passphrase>", "the passphrase to unlock private SSH key");
481 extcap_help_add_option(extcap_conf
, "--proxycommand <proxy command>", "the command to use as proxy for the SSH connection");
482 extcap_help_add_option(extcap_conf
, "--ssh-sha1", "support keys and key exchange using SHA-1 (deprecated)");
483 extcap_help_add_option(extcap_conf
, "--remote-interface <iface>", "the remote capture interface");
484 extcap_help_add_option(extcap_conf
, "--remote-capture-command-select <selection>", "dumpcap, tcpdump or other remote capture command");
485 extcap_help_add_option(extcap_conf
, "--remote-capture-command <capture command>", "the remote capture command");
486 //extcap_help_add_option(extcap_conf, "--remote-sudo", "use sudo on the remote machine to capture"); // Deprecated
487 extcap_help_add_option(extcap_conf
, "--remote-priv <selection>", "none, sudo or doas");
488 extcap_help_add_option(extcap_conf
, "--remote-priv-user <username>", "privileged user name");
489 extcap_help_add_option(extcap_conf
, "--remote-noprom", "don't use promiscuous mode on the remote machine");
490 extcap_help_add_option(extcap_conf
, "--remote-filter <filter>", "a filter for remote capture (default: don't listen on local interfaces IPs)");
491 extcap_help_add_option(extcap_conf
, "--remote-count <count>", "the number of packets to capture");
497 extcap_help_print(extcap_conf
);
501 while ((result
= ws_getopt_long(argc
, argv
, ":", longopts
, &option_idx
)) != -1) {
506 extcap_help_print(extcap_conf
);
511 extcap_version_print(extcap_conf
);
515 case OPT_REMOTE_HOST
:
516 g_free(ssh_params
->host
);
517 ssh_params
->host
= g_strdup(ws_optarg
);
520 case OPT_REMOTE_PORT
:
521 if (!ws_strtou16(ws_optarg
, NULL
, &ssh_params
->port
) || ssh_params
->port
== 0) {
522 ws_warning("Invalid port: %s", ws_optarg
);
527 case OPT_REMOTE_USERNAME
:
528 g_free(ssh_params
->username
);
529 ssh_params
->username
= g_strdup(ws_optarg
);
532 case OPT_REMOTE_PASSWORD
:
533 g_free(ssh_params
->password
);
534 ssh_params
->password
= g_strdup(ws_optarg
);
535 memset(ws_optarg
, 'X', strlen(ws_optarg
));
539 g_free(ssh_params
->sshkey_path
);
540 ssh_params
->sshkey_path
= g_strdup(ws_optarg
);
543 case OPT_SSHKEY_PASSPHRASE
:
544 g_free(ssh_params
->sshkey_passphrase
);
545 ssh_params
->sshkey_passphrase
= g_strdup(ws_optarg
);
546 memset(ws_optarg
, 'X', strlen(ws_optarg
));
549 case OPT_PROXYCOMMAND
:
550 g_free(ssh_params
->proxycommand
);
551 ssh_params
->proxycommand
= g_strdup(ws_optarg
);
555 ssh_params
->ssh_sha1
= true;
558 case OPT_REMOTE_INTERFACE
:
559 g_free(remote_interface
);
560 remote_interface
= g_strdup(ws_optarg
);
563 case OPT_REMOTE_CAPTURE_COMMAND_SELECT
:
564 g_free(remote_capture_command_select
);
565 remote_capture_command_select
= g_strdup(ws_optarg
);
568 case OPT_REMOTE_CAPTURE_COMMAND
:
569 g_free(remote_capture_command
);
570 remote_capture_command
= g_strdup(ws_optarg
);
573 case OPT_REMOTE_SUDO
:
576 priv
= g_strdup("sudo");
579 case OPT_REMOTE_PRIV
:
581 priv
= g_strdup(ws_optarg
);
584 case OPT_REMOTE_PRIV_USER
:
586 priv_user
= g_strdup(ws_optarg
);
589 case OPT_REMOTE_FILTER
:
590 g_free(remote_filter
);
591 remote_filter
= g_strdup(ws_optarg
);
594 case OPT_REMOTE_COUNT
:
595 if (!ws_strtou32(ws_optarg
, NULL
, &count
)) {
596 ws_warning("Invalid value for count: %s", ws_optarg
);
601 case OPT_REMOTE_NOPROM
:
606 /* missing option argument */
607 ws_warning("Option '%s' requires an argument", argv
[ws_optind
- 1]);
611 if (!extcap_base_parse_options(extcap_conf
, result
- EXTCAP_OPT_LIST_INTERFACES
, ws_optarg
)) {
612 ws_warning("Invalid option: %s", argv
[ws_optind
- 1]);
618 extcap_cmdline_debug(argv
, argc
);
620 if (extcap_base_handle_interface(extcap_conf
)) {
625 if (extcap_conf
->show_config
) {
626 ret
= list_config(extcap_conf
->interface
, ssh_params
->port
);
630 err_msg
= ws_init_sockets();
631 if (err_msg
!= NULL
) {
632 ws_warning("ERROR: %s", err_msg
);
634 ws_warning("%s", please_report_bug());
638 if (extcap_conf
->capture
) {
642 if (!ssh_params
->host
) {
643 ws_warning("Missing parameter: --remote-host");
647 if ((priv
) && g_strcmp0(priv
, "none") && strlen(g_strstrip(priv
))) {
648 if ((priv_user
) && strlen(g_strstrip(priv_user
)))
649 /* Both sudo and doas use the same command line option */
650 privilege
= g_strconcat(priv
, " -u ", priv_user
, NULL
);
652 privilege
= g_strdup(priv
);
654 privilege
= g_strdup("");
657 // This may result in the use of a different port number than was given in
658 // the default filter string, as presented in the config dialog. The default
659 // given is always using the default SSH port since there's no remote SSH port
660 // given on the command line to get the extcap arguments.
661 // However the remote SSH port used here is the one given on the command line
662 // when the capture us started, which is the intended one.
663 // And this is only happening when no remote filter is specified on the command
664 // line to start the capture.
665 if (remote_filter
== NULL
)
666 remote_filter
= local_interfaces_to_filter(ssh_params
->port
);
667 filter
= concat_filters(extcap_conf
->capture_filter
, remote_filter
);
668 ssh_params_set_log_level(ssh_params
, extcap_conf
->debug
);
669 ret
= ssh_open_remote_connection(ssh_params
, remote_interface
,
670 filter
, remote_capture_command_select
, remote_capture_command
,
671 privilege
, noprom
, count
, extcap_conf
->fifo
);
675 ws_debug("You should not come here... maybe some parameter missing?");
681 ssh_params_free(ssh_params
);
682 g_free(remote_capture_command_select
);
683 g_free(remote_capture_command
);
684 g_free(remote_interface
);
685 g_free(remote_filter
);
688 extcap_base_cleanup(&extcap_conf
);
693 * Editor modelines - https://www.wireshark.org/tools/modelines.html
698 * indent-tabs-mode: t
701 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
702 * :indentSize=8:tabSize=8:noTabs=false: