-
Notifications
You must be signed in to change notification settings - Fork 71
/
crypt.c
283 lines (244 loc) · 7.74 KB
/
crypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
+----------------------------------------------------------------------+
| Suhosin Version 1 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2007 The Hardened-PHP Project |
| Copyright (c) 2007-2015 SektionEins GmbH |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Stefan Esser <[email protected]> |
| Ben Fuhrmannek <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "TSRM.h"
#include "php_suhosin.h"
#include "ext/standard/base64.h"
#include "sha256.h"
static void suhosin_get_ipv4(char *buf TSRMLS_DC)
{
char *raddr = suhosin_getenv(ZEND_STRL("REMOTE_ADDR") TSRMLS_CC);
int i;
if (raddr == NULL) {
memset(buf, 0, 4);
return;
}
for (i=0; i<4; i++) {
if (raddr[0] == 0) {
buf[i] = 0;
} else {
buf[i] = strtol(raddr, &raddr, 10);
if (raddr[0] == '.') {
raddr++;
}
}
}
}
char *suhosin_encrypt_string(char *str, int len, char *var, int vlen, char *key TSRMLS_DC)
{
int padded_len, i, slen;
unsigned char *crypted, *tmp;
unsigned int check = 0x13579BDF;
if (str == NULL) {
return NULL;
}
if (len == 0) {
return estrndup("", 0);
}
suhosin_aes_gkey(4,8,key TSRMLS_CC);
padded_len = ((len+15) & ~0xF);
crypted = emalloc(16+padded_len+1);
memset(crypted, 0xff, 16+padded_len+1);
memcpy(crypted+16, str, len+1);
/* calculate check value */
for (i = 0; i<vlen; i++) {
check = (check << 3) | (check >> (32-3));
check += check << 1;
check ^= (unsigned char)var[i];
}
for (i = 0; i<len; i++) {
check = (check << 3) | (check >> (32-3));
check += check << 1;
check ^= (unsigned char)str[i];
}
/* store ip value */
suhosin_get_ipv4((char *)crypted+4 TSRMLS_CC);
/* store check value */
crypted[8] = check & 0xff;
crypted[9] = (check >> 8) & 0xff;
crypted[10] = (check >> 16) & 0xff;
crypted[11] = (check >> 24) & 0xff;
/* store original length */
crypted[12] = len & 0xff;
crypted[13] = (len >> 8) & 0xff;
crypted[14] = (len >> 16) & 0xff;
crypted[15] = (len >> 24) & 0xff;
for (i=0, tmp=crypted; i<padded_len+16; i+=16, tmp+=16) {
if (i > 0) {
int j;
for (j=0; j<16; j++) tmp[j] ^= tmp[j-16];
}
suhosin_aes_encrypt((char *)tmp TSRMLS_CC);
}
tmp = php_base64_encode(crypted, padded_len+16, NULL);
efree(crypted);
slen=strlen((char *)tmp);
for (i=0; i<slen; i++) {
switch (tmp[i]) {
case '/': tmp[i]='-'; break;
case '=': tmp[i]='.'; break;
case '+': tmp[i]='_'; break;
}
}
return (char *)tmp;
}
char *suhosin_decrypt_string(char *str, int padded_len, char *var, int vlen, char *key, int *orig_len, int check_ra TSRMLS_DC)
{
int len, i, o_len, invalid = 0;
unsigned char *decrypted, *tmp;
unsigned int check = 0x13579BDF;
char buf[4];
if (str == NULL) {
return NULL;
}
if (padded_len == 0) {
if (orig_len) {
*orig_len = 0;
}
return estrndup("", 0);
}
suhosin_aes_gkey(4,8,key TSRMLS_CC);
for (i=0; i<padded_len; i++) {
switch (str[i]) {
case '-': str[i]='/'; break;
case '.': str[i]='='; break;
case '_': str[i]='+'; break;
}
}
decrypted = php_base64_decode((unsigned char *)str, padded_len, &len);
if (decrypted == NULL || len < 2*16 || (len % 16) != 0) {
error_out:
if (decrypted != NULL) {
efree(decrypted);
}
if (orig_len) {
*orig_len = 0;
}
return NULL;
}
for (i=len-16, tmp=decrypted+i; i>=0; i-=16, tmp-=16) {
suhosin_aes_decrypt((char *)tmp TSRMLS_CC);
if (i > 0) {
int j;
for (j=0; j<16; j++) tmp[j] ^= tmp[j-16];
}
}
/* retrieve orig_len */
o_len = decrypted[15];
o_len <<= 8;
o_len |= decrypted[14];
o_len <<= 8;
o_len |= decrypted[13];
o_len <<= 8;
o_len |= decrypted[12];
if (o_len < 0 || o_len > len-16) {
goto error_out;
}
/* calculate check value */
for (i = 0; i<vlen; i++) {
check = (check << 3) | (check >> (32-3));
check += check << 1;
check ^= (unsigned char)var[i];
}
for (i = 0; i<o_len; i++) {
check = (check << 3) | (check >> (32-3));
check += check << 1;
check ^= decrypted[16+i];
}
/* check value */
invalid = (decrypted[8] != (check & 0xff)) ||
(decrypted[9] != ((check >> 8) & 0xff)) ||
(decrypted[10] != ((check >> 16) & 0xff)) ||
(decrypted[11] != ((check >> 24) & 0xff));
/* check IP */
if (check_ra > 0) {
if (check_ra > 4) {
check_ra = 4;
}
suhosin_get_ipv4(&buf[0] TSRMLS_CC);
if (memcmp(buf, decrypted+4, check_ra) != 0) {
goto error_out;
}
}
if (invalid) {
goto error_out;
}
if (orig_len) {
*orig_len = o_len;
}
memmove(decrypted, decrypted+16, o_len);
decrypted[o_len] = 0;
/* we do not realloc() here because 16 byte less
is simply not worth the overhead */
return (char *)decrypted;
}
char *suhosin_generate_key(char *key, zend_bool ua, zend_bool dr, long raddr, char *cryptkey TSRMLS_DC)
{
char *_ua = NULL;
char *_dr = NULL;
char *_ra = NULL;
suhosin_SHA256_CTX ctx;
if (ua) {
_ua = suhosin_getenv(ZEND_STRL("HTTP_USER_AGENT") TSRMLS_CC);
}
if (dr) {
_dr = suhosin_getenv(ZEND_STRL("DOCUMENT_ROOT") TSRMLS_CC);
}
if (raddr > 0) {
_ra = suhosin_getenv(ZEND_STRL("REMOTE_ADDR") TSRMLS_CC);
}
SDEBUG("(suhosin_generate_key) KEY: %s - UA: %s - DR: %s - RA: %s", key,_ua,_dr,_ra);
suhosin_SHA256Init(&ctx);
if (key == NULL || *key == 0) {
suhosin_SHA256Update(&ctx, (unsigned char*)"D3F4UL7", strlen("D3F4UL7"));
} else {
suhosin_SHA256Update(&ctx, (unsigned char*)key, strlen(key));
}
if (_ua) {
suhosin_SHA256Update(&ctx, (unsigned char*)_ua, strlen(_ua));
}
if (_dr) {
suhosin_SHA256Update(&ctx, (unsigned char*)_dr, strlen(_dr));
}
if (_ra) {
if (raddr >= 4) {
suhosin_SHA256Update(&ctx, (unsigned char*)_ra, strlen(_ra));
} else {
long dots = 0;
char *tmp = _ra;
while (*tmp) {
if (*tmp == '.') {
dots++;
if (dots == raddr) {
break;
}
}
tmp++;
}
suhosin_SHA256Update(&ctx, (unsigned char*)_ra, tmp-_ra);
}
}
suhosin_SHA256Final((unsigned char *)cryptkey, &ctx);
cryptkey[32] = 0; /* uhmm... not really a string */
return cryptkey;
}