Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 337 lines (278 sloc) 9.076 kb
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/*
+----------------------------------------------------------------------+
| Suhosin Version 1 |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2007 The Hardened-PHP Project |
| Copyright (c) 2007-2012 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Stefan Esser <sesser@sektioneins.de> |
+----------------------------------------------------------------------+
*/
/*
$Id: header.c,v 1.1.1.1 2007-11-28 01:15:35 sesser Exp $
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/url.h"
#include "php_suhosin.h"
#include "SAPI.h"
#include "php_variables.h"

#if PHP_VERSION_ID >= 50300
static int (*orig_header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC) = NULL;
#else
static int (*orig_header_handler)(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC) = NULL;
#endif

char *suhosin_encrypt_single_cookie(char *name, int name_len, char *value, int value_len, char *key TSRMLS_DC)
{
char *buf, *buf2, *d, *d_url;
int l;

buf = estrndup(name, name_len);


name_len = php_url_decode(buf, name_len);
normalize_varname(buf);
name_len = strlen(buf);

if (SUHOSIN_G(cookie_plainlist)) {
if (zend_hash_exists(SUHOSIN_G(cookie_plainlist), buf, name_len+1)) {
encrypt_return_plain:
efree(buf);
return estrndup(value, value_len);
}
} else if (SUHOSIN_G(cookie_cryptlist)) {
if (!zend_hash_exists(SUHOSIN_G(cookie_cryptlist), buf, name_len+1)) {
goto encrypt_return_plain;
}
}

buf2 = estrndup(value, value_len);

value_len = php_url_decode(buf2, value_len);

d = suhosin_encrypt_string(buf2, value_len, buf, name_len, key TSRMLS_CC);
d_url = php_url_encode(d, strlen(d), &l);
efree(d);
efree(buf);
efree(buf2);
return d_url;
}

char *suhosin_decrypt_single_cookie(char *name, int name_len, char *value, int value_len, char *key, char **where TSRMLS_DC)
{
    int o_name_len = name_len;
char *buf, *buf2, *d, *d_url;
int l;

buf = estrndup(name, name_len);

name_len = php_url_decode(buf, name_len);
normalize_varname(buf);
name_len = strlen(buf);

if (SUHOSIN_G(cookie_plainlist)) {
if (zend_hash_exists(SUHOSIN_G(cookie_plainlist), buf, name_len+1)) {
decrypt_return_plain:
efree(buf);
            memcpy(*where, name, o_name_len);
            *where += o_name_len;
            **where = '='; *where +=1;
memcpy(*where, value, value_len);
*where += value_len;
return *where;
}
} else if (SUHOSIN_G(cookie_cryptlist)) {
if (!zend_hash_exists(SUHOSIN_G(cookie_cryptlist), buf, name_len+1)) {
goto decrypt_return_plain;
}
}


buf2 = estrndup(value, value_len);

value_len = php_url_decode(buf2, value_len);

d = suhosin_decrypt_string(buf2, value_len, buf, name_len, key, &l, SUHOSIN_G(cookie_checkraddr) TSRMLS_CC);
    if (d == NULL) {
        goto skip_cookie;
    }
d_url = php_url_encode(d, l, &l);
efree(d);
    memcpy(*where, name, o_name_len);
    *where += o_name_len;
    **where = '=';*where += 1;
memcpy(*where, d_url, l);
*where += l;
efree(d_url);
skip_cookie:
efree(buf);
efree(buf2);
return *where;
}

/* {{{ suhosin_cookie_decryptor
*/
char *suhosin_cookie_decryptor(TSRMLS_D)
{
char *raw_cookie = SG(request_info).cookie_data;
char *decrypted, *ret, *var, *val, *tmp;
int j;
char cryptkey[33];

/*
if (...deactivated...) {
return estrdup(raw_cookie);
}
*/

suhosin_generate_key(SUHOSIN_G(cookie_cryptkey), SUHOSIN_G(cookie_cryptua), SUHOSIN_G(cookie_cryptdocroot), SUHOSIN_G(cookie_cryptraddr), (char *)&cryptkey TSRMLS_CC);

ret = decrypted = emalloc(strlen(raw_cookie)*4+1);
raw_cookie = estrdup(raw_cookie);
    SUHOSIN_G(raw_cookie) = estrdup(raw_cookie);


j = 0; tmp = raw_cookie;
while (*tmp) {
char *d_url;int varlen;
while (*tmp == '\t' || *tmp == ' ') tmp++;
var = tmp;
while (*tmp && *tmp != ';' && *tmp != '=') tmp++;

varlen = tmp-var;
/*memcpy(decrypted, var, varlen);
decrypted += varlen;*/
if (*tmp == 0) break;

if (*tmp++ == ';') {
*decrypted++ = ';';
continue;
}

/**decrypted++ = '=';*/

val = tmp;
while (*tmp && *tmp != ';') tmp++;

d_url = suhosin_decrypt_single_cookie(var, varlen, val, tmp-val, (char *)&cryptkey, &decrypted TSRMLS_CC);
if (*tmp == ';') {
*decrypted++ = ';';
}

if (*tmp == 0) break;
tmp++;
}
*decrypted++ = 0;
ret = erealloc(ret, decrypted-ret);

SUHOSIN_G(decrypted_cookie) = ret;
efree(raw_cookie);

return ret;
}
/* }}} */

