-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph_packet.cc
395 lines (300 loc) · 11.1 KB
/
ph_packet.cc
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (c) 2001 - 2003
* Sebastian Majewski. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Sebastian Majewski
* and his contributors.
* 4. Neither the name of Sebastian Majewski nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <unistd.h>
#include <pcap.h>
#include <netz.h>
#include "support.h"
#include "ph_packet.h"
extern bool debug_mode;
extern bool file_output;
extern bool screen_output;
extern int output_file;
extern bool hide_ether_loopback;
extern bool hide_cisco_cdp;
extern bool hide_eigrp_hello;
extern bool hide_ospf_hello;
void packet_handler(byte* linklayer, struct pcap_pkthdr* pkthdr, byte* packet)
{
bool hide_packet = false;
c_string output_string;
c_packet_info captured_packet_info;
captured_packet_info.next_packet = packet;
captured_packet_info.next_packet_len = pkthdr->caplen;
switch(*((int*)linklayer))
{
case DLT_EN10MB:
captured_packet_info.next_packet_type = get_ether_type(packet);
break;
#ifdef OS_OPENBSD
case DLT_LOOP:
captured_packet_info.next_packet_type = PACKET_TYPE_LOOP;
break;
case DLT_ENC:
captured_packet_info.next_packet_type = PACKET_TYPE_ENC;
break;
#endif
case DLT_NULL:
captured_packet_info.next_packet_type = PACKET_TYPE_GIF;
break;
default:
captured_packet_info.next_packet_type = PACKET_TYPE_UNKNOWN;
}
c_packet_handler packet_handler(captured_packet_info);
u_int i = 0;
c_packet_info packet_info;
output_string.add(print_line());
do
{
packet_info = packet_handler.get_packet_info(i++);
switch(packet_info.packet_type)
{
case PACKET_TYPE_ARP:
output_string += arp_packet_handler(packet_info);
break;
case PACKET_TYPE_ETHER:
{
output_string += ether_packet_handler(packet_info);
c_ether_header header(packet_info.packet);
if(hide_ether_loopback &&
(header.get_type() == ETHER_TYPE_LOOPBACK))
{
hide_packet = true;
}
break;
}
case PACKET_TYPE_ETHER_VLAN:
output_string += ether_vlan_packet_handler(packet_info);
break;
case PACKET_TYPE_IEEE8023:
output_string += ieee8023_packet_handler(packet_info);
break;
case PACKET_TYPE_RAW8023:
output_string += ieee8023_packet_handler(packet_info);
break;
case PACKET_TYPE_ICMP:
output_string += icmp_packet_handler(packet_info);
break;
case PACKET_TYPE_ICMP6:
output_string += icmp6_packet_handler(packet_info);
break;
case PACKET_TYPE_IGMP:
output_string += igmp_packet_handler(packet_info);
break;
case PACKET_TYPE_IP:
output_string += ip_packet_handler(packet_info);
break;
case PACKET_TYPE_IP6:
output_string += ip6_packet_handler(packet_info);
break;
case PACKET_TYPE_IPX:
output_string += ipx_packet_handler(packet_info);
break;
case PACKET_TYPE_LLC_I:
output_string += llc_i_packet_handler(packet_info);
break;
case PACKET_TYPE_LLC_S:
output_string += llc_s_packet_handler(packet_info);
break;
case PACKET_TYPE_LLC_U:
output_string += llc_u_packet_handler(packet_info);
break;
case PACKET_TYPE_GIF:
output_string += gif_packet_handler(packet_info);
break;
case PACKET_TYPE_LOOP:
output_string += loop_packet_handler(packet_info);
break;
case PACKET_TYPE_ENC:
output_string += enc_packet_handler(packet_info);
break;
case PACKET_TYPE_SNAP:
{
output_string += snap_packet_handler(packet_info);
c_snap_header header(packet_info.packet);
if(hide_cisco_cdp && (header.get_oui() == OUI_CISCO) &&
(header.get_type() == SNAP_TYPE_CDP))
{
hide_packet = true;
}
break;
}
case PACKET_TYPE_TCP:
output_string += tcp_packet_handler(packet_info);
break;
case PACKET_TYPE_UDP:
output_string += udp_packet_handler(packet_info);
break;
/* case PACKET_TYPE_SPX:
output_string += spx_packet_handler(packet_info);
break; */
case PACKET_TYPE_RIP:
output_string += rip_packet_handler(packet_info);
break;
case PACKET_TYPE_RIPNG:
output_string += ripng_packet_handler(packet_info);
break;
case PACKET_TYPE_CDP:
output_string += cdp_packet_handler(packet_info);
break;
// case PACKET_TYPE_ESP:
// output_string += esp_packet_handler(packet_info);
// break;
case PACKET_TYPE_AH:
output_string += ah_packet_handler(packet_info);
break;
/* case PACKET_TYPE_ISAKMP:
output_string += isakmp_packet_handler(packet_info);
break;
*/
case PACKET_TYPE_GRE:
output_string += gre_packet_handler(packet_info);
break;
case PACKET_TYPE_ETHLOOP:
output_string += ethloop_packet_handler(packet_info);
break;
case PACKET_TYPE_IGRP:
output_string += igrp_packet_handler(packet_info);
break;
case PACKET_TYPE_EIGRP:
{
output_string += eigrp_packet_handler(packet_info);
c_eigrp_header header(packet_info.packet);
if(hide_eigrp_hello && (header.get_opcode() == EIGRP_OPCODE_HELLO))
{
hide_packet = true;
}
break;
}
case PACKET_TYPE_OSPF:
{
output_string += ospf_packet_handler(packet_info);
c_ospf_header header(packet_info.packet);
if(hide_ospf_hello && (header.get_type() == OSPF_TYPE_HELLO_PACKET))
{
hide_packet = true;
}
break;
}
case PACKET_TYPE_DHCP:
output_string += dhcp_packet_handler(packet_info);
break;
case PACKET_TYPE_UNKNOWN:
output_string += unknown_packet_handler(packet_info);
break;
default:
output_string += missing_packet_handler(packet_info);
}
}
while(packet_info.next_packet_type != PACKET_TYPE_NONE);
if(screen_output && !hide_packet)
{
write(1, output_string.get_data(), output_string.get_len());
}
if(file_output && !hide_packet)
{
write(output_file, output_string.get_data(), output_string.get_len());
}
}
c_string print_hex_data(byte* data, u_int data_len, bool newline, c_string head)
{
c_string output_string;
if(data_len)
{
c_string ascii_string;
if(newline)
{
output_string.add((char*)"\n");
}
for(u_int i = 0; i < data_len; i++)
{
if((i == 0) && newline)
{
output_string.add(head);
}
else if((!(i & 15)) && newline)
{
output_string.add((char*)" \t");
}
output_string.add((char*)"%02X ", *(data + i));
if((*(data + i) < 32) || (*(data + i) > 126))
{
ascii_string.add((char*)".");
}
else
{
ascii_string.add(char(*(data + i)));
}
if((!((i + 1) & 15)) && (i + 1) < data_len)
{
output_string.add((char*)" |");
output_string.add(ascii_string);
output_string.add((char*)"|");
ascii_string.clear();
output_string.add((char*)"\n");
}
}
for (int i = 0; i < int((16 - data_len) & 15); i++)
{
output_string.add((char*)" ");
}
output_string.add((char*)" |");
output_string.add(ascii_string);
ascii_string.clear();
for (int i = 0; i < int((16 - data_len) & 15); i++)
{
output_string.add((char*)" ");
}
output_string.add((char*)"|");
if(newline)
{
output_string.add((char*)"\n");
}
}
return output_string;
}
c_string debug(c_packet_info packet_info)
{
c_string output_string;
if(debug_mode)
{
output_string.add((char*)"\n");
output_string.add((char*)"<debug>\tpacket_info.packet_len = %u\n",
packet_info.packet_len);
output_string.add((char*)"\tpacket_info.next_packet_len = %u\n",
packet_info.next_packet_len);
output_string += print_hex_data(packet_info.packet,
packet_info.packet_len, true, (char*)"\t");
}
return output_string;
}