1 /* Edit capture files. We can delete packets, adjust timestamps, or
2 * simply convert from one format to another format.
6 * Originally written by Richard Sharpe.
7 * Improved by Guy Harris.
8 * Further improved by Richard Sharpe.
21 * Just make sure we include the prototype for strptime as well
22 * (needed for glibc 2.2) but make sure we do this only if not
39 #ifdef HAVE_SYS_TIME_H
50 #include <process.h> /* getpid */
53 #ifdef NEED_STRPTIME_H
54 # include "strptime.h"
57 #include "epan/crypt/crypt-md5.h"
58 #include "epan/plugins.h"
59 #include "epan/report_err.h"
60 #include "epan/filesystem.h"
62 #include "svnversion.h"
65 * Some globals so we can pass things to various routines
77 * Duplicate frame detection
79 typedef struct _fd_hash_t {
80 md5_byte_t digest[16];
85 fd_hash_t fd_hash[DUP_DEPTH];
88 #define ONE_MILLION 1000000
90 /* Weights of different errors we can introduce */
91 /* We should probably make these command-line arguments */
92 /* XXX - Should we add a bit-level error? */
93 #define ERR_WT_BIT 5 /* Flip a random bit */
94 #define ERR_WT_BYTE 5 /* Substitute a random byte */
95 #define ERR_WT_ALNUM 5 /* Substitute a random character in [A-Za-z0-9] */
96 #define ERR_WT_FMT 2 /* Substitute "%s" */
97 #define ERR_WT_AA 1 /* Fill the remainder of the buffer with 0xAA */
98 #define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
100 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
101 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
104 struct time_adjustment {
109 #define MAX_SELECTIONS 512
110 static struct select_item selectfrm[MAX_SELECTIONS];
111 static int max_selected = -1;
112 static int keep_em = 0;
113 static int out_file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
114 static int out_frame_type = -2; /* Leave frame type alone */
115 static int verbose = 0; /* Not so verbose */
116 static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
117 static double err_prob = 0.0;
118 static time_t starttime = 0;
119 static time_t stoptime = 0;
120 static gboolean check_startstop = FALSE;
121 static gboolean dup_detect = FALSE;
123 static int find_dct2000_real_data(guint8 *buf);
125 /* Add a selection item, a simple parser for now */
127 add_selection(char *sel)
132 if (++max_selected >= MAX_SELECTIONS) {
133 /* Let the user know we stopped selecting */
134 printf("Out of room for packet selections!\n");
138 printf("Add_Selected: %s\n", sel);
140 if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
142 printf("Not inclusive ...");
144 selectfrm[max_selected].inclusive = 0;
145 selectfrm[max_selected].first = atoi(sel);
147 printf(" %i\n", selectfrm[max_selected].first);
152 printf("Inclusive ...");
155 selectfrm[max_selected].inclusive = 1;
156 selectfrm[max_selected].first = atoi(sel);
157 selectfrm[max_selected].second = atoi(next);
159 printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
166 /* Was the packet selected? */
173 for (i = 0; i<= max_selected; i++) {
175 if (selectfrm[i].inclusive) {
176 if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
180 if (recno == selectfrm[i].first)
189 /* is the packet in the selected timeframe */
191 check_timestamp(wtap *wth)
194 struct wtap_pkthdr* pkthdr = wtap_phdr(wth);
197 printf("== %d starttime=%lu stoptime=%lu ts=%lu\n",i,
198 (unsigned long)starttime,
199 (unsigned long)stoptime,
200 (unsigned long)pkthdr->ts.secs);
201 return ( pkthdr->ts.secs >= starttime ) && ( pkthdr->ts.secs <= stoptime );
205 set_time_adjustment(char *optarg)
214 /* skip leading whitespace */
215 while (*optarg == ' ' || *optarg == '\t') {
219 /* check for a negative adjustment */
220 if (*optarg == '-') {
221 time_adj.is_negative = 1;
225 /* collect whole number of seconds, if any */
226 if (*optarg == '.') { /* only fractional (i.e., .5 is ok) */
230 val = strtol(optarg, &frac, 10);
231 if (frac == NULL || frac == optarg || val == LONG_MIN || val == LONG_MAX) {
232 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
236 if (val < 0) { /* implies '--' since we caught '-' above */
237 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
242 time_adj.tv.tv_sec = val;
244 /* now collect the partial seconds, if any */
245 if (*frac != '\0') { /* chars left, so get fractional part */
246 val = strtol(&(frac[1]), &end, 10);
247 if (*frac != '.' || end == NULL || end == frac
248 || val < 0 || val > ONE_MILLION || val == LONG_MIN || val == LONG_MAX) {
249 fprintf(stderr, "editcap: \"%s\" isn't a valid time adjustment\n",
255 return; /* no fractional digits */
258 /* adjust fractional portion from fractional to numerator
259 * e.g., in "1.5" from 5 to 500000 since .5*10^6 = 500000 */
260 if (frac && end) { /* both are valid */
261 frac_digits = end - frac - 1; /* fractional digit count (remember '.') */
262 while(frac_digits < 6) { /* this is frac of 10^6 */
267 time_adj.tv.tv_usec = val;
271 is_duplicate(guint8* fd, guint32 len) {
276 if (cur_dup >= DUP_DEPTH)
279 /* Calculate our digest */
281 md5_append(&ms, fd, len);
282 md5_finish(&ms, fd_hash[cur_dup].digest);
284 fd_hash[cur_dup].len = len;
286 /* Look for duplicates */
287 for (i = 0; i < DUP_DEPTH; i++) {
291 if (fd_hash[i].len == fd_hash[cur_dup].len &&
292 memcmp(fd_hash[i].digest, fd_hash[cur_dup].digest, 16) == 0) {
300 static void usage(void)
302 fprintf(stderr, "Editcap %s"
307 fprintf(stderr, "Edit and/or translate the format of capture files.\n");
308 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
309 fprintf(stderr, "\n");
310 fprintf(stderr, "Usage: editcap [options] ... <infile> <outfile> [ <packet#>[-<packet#>] ... ]\n");
311 fprintf(stderr, "\n");
312 fprintf(stderr, "A single packet or a range of packets can be selected.\n");
313 fprintf(stderr, "\n");
314 fprintf(stderr, "Packet selection:\n");
315 fprintf(stderr, " -r keep the selected packets, default is to delete them\n");
316 fprintf(stderr, " -A <start time> don't output packets whose timestamp is before the\n");
317 fprintf(stderr, " given time (format as YYYY-MM-DD hh:mm:ss)\n");
318 fprintf(stderr, " -B <stop time> don't output packets whose timestamp is after the\n");
319 fprintf(stderr, " given time (format as YYYY-MM-DD hh:mm:ss)\n");
320 fprintf(stderr, " -d remove duplicate packets\n");
321 fprintf(stderr, "\n");
322 fprintf(stderr, "Packet manipulation:\n");
323 fprintf(stderr, " -s <snaplen> truncate each packet to max. <snaplen> bytes of data\n");
324 fprintf(stderr, " -C <choplen> chop each packet at the end by <choplen> bytes\n");
325 fprintf(stderr, " -t <time adjustment> adjust the timestamp of each packet,\n");
326 fprintf(stderr, " <time adjustment> is in relative seconds (e.g. -0.5)\n");
327 fprintf(stderr, " -E <error probability> set the probability (between 0.0 and 1.0 incl.)\n");
328 fprintf(stderr, " that a particular packet byte will be randomly changed\n");
329 fprintf(stderr, "\n");
330 fprintf(stderr, "Output File(s):\n");
331 fprintf(stderr, " -c <packets per file> split the packet output to different files,\n");
332 fprintf(stderr, " with a maximum of <packets per file> each\n");
333 fprintf(stderr, " -F <capture type> set the output file type, default is libpcap\n");
334 fprintf(stderr, " an empty \"-F\" option will list the file types\n");
335 fprintf(stderr, " -T <encap type> set the output file encapsulation type,\n");
336 fprintf(stderr, " default is the same as the input file\n");
337 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types\n");
338 fprintf(stderr, "\n");
339 fprintf(stderr, "Miscellaneous:\n");
340 fprintf(stderr, " -h display this help and exit\n");
341 fprintf(stderr, " -v verbose output\n");
342 fprintf(stderr, "\n");
345 static void list_capture_types(void) {
348 fprintf(stderr, "editcap: The available capture file types for \"F\":\n");
349 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
350 if (wtap_dump_can_open(i))
351 fprintf(stderr, " %s - %s\n",
352 wtap_file_type_short_string(i), wtap_file_type_string(i));
356 static void list_encap_types(void) {
360 fprintf(stderr, "editcap: The available encapsulation types for \"T\":\n");
361 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
362 string = wtap_encap_short_string(i);
364 fprintf(stderr, " %s - %s\n",
365 string, wtap_encap_string(i));
370 failure_message(const char *msg_format, va_list ap)
372 fprintf(stderr, "editcap: ");
373 vfprintf(stderr, msg_format, ap);
374 fprintf(stderr, "\n");
377 int main(int argc, char *argv[])
387 unsigned int snaplen = 0; /* No limit */
388 unsigned int choplen = 0; /* No chop */
392 struct wtap_pkthdr snap_phdr;
393 const struct wtap_pkthdr *phdr;
396 int split_packet_count = 0;
397 int written_count = 0;
399 size_t filenamelen = 0;
402 char* init_progfile_dir_error;
404 /* Register wiretap plugins */
405 if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
406 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
407 g_free(init_progfile_dir_error);
409 init_report_err(failure_message,NULL,NULL);
414 /* Process the options */
415 while ((opt = getopt(argc, argv, "A:B:c:C:dE:F:hrs:t:T:v")) !=-1) {
420 err_prob = strtod(optarg, &p);
421 if (p == optarg || err_prob < 0.0 || err_prob > 1.0) {
422 fprintf(stderr, "editcap: probability \"%s\" must be between 0.0 and 1.0\n",
426 srand( (unsigned int) (time(NULL) + getpid()) );
430 out_file_type = wtap_short_string_to_file_type(optarg);
431 if (out_file_type < 0) {
432 fprintf(stderr, "editcap: \"%s\" isn't a valid capture file type\n\n",
434 list_capture_types();
440 split_packet_count = strtol(optarg, &p, 10);
441 if (p == optarg || *p != '\0') {
442 fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
446 if (split_packet_count <= 0) {
447 fprintf(stderr, "editcap: \"%d\" packet count must be larger than zero\n",
454 choplen = strtol(optarg, &p, 10);
455 if (p == optarg || *p != '\0') {
456 fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
464 for (i = 0; i < DUP_DEPTH; i++) {
465 memset(&fd_hash[i].digest, 0, 16);
470 case '?': /* Bad options if GNU getopt */
473 list_capture_types();
490 keep_em = !keep_em; /* Just invert */
494 snaplen = strtol(optarg, &p, 10);
495 if (p == optarg || *p != '\0') {
496 fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
503 set_time_adjustment(optarg);
507 out_frame_type = wtap_short_string_to_encap(optarg);
508 if (out_frame_type < 0) {
509 fprintf(stderr, "editcap: \"%s\" isn't a valid encapsulation type\n\n",
517 verbose = !verbose; /* Just invert */
524 memset(&starttm,0,sizeof(struct tm));
526 if(!strptime(optarg,"%Y-%m-%d %T",&starttm)) {
527 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
531 check_startstop = TRUE;
532 starttm.tm_isdst = -1;
534 starttime = mktime(&starttm);
535 printf("=START=> given='%s' stoptime=%lu\n",optarg,(unsigned long)starttime);
543 memset(&stoptm,0,sizeof(struct tm));
545 if(!strptime(optarg,"%Y-%m-%d %T",&stoptm)) {
546 fprintf(stderr, "editcap: \"%s\" isn't a valid time format\n\n", optarg);
549 check_startstop = TRUE;
550 stoptm.tm_isdst = -1;
551 stoptime = mktime(&stoptm);
552 printf("=STOP=> given='%s' stoptime=%lu\n",optarg,(unsigned long)stoptime);
560 printf("Optind = %i, argc = %i\n", optind, argc);
563 if ((argc - optind) < 1) {
570 if (check_startstop && !stoptime) {
572 /* XXX: will work until 2035 */
573 memset(&stoptm,0,sizeof(struct tm));
574 stoptm.tm_year = 135;
578 stoptime = mktime(&stoptm);
579 printf("=STOP=NEVER=> stoptime=%lu\n",(unsigned long)stoptime);
582 if (starttime > stoptime) {
583 fprintf(stderr, "editcap: start time is after the stop time\n");
586 printf("==> stoptime=%lu stoptime=%lu\n",(unsigned long)starttime,(unsigned long)stoptime);
588 wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
591 fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
595 case WTAP_ERR_UNSUPPORTED:
596 case WTAP_ERR_UNSUPPORTED_ENCAP:
597 case WTAP_ERR_BAD_RECORD:
598 fprintf(stderr, "(%s)\n", err_info);
607 fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
608 wtap_file_type_string(wtap_file_type(wth)));
612 * Now, process the rest, if any ... we only write if there is an extra
616 if ((argc - optind) >= 2) {
618 if (out_frame_type == -2)
619 out_frame_type = wtap_file_encap(wth);
621 if (split_packet_count > 0) {
622 filenamelen = strlen(argv[optind+1]) + 20;
623 filename = (char *) g_malloc(filenamelen);
627 g_snprintf(filename, filenamelen, "%s-%05d", argv[optind+1], 0);
629 filename = argv[optind+1];
632 pdh = wtap_dump_open(filename, out_file_type,
633 out_frame_type, wtap_snapshot_length(wth),
634 FALSE /* compressed */, &err);
637 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
642 for (i = optind + 2; i < argc; i++)
643 if (add_selection(argv[i]) == FALSE)
646 while (wtap_read(wth, &err, &err_info, &data_offset)) {
648 if (split_packet_count > 0 && (written_count % split_packet_count == 0)) {
649 if (!wtap_dump_close(pdh, &err)) {
651 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
656 g_snprintf(filename, filenamelen, "%s-%05d",argv[optind+1], count / split_packet_count);
659 fprintf(stderr, "Continuing writing in file %s\n", filename);
662 pdh = wtap_dump_open(filename, out_file_type,
663 out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
666 fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
672 check_ts = check_timestamp(wth);
674 if ( ((check_startstop && check_ts) || (!check_startstop && !check_ts)) && ((!selected(count) && !keep_em) ||
675 (selected(count) && keep_em)) ) {
678 printf("Packet: %u\n", count);
680 /* We simply write it, perhaps after truncating it; we could do other
681 things, like modify it. */
683 phdr = wtap_phdr(wth);
685 if (choplen != 0 && phdr->caplen > choplen) {
687 snap_phdr.caplen -= choplen;
691 if (snaplen != 0 && phdr->caplen > snaplen) {
693 snap_phdr.caplen = snaplen;
697 /* assume that if the frame's tv_sec is 0, then
698 * the timestamp isn't supported */
699 if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
701 if (time_adj.is_negative)
702 snap_phdr.ts.secs -= time_adj.tv.tv_sec;
704 snap_phdr.ts.secs += time_adj.tv.tv_sec;
708 /* assume that if the frame's tv_sec is 0, then
709 * the timestamp isn't supported */
710 if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
712 if (time_adj.is_negative) { /* subtract */
713 if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
715 snap_phdr.ts.nsecs += ONE_MILLION * 1000;
717 snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
719 if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
722 snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
724 snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
731 buf = wtap_buf_ptr(wth);
732 if (is_duplicate(buf, phdr->caplen)) {
734 printf("Skipping duplicate: %u\n", count);
740 /* Random error mutation */
741 if (err_prob > 0.0) {
742 int real_data_start = 0;
743 buf = wtap_buf_ptr(wth);
744 /* Protect non-protocol data */
745 if (wtap_file_type(wth) == WTAP_FILE_CATAPULT_DCT2000) {
746 real_data_start = find_dct2000_real_data(buf);
748 for (i = real_data_start; i < (int) phdr->caplen; i++) {
749 if (rand() <= err_prob * RAND_MAX) {
750 err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
752 if (err_type < ERR_WT_BIT) {
753 buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
754 err_type = ERR_WT_TOTAL;
756 err_type -= ERR_WT_BYTE;
759 if (err_type < ERR_WT_BYTE) {
760 buf[i] = rand() / (RAND_MAX / 255 + 1);
761 err_type = ERR_WT_TOTAL;
763 err_type -= ERR_WT_BYTE;
766 if (err_type < ERR_WT_ALNUM) {
767 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
768 err_type = ERR_WT_TOTAL;
770 err_type -= ERR_WT_ALNUM;
773 if (err_type < ERR_WT_FMT) {
774 if ((unsigned int)i < phdr->caplen - 2)
775 strncpy((char*) &buf[i], "%s", 2);
776 err_type = ERR_WT_TOTAL;
778 err_type -= ERR_WT_FMT;
781 if (err_type < ERR_WT_AA) {
782 for (j = i; j < (int) phdr->caplen; j++) {
791 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), wtap_buf_ptr(wth),
793 fprintf(stderr, "editcap: Error writing to %s: %s\n",
794 filename, wtap_strerror(err));
803 /* Print a message noting that the read failed somewhere along the line. */
805 "editcap: An error occurred while reading \"%s\": %s.\n",
806 argv[optind], wtap_strerror(err));
809 case WTAP_ERR_UNSUPPORTED:
810 case WTAP_ERR_UNSUPPORTED_ENCAP:
811 case WTAP_ERR_BAD_RECORD:
812 fprintf(stderr, "(%s)\n", err_info);
817 if (!wtap_dump_close(pdh, &err)) {
819 fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
829 /* Skip meta-information read from file to return offset of real
831 static int find_dct2000_real_data(guint8 *buf)
835 for (n=0; buf[n] != '\0'; n++); /* Context name */
837 n++; /* Context port number */
838 for (; buf[n] != '\0'; n++); /* Timestamp */
840 for (; buf[n] != '\0'; n++); /* Protocol name */
842 for (; buf[n] != '\0'; n++); /* Variant number (as string) */
844 for (; buf[n] != '\0'; n++); /* Outhdr (as string) */
846 n += 2; /* Direction & encap */