1 #ifndef __ARGPARSER_H__
2 #define __ARGPARSER_H__
3 
4 #include <stdio.h>
5 #include <string.h>
6 
7 #include <getopt.h>
8 
9 #include "hexinject.h"
10 
11 #define VERSION "1.6"
12 
13 /*
14  * Cmdline options
15  */
16 struct {
17     int inject;                    // inject mode
18     int sniff;                     // sniff mode
19     int raw;                       // raw mode
20     char *filter;                  // custom pcap filter
21     char *device;                  // interface
22     char *pcap_file;                // pcap file
23     int count;                     // number of packets to capture
24     int count_on;                  // enable count
25     int sleep_time;                // sleep time in microseconds
26     int list_devices;              // list all available network devices
27     int no_cksum;                  // disable packet checksum
28     int no_size;                   // disable packet size
29     int promisc;                   // promiscuous mode
30     int monitor;                   // enable monitor mode
31 } options;
32 
33 /*
34  * Program usage template
35  */
36 const char usage_template[] =
37     "HexInject " VERSION " [hexadecimal packet injector/sniffer]\n"
38     "written by: Emanuele Acri <crossbower@tuta.io>\n\n"
39     "Usage:\n"
40     "   hexinject <mode> <options>\n"
41     "\nOptions:\n"
42     "  -s sniff mode\n"
43     "  -p inject mode\n"
44     "  -r raw mode (instead of the default hexadecimal mode)\n"
45     "  -f <filter> custom pcap filter\n"
46     "  -i <device> network device to use\n"
47     "  -F <file> pcap file to use as device (sniff mode only)\n"
48     "  -c <count> number of packets to capture\n"
49     "  -t <time> sleep time in microseconds (default 100)\n"
50     "  -I list all available network devices\n"
51     "\nInjection options:\n"
52     "  -C disable automatic packet checksum\n"
53     "  -S disable automatic packet size fields\n"
54     "\nInterface options:\n"
55     "  -P disable promiscuous mode\n"
56     "  -M put the wireless interface in monitor mode\n"
57     "     (experimental: use airmon-ng instead of this...)\n"
58     "\nOther options:\n"
59     "  -h help screen\n";
60 
61 /*
62  * Program usage
63  */
usage(FILE * out,const char * error)64 void usage(FILE *out, const char *error)
65 {
66     fputs(usage_template, out);
67 
68     if(error)
69         fputs(error, out);
70 
71     exit(1);
72 }
73 
74 /*
75  * Parser for command line options
76  * See getopt(3)...
77  */
parseopt(int argc,char ** argv)78 int parseopt(int argc, char **argv)
79 {
80     char ch;
81 
82     // cleaning
83     memset(&options, 0, sizeof(options));
84 
85     // default options
86     options.sleep_time = 100;
87     options.promisc    = 1;
88 
89     const char *shortopt = "sprf:i:F:c:t:ICSPMh"; // short options
90 
91     while ((ch = getopt (argc, argv, shortopt)) != -1) {
92         switch (ch) {
93 
94             case 's': // sniff mode
95                 options.sniff = 1;
96                 break;
97 
98             case 'p': // inject mode
99                 options.inject = 1;
100                 break;
101 
102             case 'r': // raw mode
103                 options.raw = 1;
104                 break;
105 
106             case 'f': // custom filter
107                 options.filter = optarg;
108                 break;
109 
110             case 'i': // interface
111                 options.device = optarg;
112                 break;
113 
114             case 'F': // pcap file
115                 options.pcap_file = optarg;
116                 break;
117 
118             case 'c': // packet count
119                 options.count = atoi(optarg);
120                 options.count_on = 1;
121                 break;
122 
123             case 't': // sleep time in microseconds
124                 options.sleep_time = atoi(optarg);
125                 break;
126 
127             case 'I': // list devices
128                 options.list_devices = 1;
129                 break;
130 
131             case 'C': // disable packet checksum
132                 options.no_cksum = 1;
133                 break;
134 
135             case 'S': // disable packet size
136                 options.no_size = 1;
137                 break;
138 
139             case 'P': // disable promiscuous mode
140                 options.promisc = 0;
141                 break;
142 
143             case 'M': // enable monitor mode
144                 options.monitor = 1;
145                 break;
146 
147             case '\377':
148                 /* Octal escape sequence character EOF of decimal value 255.
149                    Needed on some systems to avoid the usage() screen. */
150                 goto GETOPT_END;
151 
152             case 'h': //help
153                 usage(stdout, NULL);
154 
155             case '?':
156             default:
157                 usage(stderr, NULL);
158         }
159     }
160 GETOPT_END:
161 
162     // check mode
163     if ( !options.inject && !options.sniff && !options.list_devices ) {
164         usage(stderr, "\nError: no mode selected.\n");
165     }
166 
167     if ( options.pcap_file && !options.sniff ) {
168         usage(stderr, "\nError: using a pcap file is compatible only with sniff mode\n");
169     }
170 
171     if ( options.inject && options.sniff ) {
172         usage(stderr, "\nError: too many modes selected, see -s and -p options.\n");
173     }
174 
175     return 1;
176 }
177 
178 #endif /* __ARGPARSER_H__ */
179 
180