2 *****************************************************************************
6 * Author: Damien S. Stuart
8 * Purpose: Provides the functions to check for possible replay attacks
9 * by using a cache of previously seen digests. This cache is a
10 * simple file by default, but can be made to use a dbm solution
11 * (ndbm or gdbm in ndbm compatibility mode) file to store the digest
12 * of a previously received SPA packets.
14 * Copyright 2010 Damien Stuart (dstuart@dstuart.org)
16 * License (GNU Public License):
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 *****************************************************************************
35 #include "replay_cache.h"
37 #include "fwknopd_errors.h"
45 #define MY_DBM_FETCH(d, k) gdbm_fetch(d, k)
46 #define MY_DBM_STORE(d, k, v, m) gdbm_store(d, k, v, m)
47 #define MY_DBM_STRERROR(x) gdbm_strerror(x)
48 #define MY_DBM_CLOSE(d) gdbm_close(d)
50 #define MY_DBM_REPLACE GDBM_REPLACE
51 #define MY_DBM_INSERT GDBM_INSERT
56 #define MY_DBM_FETCH(d, k) dbm_fetch(d, k)
57 #define MY_DBM_STORE(d, k, v, m) dbm_store(d, k, v, m)
58 #define MY_DBM_STRERROR(x) strerror(x)
59 #define MY_DBM_CLOSE(d) dbm_close(d)
61 #define MY_DBM_REPLACE DBM_REPLACE
62 #define MY_DBM_INSERT DBM_INSERT
66 #error "File cache method disabled, and No GDBM or NDBM header file found. WTF?"
71 #include <sys/socket.h>
73 #include <arpa/inet.h>
78 #define MAX_DIGEST_SIZE 64
81 get_raw_digest(char **digest, char *pkt_data)
84 char *tmp_digest = NULL;
85 int res = FKO_SUCCESS;
87 /* initialize an FKO context with no decryption key just so
88 * we can get the outer message digest
90 res = fko_new_with_data(&ctx, (char *)pkt_data, NULL);
91 if(res != FKO_SUCCESS)
93 log_msg(LOG_WARNING, "Error initializing FKO context from SPA data: %s",
96 return(SPA_MSG_FKO_CTX_ERROR);
99 res = fko_set_raw_spa_digest_type(ctx, FKO_DEFAULT_DIGEST);
100 if(res != FKO_SUCCESS)
102 log_msg(LOG_WARNING, "Error setting digest type for SPA data: %s",
105 return(SPA_MSG_DIGEST_ERROR);
108 res = fko_set_raw_spa_digest(ctx);
109 if(res != FKO_SUCCESS)
111 log_msg(LOG_WARNING, "Error setting digest for SPA data: %s",
114 return(SPA_MSG_DIGEST_ERROR);
117 res = fko_get_raw_spa_digest(ctx, &tmp_digest);
118 if(res != FKO_SUCCESS)
120 log_msg(LOG_WARNING, "Error getting digest from SPA data: %s",
123 return(SPA_MSG_DIGEST_ERROR);
126 *digest = strdup(tmp_digest);
129 return SPA_MSG_ERROR;
135 /* Rotate the digest file by simply renaming it.
138 rotate_digest_cache_file(fko_srv_options_t *opts)
140 #ifdef NO_DIGEST_CACHE
141 log_msg(LOG_WARNING, "Digest cache not supported. Nothing to rotate.");
144 char *new_file = NULL;
146 log_msg(LOG_INFO, "Rotating digest cache file.");
149 new_file = malloc(strlen(opts->config[CONF_DIGEST_FILE])+5);
151 new_file = malloc(strlen(opts->config[CONF_DIGEST_DB_FILE])+5);
156 log_msg(LOG_ERR, "rotate_digest_cache_file: Memory allocation error.");
157 clean_exit(opts, NO_FW_CLEANUP, EXIT_FAILURE);
160 /* The new filename is just the original with a trailing '-old'.
163 strlcpy(new_file, opts->config[CONF_DIGEST_FILE],
164 strlen(opts->config[CONF_DIGEST_FILE])+5);
166 strlcpy(new_file, opts->config[CONF_DIGEST_DB_FILE],
167 strlen(opts->config[CONF_DIGEST_DB_FILE])+5);
169 strcat(new_file, "-old");
172 res = rename(opts->config[CONF_DIGEST_FILE], new_file);
174 res = rename(opts->config[CONF_DIGEST_DB_FILE], new_file);
178 log_msg(LOG_ERR, "Unable to rename digest file: %s to %s: %s",
180 opts->config[CONF_DIGEST_FILE], new_file, strerror(errno)
182 opts->config[CONF_DIGEST_DB_FILE], new_file, strerror(errno)
185 #endif /* NO_DIGEST_CACHE */
189 replay_warning(fko_srv_options_t *opts, digest_cache_info_t *digest_info)
191 char src_ip[INET_ADDRSTRLEN+1] = {0};
192 char orig_src_ip[INET_ADDRSTRLEN+1] = {0};
193 char created[DATE_LEN];
196 char last_ip[INET_ADDRSTRLEN+1] = {0};
197 char first[DATE_LEN], last[DATE_LEN];
200 /* Convert the IPs to a human readable form
202 inet_ntop(AF_INET, &(opts->spa_pkt.packet_src_ip),
203 src_ip, INET_ADDRSTRLEN);
204 inet_ntop(AF_INET, &(digest_info->src_ip), orig_src_ip, INET_ADDRSTRLEN);
207 /* Mark the last_replay time.
209 digest_info->last_replay = time(NULL);
211 /* Increment the replay count and check to see if it is the first one.
213 if(++(digest_info->replay_count) == 1)
215 /* This is the first replay so make it the same as last_replay
217 digest_info->first_replay = digest_info->last_replay;
220 strftime(first, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->first_replay)));
221 strftime(last, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->last_replay)));
224 strftime(created, DATE_LEN, "%D %H:%M:%S", localtime(&(digest_info->created)));
227 "Replay detected from source IP: %s\n"
228 " Destination proto/port: %d/%d\n"
229 " Original source IP: %s\n"
230 " Original dst proto/port: %d/%d\n"
232 " Entry created: %s\n",
234 " Entry created: %s\n"
235 " First replay: %s\n"
237 " Replay count: %i\n",
240 opts->spa_pkt.packet_proto,
241 opts->spa_pkt.packet_dst_port,
244 digest_info->dst_port,
251 digest_info->replay_count
259 replay_cache_init(fko_srv_options_t *opts)
261 #ifdef NO_DIGEST_CACHE
265 /* If rotation was specified, do it.
267 if(opts->rotate_digest_cache)
268 rotate_digest_cache_file(opts);
271 return replay_file_cache_init(opts);
273 return replay_db_cache_init(opts);
276 #endif /* NO_DIGEST_CACHE */
281 replay_file_cache_init(fko_srv_options_t *opts)
283 FILE *digest_file_ptr = NULL;
284 unsigned int num_lines = 0, digest_ctr = 0;
285 char line_buf[MAX_LINE_LEN] = {0};
286 char src_ip[INET_ADDRSTRLEN+1] = {0};
287 char dst_ip[INET_ADDRSTRLEN+1] = {0};
290 struct digest_cache_list *digest_elm = NULL;
292 /* if the file exists, import the previous SPA digests into
295 if (access(opts->config[CONF_DIGEST_FILE], F_OK) == 0)
299 if (access(opts->config[CONF_DIGEST_FILE], R_OK|W_OK) != 0)
301 log_msg(LOG_WARNING, "Digest file '%s' exists but: '%s'",
302 opts->config[CONF_DIGEST_FILE], strerror(errno));
308 /* the file does not exist yet, so it will be created when the first
309 * successful SPA packet digest is written to disk
311 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "w")) == NULL)
313 log_msg(LOG_WARNING, "Could not open digest cache: %s",
314 opts->config[CONF_DIGEST_FILE]);
316 fprintf(digest_file_ptr,
317 "# <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>\n");
318 fclose(digest_file_ptr);
322 /* File exist, and we have access - create in-memory digest cache
324 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "r")) == NULL)
326 log_msg(LOG_WARNING, "Could not open digest cache: %s",
327 opts->config[CONF_DIGEST_FILE]);
332 * <digest> <proto> <src_ip> <src_port> <dst_ip> <dst_port> <time>
334 * 7XgadOyqv0tF5xG8uhg2iIrheeNKglCWKmxQDgYP1dY 17 127.0.0.1 40305 127.0.0.1 62201 1313283481
336 while ((fgets(line_buf, MAX_LINE_LEN, digest_file_ptr)) != NULL)
339 line_buf[MAX_LINE_LEN-1] = '\0';
341 if(IS_EMPTY_LINE(line_buf[0]))
344 /* Initialize a digest cache list element, and add it into the list if
347 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
349 fprintf(stderr, "Could not allocate digest list element\n");
352 if ((digest_elm->cache_info.digest = calloc(1, MAX_DIGEST_SIZE+1)) == NULL)
355 fprintf(stderr, "Could not allocate digest string\n");
361 if(sscanf(line_buf, "%s %hhu %s %hu %s %hu %ld",
362 digest_elm->cache_info.digest,
363 &(digest_elm->cache_info.proto),
365 &(digest_elm->cache_info.src_port),
367 &(digest_elm->cache_info.dst_port),
372 "*Skipping invalid digest file entry in %s at line %i.\n - %s",
373 opts->config[CONF_DIGEST_FILE], num_lines, line_buf
375 free(digest_elm->cache_info.digest);
379 digest_elm->cache_info.created = time_tmp;
382 if (inet_pton(AF_INET, src_ip, &(digest_elm->cache_info.src_ip)) != 1)
384 free(digest_elm->cache_info.digest);
389 if (inet_pton(AF_INET, dst_ip, &(digest_elm->cache_info.dst_ip)) != 1)
391 free(digest_elm->cache_info.digest);
396 digest_elm->next = opts->digest_cache;
397 opts->digest_cache = digest_elm;
400 if(opts->verbose > 3)
402 "DIGEST FILE: %s, VALID LINE: %s",
403 opts->config[CONF_DIGEST_FILE], line_buf
408 fclose(digest_file_ptr);
413 #else /* USE_FILE_CACHE */
415 /* Check for the existence of the replay dbm file, and create it if it does
416 * not exist. Returns the number of db entries or -1 on error.
419 replay_db_cache_init(fko_srv_options_t *opts)
421 #ifdef NO_DIGEST_CACHE
431 datum db_key, db_ent, db_next_key;
436 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
440 opts->config[CONF_DIGEST_DB_FILE], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR
447 "Unable to open digest cache file: '%s': %s",
448 opts->config[CONF_DIGEST_DB_FILE],
449 MY_DBM_STRERROR(errno)
456 db_key = gdbm_firstkey(rpdb);
458 while (db_key.dptr != NULL)
461 db_next_key = gdbm_nextkey(rpdb, db_key);
463 db_key = db_next_key;
466 for (db_key = dbm_firstkey(rpdb); db_ent.dptr != NULL; db_key = dbm_nextkey(rpdb))
473 #endif /* NO_DIGEST_CACHE */
475 #endif /* USE_FILE_CACHE */
477 /* Take an fko context, pull the digest and use it as the key to check the
478 * replay db (digest cache). Returns 1 if there was a match (a replay),
479 * 0 for no match, and -1 on error.
482 is_replay(fko_srv_options_t *opts, unsigned char *pkt_data)
484 #ifdef NO_DIGEST_CACHE
489 return is_replay_file_cache(opts, pkt_data);
491 return is_replay_dbm_cache(opts, pkt_data);
493 #endif /* NO_DIGEST_CACHE */
498 is_replay_file_cache(fko_srv_options_t *opts, unsigned char *pkt_data)
501 char src_ip[INET_ADDRSTRLEN+1] = {0};
502 char dst_ip[INET_ADDRSTRLEN+1] = {0};
503 int res = 0, digest_len = 0;
504 FILE *digest_file_ptr = NULL;
506 struct digest_cache_list *digest_list_ptr = NULL, *digest_elm = NULL;
508 res = get_raw_digest(&digest, (char *)pkt_data);
510 if(res != FKO_SUCCESS)
518 return SPA_MSG_ERROR;
520 digest_len = strlen(digest);
522 /* Check the cache for the SPA packet digest
524 for (digest_list_ptr = opts->digest_cache;
525 digest_list_ptr != NULL;
526 digest_list_ptr = digest_list_ptr->next) {
528 if (strncmp(digest_list_ptr->cache_info.digest, digest, digest_len) == 0) {
530 replay_warning(opts, &(digest_list_ptr->cache_info));
533 return(SPA_MSG_REPLAY);
537 /* If we make it here, then this is a new SPA packet that needs to be
538 * added to the cache. We've already decrypted the data, so we know that
539 * the contents are valid.
541 if ((digest_elm = calloc(1, sizeof(struct digest_cache_list))) == NULL)
543 log_msg(LOG_WARNING, "Error calloc() returned NULL for digest cache element",
544 fko_errstr(SPA_MSG_ERROR));
547 return(SPA_MSG_ERROR);
549 if ((digest_elm->cache_info.digest = calloc(1, digest_len+1)) == NULL)
551 log_msg(LOG_WARNING, "Error calloc() returned NULL for digest cache string",
552 fko_errstr(SPA_MSG_ERROR));
555 return(SPA_MSG_ERROR);
558 strlcpy(digest_elm->cache_info.digest, digest, digest_len+1);
559 digest_elm->cache_info.proto = opts->spa_pkt.packet_proto;
560 digest_elm->cache_info.src_ip = opts->spa_pkt.packet_src_ip;
561 digest_elm->cache_info.dst_ip = opts->spa_pkt.packet_dst_ip;
562 digest_elm->cache_info.src_port = opts->spa_pkt.packet_src_port;
563 digest_elm->cache_info.dst_port = opts->spa_pkt.packet_dst_port;
564 digest_elm->cache_info.created = time(NULL);
566 /* First, add the digest at the head of the in-memory list
568 digest_elm->next = opts->digest_cache;
569 opts->digest_cache = digest_elm;
571 /* Now, write the digest to disk
573 if ((digest_file_ptr = fopen(opts->config[CONF_DIGEST_FILE], "a")) == NULL)
575 log_msg(LOG_WARNING, "Could not open digest cache: %s",
576 opts->config[CONF_DIGEST_FILE]);
578 return(SPA_MSG_DIGEST_CACHE_ERROR);
581 inet_ntop(AF_INET, &(digest_elm->cache_info.src_ip),
582 src_ip, INET_ADDRSTRLEN);
583 inet_ntop(AF_INET, &(digest_elm->cache_info.dst_ip),
584 dst_ip, INET_ADDRSTRLEN);
585 fprintf(digest_file_ptr, "%s %d %s %d %s %d %d\n",
587 digest_elm->cache_info.proto,
589 (int) digest_elm->cache_info.src_port,
591 digest_elm->cache_info.dst_port,
592 (int) digest_elm->cache_info.created);
594 fclose(digest_file_ptr);
597 return(SPA_MSG_SUCCESS);
599 #endif /* USE_FILE_CACHE */
603 is_replay_dbm_cache(fko_srv_options_t *opts, unsigned char *pkt_data)
605 #ifdef NO_DIGEST_CACHE
614 datum db_key, db_ent;
619 digest_cache_info_t dc_info;
621 res = get_raw_digest(&digest, (char *)pkt_data);
623 if(res != FKO_SUCCESS)
631 return SPA_MSG_ERROR;
633 digest_len = strlen(digest);
635 db_key.dptr = digest;
636 db_key.dsize = digest_len;
638 /* Check the db for the key
642 opts->config[CONF_DIGEST_DB_FILE], 512, GDBM_WRCREAT, S_IRUSR|S_IWUSR, 0
645 rpdb = dbm_open(opts->config[CONF_DIGEST_DB_FILE], O_RDWR, 0);
650 log_msg(LOG_WARNING, "Error opening digest_cache: '%s': %s",
651 opts->config[CONF_DIGEST_DB_FILE],
652 MY_DBM_STRERROR(errno)
656 return(SPA_MSG_DIGEST_CACHE_ERROR);
659 db_ent = MY_DBM_FETCH(rpdb, db_key);
661 /* If the datum is not null, we have a match. Otherwise, we add
662 * this entry to the cache.
664 if(db_ent.dptr != NULL)
666 replay_warning(opts, (digest_cache_info_t *)db_ent.dptr);
668 /* Save it back to the digest cache
670 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_REPLACE) != 0)
671 log_msg(LOG_WARNING, "Error updating entry in digest_cache: '%s': %s",
672 opts->config[CONF_DIGEST_DB_FILE],
673 MY_DBM_STRERROR(errno)
680 res = SPA_MSG_REPLAY;
682 /* This is a new SPA packet that needs to be added to the cache.
684 dc_info.src_ip = opts->spa_pkt.packet_src_ip;
685 dc_info.dst_ip = opts->spa_pkt.packet_dst_ip;
686 dc_info.src_port = opts->spa_pkt.packet_src_port;
687 dc_info.dst_port = opts->spa_pkt.packet_dst_port;
688 dc_info.proto = opts->spa_pkt.packet_proto;
689 dc_info.created = time(NULL);
690 dc_info.first_replay = dc_info.last_replay = dc_info.replay_count = 0;
692 db_ent.dsize = sizeof(digest_cache_info_t);
693 db_ent.dptr = (char*)&(dc_info);
695 if(MY_DBM_STORE(rpdb, db_key, db_ent, MY_DBM_INSERT) != 0)
697 log_msg(LOG_WARNING, "Error adding entry digest_cache: %s",
698 MY_DBM_STRERROR(errno)
701 res = SPA_MSG_DIGEST_CACHE_ERROR;
704 res = SPA_MSG_SUCCESS;
711 #endif /* NO_DIGEST_CACHE */
713 #endif /* USE_FILE_CACHE */
716 /* Free replay list memory
719 free_replay_list(fko_srv_options_t *opts)
721 #ifdef NO_DIGEST_CACHE
724 struct digest_cache_list *digest_list_ptr = NULL, *digest_tmp = NULL;
726 if (opts->digest_cache == NULL)
729 digest_list_ptr = opts->digest_cache;
730 while (digest_list_ptr != NULL)
732 digest_tmp = digest_list_ptr->next;
733 if (digest_list_ptr->cache_info.digest != NULL
734 && digest_list_ptr->cache_info.digest[0] != '\0')
736 free(digest_list_ptr->cache_info.digest);
738 free(digest_list_ptr);
739 digest_list_ptr = digest_tmp;