blob: b102401e77d37bf55154f8eb738838aff7ca2d15 [file] [log] [blame]
markus@openbsd.org091c3022015-01-19 19:52:16 +00001/* $OpenBSD: deattack.c,v 1.31 2015/01/19 19:52:16 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003 * Cryptographic attack detector for ssh - source code
4 *
5 * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.
6 *
7 * All rights reserved. Redistribution and use in source and binary
8 * forms, with or without modification, are permitted provided that
9 * this copyright notice is retained.
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE
13 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR
14 * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS
15 * SOFTWARE.
16 *
17 * Ariel Futoransky <futo@core-sdi.com>
Damien Miller95def091999-11-25 00:26:21 +110018 * <http://www.core-sdi.com>
19 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21#include "includes.h"
Ben Lindstrom05764b92002-03-05 01:53:02 +000022
markus@openbsd.org091c3022015-01-19 19:52:16 +000023#include <sys/param.h>
Damien Millerd7834352006-08-05 12:39:39 +100024#include <string.h>
25#include <stdio.h>
markus@openbsd.org091c3022015-01-19 19:52:16 +000026#include <stdlib.h>
Damien Millerd7834352006-08-05 12:39:39 +100027
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028#include "deattack.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#include "crc32.h"
markus@openbsd.org091c3022015-01-19 19:52:16 +000030#include "sshbuf.h"
Damien Miller3f941882006-03-31 23:13:02 +110031#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
Damien Miller3c9c1fb2006-09-17 06:08:53 +100033/*
34 * CRC attack detection has a worst-case behaviour that is O(N^3) over
35 * the number of identical blocks in a packet. This behaviour can be
36 * exploited to create a limited denial of service attack.
37 *
38 * However, because we are dealing with encrypted data, identical
39 * blocks should only occur every 2^35 maximally-sized packets or so.
40 * Consequently, we can detect this DoS by looking for identical blocks
41 * in a packet.
42 *
43 * The parameter below determines how many identical blocks we will
44 * accept in a single packet, trading off between attack detection and
45 * likelihood of terminating a legitimate connection. A value of 32
46 * corresponds to an average of 2^40 messages before an attack is
47 * misdetected
48 */
49#define MAX_IDENTICAL 32
50
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051/* SSH Constants */
Damien Miller95def091999-11-25 00:26:21 +110052#define SSH_MAXBLOCKS (32 * 1024)
53#define SSH_BLOCKSIZE (8)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
55/* Hashing constants */
Damien Miller95def091999-11-25 00:26:21 +110056#define HASH_MINSIZE (8 * 1024)
57#define HASH_ENTRYSIZE (2)
58#define HASH_FACTOR(x) ((x)*3/2)
59#define HASH_UNUSEDCHAR (0xff)
60#define HASH_UNUSED (0xffff)
Damien Miller9f0f5c62001-12-21 14:45:46 +110061#define HASH_IV (0xfffe)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller95def091999-11-25 00:26:21 +110063#define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65
66/* Hash function (Input keys are cipher results) */
markus@openbsd.org091c3022015-01-19 19:52:16 +000067#define HASH(x) PEEK_U32(x)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Ben Lindstrom204e4882001-03-05 06:47:00 +000069#define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Ben Lindstrombba81212001-06-25 05:01:22 +000071static void
Damien Miller95def091999-11-25 00:26:21 +110072crc_update(u_int32_t *a, u_int32_t b)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073{
Damien Miller95def091999-11-25 00:26:21 +110074 b ^= *a;
Damien Miller2dbbf8e2006-03-26 00:11:46 +110075 *a = ssh_crc32((u_char *)&b, sizeof(b));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076}
77
Damien Miller95def091999-11-25 00:26:21 +110078/* detect if a block is used in a particular pattern */
Ben Lindstrombba81212001-06-25 05:01:22 +000079static int
markus@openbsd.org091c3022015-01-19 19:52:16 +000080check_crc(const u_char *S, const u_char *buf, u_int32_t len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081{
Damien Miller95def091999-11-25 00:26:21 +110082 u_int32_t crc;
markus@openbsd.org091c3022015-01-19 19:52:16 +000083 const u_char *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller95def091999-11-25 00:26:21 +110085 crc = 0;
Damien Miller95def091999-11-25 00:26:21 +110086 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
87 if (!CMP(S, c)) {
88 crc_update(&crc, 1);
89 crc_update(&crc, 0);
90 } else {
91 crc_update(&crc, 0);
92 crc_update(&crc, 0);
93 }
94 }
markus@openbsd.org091c3022015-01-19 19:52:16 +000095 return crc == 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096}
97
markus@openbsd.org091c3022015-01-19 19:52:16 +000098void
99deattack_init(struct deattack_ctx *dctx)
100{
101 bzero(dctx, sizeof(*dctx));
102 dctx->n = HASH_MINSIZE / HASH_ENTRYSIZE;
103}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller95def091999-11-25 00:26:21 +1100105/* Detect a crc32 compensation attack on a packet */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106int
markus@openbsd.org091c3022015-01-19 19:52:16 +0000107detect_attack(struct deattack_ctx *dctx, const u_char *buf, u_int32_t len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108{
markus@openbsd.org091c3022015-01-19 19:52:16 +0000109 u_int32_t i, j, l, same;
110 u_int16_t *tmp;
111 const u_char *c, *d;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
Damien Miller95def091999-11-25 00:26:21 +1100113 if (len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
markus@openbsd.org091c3022015-01-19 19:52:16 +0000114 len % SSH_BLOCKSIZE != 0)
115 return DEATTACK_ERROR;
116 for (l = dctx->n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
Damien Miller95def091999-11-25 00:26:21 +1100117 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
markus@openbsd.org091c3022015-01-19 19:52:16 +0000119 if (dctx->h == NULL) {
120 if ((dctx->h = calloc(l, HASH_ENTRYSIZE)) == NULL)
121 return DEATTACK_ERROR;
122 dctx->n = l;
Damien Miller95def091999-11-25 00:26:21 +1100123 } else {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000124 if (l > dctx->n) {
125 if ((tmp = reallocarray(dctx->h, l, HASH_ENTRYSIZE))
126 == NULL) {
127 free(dctx->h);
128 dctx->h = NULL;
129 return DEATTACK_ERROR;
130 }
131 dctx->h = tmp;
132 dctx->n = l;
Damien Miller95def091999-11-25 00:26:21 +1100133 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller95def091999-11-25 00:26:21 +1100136 if (len <= HASH_MINBLOCKS) {
137 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
Damien Miller95def091999-11-25 00:26:21 +1100138 for (d = buf; d < c; d += SSH_BLOCKSIZE) {
139 if (!CMP(c, d)) {
Damien Miller2dbbf8e2006-03-26 00:11:46 +1100140 if ((check_crc(c, buf, len)))
markus@openbsd.org091c3022015-01-19 19:52:16 +0000141 return DEATTACK_DETECTED;
Damien Miller95def091999-11-25 00:26:21 +1100142 else
143 break;
144 }
145 }
146 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000147 return DEATTACK_OK;
Damien Miller95def091999-11-25 00:26:21 +1100148 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000149 memset(dctx->h, HASH_UNUSEDCHAR, dctx->n * HASH_ENTRYSIZE);
Damien Miller95def091999-11-25 00:26:21 +1100150
Damien Miller3c9c1fb2006-09-17 06:08:53 +1000151 for (c = buf, same = j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
markus@openbsd.org091c3022015-01-19 19:52:16 +0000152 for (i = HASH(c) & (dctx->n - 1); dctx->h[i] != HASH_UNUSED;
153 i = (i + 1) & (dctx->n - 1)) {
154 if (!CMP(c, buf + dctx->h[i] * SSH_BLOCKSIZE)) {
Damien Miller3c9c1fb2006-09-17 06:08:53 +1000155 if (++same > MAX_IDENTICAL)
markus@openbsd.org091c3022015-01-19 19:52:16 +0000156 return DEATTACK_DOS_DETECTED;
Damien Miller2dbbf8e2006-03-26 00:11:46 +1100157 if (check_crc(c, buf, len))
markus@openbsd.org091c3022015-01-19 19:52:16 +0000158 return DEATTACK_DETECTED;
Damien Miller95def091999-11-25 00:26:21 +1100159 else
160 break;
161 }
162 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000163 dctx->h[i] = j;
Damien Miller95def091999-11-25 00:26:21 +1100164 }
markus@openbsd.org091c3022015-01-19 19:52:16 +0000165 return DEATTACK_OK;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166}