1 /* $OpenBSD: deattack.c,v 1.30 2006/09/16 19:53:37 djm Exp $ */
2 /*
3 * 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>
18 * <http://www.core-sdi.com>
19 */
20
21 #include <sys/types.h>
22 #include <sys/time.h>
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #include "xmalloc.h"
29 #include "deattack.h"
30 #include "log.h"
31 #include "crc32.h"
32 #include "misc.h"
33
34 __RCSID("$MirOS: src/usr.bin/ssh/deattack.c,v 1.3 2008/12/16 22:13:27 tg Exp $");
35
36 /*
37 * CRC attack detection has a worst-case behaviour that is O(N^3) over
38 * the number of identical blocks in a packet. This behaviour can be
39 * exploited to create a limited denial of service attack.
40 *
41 * However, because we are dealing with encrypted data, identical
42 * blocks should only occur every 2^35 maximally-sized packets or so.
43 * Consequently, we can detect this DoS by looking for identical blocks
44 * in a packet.
45 *
46 * The parameter below determines how many identical blocks we will
47 * accept in a single packet, trading off between attack detection and
48 * likelihood of terminating a legitimate connection. A value of 32
49 * corresponds to an average of 2^40 messages before an attack is
50 * misdetected
51 */
52 #define MAX_IDENTICAL 32
53
54 /* SSH Constants */
55 #define SSH_MAXBLOCKS (32 * 1024)
56 #define SSH_BLOCKSIZE (8)
57
58 /* Hashing constants */
59 #define HASH_MINSIZE (8 * 1024)
60 #define HASH_ENTRYSIZE (2)
61 #define HASH_FACTOR(x) ((x)*3/2)
62 #define HASH_UNUSEDCHAR (0xff)
63 #define HASH_UNUSED (0xffff)
64 #define HASH_IV (0xfffe)
65
66 #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
67
68
69 /* Hash function (Input keys are cipher results) */
70 #define HASH(x) get_u32(x)
71
72 #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
73
74 static void
crc_update(u_int32_t * a,u_int32_t b)75 crc_update(u_int32_t *a, u_int32_t b)
76 {
77 b ^= *a;
78 *a = ssh_crc32((u_char *)&b, sizeof(b));
79 }
80
81 /* detect if a block is used in a particular pattern */
82 static int
check_crc(u_char * S,u_char * buf,u_int32_t len)83 check_crc(u_char *S, u_char *buf, u_int32_t len)
84 {
85 u_int32_t crc;
86 u_char *c;
87
88 crc = 0;
89 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
90 if (!CMP(S, c)) {
91 crc_update(&crc, 1);
92 crc_update(&crc, 0);
93 } else {
94 crc_update(&crc, 0);
95 crc_update(&crc, 0);
96 }
97 }
98 return (crc == 0);
99 }
100
101
102 /* Detect a crc32 compensation attack on a packet */
103 int
detect_attack(u_char * buf,u_int32_t len)104 detect_attack(u_char *buf, u_int32_t len)
105 {
106 static u_int16_t *h = (u_int16_t *) NULL;
107 static u_int32_t n = HASH_MINSIZE / HASH_ENTRYSIZE;
108 u_int32_t i, j;
109 u_int32_t l, same;
110 u_char *c;
111 u_char *d;
112
113 if (len > (SSH_MAXBLOCKS * SSH_BLOCKSIZE) ||
114 len % SSH_BLOCKSIZE != 0) {
115 fatal("detect_attack: bad length %d", len);
116 }
117 for (l = n; l < HASH_FACTOR(len / SSH_BLOCKSIZE); l = l << 2)
118 ;
119
120 if (h == NULL) {
121 debug("Installing crc compensation attack detector.");
122 h = (u_int16_t *) xcalloc(l, HASH_ENTRYSIZE);
123 n = l;
124 } else {
125 if (l > n) {
126 h = (u_int16_t *)xrealloc(h, l, HASH_ENTRYSIZE);
127 n = l;
128 }
129 }
130
131 if (len <= HASH_MINBLOCKS) {
132 for (c = buf; c < buf + len; c += SSH_BLOCKSIZE) {
133 for (d = buf; d < c; d += SSH_BLOCKSIZE) {
134 if (!CMP(c, d)) {
135 if ((check_crc(c, buf, len)))
136 return (DEATTACK_DETECTED);
137 else
138 break;
139 }
140 }
141 }
142 return (DEATTACK_OK);
143 }
144 memset(h, HASH_UNUSEDCHAR, n * HASH_ENTRYSIZE);
145
146 for (c = buf, same = j = 0; c < (buf + len); c += SSH_BLOCKSIZE, j++) {
147 for (i = HASH(c) & (n - 1); h[i] != HASH_UNUSED;
148 i = (i + 1) & (n - 1)) {
149 if (!CMP(c, buf + h[i] * SSH_BLOCKSIZE)) {
150 if (++same > MAX_IDENTICAL)
151 return (DEATTACK_DOS_DETECTED);
152 if (check_crc(c, buf, len))
153 return (DEATTACK_DETECTED);
154 else
155 break;
156 }
157 }
158 h[i] = j;
159 }
160 return (DEATTACK_OK);
161 }
162