/
mydump.c
280 lines (235 loc) · 8.57 KB
/
mydump.c
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
#include "mydump.h"
#include <netinet/if_ether.h>
pcap_t *handle = NULL;
int main(int argc, char * argv[]) {
char *ival = NULL;
char *rval = NULL;
char *sval = NULL;
char *bpf = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
int index;
int c;
int rtn;
bpf_u_int32 mask = 0;
bpf_u_int32 net = 0;
struct bpf_program fp;
FILE *pcap_file = NULL;
opterr = 0;
while ((c = getopt (argc, argv, "i:r:s:")) != -1) {
switch (c) {
case 'h':
print_help(stdout);
exit(EXIT_SUCCESS);
break;
case 'i':
ival = optarg;
break;
case 'r':
rval = optarg;
break;
case 's':
sval = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
print_help(stderr);
return 1;
default:
abort ();
}
}
printf ("i = %s, r = %s, s = %s\n", ival, rval, sval);
for (index = optind; index < argc; index++) {
bpf = argv[index];
if(index > optind) {
fprintf(stderr, "Please put BPF in quotes\n");
exit(EXIT_FAILURE);
}
//printf ("Non-option argument %s\n", argv[index]);
}
if(rval != NULL) {
pcap_file = fopen(rval, "r");
if(pcap_file == NULL) {
perror(NULL);
exit(EXIT_FAILURE);
}
handle = pcap_fopen_offline(pcap_file, errbuf);
if (handle == NULL) {
fprintf(stderr, "Could not open pcap file: %s\n", errbuf);
return(2);
}
} else {
if(ival == NULL) {
ival = pcap_lookupdev(errbuf);
if(ival == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
} else {
printf("sniffing on: %s\n", ival);
}
}
if (pcap_lookupnet(ival, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", ival, errbuf);
net = 0;
mask = 0;
}
handle = pcap_open_live(ival, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
}
if(bpf != NULL) {
if (pcap_compile(handle, &fp, bpf, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", bpf, pcap_geterr(handle));
return(2);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", bpf, pcap_geterr(handle));
return(2);
}
}
signal(SIGINT, int_handler);
rtn = pcap_loop(handle, -1, got_packet, (u_char*)sval);
if(rtn == -1) {
fprintf(stderr, "%s\n", pcap_geterr(handle));
return EXIT_FAILURE;
}
pcap_close(handle);
return EXIT_SUCCESS;
}
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
struct ether_header *eth_hdr;
struct iphdr *ip_hdr;
struct tcphdr *tcp_hdr;
struct udphdr *udp_hdr;
eth_hdr = (struct ether_header *) packet;
uint32_t eth_type = ntohs(eth_hdr->ether_type);
if(eth_type != ETHERTYPE_IP) {
return;
}
/* Print IP stuff */
char *ip_src = malloc(sizeof(char) * BUFF_SIZE);
char *ip_dst = malloc(sizeof(char) * BUFF_SIZE);
char *type_name = malloc(sizeof(char) * BUFF_SIZE);
char *tmp_src = NULL;
char *tmp_dst = NULL;
ip_hdr = (struct iphdr*)(eth_hdr + 1);
u_char *data = NULL;
memset(ip_src, 0, BUFF_SIZE);
memset(ip_dst, 0, BUFF_SIZE);
memset(type_name, 0, BUFF_SIZE);
tmp_src = inet_ntoa(*(struct in_addr*)&ip_hdr->saddr);
strncpy(ip_src, tmp_src, 15);
tmp_src = ip_src + strlen(ip_src);
tmp_dst = inet_ntoa(*(struct in_addr*)&ip_hdr->daddr);
strncpy(ip_dst, tmp_dst, 15);
tmp_dst = ip_dst + strlen(ip_dst);
switch(ip_hdr->protocol) {
case IPPROTO_TCP:
tcp_hdr = (struct tcphdr*) (ip_hdr + 1);
sprintf(tmp_src, ":%hu", ntohs(tcp_hdr->source));
sprintf(tmp_dst, ":%hu", ntohs(tcp_hdr->dest));
sprintf(type_name, "%s", "TCP");
data = (u_char *)((unsigned char *)tcp_hdr + (tcp_hdr->doff * 4));
break;
case IPPROTO_UDP:
udp_hdr = (struct udphdr*) (ip_hdr + 1);
sprintf(tmp_src, ":%hu", ntohs(udp_hdr->source));
sprintf(tmp_dst, ":%hu", ntohs(udp_hdr->dest));
sprintf(type_name, "%s", "UDP");
data = (u_char *) (udp_hdr + 1);
break;
case IPPROTO_ICMP:
sprintf(type_name, "%s", "ICMP");
data = (u_char *) (ip_hdr + 1);
break;
default:
sprintf(type_name, "%s", "OTHER");
data = (u_char *) (ip_hdr + 1);
break;
}
if(args != NULL) {
if(NULL == strstr((const char*)data, (const char *)args)) {
return;
}
}
print_time(header->ts);
printf("%02X:%02X:%02X:%02X:%02X:%02X",
(unsigned char) eth_hdr->ether_shost[0],
(unsigned char) eth_hdr->ether_shost[1],
(unsigned char) eth_hdr->ether_shost[2],
(unsigned char) eth_hdr->ether_shost[3],
(unsigned char) eth_hdr->ether_shost[4],
(unsigned char) eth_hdr->ether_shost[5]);
printf(" -> %02X:%02X:%02X:%02X:%02X:%02X type 0x%04X len %d\n",
(unsigned char) eth_hdr->ether_dhost[0],
(unsigned char) eth_hdr->ether_dhost[1],
(unsigned char) eth_hdr->ether_dhost[2],
(unsigned char) eth_hdr->ether_dhost[3],
(unsigned char) eth_hdr->ether_dhost[4],
(unsigned char) eth_hdr->ether_dhost[5],
eth_type,
header->len);
printf("%s -> %s %s\n", ip_src, ip_dst, type_name);
while(1) {
if(data >= (packet + header->caplen)) break;
memset(ip_src, 0, BUFF_SIZE);
memset(ip_dst, 0, BUFF_SIZE);
tmp_src = ip_src;
tmp_dst = ip_dst;
int i = 0;
int j = 0;
for(i = 0, j = 0; i < 48 && data < (packet + header->caplen); i = i + 3, j++, data++) {
sprintf(ip_src + i, " %02X", (*data));
if(isprint(*data)) {
sprintf(ip_dst + j, "%c", *data);
} else {
sprintf(ip_dst + j, ".");
}
}
printf("%s %s\n", ip_src + 1, ip_dst);
}
printf("\n\n");
free(ip_dst);
free(ip_src);
free(type_name);
}
void int_handler(int sig) {
if(handle != NULL)
pcap_breakloop(handle);
}
void print_time (struct timeval tv) {
struct tm* ptm;
char time_string[40];
long milliseconds;
/* Obtain the time of day, and convert it to a tm struct. */
gettimeofday (&tv, NULL);
ptm = localtime (&tv.tv_sec);
/* Format the date and time, down to a single second. */
strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm);
/* Compute milliseconds from microseconds. */
milliseconds = tv.tv_usec / 1000;
/* Print the formatted time, in seconds, followed by a decimal point
* and the milliseconds. */
printf ("%s.%03ld ", time_string, milliseconds);
}
void print_help(FILE *fd) {
fprintf(fd, "mydump [-i interface] [-r file] [-s string] expression\n\n");
fprintf(fd, "-i Listen on network device <interface> (e.g., eth0). If not specified, mydump\n");
fprintf(fd, " should select a default interface to listen on.\n\n");
fprintf(fd, "-r Read packets from <file> (tcpdump format).\n\n");
fprintf(fd, "-s Keep only packets that contain <string> in their payload. You are not\n");
fprintf(fd, " required to implement wildcard or regular expression matching. A simple\n");
fprintf(fd, " string matching operation should suffice.\n\n");
fprintf(fd, "<expression> is a BPF filter that specifies which packets will be dumped. If no\n");
fprintf(fd, "filter is given, all packets seen on the interface (or contained in the trace)\n");
fprintf(fd, "will be dumped. Otherwise, only packets matching <expression> will be dumped.\n\n");
}