/* {{{ suhosin_header_handler
*/
#if PHP_VERSION_ID >= 50300
int suhosin_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC)
#else
int suhosin_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC)
#endif
{
int retval = SAPI_HEADER_ADD, i;
char *tmp;

#if PHP_VERSION_ID >= 50300
if (op != SAPI_HEADER_ADD && op != SAPI_HEADER_REPLACE) {
                goto suhosin_skip_header_handling;
}
#endif

if (sapi_header && sapi_header->header) {

tmp = sapi_header->header;

for (i=0; i<sapi_header->header_len; i++, tmp++) {
if (tmp[0] == 0) {
char *fname = get_active_function_name(TSRMLS_C);

if (!fname) {
fname = "unknown";
}

suhosin_log(S_MISC, "%s() - wanted to send a HTTP header with an ASCII NUL in it", fname);
if (!SUHOSIN_G(simulation)) {
sapi_header->header_len = i;
}
}
if (SUHOSIN_G(allow_multiheader)) {
continue;
} else if ((tmp[0] == '\r' && (tmp[1] != '\n' || i == 0)) ||
(tmp[0] == '\n' && (i == sapi_header->header_len-1 || i == 0 || (tmp[1] != ' ' && tmp[1] != '\t')))) {
char *fname = get_active_function_name(TSRMLS_C);

if (!fname) {
fname = "unknown";
}

suhosin_log(S_MISC, "%s() - wanted to send multiple HTTP headers at once", fname);
if (!SUHOSIN_G(simulation)) {
sapi_header->header_len = i;
tmp[0] = 0;
}
}
}
}

/* Handle a potential cookie */

if (SUHOSIN_G(cookie_encrypt) && (strncasecmp("Set-Cookie:", sapi_header->header, sizeof("Set-Cookie:")-1) == 0)) {

char *start, *end, *rend, *tmp;
     char *name, *value;
     int nlen, vlen, len, tlen;
char cryptkey[33];

suhosin_generate_key(SUHOSIN_G(cookie_cryptkey), SUHOSIN_G(cookie_cryptua), SUHOSIN_G(cookie_cryptdocroot), SUHOSIN_G(cookie_cryptraddr), (char *)&cryptkey TSRMLS_CC);
start = estrndup(sapi_header->header, sapi_header->header_len);
rend = end = start + sapi_header->header_len;

tmp = memchr(start, ';', end-start);
if (tmp != NULL) {
end = tmp;
}

tmp = start + sizeof("Set-Cookie:") - 1;
while (tmp < end && tmp[0]==' ') {
tmp++;
}
name = tmp;
nlen = end-name;
tmp = memchr(name, '=', nlen);
if (tmp == NULL) {
value = end;
} else {
value = tmp+1;
nlen = tmp-name;
}
vlen = end-value;

value = suhosin_encrypt_single_cookie(name, nlen, value, vlen, (char *)&cryptkey TSRMLS_CC);
vlen = strlen(value);

len = sizeof("Set-Cookie: ")-1 + nlen + 1 + vlen + rend-end;
tmp = emalloc(len + 1);
tlen = sprintf(tmp, "Set-Cookie: %.*s=%s", nlen,name, value);
memcpy(tmp + tlen, end, rend-end);
tmp[len] = 0;

efree(sapi_header->header);
efree(value);
efree(start);

sapi_header->header = tmp;
sapi_header->header_len = len;
}

suhosin_skip_header_handling:
/* If existing call the sapi header handler */
if (orig_header_handler) {
#if PHP_VERSION_ID >= 50300
retval = orig_header_handler(sapi_header, op, sapi_headers TSRMLS_CC);
#else
retval = orig_header_handler(sapi_header, sapi_headers TSRMLS_CC);
#endif
}

return retval;
}
/* }}} */


/* {{{ suhosin_hook_header_handler
*/
void suhosin_hook_header_handler()
{
if (orig_header_handler == NULL) {
orig_header_handler = sapi_module.header_handler;
sapi_module.header_handler = suhosin_header_handler;
}
}
/* }}} */

/* {{{ suhosin_unhook_header_handler
*/
void suhosin_unhook_header_handler()
{
sapi_module.header_handler = orig_header_handler;
orig_header_handler = NULL;
}
/* }}} */


/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
Something went wrong with that request. Please try again.