3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <cyassl/ctaocrypt/settings.h>
29 #define CYASSL_SNIFFER
32 #ifndef CYASSL_SNIFFER
39 printf("do ./configure --enable-sniffer to enable build support\n");
47 /* builds on *nix too, for scanf device and port */
48 #define _CRT_SECURE_NO_WARNINGS
51 #include <pcap/pcap.h> /* pcap stuff */
52 #include <stdio.h> /* printf */
53 #include <stdlib.h> /* EXIT_SUCCESS */
54 #include <string.h> /* strcmp */
55 #include <signal.h> /* signal */
57 #include <cyassl/sniffer.h>
61 #include <arpa/inet.h>
64 typedef unsigned char byte;
67 ETHER_IF_FRAME_LEN = 14, /* ethernet interface frame length */
68 NULL_IF_FRAME_LEN = 4, /* no link interface frame length */
73 pcap_if_t* alldevs = NULL;
76 static void FreeAll(void)
81 pcap_freealldevs(alldevs);
87 static void sig_handler(const int sig)
89 printf("SIGINT handled = %d.\n", sig);
96 static void err_sys(const char* msg)
98 fprintf(stderr, "%s\n", msg);
105 #define SNPRINTF _snprintf
107 #define SNPRINTF snprintf
111 static char* iptos(unsigned int addr)
113 static char output[32];
114 byte *p = (byte*)&addr;
116 SNPRINTF(output, sizeof(output), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
122 int main(int argc, char** argv)
129 int frame = ETHER_IF_FRAME_LEN;
130 char err[PCAP_ERRBUF_SIZE];
132 const char *server = NULL;
133 struct bpf_program fp;
137 signal(SIGINT, sig_handler);
140 ssl_InitSniffer(); /* dll load on Windows */
142 ssl_Trace("./tracefile.txt", err);
145 /* normal case, user chooses device and port */
147 if (pcap_findalldevs(&alldevs, err) == -1)
148 err_sys("Error in pcap_findalldevs");
150 for (d = alldevs; d; d=d->next) {
151 printf("%d. %s", ++i, d->name);
153 printf(" (%s)\n", d->description);
155 printf(" (No description available)\n");
159 err_sys("No interfaces found! Make sure pcap or WinPcap is"
160 " installed correctly and you have sufficient permissions");
162 printf("Enter the interface number (1-%d): ", i);
163 ret = scanf("%d", &inum);
165 printf("scanf port failed\n");
167 if (inum < 1 || inum > i)
168 err_sys("Interface number out of range");
170 /* Jump to the selected adapter */
171 for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
173 pcap = pcap_create(d->name, err);
175 if (pcap == NULL) printf("pcap_create failed %s\n", err);
177 /* get an IPv4 address */
178 for (a = d->addresses; a; a = a->next) {
179 switch(a->addr->sa_family)
183 iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr);
184 printf("server = %s\n", server);
192 err_sys("Unable to get device IPv4 address");
194 ret = pcap_set_snaplen(pcap, 65536);
195 if (ret != 0) printf("pcap_set_snaplen failed %s\n", pcap_geterr(pcap));
197 ret = pcap_set_timeout(pcap, 1000);
198 if (ret != 0) printf("pcap_set_timeout failed %s\n", pcap_geterr(pcap));
200 ret = pcap_set_buffer_size(pcap, 1000000);
202 printf("pcap_set_buffer_size failed %s\n", pcap_geterr(pcap));
204 ret = pcap_set_promisc(pcap, 1);
205 if (ret != 0) printf("pcap_set_promisc failed %s\n", pcap_geterr(pcap));
208 ret = pcap_activate(pcap);
209 if (ret != 0) printf("pcap_activate failed %s\n", pcap_geterr(pcap));
211 printf("Enter the port to scan: ");
212 ret = scanf("%d", &port);
214 printf("scanf port failed\n");
216 SNPRINTF(filter, sizeof(filter), "tcp and port %d", port);
218 ret = pcap_compile(pcap, &fp, filter, 0, 0);
219 if (ret != 0) printf("pcap_compile failed %s\n", pcap_geterr(pcap));
221 ret = pcap_setfilter(pcap, &fp);
222 if (ret != 0) printf("pcap_setfilter failed %s\n", pcap_geterr(pcap));
224 ret = ssl_SetPrivateKey(server, port, "../../certs/server-key.pem",
225 FILETYPE_PEM, NULL, err);
227 printf("Please run directly from sslSniffer/sslSnifferTest dir\n");
230 else if (argc >= 3) {
232 pcap = pcap_open_offline(argv[1], err);
234 printf("pcap_open_offline failed %s\n", err);
238 const char* passwd = NULL;
239 /* defaults for server and port */
241 server = "127.0.0.1";
247 port = atoi(argv[4]);
252 ret = ssl_SetPrivateKey(server, port, argv[2],
253 FILETYPE_PEM, passwd, err);
258 printf( "usage: ./snifftest or ./snifftest dump pemKey"
259 " [server] [port] [password]\n");
266 if (pcap_datalink(pcap) == DLT_NULL)
267 frame = NULL_IF_FRAME_LEN;
270 static int packetNumber = 0;
271 struct pcap_pkthdr header;
272 const unsigned char* packet = pcap_next(pcap, &header);
276 byte data[65535+16384]; /* may have a partial 16k record cached */
278 if (header.caplen > 40) { /* min ip(20) + min tcp(20) */
280 header.caplen -= frame;
285 ret = ssl_DecodePacket(packet, header.caplen, data, err);
287 printf("ssl_Decode ret = %d, %s\n", ret, err);
290 printf("SSL App Data(%d:%d):%s\n", packetNumber, ret, data);
294 break; /* we're done reading file */
301 #endif /* full build */