2 /* $OpenBSD: deattack.c,v 1.30 2006/09/16 19:53:37 djm Exp $ */
4 * Cryptographic attack detector for ssh - source code
6 * Copyright (c) 1998 CORE SDI S.A., Buenos Aires, Argentina.
8 * All rights reserved. Redistribution and use in source and binary
9 * forms, with or without modification, are permitted provided that
10 * this copyright notice is retained.
12 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES ARE DISCLAIMED. IN NO EVENT SHALL CORE SDI S.A. BE
14 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR
15 * CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OR MISUSE OF THIS
18 * Ariel Futoransky <futo@core-sdi.com>
19 * <http://www.core-sdi.com>
23 __RCSID("$NetBSD: deattack.c,v 1.17 2009/02/16 20:53:54 christos Exp $");
24 #include <sys/types.h>
38 * CRC attack detection has a worst-case behaviour that is O(N^3) over
39 * the number of identical blocks in a packet. This behaviour can be
40 * exploited to create a limited denial of service attack.
42 * However, because we are dealing with encrypted data, identical
43 * blocks should only occur every 2^35 maximally-sized packets or so.
44 * Consequently, we can detect this DoS by looking for identical blocks
47 * The parameter below determines how many identical blocks we will
48 * accept in a single packet, trading off between attack detection and
49 * likelihood of terminating a legitimate connection. A value of 32
50 * corresponds to an average of 2^40 messages before an attack is
53 #define MAX_IDENTICAL 32
56 #define SSH_MAXBLOCKS (32 * 1024)
57 #define SSH_BLOCKSIZE (8)
59 /* Hashing constants */
60 #define HASH_MINSIZE (8 * 1024)
61 #define HASH_ENTRYSIZE (2)
62 #define HASH_FACTOR(x) ((x)*3/2)
63 #define HASH_UNUSEDCHAR (0xff)
64 #define HASH_UNUSED (0xffff)
65 #define HASH_IV (0xfffe)
67 #define HASH_MINBLOCKS (7*SSH_BLOCKSIZE)
70 /* Hash function (Input keys are cipher results) */
71 #define HASH(x) get_u32(x)
73 #define CMP(a, b) (memcmp(a, b, SSH_BLOCKSIZE))
76 crc_update(u_int32_t
*a
, u_int32_t b
)
79 *a
= ssh_crc32((u_char
*)&b
, sizeof(b
));
82 /* detect if a block is used in a particular pattern */
84 check_crc(u_char
*S
, u_char
*buf
, u_int32_t len
)
90 for (c
= buf
; c
< buf
+ len
; c
+= SSH_BLOCKSIZE
) {
103 /* Detect a crc32 compensation attack on a packet */
105 detect_attack(u_char
*buf
, u_int32_t len
)
107 static u_int16_t
*h
= (u_int16_t
*) NULL
;
108 static u_int32_t n
= HASH_MINSIZE
/ HASH_ENTRYSIZE
;
114 if (len
> (SSH_MAXBLOCKS
* SSH_BLOCKSIZE
) ||
115 len
% SSH_BLOCKSIZE
!= 0) {
116 fatal("detect_attack: bad length %d", len
);
118 for (l
= n
; l
< HASH_FACTOR(len
/ SSH_BLOCKSIZE
); l
= l
<< 2)
122 debug("Installing crc compensation attack detector.");
123 h
= (u_int16_t
*) xcalloc(l
, HASH_ENTRYSIZE
);
127 h
= (u_int16_t
*)xrealloc(h
, l
, HASH_ENTRYSIZE
);
132 if (len
<= HASH_MINBLOCKS
) {
133 for (c
= buf
; c
< buf
+ len
; c
+= SSH_BLOCKSIZE
) {
134 for (d
= buf
; d
< c
; d
+= SSH_BLOCKSIZE
) {
136 if ((check_crc(c
, buf
, len
)))
137 return (DEATTACK_DETECTED
);
143 return (DEATTACK_OK
);
145 memset(h
, HASH_UNUSEDCHAR
, n
* HASH_ENTRYSIZE
);
147 for (c
= buf
, same
= j
= 0; c
< (buf
+ len
); c
+= SSH_BLOCKSIZE
, j
++) {
148 for (i
= HASH(c
) & (n
- 1); h
[i
] != HASH_UNUSED
;
149 i
= (i
+ 1) & (n
- 1)) {
150 if (!CMP(c
, buf
+ h
[i
] * SSH_BLOCKSIZE
)) {
151 if (++same
> MAX_IDENTICAL
)
152 return (DEATTACK_DOS_DETECTED
);
153 if (check_crc(c
, buf
, len
))
154 return (DEATTACK_DETECTED
);
161 return (DEATTACK_OK
);