forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tshark.c
5078 lines (4526 loc) · 197 KB
/
tshark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* tshark.c
*
* Text-mode variant of Wireshark, along the lines of tcpdump and snoop,
* by Gilbert Ramirez <[email protected]> and Guy Harris <[email protected]>.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <limits.h>
#include <wsutil/ws_getopt.h>
#include <errno.h>
#ifdef _WIN32
# include <winsock2.h>
#endif
#ifndef _WIN32
#include <signal.h>
#endif
#include <glib.h>
#include <epan/exceptions.h>
#include <epan/epan.h>
#include <ws_exit_codes.h>
#include <wsutil/clopts_common.h>
#include <wsutil/cmdarg_err.h>
#include <ui/urls.h>
#include <wsutil/filesystem.h>
#include <wsutil/file_util.h>
#include <wsutil/time_util.h>
#include <wsutil/socket.h>
#include <wsutil/privileges.h>
#include <wsutil/report_message.h>
#include <wsutil/please_report_bug.h>
#include <wsutil/wslog.h>
#include <wsutil/ws_assert.h>
#include <wsutil/strtoi.h>
#include <cli_main.h>
#include <wsutil/version_info.h>
#include <wiretap/wtap_opttypes.h>
#include "globals.h"
#include <epan/timestamp.h>
#include <epan/packet.h>
#ifdef HAVE_LUA
#include <epan/wslua/init_wslua.h>
#endif
#include "frame_tvbuff.h"
#include <epan/disabled_protos.h>
#include <epan/prefs.h>
#include <epan/column.h>
#include <epan/decode_as.h>
#include <epan/print.h>
#include <epan/addr_resolv.h>
#include <epan/enterprises.h>
#include <epan/manuf.h>
#include <epan/services.h>
#ifdef HAVE_LIBPCAP
#include "ui/capture_ui_utils.h"
#endif
#include "ui/taps.h"
#include "ui/util.h"
#include "ui/ws_ui_util.h"
#include "ui/decode_as_utils.h"
#include "wsutil/filter_files.h"
#include "ui/cli/tshark-tap.h"
#include "ui/cli/tap-exportobject.h"
#include "ui/tap_export_pdu.h"
#include "ui/dissect_opts.h"
#include "ui/ssl_key_export.h"
#include "ui/failure_message.h"
#if defined(HAVE_LIBSMI)
#include "epan/oids.h"
#endif
#include "epan/maxmind_db.h"
#include <epan/epan_dissect.h>
#include <epan/tap.h>
#include <epan/stat_tap_ui.h>
#include <epan/conversation_table.h>
#include <epan/srt_table.h>
#include <epan/rtd_table.h>
#include <epan/ex-opt.h>
#include <epan/exported_pdu.h>
#include <epan/secrets.h>
#include "capture_opts.h"
#include "capture/capture-pcap-util.h"
#ifdef HAVE_LIBPCAP
#include "capture/capture_ifinfo.h"
#ifdef _WIN32
#include "capture/capture-wpcap.h"
#endif /* _WIN32 */
#include <capture/capture_session.h>
#include <capture/capture_sync.h>
#include <ui/capture_info.h>
#endif /* HAVE_LIBPCAP */
#include <epan/funnel.h>
#include <wsutil/str_util.h>
#include <wsutil/utf8_entities.h>
#include <wsutil/json_dumper.h>
#include <wsutil/wslog.h>
#ifdef _WIN32
#include <wsutil/win32-utils.h>
#endif
#include "extcap.h"
#ifdef HAVE_PLUGINS
#include <wsutil/codecs.h>
#include <wsutil/plugins.h>
#endif
/* Additional exit codes */
#define INVALID_EXPORT 2
#define INVALID_TAP 2
#define INVALID_CAPTURE 2
#define LONGOPT_EXPORT_OBJECTS LONGOPT_BASE_APPLICATION+1
#define LONGOPT_COLOR LONGOPT_BASE_APPLICATION+2
#define LONGOPT_NO_DUPLICATE_KEYS LONGOPT_BASE_APPLICATION+3
#define LONGOPT_ELASTIC_MAPPING_FILTER LONGOPT_BASE_APPLICATION+4
#define LONGOPT_EXPORT_TLS_SESSION_KEYS LONGOPT_BASE_APPLICATION+5
#define LONGOPT_CAPTURE_COMMENT LONGOPT_BASE_APPLICATION+6
#define LONGOPT_HEXDUMP LONGOPT_BASE_APPLICATION+7
#define LONGOPT_SELECTED_FRAME LONGOPT_BASE_APPLICATION+8
#define LONGOPT_PRINT_TIMERS LONGOPT_BASE_APPLICATION+9
#define LONGOPT_GLOBAL_PROFILE LONGOPT_BASE_APPLICATION+10
#define LONGOPT_COMPRESS LONGOPT_BASE_APPLICATION+11
capture_file cfile;
static uint32_t cum_bytes;
static frame_data ref_frame;
static frame_data prev_dis_frame;
static frame_data prev_cap_frame;
static bool perform_two_pass_analysis;
static uint32_t epan_auto_reset_count;
static bool epan_auto_reset;
static uint32_t selected_frame_number;
/*
* The way the packet decode is to be written.
*/
typedef enum {
WRITE_NONE, /* dummy initial state */
WRITE_TEXT, /* summary or detail text */
WRITE_XML, /* PDML or PSML */
WRITE_FIELDS, /* User defined list of fields */
WRITE_JSON, /* JSON */
WRITE_JSON_RAW, /* JSON only raw hex */
WRITE_EK /* JSON bulk insert to Elasticsearch */
/* Add CSV and the like here */
} output_action_e;
static output_action_e output_action;
static bool do_dissection; /* true if we have to dissect each packet */
static bool print_packet_info; /* true if we're to print packet information */
static bool print_summary; /* true if we're to print packet summary information */
static bool print_details; /* true if we're to print packet details information */
static bool print_hex; /* true if we're to print hex/ascii information */
static bool line_buffered;
static bool quiet;
static bool really_quiet;
static char* delimiter_char = " ";
static bool dissect_color;
static unsigned hexdump_source_option = HEXDUMP_SOURCE_MULTI; /* Default - Enable legacy multi-source mode */
static unsigned hexdump_ascii_option = HEXDUMP_ASCII_INCLUDE; /* Default - Enable legacy undelimited ASCII dump */
static print_format_e print_format = PR_FMT_TEXT;
static print_stream_t *print_stream;
static char *output_file_name;
static output_fields_t* output_fields;
static bool no_duplicate_keys;
static proto_node_children_grouper_func node_children_grouper = proto_node_group_children_by_unique;
static json_dumper jdumper;
/* The line separator used between packets, changeable via the -S option */
static const char *separator = "";
/* Per-file comments to be added to the output file. */
static GPtrArray *capture_comments;
static bool prefs_loaded;
#ifdef HAVE_LIBPCAP
/*
* true if we're to print packet counts to keep track of captured packets.
*/
static bool print_packet_counts;
static capture_options global_capture_opts;
static capture_session global_capture_session;
static info_data_t global_info_data;
#ifdef SIGINFO
static bool infodelay; /* if true, don't print capture info in SIGINFO handler */
static bool infoprint; /* if true, print capture info after clearing infodelay */
#endif /* SIGINFO */
static bool capture(void);
static bool capture_input_new_file(capture_session *cap_session,
char *new_file);
static void capture_input_new_packets(capture_session *cap_session,
int to_read);
static void capture_input_drops(capture_session *cap_session, uint32_t dropped,
const char* interface_name);
static void capture_input_error(capture_session *cap_session,
char *error_msg, char *secondary_error_msg);
static void capture_input_cfilter_error(capture_session *cap_session,
unsigned i, const char *error_message);
static void capture_input_closed(capture_session *cap_session, char *msg);
static void report_counts(void);
#ifdef _WIN32
static BOOL WINAPI capture_cleanup(DWORD);
#else /* _WIN32 */
static void capture_cleanup(int);
#ifdef SIGINFO
static void report_counts_siginfo(int);
#endif /* SIGINFO */
#endif /* _WIN32 */
#endif /* HAVE_LIBPCAP */
static void reset_epan_mem(capture_file *cf, epan_dissect_t *edt, bool tree, bool visual);
typedef enum {
PROCESS_FILE_SUCCEEDED,
PROCESS_FILE_NO_FILE_PROCESSED,
PROCESS_FILE_ERROR,
PROCESS_FILE_INTERRUPTED
} process_file_status_t;
static process_file_status_t process_cap_file(capture_file *, char *, int, bool, int, int64_t, int, wtap_compression_type);
static bool process_packet_single_pass(capture_file *cf,
epan_dissect_t *edt, int64_t offset, wtap_rec *rec, Buffer *buf,
unsigned tap_flags);
static void show_print_file_io_error(void);
static bool write_preamble(capture_file *cf);
static bool print_packet(capture_file *cf, epan_dissect_t *edt);
static bool write_finale(void);
static void tshark_cmdarg_err(const char *msg_format, va_list ap);
static void tshark_cmdarg_err_cont(const char *msg_format, va_list ap);
static GHashTable *output_only_tables;
static bool opt_print_timers;
struct elapsed_pass_s {
int64_t dissect;
int64_t dfilter_read;
int64_t dfilter_filter;
};
static struct {
int64_t dfilter_expand;
int64_t dfilter_compile;
struct elapsed_pass_s first_pass;
int64_t elapsed_first_pass;
struct elapsed_pass_s second_pass;
int64_t elapsed_second_pass;
}
tshark_elapsed;
static void
print_elapsed_json(const char *cf_name, const char *dfilter)
{
json_dumper dumper = {
.output_file = stderr,
.flags = JSON_DUMPER_FLAGS_PRETTY_PRINT,
};
if (tshark_elapsed.elapsed_first_pass == 0) {
// Should not happen
ws_warning("Print timers requested but no timing info provided");
return;
}
#define DUMP(name, val) \
json_dumper_set_member_name(&dumper, name); \
json_dumper_value_anyf(&dumper, "%"PRId64, val)
json_dumper_begin_object(&dumper);
json_dumper_set_member_name(&dumper, "version");
json_dumper_value_string(&dumper, get_ws_vcs_version_info_short());
if (cf_name) {
json_dumper_set_member_name(&dumper, "path");
json_dumper_value_string(&dumper, cf_name);
}
if (dfilter) {
json_dumper_set_member_name(&dumper, "filter");
json_dumper_value_string(&dumper, dfilter);
}
json_dumper_set_member_name(&dumper, "time_unit");
json_dumper_value_string(&dumper, "microseconds");
DUMP("elapsed", tshark_elapsed.elapsed_first_pass +
tshark_elapsed.elapsed_second_pass);
DUMP("dfilter_expand", tshark_elapsed.dfilter_expand);
DUMP("dfilter_compile", tshark_elapsed.dfilter_compile);
json_dumper_begin_array(&dumper);
json_dumper_begin_object(&dumper);
DUMP("elapsed", tshark_elapsed.elapsed_first_pass);
DUMP("dissect", tshark_elapsed.first_pass.dissect);
DUMP("display_filter", tshark_elapsed.first_pass.dfilter_filter);
DUMP("read_filter", tshark_elapsed.first_pass.dfilter_read);
json_dumper_end_object(&dumper);
if (tshark_elapsed.elapsed_second_pass) {
json_dumper_begin_object(&dumper);
DUMP("elapsed", tshark_elapsed.elapsed_second_pass);
DUMP("dissect", tshark_elapsed.second_pass.dissect);
DUMP("display_filter", tshark_elapsed.second_pass.dfilter_filter);
DUMP("read_filter", tshark_elapsed.second_pass.dfilter_read);
json_dumper_end_object(&dumper);
}
json_dumper_end_array(&dumper);
json_dumper_end_object(&dumper);
json_dumper_finish(&dumper);
}
static void
list_capture_types(void)
{
GArray *writable_type_subtypes;
fprintf(stderr, "tshark: The available capture file types for the \"-F\" flag are:\n");
writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
for (unsigned i = 0; i < writable_type_subtypes->len; i++) {
int ft = g_array_index(writable_type_subtypes, int, i);
fprintf(stderr, " %s - %s\n", wtap_file_type_subtype_name(ft),
wtap_file_type_subtype_description(ft));
}
g_array_free(writable_type_subtypes, TRUE);
}
static void
list_output_compression_types(void) {
GSList *output_compression_types;
fprintf(stderr, "tshark: The available output compression type(s) for the \"--compress\" flag are:\n");
output_compression_types = wtap_get_all_output_compression_type_names_list();
for (GSList *compression_type = output_compression_types;
compression_type != NULL;
compression_type = g_slist_next(compression_type)) {
fprintf(stderr, " %s\n", (const char *)compression_type->data);
}
g_slist_free(output_compression_types);
}
struct string_elem {
const char *sstr; /* The short string */
const char *lstr; /* The long string */
};
static int
string_compare(const void *a, const void *b)
{
return strcmp(((const struct string_elem *)a)->sstr,
((const struct string_elem *)b)->sstr);
}
static void
string_elem_print(void *data)
{
fprintf(stderr, " %s - %s\n",
((struct string_elem *)data)->sstr,
((struct string_elem *)data)->lstr);
}
static void
list_read_capture_types(void)
{
unsigned i;
size_t num_file_types;
struct string_elem *captypes;
GSList *list = NULL;
const char *magic = "Magic-value-based";
const char *heuristic = "Heuristics-based";
/* How many readable file types are there? */
num_file_types = 0;
for (i = 0; open_routines[i].name != NULL; i++)
num_file_types++;
captypes = g_new(struct string_elem, num_file_types);
fprintf(stderr, "tshark: The available read file types for the \"-X read_format:\" option are:\n");
for (i = 0; i < num_file_types && open_routines[i].name != NULL; i++) {
captypes[i].sstr = open_routines[i].name;
captypes[i].lstr = (open_routines[i].type == OPEN_INFO_MAGIC) ? magic : heuristic;
list = g_slist_insert_sorted(list, &captypes[i], string_compare);
}
g_slist_free_full(list, string_elem_print);
g_free(captypes);
}
static void
list_export_pdu_taps(void)
{
fprintf(stderr, "tshark: The available export tap names and the encapsulation types they produce for the \"-U tap_name\" option are:\n");
for (GSList *export_pdu_tap_name_list = get_export_pdu_tap_list();
export_pdu_tap_name_list != NULL;
export_pdu_tap_name_list = g_slist_next(export_pdu_tap_name_list)) {
fprintf(stderr, " %s - %s\n", (const char*)(export_pdu_tap_name_list->data), wtap_encap_description(export_pdu_tap_get_encap((const char*)export_pdu_tap_name_list->data)));
}
}
static void
print_usage(FILE *output)
{
fprintf(output, "\n");
fprintf(output, "Usage: tshark [options] ...\n");
fprintf(output, "\n");
#ifdef HAVE_LIBPCAP
fprintf(output, "Capture interface:\n");
fprintf(output, " -i <interface>, --interface <interface>\n");
fprintf(output, " name or idx of interface (def: first non-loopback)\n");
fprintf(output, " -f <capture filter> packet filter in libpcap filter syntax\n");
fprintf(output, " -s <snaplen>, --snapshot-length <snaplen>\n");
#ifdef HAVE_PCAP_CREATE
fprintf(output, " packet snapshot length (def: appropriate maximum)\n");
#else
fprintf(output, " packet snapshot length (def: %u)\n", WTAP_MAX_PACKET_SIZE_STANDARD);
#endif
fprintf(output, " -p, --no-promiscuous-mode\n");
fprintf(output, " don't capture in promiscuous mode\n");
#ifdef HAVE_PCAP_CREATE
fprintf(output, " -I, --monitor-mode capture in monitor mode, if available\n");
#endif
#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
fprintf(output, " -B <buffer size>, --buffer-size <buffer size>\n");
fprintf(output, " size of kernel buffer (def: %dMB)\n", DEFAULT_CAPTURE_BUFFER_SIZE);
#endif
fprintf(output, " -y <link type>, --linktype <link type>\n");
fprintf(output, " link layer type (def: first appropriate)\n");
fprintf(output, " --time-stamp-type <type> timestamp method for interface\n");
fprintf(output, " -D, --list-interfaces print list of interfaces and exit\n");
fprintf(output, " -L, --list-data-link-types\n");
fprintf(output, " print list of link-layer types of iface and exit\n");
fprintf(output, " --list-time-stamp-types print list of timestamp types for iface and exit\n");
fprintf(output, " --update-interval interval between updates with new packets (def: %dms)\n", DEFAULT_UPDATE_INTERVAL);
fprintf(output, "\n");
fprintf(output, "Capture stop conditions:\n");
fprintf(output, " -c <packet count> stop after n packets (def: infinite)\n");
fprintf(output, " -a <autostop cond.> ..., --autostop <autostop cond.> ...\n");
fprintf(output, " duration:NUM - stop after NUM seconds\n");
fprintf(output, " filesize:NUM - stop this file after NUM KB\n");
fprintf(output, " files:NUM - stop after NUM files\n");
fprintf(output, " packets:NUM - stop after NUM packets\n");
/*fprintf(output, "\n");*/
fprintf(output, "Capture output:\n");
fprintf(output, " -b <ringbuffer opt.> ..., --ring-buffer <ringbuffer opt.>\n");
fprintf(output, " duration:NUM - switch to next file after NUM secs\n");
fprintf(output, " filesize:NUM - switch to next file after NUM KB\n");
fprintf(output, " files:NUM - ringbuffer: replace after NUM files\n");
fprintf(output, " packets:NUM - switch to next file after NUM packets\n");
fprintf(output, " interval:NUM - switch to next file when the time is\n");
fprintf(output, " an exact multiple of NUM secs\n");
fprintf(output, " printname:FILE - print filename to FILE when written\n");
fprintf(output, " (can use 'stdout' or 'stderr')\n");
#endif /* HAVE_LIBPCAP */
#ifdef HAVE_PCAP_REMOTE
fprintf(output, "RPCAP options:\n");
fprintf(output, " -A <user>:<password> use RPCAP password authentication\n");
#endif
/*fprintf(output, "\n");*/
fprintf(output, "Input file:\n");
fprintf(output, " -r <infile>, --read-file <infile>\n");
fprintf(output, " set the filename to read from (or '-' for stdin)\n");
fprintf(output, "\n");
fprintf(output, "Processing:\n");
fprintf(output, " -2 perform a two-pass analysis\n");
fprintf(output, " -M <packet count> perform session auto reset\n");
fprintf(output, " -R <read filter>, --read-filter <read filter>\n");
fprintf(output, " packet Read filter in Wireshark display filter syntax\n");
fprintf(output, " (requires -2)\n");
fprintf(output, " -Y <display filter>, --display-filter <display filter>\n");
fprintf(output, " packet displaY filter in Wireshark display filter\n");
fprintf(output, " syntax\n");
fprintf(output, " -n disable all name resolutions (def: \"mNd\" enabled, or\n");
fprintf(output, " as set in preferences)\n");
// Note: the order of the flags here matches the options in the settings dialog e.g. "dsN" only have an effect if "n" is set
fprintf(output, " -N <name resolve flags> enable specific name resolution(s): \"mtndsNvg\"\n");
fprintf(output, " -d %s ...\n", DECODE_AS_ARG_TEMPLATE);
fprintf(output, " \"Decode As\", see the man page for details\n");
fprintf(output, " Example: tcp.port==8888,http\n");
fprintf(output, " -H <hosts file> read a list of entries from a hosts file, which will\n");
fprintf(output, " then be written to a capture file. (Implies -W n)\n");
fprintf(output, " --enable-protocol <proto_name>\n");
fprintf(output, " enable dissection of proto_name\n");
fprintf(output, " --disable-protocol <proto_name>\n");
fprintf(output, " disable dissection of proto_name\n");
fprintf(output, " --only-protocols <protocols>\n");
fprintf(output, " Only enable dissection of these protocols, comma\n");
fprintf(output, " separated. Disable everything else\n");
fprintf(output, " --disable-all-protocols\n");
fprintf(output, " Disable dissection of all protocols\n");
fprintf(output, " --enable-heuristic <short_name>\n");
fprintf(output, " enable dissection of heuristic protocol\n");
fprintf(output, " --disable-heuristic <short_name>\n");
fprintf(output, " disable dissection of heuristic protocol\n");
/*fprintf(output, "\n");*/
fprintf(output, "Output:\n");
fprintf(output, " -w <outfile|-> write packets to a pcapng-format file named \"outfile\"\n");
fprintf(output, " (or '-' for stdout). If the output filename has the\n");
fprintf(output, " .gz extension, it will be compressed to a gzip archive\n");
fprintf(output, " --capture-comment <comment>\n");
fprintf(output, " add a capture file comment, if supported\n");
fprintf(output, " -C <config profile> start with specified configuration profile\n");
fprintf(output, " --global-profile use the global profile instead of personal profile\n");
fprintf(output, " -F <output file type> set the output file type; default is pcapng.\n");
fprintf(output, " an empty \"-F\" option will list the file types\n");
fprintf(output, " -V add output of packet tree (Packet Details)\n");
fprintf(output, " -O <protocols> Only show packet details of these protocols, comma\n");
fprintf(output, " separated\n");
fprintf(output, " -P, --print print packet summary even when writing to a file\n");
fprintf(output, " -S <separator> the line separator to print between packets\n");
fprintf(output, " -x add output of hex and ASCII dump (Packet Bytes)\n");
fprintf(output, " --hexdump <hexoption> add hexdump, set options for data source and ASCII dump\n");
fprintf(output, " all dump all data sources (-x default)\n");
fprintf(output, " frames dump only frame data source\n");
fprintf(output, " ascii include ASCII dump text (-x default)\n");
fprintf(output, " delimit delimit ASCII dump text with '|' characters\n");
fprintf(output, " noascii exclude ASCII dump text\n");
fprintf(output, " help display help for --hexdump and exit\n");
fprintf(output, " -T pdml|ps|psml|json|jsonraw|ek|tabs|text|fields|?\n");
fprintf(output, " format of text output (def: text)\n");
fprintf(output, " -j <protocolfilter> protocols layers filter if -T ek|pdml|json selected\n");
fprintf(output, " (e.g. \"ip ip.flags text\", filter does not expand child\n");
fprintf(output, " nodes, unless child is specified also in the filter)\n");
fprintf(output, " -J <protocolfilter> top level protocol filter if -T ek|pdml|json selected\n");
fprintf(output, " (e.g. \"http tcp\", filter which expands all child nodes)\n");
fprintf(output, " -e <field> field to print if -Tfields selected (e.g. tcp.port,\n");
fprintf(output, " _ws.col.info)\n");
fprintf(output, " this option can be repeated to print multiple fields\n");
fprintf(output, " -E<fieldsoption>=<value> set options for output when -Tfields selected:\n");
fprintf(output, " bom=y|n print a UTF-8 BOM\n");
fprintf(output, " header=y|n switch headers on and off\n");
fprintf(output, " separator=/t|/s|<char> select tab, space, printable character as separator\n");
fprintf(output, " occurrence=f|l|a print first, last or all occurrences of each field\n");
fprintf(output, " aggregator=,|/s|<char> select comma, space, printable character as\n");
fprintf(output, " aggregator\n");
fprintf(output, " quote=d|s|n select double, single, no quotes for values\n");
fprintf(output, " -t (a|ad|adoy|d|dd|e|r|u|ud|udoy)[.[N]]|.[N]\n");
fprintf(output, " output format of time stamps (def: r: rel. to first)\n");
fprintf(output, " -u s|hms output format of seconds (def: s: seconds)\n");
fprintf(output, " -l flush standard output after each packet\n");
fprintf(output, " (implies --update-interval 0)\n");
fprintf(output, " -q be more quiet on stdout (e.g. when using statistics)\n");
fprintf(output, " -Q only log true errors to stderr (quieter than -q)\n");
fprintf(output, " -g enable group read access on the output file(s)\n");
fprintf(output, " -W n Save extra information in the file, if supported.\n");
fprintf(output, " n = write network address resolution information\n");
fprintf(output, " -X <key>:<value> eXtension options, see the man page for details\n");
fprintf(output, " -U tap_name PDUs export mode, see the man page for details\n");
fprintf(output, " -z <statistics> various statistics, see the man page for details\n");
fprintf(output, " --export-objects <protocol>,<destdir>\n");
fprintf(output, " save exported objects for a protocol to a directory\n");
fprintf(output, " named \"destdir\"\n");
fprintf(output, " --export-tls-session-keys <keyfile>\n");
fprintf(output, " export TLS Session Keys to a file named \"keyfile\"\n");
fprintf(output, " --color color output text similarly to the Wireshark GUI,\n");
fprintf(output, " requires a terminal with 24-bit color support\n");
fprintf(output, " Also supplies color attributes to pdml and psml formats\n");
fprintf(output, " (Note that attributes are nonstandard)\n");
fprintf(output, " --no-duplicate-keys If -T json is specified, merge duplicate keys in an object\n");
fprintf(output, " into a single key with as value a json array containing all\n");
fprintf(output, " values\n");
fprintf(output, " --elastic-mapping-filter <protocols> If -G elastic-mapping is specified, put only the\n");
fprintf(output, " specified protocols within the mapping file\n");
fprintf(output, " --temp-dir <directory> write temporary files to this directory\n");
fprintf(output, " (default: %s)\n", g_get_tmp_dir());
fprintf(output, " --compress <type> compress the output file using the type compression format\n");
fprintf(output, "\n");
ws_log_print_usage(output);
fprintf(output, "\n");
fprintf(output, "Miscellaneous:\n");
fprintf(output, " -h, --help display this help and exit\n");
fprintf(output, " -v, --version display version info and exit\n");
fprintf(output, " -o <name>:<value> ... override preference setting\n");
fprintf(output, " -K <keytab> keytab file to use for kerberos decryption\n");
fprintf(output, " -G [report] dump one of several available reports and exit\n");
fprintf(output, " default report=\"fields\"\n");
fprintf(output, " use \"-G help\" for more help\n");
#ifdef __linux__
fprintf(output, "\n");
fprintf(output, "Dumpcap can benefit from an enabled BPF JIT compiler if available.\n");
fprintf(output, "You might want to enable it by executing:\n");
fprintf(output, " \"echo 1 > /proc/sys/net/core/bpf_jit_enable\"\n");
fprintf(output, "Note that this can make your system less secure!\n");
#endif
}
static void
glossary_option_help(void)
{
FILE *output;
output = stdout;
fprintf(output, "%s\n", get_appname_and_version());
fprintf(output, "\n");
fprintf(output, "Usage: tshark -G [report]\n");
fprintf(output, "\n");
fprintf(output, "Glossary table reports:\n");
fprintf(output, " -G column-formats dump column format codes and exit\n");
fprintf(output, " -G decodes dump \"layer type\"/\"decode as\" associations and exit\n");
fprintf(output, " -G dissector-tables dump dissector table names, types, and properties\n");
fprintf(output, " -G dissectors dump registered dissector names\n");
fprintf(output, " -G elastic-mapping dump ElasticSearch mapping file\n");
fprintf(output, " -G enterprises dump IANA Private Enterprise Number (PEN) table\n");
fprintf(output, " -G fieldcount dump count of header fields and exit\n");
fprintf(output, " -G fields [prefix] dump fields glossary and exit\n");
fprintf(output, " -G ftypes dump field type basic and descriptive names\n");
fprintf(output, " -G heuristic-decodes dump heuristic dissector tables\n");
fprintf(output, " -G manuf dump ethernet manufacturer tables\n");
fprintf(output, " -G plugins dump installed plugins and exit\n");
fprintf(output, " -G protocols dump protocols in registration database and exit\n");
fprintf(output, " -G services dump transport service (port) names\n");
fprintf(output, " -G values dump value, range, true/false strings and exit\n");
fprintf(output, "\n");
fprintf(output, "Preference reports:\n");
fprintf(output, " -G currentprefs dump current preferences and exit\n");
fprintf(output, " -G defaultprefs dump default preferences and exit\n");
fprintf(output, " -G folders dump about:folders\n");
fprintf(output, "\n");
}
static void
hexdump_option_help(FILE *output)
{
fprintf(output, "%s\n", get_appname_and_version());
fprintf(output, "\n");
fprintf(output, "tshark: Valid --hexdump <hexoption> values include:\n");
fprintf(output, "\n");
fprintf(output, "Data source options:\n");
fprintf(output, " all add hexdump, dump all data sources (-x default)\n");
fprintf(output, " frames add hexdump, dump only frame data source\n");
fprintf(output, "\n");
fprintf(output, "ASCII options:\n");
fprintf(output, " ascii add hexdump, include ASCII dump text (-x default)\n");
fprintf(output, " delimit add hexdump, delimit ASCII dump text with '|' characters\n");
fprintf(output, " noascii add hexdump, exclude ASCII dump text\n");
fprintf(output, "\n");
fprintf(output, "Miscellaneous:\n");
fprintf(output, " help display this help and exit\n");
fprintf(output, "\n");
fprintf(output, "Example:\n");
fprintf(output, "\n");
fprintf(output, " $ tshark ... --hexdump frames --hexdump delimit ...\n");
fprintf(output, "\n");
}
static void
print_current_user(void)
{
char *cur_user, *cur_group;
if (started_with_special_privs()) {
cur_user = get_cur_username();
cur_group = get_cur_groupname();
fprintf(stderr, "Running as user \"%s\" and group \"%s\".",
cur_user, cur_group);
g_free(cur_user);
g_free(cur_group);
if (running_with_special_privs()) {
fprintf(stderr, " This could be dangerous.");
}
fprintf(stderr, "\n");
}
}
static void
gather_tshark_compile_info(feature_list l)
{
/* Capture libraries */
gather_caplibs_compile_info(l);
epan_gather_compile_info(l);
}
static void
gather_tshark_runtime_info(feature_list l)
{
#ifdef HAVE_LIBPCAP
gather_caplibs_runtime_info(l);
#endif
/* stuff used by libwireshark */
epan_gather_runtime_info(l);
}
static bool
_compile_dfilter(const char *text, dfilter_t **dfp, const char *caller)
{
bool ok;
df_error_t *df_err;
char *err_off;
char *expanded;
int64_t elapsed_start;
elapsed_start = g_get_monotonic_time();
expanded = dfilter_expand(text, &df_err);
if (expanded == NULL) {
cmdarg_err("%s", df_err->msg);
df_error_free(&df_err);
return false;
}
tshark_elapsed.dfilter_expand = g_get_monotonic_time() - elapsed_start;
elapsed_start = g_get_monotonic_time();
ok = dfilter_compile_full(expanded, dfp, &df_err, DF_OPTIMIZE, caller);
if (!ok ) {
cmdarg_err("%s", df_err->msg);
if (df_err->loc.col_start >= 0) {
err_off = ws_strdup_underline(NULL, df_err->loc.col_start, df_err->loc.col_len);
cmdarg_err_cont(" %s", expanded);
cmdarg_err_cont(" %s", err_off);
g_free(err_off);
}
df_error_free(&df_err);
}
tshark_elapsed.dfilter_compile = g_get_monotonic_time() - elapsed_start;
g_free(expanded);
return ok;
}
#define compile_dfilter(text, dfp) _compile_dfilter(text, dfp, __func__)
static bool
protocolfilter_add_opt(const char* arg, pf_flags filter_flags)
{
char **newfilter = NULL;
for (newfilter = wmem_strsplit(wmem_epan_scope(), arg, " ", -1); *newfilter; newfilter++) {
if (strcmp(*newfilter, "") == 0) {
/* Don't treat the empty string as an intended field abbreviation
* to output, consecutive spaces on the command line probably
* aren't intentional.
*/
continue;
}
if (!output_fields_add_protocolfilter(output_fields, *newfilter, filter_flags)) {
cmdarg_err("%s was already specified with different filter flags. Overwriting previous protocol filter.", *newfilter);
}
}
return true;
}
static void
about_folders(void)
{
const char *constpath;
char *path;
int i;
char **resultArray;
/* "file open" */
/*
* Fetching the "File" dialogs folder not implemented.
* This is arguably just a pwd for a ui/cli .
*/
/* temp */
constpath = g_get_tmp_dir();
#ifdef HAVE_LIBPCAP
/* global_capture_opts only exists in this case */
if (global_capture_opts.temp_dir)
constpath = global_capture_opts.temp_dir;
#endif
printf("%-21s\t%s\n", "Temp:", constpath);
/* pers conf */
path = get_persconffile_path("", false);
printf("%-21s\t%s\n", "Personal configuration:", path);
g_free(path);
/* global conf */
constpath = get_datafile_dir();
if (constpath != NULL) {
printf("%-21s\t%s\n", "Global configuration:", constpath);
}
/* system */
constpath = get_systemfile_dir();
printf("%-21s\t%s\n", "System:", constpath);
/* program */
constpath = get_progfile_dir();
printf("%-21s\t%s\n", "Program:", constpath);
#ifdef HAVE_PLUGINS
/* pers plugins */
printf("%-21s\t%s\n", "Personal Plugins:", get_plugins_pers_dir_with_version());
/* global plugins */
printf("%-21s\t%s\n", "Global Plugins:", get_plugins_dir_with_version());
#endif
#ifdef HAVE_LUA
/* pers lua plugins */
printf("%-21s\t%s\n", "Personal Lua Plugins:", get_plugins_pers_dir());
/* global lua plugins */
printf("%-21s\t%s\n", "Global Lua Plugins:", get_plugins_dir());
#endif
/* Personal Extcap */
constpath = get_extcap_pers_dir();
resultArray = g_strsplit(constpath, G_SEARCHPATH_SEPARATOR_S, 10);
for(i = 0; resultArray[i]; i++)
printf("%-21s\t%s\n", "Personal Extcap path:", g_strstrip(resultArray[i]));
g_strfreev(resultArray);
/* Global Extcap */
constpath = get_extcap_dir();
resultArray = g_strsplit(constpath, G_SEARCHPATH_SEPARATOR_S, 10);
for(i = 0; resultArray[i]; i++)
printf("%-21s\t%s\n", "Global Extcap path:", g_strstrip(resultArray[i]));
g_strfreev(resultArray);
/* MaxMindDB */
path = maxmind_db_get_paths();
resultArray = g_strsplit(path, G_SEARCHPATH_SEPARATOR_S, 10);
for(i = 0; resultArray[i]; i++)
printf("%-21s\t%s\n", "MaxMind database path:", g_strstrip(resultArray[i]));
g_strfreev(resultArray);
g_free(path);
#ifdef HAVE_LIBSMI
/* SMI MIBs/PIBs */
path = oid_get_default_mib_path();
resultArray = g_strsplit(path, G_SEARCHPATH_SEPARATOR_S, 20);
for(i = 0; resultArray[i]; i++)
printf("%-21s\t%s\n", "MIB/PIB path:", g_strstrip(resultArray[i]));
g_strfreev(resultArray);
g_free(path);
#endif
}
static bool
must_do_dissection(dfilter_t *rfcode, dfilter_t *dfcode,
char *volatile pdu_export_arg)
{
/* We have to dissect each packet if:
we're printing information about each packet;
we're using a read filter on the packets;
we're using a display filter on the packets;
we're exporting PDUs;
we're using any taps that need dissection. */
return print_packet_info || rfcode || dfcode || pdu_export_arg ||
tap_listeners_require_dissection();
}
#ifdef HAVE_LIBPCAP
/*
* Check whether a purported *shark packet-matching expression (display
* or read filter) looks like a capture filter and, if so, print a
* warning.
*
* Used, for example, if the string in question isn't a valid packet-
* matching expression.
*/
static void
warn_about_capture_filter(const char *rfilter)
{
struct bpf_program fcode;
pcap_t *pc;
pc = pcap_open_dead(DLT_EN10MB, MIN_PACKET_SIZE);
if (pc != NULL) {
if (pcap_compile(pc, &fcode, rfilter, 0, 0) != -1) {
pcap_freecode(&fcode);
cmdarg_err_cont(
" Note: That read filter code looks like a valid capture filter;\n"
" maybe you mixed them up?");
}
pcap_close(pc);
}
}
#endif
#ifdef HAVE_LIBPCAP
static GList *cached_if_list;
static GList *
capture_opts_get_interface_list(int *err, char **err_str)
{
if (cached_if_list == NULL) {
/*
* This isn't a GUI tool, so no need for a callback.
*/
cached_if_list = capture_interface_list(err, err_str, NULL);
}
/*
* Routines expect to free the returned interface list, so return
* a deep copy.
*/
return interface_list_copy(cached_if_list);
}
#endif
int
main(int argc, char *argv[])
{
char *err_msg;
static const struct report_message_routines tshark_report_routines = {
failure_message,
failure_message,
open_failure_message,
read_failure_message,
write_failure_message,
cfile_open_failure_message,
cfile_dump_open_failure_message,
cfile_read_failure_message,
cfile_write_failure_message,
cfile_close_failure_message
};
int opt;
static const struct ws_option long_options[] = {
{"help", ws_no_argument, NULL, 'h'},
{"version", ws_no_argument, NULL, 'v'},
LONGOPT_CAPTURE_COMMON
LONGOPT_DISSECT_COMMON
LONGOPT_READ_CAPTURE_COMMON
{"print", ws_no_argument, NULL, 'P'},
{"export-objects", ws_required_argument, NULL, LONGOPT_EXPORT_OBJECTS},
{"export-tls-session-keys", ws_required_argument, NULL, LONGOPT_EXPORT_TLS_SESSION_KEYS},
{"color", ws_no_argument, NULL, LONGOPT_COLOR},
{"no-duplicate-keys", ws_no_argument, NULL, LONGOPT_NO_DUPLICATE_KEYS},
{"elastic-mapping-filter", ws_required_argument, NULL, LONGOPT_ELASTIC_MAPPING_FILTER},
{"capture-comment", ws_required_argument, NULL, LONGOPT_CAPTURE_COMMENT},
{"hexdump", ws_required_argument, NULL, LONGOPT_HEXDUMP},
{"selected-frame", ws_required_argument, NULL, LONGOPT_SELECTED_FRAME},
{"print-timers", ws_no_argument, NULL, LONGOPT_PRINT_TIMERS},
{"global-profile", ws_no_argument, NULL, LONGOPT_GLOBAL_PROFILE},
{"compress", ws_required_argument, NULL, LONGOPT_COMPRESS},
{0, 0, 0, 0}
};
bool arg_error = false;
bool has_extcap_options = false;
volatile bool is_capturing = true;
int err;
char *err_info;
bool exp_pdu_status;
volatile process_file_status_t status;
volatile bool draw_taps = false;
volatile int exit_status = EXIT_SUCCESS;
#ifdef HAVE_LIBPCAP
int caps_queries = 0;
GList *if_list;
char *err_str, *err_str_secondary;
#else