-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexinject.c
More file actions
279 lines (223 loc) · 6.04 KB
/
hexinject.c
File metadata and controls
279 lines (223 loc) · 6.04 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
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
/*
* hexinject.c
*
* Created on: 11/mag/2010
* Author: Acri Emanuele
*/
#include "hexinject.h"
/**
* Injection loop using hexstring format
*/
int inject_hexstr_loop(pcap_t *fp)
{
char buffer[BUFFER_SIZE];
while (!feof(stdin)) {
/* Read hexstring */
if( !fgets(buffer, BUFFER_SIZE, stdin) ) {
continue;
}
/* Send down the packet */
if (inject_hexstr(fp, buffer, options.no_cksum, options.no_size) != 0) {
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));
return 1;
}
}
return 0;
}
/*
* Injection loop using raw format
*/
int inject_raw_loop(pcap_t *fp)
{
char buffer[BUFFER_SIZE];
char size_buf[sizeof(size_t)];
size_t size;
while (!feof(stdin)) {
/* Read raw */
fread(size_buf, 1, sizeof(size), stdin);
size=*((size_t*)size_buf);
size = fread(buffer,1,size, stdin);
/* Send down the packet */
if (inject_raw(fp, buffer, size, options.no_cksum, options.no_size) != 0) {
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));
return 1;
}
}
return 0;
}
/*
* Sniffing loop with hexstring output
*/
int sniff_hexstr_loop(pcap_t *fp, int from_file)
{
char *hexstr;
while ( 1 ) {
/* Count */
if (options.count_on) {
if (options.count <= 0) {
break;
}
}
/* Sniff and print a packet */
if ((hexstr = sniff_hexstr(fp))) {
printf("%s\n", hexstr);
fflush(stdout);
free(hexstr);
/* Count */
if (options.count_on) {
options.count--;
}
} else if (from_file) {
/* File terminated */
return 0;
}
usleep(options.sleep_time);
}
return 0;
}
/*
* Sniffing loop with raw output
*/
int sniff_raw_loop(pcap_t *fp, int from_file)
{
const uint8_t *packet;
size_t size;
while ( 1 ) {
/* Count */
if (options.count_on) {
if (options.count <= 0) {
break;
}
}
size = BUFFER_SIZE;
/* Sniff and print a packet */
if ((packet = sniff_raw(fp, &size))) {
fwrite((char*)&size, 1, sizeof(size), stdout);
fwrite(packet, 1, size, stdout);
fflush(stdout);
/* Count */
if (options.count_on) {
options.count--;
}
} else if (from_file) {
/* File terminated */
return 0;
}
usleep(options.sleep_time);
}
return 0;
}
/*
* Main function
*/
int main(int argc, char **argv) {
pcap_t *fp;
struct bpf_program bpf;
char errbuf[PCAP_ERRBUF_SIZE];
int ret_val;
pcap_if_t *alldevsp;
char *dev=NULL;
/* Parse cmdline options */
parseopt(argc, argv);
/* in case of device listing */
if(options.list_devices) {
if(pcap_findalldevs(&alldevsp, errbuf) != 0) {
fprintf(stderr,"Unable to list devices: %s.\n", errbuf);
return 1;
}
for (; alldevsp; alldevsp=alldevsp->next) {
printf("%s", alldevsp->name);
if (alldevsp->description != NULL)
printf(" (%s)", alldevsp->description);
printf("\n");
}
return 0;
}
/* Network interface stuff */
if(!options.pcap_file) {
/* find a device if not specified */
if(!options.device) {
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr,"Unable to find a network adapter: %s.\n", errbuf);
return 1;
}
}
else {
dev = options.device;
}
/* Create packet capture handle */
if((fp = pcap_create(dev, errbuf)) == NULL) {
fprintf(stderr,"Unable to create pcap handle: %s\n", errbuf);
return 1;
}
/* Set snapshot length */
if(pcap_set_snaplen(fp, BUFSIZ) != 0) {
fprintf(stderr,"Unable to set snapshot length: the interface may be already activated\n");
return 1;
}
/* Set promiscuous mode */
if(pcap_set_promisc(fp, options.promisc) != 0) {
fprintf(stderr,"Unable to set promiscuous mode: the interface may be already activated\n");
return 1;
}
/* Set read timeout */
if(pcap_set_timeout(fp, 10) != 0) { // a little patch i've seen in freebsd ports: thank you guys ;)
fprintf(stderr,"Unable to set read timeout: the interface may be already activated\n");
return 1;
}
/* Set monitor mode */
if(options.monitor) {
if(pcap_can_set_rfmon(fp)==0) {
fprintf(stderr, "Monitor mode not supported by %s.\n", dev);
return 1;
}
if((ret_val=pcap_set_rfmon(fp, 1)) != 0) {
fprintf(stderr, "Unable to set monitor mode: the interface may be already activated.\n");
return 1;
}
}
/* Activate interface */
if(pcap_activate(fp) != 0) {
fprintf(stderr, "Unable to activate the interface: %s\n", pcap_geterr(fp));
return 1;
}
}
/* PCAP File Stuff */
else {
/* Create packet capture handle */
if((fp = pcap_open_offline(options.pcap_file, errbuf)) == NULL) {
fprintf(stderr,"Unable to open pcap file: %s\n", errbuf);
return 1;
}
}
/* Apply filter */
if ( options.filter ) {
if(pcap_compile(fp, &bpf, options.filter, 0, 0) != 0) {
fprintf(stderr, "Error compiling filter: %s\n", pcap_geterr(fp));
return 1;
}
if(pcap_setfilter(fp, &bpf) != 0) {
fprintf(stderr, "Error setting filter: %s\n", pcap_geterr(fp));
return 1;
}
}
/* Inject mode - Hexstring */
if (options.inject && !options.raw) {
ret_val = inject_hexstr_loop(fp);
}
/* Inject mode - Raw */
else if (options.inject && options.raw) {
ret_val = inject_raw_loop(fp);
}
/* Sniff mode - Hexstring */
else if (options.sniff && !options.raw) {
ret_val = sniff_hexstr_loop(fp, options.pcap_file ? 1 : 0);
}
/* Sniff mode - Raw */
else if (options.sniff && options.raw) {
ret_val = sniff_raw_loop(fp, options.pcap_file ? 1 : 0);
}
pcap_close(fp);
return ret_val;
}