d33df610461b16b8a23af73143f3b269f9663ae9
8 define('_ECRAN_SECURITE', '1.3.8'); // 2018-10-31
11 * Documentation : http://www.spip.net/fr_article4200.html
17 if (isset($_GET['test_ecran_securite']))
18 $ecran_securite_raison = 'test '._ECRAN_SECURITE
;
22 * var_isbot=0 peut etre utilise par un bot de monitoring pour surveiller la disponibilite d'un site vu par les users
23 * var_isbot=1 peut etre utilise pour monitorer la disponibilite pour les bots (sujets a 503 de delestage si
24 * le load depasse ECRAN_SECURITE_LOAD)
26 if (!defined('_IS_BOT') and isset($_GET['var_isbot'])){
27 define('_IS_BOT', $_GET['var_isbot'] ?
true : false);
31 * Détecteur de robot d'indexation
33 if (!defined('_IS_BOT')){
35 isset($_SERVER['HTTP_USER_AGENT'])
37 . implode ('|', array(
45 'MSIE 6\.0', // botnet 99,9% du temps
53 'adressendeutschland',
78 'facebookexternalhit',
95 'iltrovatore-setaccio',
108 'loadimpactpageanalyzer',
112 'mediapartners-google',
115 'netcraftsurveyagent',
179 (string)$_SERVER['HTTP_USER_AGENT'])
182 if (!defined('_IS_BOT_FRIEND')){
183 define('_IS_BOT_FRIEND',
184 isset($_SERVER['HTTP_USER_AGENT'])
185 and preg_match(',' . implode ('|', array(
186 'facebookexternalhit',
189 (string)$_SERVER['HTTP_USER_AGENT'])
194 * Interdit de passer une variable id_article (ou id_xxx) qui ne
195 * soit pas numérique (ce qui bloque l'exploitation de divers trous
196 * de sécurité, dont celui de toutes les versions < 1.8.2f)
197 * (sauf pour id_table, qui n'est pas numérique jusqu'à [5743])
198 * (id_base est une variable de la config des widgets de WordPress)
200 foreach ($_GET as $var => $val)
201 if ($_GET[$var] and strncmp($var, "id_", 3) == 0
202 and !in_array($var, array('id_table', 'id_base')))
203 $_GET[$var] = is_array($_GET[$var])?@array_map
('intval', $_GET[$var]):intval($_GET[$var]);
204 foreach ($_POST as $var => $val)
205 if ($_POST[$var] and strncmp($var, "id_", 3) == 0
206 and !in_array($var, array('id_table', 'id_base')))
207 $_POST[$var] = is_array($_POST[$var])?@array_map
('intval', $_POST[$var]):intval($_POST[$var]);
208 foreach ($GLOBALS as $var => $val)
209 if ($GLOBALS[$var] and strncmp($var, "id_", 3) == 0
210 and !in_array($var, array('id_table', 'id_base')))
211 $GLOBALS[$var] = is_array($GLOBALS[$var])?@array_map
('intval', $GLOBALS[$var]):intval($GLOBALS[$var]);
214 * Interdit la variable $cjpeg_command, qui était utilisée sans
215 * précaution dans certaines versions de dev (1.8b2 -> 1.8b5)
220 * Contrôle de quelques variables (XSS)
222 foreach(array('lang', 'var_recherche', 'aide', 'var_lang_r', 'lang_r', 'var_ajax_ancre', 'nom_fichier') as $var) {
223 if (isset($_GET[$var]))
224 $_REQUEST[$var] = $GLOBALS[$var] = $_GET[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_GET[$var]);
225 if (isset($_POST[$var]))
226 $_REQUEST[$var] = $GLOBALS[$var] = $_POST[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_POST[$var]);
230 * Filtre l'accès à spip_acces_doc (injection SQL en 1.8.2x)
232 if (preg_match(',^(.*/)?spip_acces_doc\.,', (string)$_SERVER['REQUEST_URI'])) {
233 $file = addslashes((string)$_GET['file']);
237 * Pas d'inscription abusive
239 if (isset($_REQUEST['mode']) and isset($_REQUEST['page'])
240 and !in_array($_REQUEST['mode'], array("6forum", "1comite"))
241 and $_REQUEST['page'] == "identifiants")
242 $ecran_securite_raison = "identifiants";
245 * Agenda joue à l'injection php
247 if (isset($_REQUEST['partie_cal'])
248 and $_REQUEST['partie_cal'] !== htmlentities((string)$_REQUEST['partie_cal']))
249 $ecran_securite_raison = "partie_cal";
250 if (isset($_REQUEST['echelle'])
251 and $_REQUEST['echelle'] !== htmlentities((string)$_REQUEST['echelle']))
252 $ecran_securite_raison = "echelle";
257 if (isset($_REQUEST['exec'])
258 and !preg_match(',^[\w-]+$,', (string)$_REQUEST['exec']))
259 $ecran_securite_raison = "exec";
260 if (isset($_REQUEST['cherche_auteur'])
261 and preg_match(',[<],', (string)$_REQUEST['cherche_auteur']))
262 $ecran_securite_raison = "cherche_auteur";
263 if (isset($_REQUEST['exec'])
264 and $_REQUEST['exec'] == 'auteurs'
265 and preg_match(',[<],', (string)$_REQUEST['recherche']))
266 $ecran_securite_raison = "recherche";
267 if (isset($_REQUEST['exec'])
268 and $_REQUEST['exec'] == 'info_plugin'
269 and preg_match(',[<],', (string)$_REQUEST['plugin']))
270 $ecran_securite_raison = "plugin";
271 if (isset($_REQUEST['exec'])
272 and $_REQUEST['exec'] == 'puce_statut'
273 and isset($_REQUEST['id'])
274 and !intval($_REQUEST['id']))
275 $ecran_securite_raison = "puce_statut";
276 if (isset($_REQUEST['action'])
277 and $_REQUEST['action'] == 'configurer') {
278 if (@file_exists
('inc_version.php')
279 or @file_exists
('ecrire/inc_version.php')) {
280 function action_configurer() {
281 include_spip('inc/autoriser');
282 if(!autoriser('configurer', _request('configuration'))) {
283 include_spip('inc/minipres');
284 echo minipres(_T('info_acces_interdit'));
287 require _DIR_RESTREINT
.'action/configurer.php';
288 action_configurer_dist();
294 * Bloque les requêtes contenant %00 (manipulation d'include)
297 @get_magic_quotes_gpc
() ?
298 stripslashes(serialize($_REQUEST)) : serialize($_REQUEST),
301 $ecran_securite_raison = "%00";
304 * Bloque les requêtes fond=formulaire_
306 if (isset($_REQUEST['fond'])
307 and preg_match(',^formulaire_,i', $_REQUEST['fond']))
308 $ecran_securite_raison = "fond=formulaire_";
311 * Bloque les requêtes du type ?GLOBALS[type_urls]=toto (bug vieux php)
313 if (isset($_REQUEST['GLOBALS']))
314 $ecran_securite_raison = "GLOBALS[GLOBALS]";
317 * Bloque les requêtes des bots sur:
319 * les paginations entremélées
322 (isset($_REQUEST['echelle']) and isset($_REQUEST['partie_cal']) and isset($_REQUEST['type']))
323 or (strpos((string)$_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string)$_SERVER['REQUEST_URI']))
326 $ecran_securite_raison = "robot agenda/double pagination";
329 * Bloque une vieille page de tests de CFG (<1.11)
330 * Bloque un XSS sur une page inexistante
332 if (isset($_REQUEST['page'])) {
333 if ($_REQUEST['page'] == 'test_cfg')
334 $ecran_securite_raison = "test_cfg";
335 if ($_REQUEST['page'] !== htmlspecialchars((string)$_REQUEST['page']))
336 $ecran_securite_raison = "xsspage";
337 if ($_REQUEST['page'] == '404'
338 and isset($_REQUEST['erreur']))
339 $ecran_securite_raison = "xss404";
345 foreach (array('var_login') as $var)
346 if (isset($_REQUEST[$var]) and is_array($_REQUEST[$var]))
347 $ecran_securite_raison = "xss ".$var;
350 * Parade antivirale contre un cheval de troie
352 if (!function_exists('tmp_lkojfghx')) {
353 function tmp_lkojfghx() {}
354 function tmp_lkojfghx2($a = 0, $b = 0, $c = 0, $d = 0) {
355 // si jamais on est arrivé ici sur une erreur php
356 // et qu'un autre gestionnaire d'erreur est défini, l'appeller
357 if ($b && $GLOBALS['tmp_xhgfjokl'])
358 call_user_func($GLOBALS['tmp_xhgfjokl'], $a, $b, $c, $d);
361 if (isset($_POST['tmp_lkojfghx3']))
362 $ecran_securite_raison = "gumblar";
365 * Outils XML mal sécurisés < 2.0.9
367 if (isset($_REQUEST['transformer_xml']))
368 $ecran_securite_raison = "transformer_xml";
371 * Outils XML mal sécurisés again
373 if (isset($_REQUEST['var_url']) and $_REQUEST['var_url'] and isset($_REQUEST['exec']) and $_REQUEST['exec']=='valider_xml'){
374 $url = trim($_REQUEST['var_url']);
375 if (strncmp($url,'/',1)==0
376 or (($p=strpos($url,'..'))!==false AND strpos($url,'..',$p+
3)!==false)
377 or (($p=strpos($url,'..'))!==false AND strpos($url,'IMG',$p+
3)!==false)
378 or (strpos($url,'://')!==false or strpos($url,':\\')!==false)) {
379 $ecran_securite_raison = 'URL interdite pour var_url';
384 * Sauvegarde mal securisée < 2.0.9
386 if (isset($_REQUEST['nom_sauvegarde'])
387 and strstr((string)$_REQUEST['nom_sauvegarde'], '/'))
388 $ecran_securite_raison = 'nom_sauvegarde manipulee';
389 if (isset($_REQUEST['znom_sauvegarde'])
390 and strstr((string)$_REQUEST['znom_sauvegarde'], '/'))
391 $ecran_securite_raison = 'znom_sauvegarde manipulee';
395 * op permet des inclusions arbitraires ;
396 * on vérifie 'page' pour ne pas bloquer ... drupal
398 if (isset($_REQUEST['op']) and isset($_REQUEST['page'])
399 and $_REQUEST['op'] !== preg_replace('/[^\-\w]/', '', $_REQUEST['op']))
400 $ecran_securite_raison = 'op';
403 * Forms & Table ne se méfiait pas assez des uploads de fichiers
406 foreach($_FILES as $k => $v){
407 if (preg_match(',^fichier_\d+$,', $k)
408 and preg_match(',\.php,i', $v['name']))
413 * et Contact trop laxiste avec une variable externe
414 * on bloque pas le post pour eviter de perdre des donnees mais on unset la variable et c'est tout
416 if (isset($_REQUEST['pj_enregistrees_nom']) and $_REQUEST['pj_enregistrees_nom']){
417 unset($_REQUEST['pj_enregistrees_nom']);
418 unset($_GET['pj_enregistrees_nom']);
419 unset($_POST['pj_enregistrees_nom']);
423 * reinstall=oui un peu trop permissif
425 if (isset($_REQUEST['reinstall'])
426 and $_REQUEST['reinstall'] == 'oui')
427 $ecran_securite_raison = 'reinstall=oui';
430 * Échappement xss referer
432 if (isset($_SERVER['HTTP_REFERER']))
433 $_SERVER['HTTP_REFERER'] = strtr($_SERVER['HTTP_REFERER'], '<>"\'', '[]##');
437 * Echappement HTTP_X_FORWARDED_HOST
439 if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
440 $_SERVER['HTTP_X_FORWARDED_HOST'] = strtr($_SERVER['HTTP_X_FORWARDED_HOST'], "<>?\"\{\}\$'` \r\n", '____________');
444 * Réinjection des clés en html dans l'admin r19561
446 if (strpos($_SERVER['REQUEST_URI'], "ecrire/") !== false){
447 $zzzz = implode("", array_keys($_REQUEST));
448 if (strlen($zzzz) != strcspn($zzzz, '<>"\''))
449 $ecran_securite_raison = 'Cle incorrecte en $_REQUEST';
453 * Injection par connect
455 if (isset($_REQUEST['connect'])
457 // cas qui permettent de sortir d'un commentaire PHP
458 (strpos($_REQUEST['connect'], "?") !== false
459 or strpos($_REQUEST['connect'], "<") !== false
460 or strpos($_REQUEST['connect'], ">") !== false
461 or strpos($_REQUEST['connect'], "\n") !== false
462 or strpos($_REQUEST['connect'], "\r") !== false)
464 $ecran_securite_raison = "malformed connect argument";
468 * S'il y a une raison de mourir, mourons
470 if (isset($ecran_securite_raison)) {
471 header("HTTP/1.0 403 Forbidden");
472 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
473 header("Cache-Control: no-cache, must-revalidate");
474 header("Pragma: no-cache");
475 header("Content-Type: text/html");
476 die("<html><title>Error 403: Forbidden</title><body><h1>Error 403</h1><p>You are not authorized to view this page ($ecran_securite_raison)</p></body></html>");
480 * Un filtre filtrer_entites securise
482 if (!function_exists('filtre_filtrer_entites_dist')) {
483 function filtre_filtrer_entites_dist($t) {
484 include_spip('inc/texte');
485 return interdire_scripts(filtrer_entites($t));
497 * Bloque les bots quand le load déborde
499 if (!defined('_ECRAN_SECURITE_LOAD'))
500 define('_ECRAN_SECURITE_LOAD', 4);
503 defined('_ECRAN_SECURITE_LOAD')
504 and _ECRAN_SECURITE_LOAD
> 0
507 and $_SERVER['REQUEST_METHOD'] === 'GET'
509 (function_exists('sys_getloadavg')
510 and $load = sys_getloadavg()
512 and $load = array_shift($load)
515 (@is_readable
('/proc/loadavg')
516 and $load = file_get_contents('/proc/loadavg')
517 and $load = floatval($load)
520 and $load > _ECRAN_SECURITE_LOAD
// eviter l'evaluation suivante si de toute facon le load est inferieur a la limite
521 and rand(0, $load * $load) > _ECRAN_SECURITE_LOAD
* _ECRAN_SECURITE_LOAD
523 header("HTTP/1.0 503 Service Unavailable");
524 header("Retry-After: 300");
525 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
526 header("Cache-Control: no-cache, must-revalidate");
527 header("Pragma: no-cache");
528 header("Content-Type: text/html");
529 die("<html><title>Status 503: Site temporarily unavailable</title><body><h1>Status 503</h1><p>Site temporarily unavailable (load average $load)</p></body></html>");