-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparser.h
More file actions
174 lines (139 loc) · 4.67 KB
/
argparser.h
File metadata and controls
174 lines (139 loc) · 4.67 KB
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
#ifndef __ARGPARSER_H__
#define __ARGPARSER_H__
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "hexinject.h"
#define VERSION "1.5"
/*
* Cmdline options
*/
struct {
int inject; // inject mode
int sniff; // sniff mode
int raw; // raw mode
char *filter; // custom pcap filter
char *device; // interface
char *pcap_file; // pcap file
int count; // number of packets to capture
int count_on; // enable count
int sleep_time; // sleep time in microseconds
int list_devices; // list all available network devices
int no_cksum; // disable packet checksum
int no_size; // disable packet size
int promisc; // promiscuous mode
int monitor; // enable monitor mode
} options;
/*
* Program usage template
*/
const char usage_template[] =
"HexInject " VERSION " [hexadecimal packet injector/sniffer]\n"
"written by: Emanuele Acri <crossbower@gmail.com>\n\n"
"Usage:\n"
" hexinject <mode> <options>\n"
"\nOptions:\n"
" -s sniff mode\n"
" -p inject mode\n"
" -r raw mode (instead of the default hexadecimal mode)\n"
" -f <filter> custom pcap filter\n"
" -i <device> network device to use\n"
" -F <file> pcap file to use as device (sniff mode only)\n"
" -c <count> number of packets to capture\n"
" -t <time> sleep time in microseconds (default 100)\n"
" -I list all available network devices\n"
"\nInjection options:\n"
" -C disable automatic packet checksum\n"
" -S disable automatic packet size\n"
"\nInterface options:\n"
" -P disable promiscuous mode\n"
" -M put the wireless interface in monitor mode\n"
" (experimental: use airmon-ng instead...)\n"
"\nOther options:\n"
" -h help screen\n";
/*
* Program usage
*/
void usage(FILE *out, const char *error)
{
fputs(usage_template, out);
if(error)
fputs(error, out);
exit(1);
}
/*
* Parser for command line options
* See getopt(3)...
*/
int parseopt(int argc, char **argv)
{
char *c=NULL, *x=NULL;
char ch;
// cleaning
memset(&options, 0, sizeof(options));
// default options
options.sleep_time = 100;
options.promisc = 1;
const char *shortopt = "sprf:i:F:c:t:ICSPMh"; // short options
while ((ch = getopt (argc, argv, shortopt)) != -1) {
switch (ch) {
case 's': // sniff mode
options.sniff = 1;
break;
case 'p': // inject mode
options.inject = 1;
break;
case 'r': // raw mode
options.raw = 1;
break;
case 'f': // custom filter
options.filter = optarg;
break;
case 'i': // interface
options.device = optarg;
break;
case 'F': // pcap file
options.pcap_file = optarg;
break;
case 'c': // packet count
options.count = atoi(optarg);
options.count_on = 1;
break;
case 't': // sleep time in microseconds
options.sleep_time = atoi(optarg);
break;
case 'I': // list devices
options.list_devices = 1;
break;
case 'C': // disable packet checksum
options.no_cksum = 1;
break;
case 'S': // disable packet size
options.no_size = 1;
break;
case 'P': // disable promiscuous mode
options.promisc = 0;
break;
case 'M': // enable monitor mode
options.monitor = 1;
break;
case 'h': //help
usage(stdout, NULL);
case '?':
default:
usage(stderr, NULL);
}
}
// check mode
if ( !options.inject && !options.sniff && !options.list_devices ) {
usage(stderr, "\nError: no mode selected.\n");
}
if ( options.pcap_file && !options.sniff ) {
usage(stderr, "\nError: using a pcap file is compatible only with sniff mode\n");
}
if ( options.inject && options.sniff ) {
usage(stderr, "\nError: too many modes selected, see -s and -p options.\n");
}
return 1;
}
#endif /* __ARGPARSER_H__ */