[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / config / ecran_securite.php
1 <?php
2
3 /*
4 * ecran_securite.php
5 * ------------------
6 */
7
8 define('_ECRAN_SECURITE', '1.3.0'); // 2017-03-06
9
10 /*
11 * Documentation : http://www.spip.net/fr_article4200.html
12 */
13
14 /*
15 * Test utilisateur
16 */
17 if (isset($_GET['test_ecran_securite']))
18 $ecran_securite_raison = 'test '._ECRAN_SECURITE;
19
20 /*
21 * Monitoring
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)
25 */
26 if (!defined('_IS_BOT') and isset($_GET['var_isbot']))
27 define('_IS_BOT',$_GET['var_isbot']?true:false);
28
29 /*
30 * Détecteur de robot d'indexation
31 */
32 if (!defined('_IS_BOT'))
33 define('_IS_BOT',
34 isset($_SERVER['HTTP_USER_AGENT'])
35 and preg_match(
36 // mots generiques
37 ',bot|slurp|crawler|spider|webvac|yandex|'
38 // MSIE 6.0 est un botnet 99,9% du temps, on traite donc ce USER_AGENT comme un bot
39 . 'MSIE 6\.0|'
40 // UA plus cibles
41 . '80legs|accoona|AltaVista|ASPSeek|Baidu|Charlotte|EC2LinkFinder|eStyle|flipboard|hootsuite|FunWebProducts|Google|Genieo|INA dlweb|InfegyAtlas|Java VM|LiteFinder|Lycos|MegaIndex|MetaURI|Moreover|Rambler|Scrapy|Scooter|ScrubbyBloglines|Yahoo|Yeti'
42 . ',i', (string) $_SERVER['HTTP_USER_AGENT'])
43 );
44
45 /*
46 * Interdit de passer une variable id_article (ou id_xxx) qui ne
47 * soit pas numérique (ce qui bloque l'exploitation de divers trous
48 * de sécurité, dont celui de toutes les versions < 1.8.2f)
49 * (sauf pour id_table, qui n'est pas numérique jusqu'à [5743])
50 * (id_base est une variable de la config des widgets de WordPress)
51 */
52 foreach ($_GET as $var => $val)
53 if ($_GET[$var] and strncmp($var, "id_", 3) == 0
54 and !in_array($var, array('id_table', 'id_base')))
55 $_GET[$var] = is_array($_GET[$var])?@array_map('intval', $_GET[$var]):intval($_GET[$var]);
56 foreach ($_POST as $var => $val)
57 if ($_POST[$var] and strncmp($var, "id_", 3) == 0
58 and !in_array($var, array('id_table', 'id_base')))
59 $_POST[$var] = is_array($_POST[$var])?@array_map('intval', $_POST[$var]):intval($_POST[$var]);
60 foreach ($GLOBALS as $var => $val)
61 if ($GLOBALS[$var] and strncmp($var, "id_", 3) == 0
62 and !in_array($var, array('id_table', 'id_base')))
63 $GLOBALS[$var] = is_array($GLOBALS[$var])?@array_map('intval', $GLOBALS[$var]):intval($GLOBALS[$var]);
64
65 /*
66 * Interdit la variable $cjpeg_command, qui était utilisée sans
67 * précaution dans certaines versions de dev (1.8b2 -> 1.8b5)
68 */
69 $cjpeg_command = '';
70
71 /*
72 * Contrôle de quelques variables (XSS)
73 */
74 foreach(array('lang', 'var_recherche', 'aide', 'var_lang_r', 'lang_r', 'var_ajax_ancre', 'nom_fichier') as $var) {
75 if (isset($_GET[$var]))
76 $_REQUEST[$var] = $GLOBALS[$var] = $_GET[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_GET[$var]);
77 if (isset($_POST[$var]))
78 $_REQUEST[$var] = $GLOBALS[$var] = $_POST[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_POST[$var]);
79 }
80
81 /*
82 * Filtre l'accès à spip_acces_doc (injection SQL en 1.8.2x)
83 */
84 if (preg_match(',^(.*/)?spip_acces_doc\.,', (string)$_SERVER['REQUEST_URI'])) {
85 $file = addslashes((string)$_GET['file']);
86 }
87
88 /*
89 * Pas d'inscription abusive
90 */
91 if (isset($_REQUEST['mode']) and isset($_REQUEST['page'])
92 and !in_array($_REQUEST['mode'], array("6forum", "1comite"))
93 and $_REQUEST['page'] == "identifiants")
94 $ecran_securite_raison = "identifiants";
95
96 /*
97 * Agenda joue à l'injection php
98 */
99 if (isset($_REQUEST['partie_cal'])
100 and $_REQUEST['partie_cal'] !== htmlentities((string)$_REQUEST['partie_cal']))
101 $ecran_securite_raison = "partie_cal";
102 if (isset($_REQUEST['echelle'])
103 and $_REQUEST['echelle'] !== htmlentities((string)$_REQUEST['echelle']))
104 $ecran_securite_raison = "echelle";
105
106 /*
107 * Espace privé
108 */
109 if (isset($_REQUEST['exec'])
110 and !preg_match(',^[\w-]+$,', (string)$_REQUEST['exec']))
111 $ecran_securite_raison = "exec";
112 if (isset($_REQUEST['cherche_auteur'])
113 and preg_match(',[<],', (string)$_REQUEST['cherche_auteur']))
114 $ecran_securite_raison = "cherche_auteur";
115 if (isset($_REQUEST['exec'])
116 and $_REQUEST['exec'] == 'auteurs'
117 and preg_match(',[<],', (string)$_REQUEST['recherche']))
118 $ecran_securite_raison = "recherche";
119 if (isset($_REQUEST['exec'])
120 and $_REQUEST['exec'] == 'info_plugin'
121 and preg_match(',[<],', (string)$_REQUEST['plugin']))
122 $ecran_securite_raison = "plugin";
123 if (isset($_REQUEST['exec'])
124 and $_REQUEST['exec'] == 'puce_statut'
125 and isset($_REQUEST['id'])
126 and !intval($_REQUEST['id']))
127 $ecran_securite_raison = "puce_statut";
128 if (isset($_REQUEST['action'])
129 and $_REQUEST['action'] == 'configurer') {
130 if (@file_exists('inc_version.php')
131 or @file_exists('ecrire/inc_version.php')) {
132 function action_configurer() {
133 include_spip('inc/autoriser');
134 if(!autoriser('configurer', _request('configuration'))) {
135 include_spip('inc/minipres');
136 echo minipres(_T('info_acces_interdit'));
137 exit;
138 }
139 require _DIR_RESTREINT.'action/configurer.php';
140 action_configurer_dist();
141 }
142 }
143 }
144
145 /*
146 * Bloque les requêtes contenant %00 (manipulation d'include)
147 */
148 if (strpos(
149 @get_magic_quotes_gpc() ?
150 stripslashes(serialize($_REQUEST)) : serialize($_REQUEST),
151 chr(0)
152 ) !== false)
153 $ecran_securite_raison = "%00";
154
155 /*
156 * Bloque les requêtes fond=formulaire_
157 */
158 if (isset($_REQUEST['fond'])
159 and preg_match(',^formulaire_,i', $_REQUEST['fond']))
160 $ecran_securite_raison = "fond=formulaire_";
161
162 /*
163 * Bloque les requêtes du type ?GLOBALS[type_urls]=toto (bug vieux php)
164 */
165 if (isset($_REQUEST['GLOBALS']))
166 $ecran_securite_raison = "GLOBALS[GLOBALS]";
167
168 /*
169 * Bloque les requêtes des bots sur:
170 * les agenda
171 * les paginations entremélées
172 */
173 if (_IS_BOT and (
174 (isset($_REQUEST['echelle']) and isset($_REQUEST['partie_cal']) and isset($_REQUEST['type']))
175 or (strpos((string)$_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string)$_SERVER['REQUEST_URI']))
176 )
177 )
178 $ecran_securite_raison = "robot agenda/double pagination";
179
180 /*
181 * Bloque une vieille page de tests de CFG (<1.11)
182 * Bloque un XSS sur une page inexistante
183 */
184 if (isset($_REQUEST['page'])) {
185 if ($_REQUEST['page'] == 'test_cfg')
186 $ecran_securite_raison = "test_cfg";
187 if ($_REQUEST['page'] !== htmlspecialchars((string)$_REQUEST['page']))
188 $ecran_securite_raison = "xsspage";
189 if ($_REQUEST['page'] == '404'
190 and isset($_REQUEST['erreur']))
191 $ecran_securite_raison = "xss404";
192 }
193
194 /*
195 * XSS par array
196 */
197 foreach (array('var_login') as $var)
198 if (isset($_REQUEST[$var]) and is_array($_REQUEST[$var]))
199 $ecran_securite_raison = "xss ".$var;
200
201 /*
202 * Parade antivirale contre un cheval de troie
203 */
204 if (!function_exists('tmp_lkojfghx')) {
205 function tmp_lkojfghx() {}
206 function tmp_lkojfghx2($a = 0, $b = 0, $c = 0, $d = 0) {
207 // si jamais on est arrivé ici sur une erreur php
208 // et qu'un autre gestionnaire d'erreur est défini, l'appeller
209 if ($b && $GLOBALS['tmp_xhgfjokl'])
210 call_user_func($GLOBALS['tmp_xhgfjokl'], $a, $b, $c, $d);
211 }
212 }
213 if (isset($_POST['tmp_lkojfghx3']))
214 $ecran_securite_raison = "gumblar";
215
216 /*
217 * Outils XML mal sécurisés < 2.0.9
218 */
219 if (isset($_REQUEST['transformer_xml']))
220 $ecran_securite_raison = "transformer_xml";
221
222 /*
223 * Outils XML mal sécurisés again
224 */
225 if (isset($_REQUEST['var_url']) and $_REQUEST['var_url'] and isset($_REQUEST['exec']) and $_REQUEST['exec']=='valider_xml'){
226 $url = trim($_REQUEST['var_url']);
227 if (strncmp($url,'/',1)==0
228 or (($p=strpos($url,'..'))!==false AND strpos($url,'..',$p+3)!==false)
229 or (strpos($url,'://')!==false or strpos($url,':\\')!==false)) {
230 $ecran_securite_raison = 'URL interdite pour var_url';
231 }
232 }
233
234 /*
235 * Sauvegarde mal securisée < 2.0.9
236 */
237 if (isset($_REQUEST['nom_sauvegarde'])
238 and strstr((string)$_REQUEST['nom_sauvegarde'], '/'))
239 $ecran_securite_raison = 'nom_sauvegarde manipulee';
240 if (isset($_REQUEST['znom_sauvegarde'])
241 and strstr((string)$_REQUEST['znom_sauvegarde'], '/'))
242 $ecran_securite_raison = 'znom_sauvegarde manipulee';
243
244
245 /*
246 * op permet des inclusions arbitraires ;
247 * on vérifie 'page' pour ne pas bloquer ... drupal
248 */
249 if (isset($_REQUEST['op']) and isset($_REQUEST['page'])
250 and $_REQUEST['op'] !== preg_replace('/[^\-\w]/', '', $_REQUEST['op']))
251 $ecran_securite_raison = 'op';
252
253 /*
254 * Forms & Table ne se méfiait pas assez des uploads de fichiers
255 */
256 if (count($_FILES)){
257 foreach($_FILES as $k => $v){
258 if (preg_match(',^fichier_\d+$,', $k)
259 and preg_match(',\.php,i', $v['name']))
260 unset($_FILES[$k]);
261 }
262 }
263 /*
264 * et Contact trop laxiste avec une variable externe
265 * on bloque pas le post pour eviter de perdre des donnees mais on unset la variable et c'est tout
266 */
267 if (isset($_REQUEST['pj_enregistrees_nom']) and $_REQUEST['pj_enregistrees_nom']){
268 unset($_REQUEST['pj_enregistrees_nom']);
269 unset($_GET['pj_enregistrees_nom']);
270 unset($_POST['pj_enregistrees_nom']);
271 }
272
273 /*
274 * reinstall=oui un peu trop permissif
275 */
276 if (isset($_REQUEST['reinstall'])
277 and $_REQUEST['reinstall'] == 'oui')
278 $ecran_securite_raison = 'reinstall=oui';
279
280 /*
281 * Échappement xss referer
282 */
283 if (isset($_SERVER['HTTP_REFERER']))
284 $_SERVER['HTTP_REFERER'] = strtr($_SERVER['HTTP_REFERER'], '<>"\'', '[]##');
285
286 /*
287 * Réinjection des clés en html dans l'admin r19561
288 */
289 if (strpos($_SERVER['REQUEST_URI'], "ecrire/") !== false){
290 $zzzz = implode("", array_keys($_REQUEST));
291 if (strlen($zzzz) != strcspn($zzzz, '<>"\''))
292 $ecran_securite_raison = 'Cle incorrecte en $_REQUEST';
293 }
294
295 /*
296 * Injection par connect
297 */
298 if (isset($_REQUEST['connect'])
299 and
300 // cas qui permettent de sortir d'un commentaire PHP
301 (strpos($_REQUEST['connect'], "?") !== false
302 or strpos($_REQUEST['connect'], "<") !== false
303 or strpos($_REQUEST['connect'], ">") !== false
304 or strpos($_REQUEST['connect'], "\n") !== false
305 or strpos($_REQUEST['connect'], "\r") !== false)
306 ) {
307 $ecran_securite_raison = "malformed connect argument";
308 }
309
310 /*
311 * S'il y a une raison de mourir, mourons
312 */
313 if (isset($ecran_securite_raison)) {
314 header("HTTP/1.0 403 Forbidden");
315 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
316 header("Cache-Control: no-cache, must-revalidate");
317 header("Pragma: no-cache");
318 header("Content-Type: text/html");
319 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>");
320 }
321
322 /*
323 * Un filtre filtrer_entites securise
324 */
325 if (!function_exists('filtre_filtrer_entites_dist')) {
326 function filtre_filtrer_entites_dist($t) {
327 include_spip('inc/texte');
328 return interdire_scripts(filtrer_entites($t));
329 }
330 }
331
332
333 /*
334 * Fin sécurité
335 */
336
337
338
339 /*
340 * Bloque les bots quand le load déborde
341 */
342 if (!defined('_ECRAN_SECURITE_LOAD'))
343 define('_ECRAN_SECURITE_LOAD', 4);
344
345 if (
346 defined('_ECRAN_SECURITE_LOAD')
347 and _ECRAN_SECURITE_LOAD > 0
348 and _IS_BOT
349 and $_SERVER['REQUEST_METHOD'] === 'GET'
350 and (
351 (function_exists('sys_getloadavg')
352 and $load = sys_getloadavg()
353 and is_array($load)
354 and $load = array_shift($load)
355 )
356 or
357 (@is_readable('/proc/loadavg')
358 and $load = file_get_contents('/proc/loadavg')
359 and $load = floatval($load)
360 )
361 )
362 and $load > _ECRAN_SECURITE_LOAD // eviter l'evaluation suivante si de toute facon le load est inferieur a la limite
363 and rand(0, $load * $load) > _ECRAN_SECURITE_LOAD * _ECRAN_SECURITE_LOAD
364 ) {
365 header("HTTP/1.0 503 Service Unavailable");
366 header("Retry-After: 300");
367 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
368 header("Cache-Control: no-cache, must-revalidate");
369 header("Pragma: no-cache");
370 header("Content-Type: text/html");
371 die("<html><title>Status 503: Site temporarily unavailable</title><body><h1>Status 503</h1><p>Site temporarily unavailable (load average $load)</p></body></html>");
372 }