2011-07-27 00:27:22 +04:00
|
|
|
/* snifftest.c
|
|
|
|
*
|
2012-02-13 23:54:10 +04:00
|
|
|
* Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
|
2011-07-27 00:27:22 +04:00
|
|
|
*
|
|
|
|
* This file is part of CyaSSL.
|
|
|
|
*
|
|
|
|
* CyaSSL is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* CyaSSL is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2011-08-25 03:37:16 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2011-08-05 02:42:55 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define CYASSL_SNIFFER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CYASSL_SNIFFER
|
|
|
|
|
|
|
|
/* blank build */
|
|
|
|
#include <stdio.h>
|
2011-08-25 02:54:58 +04:00
|
|
|
#include <stdlib.h>
|
2012-09-20 23:11:42 +04:00
|
|
|
int main(void)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
printf("do ./configure --enable-sniffer to enable build support\n");
|
2011-08-25 02:54:58 +04:00
|
|
|
return EXIT_SUCCESS;
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* do a full build */
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
/* builds on *nix too, for scanf device and port */
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <pcap/pcap.h> /* pcap stuff */
|
|
|
|
#include <stdio.h> /* printf */
|
|
|
|
#include <stdlib.h> /* EXIT_SUCCESS */
|
2012-06-29 00:37:19 +04:00
|
|
|
#include <string.h> /* strcmp */
|
2011-02-05 22:14:47 +03:00
|
|
|
#include <signal.h> /* signal */
|
|
|
|
|
2011-08-25 04:39:23 +04:00
|
|
|
#include <cyassl/sniffer.h>
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef unsigned char byte;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ETHER_IF_FRAME_LEN = 14, /* ethernet interface frame length */
|
2012-06-29 21:59:48 +04:00
|
|
|
NULL_IF_FRAME_LEN = 4, /* no link interface frame length */
|
2011-02-05 22:14:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pcap_t* pcap = 0;
|
|
|
|
pcap_if_t *alldevs;
|
|
|
|
|
|
|
|
static void sig_handler(const int sig)
|
|
|
|
{
|
2012-06-29 00:37:19 +04:00
|
|
|
printf("SIGINT handled = %d.\n", sig);
|
2011-02-05 22:14:47 +03:00
|
|
|
if (pcap)
|
|
|
|
pcap_close(pcap);
|
|
|
|
pcap_freealldevs(alldevs);
|
|
|
|
#ifndef _WIN32
|
|
|
|
ssl_FreeSniffer();
|
|
|
|
#endif
|
2012-09-25 22:14:21 +04:00
|
|
|
if (sig)
|
|
|
|
exit(EXIT_SUCCESS);
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-29 00:37:19 +04:00
|
|
|
static void err_sys(const char* msg)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
2012-09-25 22:14:21 +04:00
|
|
|
if (msg)
|
|
|
|
exit(EXIT_FAILURE);
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define SNPRINTF _snprintf
|
|
|
|
#else
|
|
|
|
#define SNPRINTF snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-06-29 00:37:19 +04:00
|
|
|
static char* iptos(unsigned int addr)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
static char output[32];
|
|
|
|
byte *p = (byte*)&addr;
|
|
|
|
|
|
|
|
SNPRINTF(output, sizeof(output), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int inum;
|
|
|
|
int port;
|
2012-06-29 00:37:19 +04:00
|
|
|
int saveFile = 0;
|
2011-02-05 22:14:47 +03:00
|
|
|
int i = 0;
|
2012-06-29 00:37:19 +04:00
|
|
|
int frame = ETHER_IF_FRAME_LEN;
|
2011-02-05 22:14:47 +03:00
|
|
|
char err[PCAP_ERRBUF_SIZE];
|
|
|
|
char filter[32];
|
2012-06-29 00:37:19 +04:00
|
|
|
const char *server = NULL;
|
2011-02-05 22:14:47 +03:00
|
|
|
struct bpf_program fp;
|
|
|
|
pcap_if_t *d;
|
|
|
|
pcap_addr_t *a;
|
|
|
|
|
|
|
|
signal(SIGINT, sig_handler);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-06-29 00:37:19 +04:00
|
|
|
ssl_InitSniffer(); /* dll load on Windows */
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
|
|
|
ssl_Trace("./tracefile.txt", err);
|
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
if (argc == 1) {
|
|
|
|
/* normal case, user chooses device and port */
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
if (pcap_findalldevs(&alldevs, err) == -1)
|
|
|
|
err_sys("Error in pcap_findalldevs");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
for (d = alldevs; d; d=d->next) {
|
|
|
|
printf("%d. %s", ++i, d->name);
|
|
|
|
if (d->description)
|
|
|
|
printf(" (%s)\n", d->description);
|
|
|
|
else
|
|
|
|
printf(" (No description available)\n");
|
|
|
|
}
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
if (i == 0)
|
|
|
|
err_sys("No interfaces found! Make sure pcap or WinPcap is"
|
|
|
|
" installed correctly and you have sufficient permissions");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
printf("Enter the interface number (1-%d): ", i);
|
2012-09-25 22:14:21 +04:00
|
|
|
ret = scanf("%d", &inum);
|
|
|
|
if (ret != 1)
|
|
|
|
printf("scanf port failed\n");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
if (inum < 1 || inum > i)
|
|
|
|
err_sys("Interface number out of range");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
/* Jump to the selected adapter */
|
|
|
|
for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
pcap = pcap_create(d->name, err);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
if (pcap == NULL) printf("pcap_create failed %s\n", err);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
/* get an IPv4 address */
|
|
|
|
for (a = d->addresses; a; a = a->next) {
|
|
|
|
switch(a->addr->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
server =
|
|
|
|
iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr);
|
|
|
|
printf("server = %s\n", server);
|
|
|
|
break;
|
2012-06-29 00:37:19 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-05-20 23:46:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (server == NULL)
|
|
|
|
err_sys("Unable to get device IPv4 address");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
ret = pcap_set_snaplen(pcap, 65536);
|
|
|
|
if (ret != 0) printf("pcap_set_snaplen failed %s\n", pcap_geterr(pcap));
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
ret = pcap_set_timeout(pcap, 1000);
|
|
|
|
if (ret != 0) printf("pcap_set_timeout failed %s\n", pcap_geterr(pcap));
|
|
|
|
|
|
|
|
ret = pcap_set_buffer_size(pcap, 1000000);
|
|
|
|
if (ret != 0)
|
|
|
|
printf("pcap_set_buffer_size failed %s\n", pcap_geterr(pcap));
|
|
|
|
|
|
|
|
ret = pcap_set_promisc(pcap, 1);
|
|
|
|
if (ret != 0) printf("pcap_set_promisc failed %s\n", pcap_geterr(pcap));
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
ret = pcap_activate(pcap);
|
|
|
|
if (ret != 0) printf("pcap_activate failed %s\n", pcap_geterr(pcap));
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
printf("Enter the port to scan: ");
|
2012-09-25 22:14:21 +04:00
|
|
|
ret = scanf("%d", &port);
|
|
|
|
if (ret != 1)
|
|
|
|
printf("scanf port failed\n");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
SNPRINTF(filter, sizeof(filter), "tcp and port %d", port);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
ret = pcap_compile(pcap, &fp, filter, 0, 0);
|
|
|
|
if (ret != 0) printf("pcap_compile failed %s\n", pcap_geterr(pcap));
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
ret = pcap_setfilter(pcap, &fp);
|
|
|
|
if (ret != 0) printf("pcap_setfilter failed %s\n", pcap_geterr(pcap));
|
|
|
|
|
|
|
|
ret = ssl_SetPrivateKey(server, port, "../../certs/server-key.pem",
|
|
|
|
FILETYPE_PEM, NULL, err);
|
|
|
|
}
|
|
|
|
else if (argc >= 3) {
|
2012-06-29 00:37:19 +04:00
|
|
|
saveFile = 1;
|
2012-05-20 23:46:50 +04:00
|
|
|
pcap = pcap_open_offline(argv[1], err);
|
|
|
|
if (pcap == NULL) {
|
|
|
|
printf("pcap_open_offline failed %s\n", err);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else {
|
2012-09-18 20:34:45 +04:00
|
|
|
const char* passwd = NULL;
|
2012-05-20 23:46:50 +04:00
|
|
|
/* defaults for server and port */
|
|
|
|
port = 443;
|
|
|
|
server = "127.0.0.1";
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
if (argc >= 4)
|
|
|
|
server = argv[3];
|
|
|
|
|
|
|
|
if (argc >= 5)
|
|
|
|
port = atoi(argv[4]);
|
|
|
|
|
2012-09-18 20:34:45 +04:00
|
|
|
if (argc >= 6)
|
|
|
|
passwd = argv[5];
|
|
|
|
|
2012-05-20 23:46:50 +04:00
|
|
|
ret = ssl_SetPrivateKey(server, port, argv[2],
|
2012-09-18 20:34:45 +04:00
|
|
|
FILETYPE_PEM, passwd, err);
|
2012-05-20 23:46:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* usage error */
|
2012-09-18 20:34:45 +04:00
|
|
|
printf( "usage: ./snifftest or ./snifftest dump pemKey"
|
|
|
|
" [server] [port] [password]\n");
|
2012-05-20 23:46:50 +04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
err_sys(err);
|
|
|
|
|
2012-06-29 21:59:48 +04:00
|
|
|
if (pcap_datalink(pcap) == DLT_NULL)
|
|
|
|
frame = NULL_IF_FRAME_LEN;
|
2012-06-29 00:37:19 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
while (1) {
|
|
|
|
struct pcap_pkthdr header;
|
|
|
|
const unsigned char* packet = pcap_next(pcap, &header);
|
|
|
|
if (packet) {
|
|
|
|
|
|
|
|
byte data[65535];
|
|
|
|
|
|
|
|
if (header.caplen > 40) { /* min ip(20) + min tcp(20) */
|
|
|
|
packet += frame;
|
|
|
|
header.caplen -= frame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = ssl_DecodePacket(packet, header.caplen, data, err);
|
|
|
|
if (ret < 0)
|
|
|
|
printf("ssl_Decode ret = %d, %s\n", ret, err);
|
|
|
|
if (ret > 0) {
|
|
|
|
data[ret] = 0;
|
|
|
|
printf("SSL App Data:%s\n", data);
|
|
|
|
}
|
|
|
|
}
|
2012-06-29 00:37:19 +04:00
|
|
|
else if (saveFile)
|
|
|
|
break; /* we're done reading file */
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
|
2011-08-25 02:54:58 +04:00
|
|
|
return EXIT_SUCCESS;
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* full build */
|