|
| 1 | +/* |
| 2 | +* pibs - Passive Identification of BackScatter |
| 3 | +* |
| 4 | +* Copyright (C) 2019 Gerard Wagener |
| 5 | +* Copyright (C) 2019 CIRCL Computer Incident Response Center Luxembourg |
| 6 | +* (SMILE gie). |
| 7 | +* |
| 8 | +* This program is free software: you can redistribute it and/or modify |
| 9 | +* it under the terms of the GNU Affero General Public License as published by |
| 10 | +* the Free Software Foundation, either version 3 of the License, or |
| 11 | +* (at your option) any later version. |
| 12 | +* |
| 13 | +* This program is distributed in the hope that it will be useful, |
| 14 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +* GNU Affero General Public License for more details. |
| 17 | +* |
| 18 | +* You should have received a copy of the GNU Affero General Public License |
| 19 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | +*/ |
| 21 | +#include "pibs.h" |
| 22 | + |
| 23 | +int main(int argc, char* argv[]) |
| 24 | +{ |
| 25 | + |
| 26 | + int opt; |
| 27 | + pibs_t* pibs; |
| 28 | + |
| 29 | + pibs = init(); |
| 30 | + |
| 31 | + fprintf(stderr, "[INFO] pid = %d\n",(int)getpid()); |
| 32 | + |
| 33 | + while ((opt = getopt(argc, argv, "r:dbsni:au:z:p:w:y:")) != -1) { |
| 34 | + switch (opt) { |
| 35 | + case 'r': |
| 36 | + strncpy(pibs->filename, optarg, FILENAME_MAX); |
| 37 | + break; |
| 38 | + case 'd': |
| 39 | + pibs->should_dump_table = 1; |
| 40 | + break; |
| 41 | + case 'b': |
| 42 | + pibs->show_backscatter = 1; |
| 43 | + break; |
| 44 | + case 's': |
| 45 | + pibs->show_stats = 1; |
| 46 | + break; |
| 47 | + case 'n': |
| 48 | + pibs->should_create_shm = 1; |
| 49 | + break; |
| 50 | + case 'i': |
| 51 | + strncpy(pibs->shmid_file, optarg, FILENAME_MAX); |
| 52 | + break; |
| 53 | + case 'a': |
| 54 | + pibs->should_attach = 1; |
| 55 | + break; |
| 56 | + case 'u': |
| 57 | + strncpy(pibs->uuid, optarg, SZUUID); |
| 58 | + break; |
| 59 | + case 'z': |
| 60 | + strncpy(pibs->server,optarg, SZSERVER); |
| 61 | + break; |
| 62 | + case 'p': |
| 63 | + pibs->port=atoi(optarg); |
| 64 | + break; |
| 65 | + case 'w': |
| 66 | + strncpy(pibs->outputfile,optarg, FILENAME_MAX); |
| 67 | + pibs->should_writepcap = 1; |
| 68 | + break; |
| 69 | + case 'y': |
| 70 | + pibs->redisdb = atoi(optarg); |
| 71 | + break; |
| 72 | + |
| 73 | + default: /* '?' */ |
| 74 | + |
| 75 | + fprintf(stderr, "[ERROR] Invalid command line was specified\n"); |
| 76 | + } |
| 77 | + } |
| 78 | + if (pibs->should_create_shm) { |
| 79 | + pibs_shmget(pibs); |
| 80 | + if (pibs->shmid >0){ |
| 81 | + printf("Create a new shared memory segment %d\n", pibs->shmid); |
| 82 | + } else { |
| 83 | + printf("Failed to get shared memory segment. Cause = %s\n", |
| 84 | + strerror(pibs->errno_copy)); |
| 85 | + } |
| 86 | + } |
| 87 | + if (pibs->should_attach) { |
| 88 | + if (pibs_shmat(pibs) > 0 ) { |
| 89 | + printf("Attached to shared memory segment %d\n", pibs->shmid); |
| 90 | + } else { |
| 91 | + printf("Failed to attach to shared memory segment. System error:%s\n", |
| 92 | + strerror(pibs->errno_copy)); |
| 93 | + return EXIT_FAILURE; |
| 94 | + } |
| 95 | + } |
| 96 | + if (pibs->uuid[0]) { |
| 97 | + if ((pibs->server[0] == 0) || (pibs->port == 0)) { |
| 98 | + fprintf(stderr,"Redis parameter server and port are incomplete. Use -z and -p options.\n"); |
| 99 | + return EXIT_FAILURE; |
| 100 | + } |
| 101 | + process_redis_list(pibs); |
| 102 | + } |
| 103 | + |
| 104 | + //FIXME Add proper error handling for writecap |
| 105 | + if (pibs->should_writepcap) { |
| 106 | + pibs->outcap = pcap_open_dead(DLT_EN10MB, 65535); |
| 107 | + pibs->dumper = pcap_dump_open(pibs->outcap, pibs->outputfile); |
| 108 | + if (pibs->dumper == NULL) { |
| 109 | + printf("Failed to open outputfile. Reason=%s\n", pcap_geterr(pibs->outcap)); |
| 110 | + return EXIT_FAILURE; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (pibs->show_backscatter) |
| 115 | + printf("#timestamp, source IP, TCP flags, source port\n"); |
| 116 | + if (pibs->filename[0]) { |
| 117 | + process_file(pibs); |
| 118 | + } |
| 119 | + if (pibs->should_dump_table){ |
| 120 | + pibs_dump_raw(pibs); |
| 121 | + pibs_dump_raw(pibs); |
| 122 | + } |
| 123 | + if (pibs->show_stats){ |
| 124 | + pibs_dump_stats(pibs); |
| 125 | + } |
| 126 | + if (pibs->should_writepcap) { |
| 127 | + pcap_dump_close(pibs->dumper); |
| 128 | + printf("[INFO] Created pcap file %s\n", pibs->outputfile); |
| 129 | + } |
| 130 | + return EXIT_FAILURE; |
| 131 | +} |
0 commit comments