[SPIP] ~v3.0.21-->v3.0.22
[lhc/web/www.git] / www / config / ecran_securite.php
1 <?php
2
3 /*
4 * ecran_securite.php
5 * ------------------
6 */
7
8 define('_ECRAN_SECURITE', '1.2.4'); // 2016-03-10
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|facebook|flipboard|hootsuite|FunWebProducts|Google|Genieo|INA dlweb|InfegyAtlas|Java VM|LiteFinder|Lycos|MetaURI|Moreover|Rambler|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') 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['action'])
120 and $_REQUEST['action'] == 'configurer') {
121 if (@file_exists('inc_version.php')
122 or @file_exists('ecrire/inc_version.php')) {
123 function action_configurer() {
124 include_spip('inc/autoriser');
125 if(!autoriser('configurer', _request('configuration'))) {
126 include_spip('inc/minipres');
127 echo minipres(_T('info_acces_interdit'));
128 exit;
129 }
130 require _DIR_RESTREINT.'action/configurer.php';
131 action_configurer_dist();
132 }
133 }
134 }
135
136 /*
137 * Bloque les requêtes contenant %00 (manipulation d'include)
138 */
139 if (strpos(
140 @get_magic_quotes_gpc() ?
141 stripslashes(serialize($_REQUEST)) : serialize($_REQUEST),
142 chr(0)
143 ) !== false)
144 $ecran_securite_raison = "%00";
145
146 /*
147 * Bloque les requêtes fond=formulaire_
148 */
149 if (isset($_REQUEST['fond'])
150 and preg_match(',^formulaire_,i', $_REQUEST['fond']))
151 $ecran_securite_raison = "fond=formulaire_";
152
153 /*
154 * Bloque les requêtes du type ?GLOBALS[type_urls]=toto (bug vieux php)
155 */
156 if (isset($_REQUEST['GLOBALS']))
157 $ecran_securite_raison = "GLOBALS[GLOBALS]";
158
159 /*
160 * Bloque les requêtes des bots sur:
161 * les agenda
162 * les paginations entremélées
163 */
164 if (_IS_BOT and (
165 (isset($_REQUEST['echelle']) and isset($_REQUEST['partie_cal']) and isset($_REQUEST['type']))
166 or (strpos((string)$_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string)$_SERVER['REQUEST_URI']))
167 )
168 )
169 $ecran_securite_raison = "robot agenda/double pagination";
170
171 /*
172 * Bloque une vieille page de tests de CFG (<1.11)
173 * Bloque un XSS sur une page inexistante
174 */
175 if (isset($_REQUEST['page'])) {
176 if ($_REQUEST['page'] == 'test_cfg')
177 $ecran_securite_raison = "test_cfg";
178 if ($_REQUEST['page'] !== htmlspecialchars((string)$_REQUEST['page']))
179 $ecran_securite_raison = "xsspage";
180 if ($_REQUEST['page'] == '404'
181 and isset($_REQUEST['erreur']))
182 $ecran_securite_raison = "xss404";
183 }
184
185 /*
186 * XSS par array
187 */
188 foreach (array('var_login') as $var)
189 if (isset($_REQUEST[$var]) and is_array($_REQUEST[$var]))
190 $ecran_securite_raison = "xss ".$var;
191
192 /*
193 * Parade antivirale contre un cheval de troie
194 */
195 if (!function_exists('tmp_lkojfghx')) {
196 function tmp_lkojfghx() {}
197 function tmp_lkojfghx2($a = 0, $b = 0, $c = 0, $d = 0) {
198 // si jamais on est arrivé ici sur une erreur php
199 // et qu'un autre gestionnaire d'erreur est défini, l'appeller
200 if ($b && $GLOBALS['tmp_xhgfjokl'])
201 call_user_func($GLOBALS['tmp_xhgfjokl'], $a, $b, $c, $d);
202 }
203 }
204 if (isset($_POST['tmp_lkojfghx3']))
205 $ecran_securite_raison = "gumblar";
206
207 /*
208 * Outils XML mal sécurisés < 2.0.9
209 */
210 if (isset($_REQUEST['transformer_xml']))
211 $ecran_securite_raison = "transformer_xml";
212
213 /*
214 * Sauvegarde mal securisée < 2.0.9
215 */
216 if (isset($_REQUEST['nom_sauvegarde'])
217 and strstr((string)$_REQUEST['nom_sauvegarde'], '/'))
218 $ecran_securite_raison = 'nom_sauvegarde manipulee';
219 if (isset($_REQUEST['znom_sauvegarde'])
220 and strstr((string)$_REQUEST['znom_sauvegarde'], '/'))
221 $ecran_securite_raison = 'znom_sauvegarde manipulee';
222
223
224 /*
225 * op permet des inclusions arbitraires ;
226 * on vérifie 'page' pour ne pas bloquer ... drupal
227 */
228 if (isset($_REQUEST['op']) and isset($_REQUEST['page'])
229 and $_REQUEST['op'] !== preg_replace('/[^\-\w]/', '', $_REQUEST['op']))
230 $ecran_securite_raison = 'op';
231
232 /*
233 * Forms & Table ne se méfiait pas assez des uploads de fichiers
234 */
235 if (count($_FILES)){
236 foreach($_FILES as $k => $v){
237 if (preg_match(',^fichier_\d+$,', $k)
238 and preg_match(',\.php,i', $v['name']))
239 unset($_FILES[$k]);
240 }
241 }
242 /*
243 * et Contact trop laxiste avec une variable externe
244 * on bloque pas le post pour eviter de perdre des donnees mais on unset la variable et c'est tout
245 */
246 if (isset($_REQUEST['pj_enregistrees_nom']) and $_REQUEST['pj_enregistrees_nom']){
247 unset($_REQUEST['pj_enregistrees_nom']);
248 unset($_GET['pj_enregistrees_nom']);
249 unset($_POST['pj_enregistrees_nom']);
250 }
251
252 /*
253 * reinstall=oui un peu trop permissif
254 */
255 if (isset($_REQUEST['reinstall'])
256 and $_REQUEST['reinstall'] == 'oui')
257 $ecran_securite_raison = 'reinstall=oui';
258
259 /*
260 * Échappement xss referer
261 */
262 if (isset($_SERVER['HTTP_REFERER']))
263 $_SERVER['HTTP_REFERER'] = strtr($_SERVER['HTTP_REFERER'], '<>"\'', '[]##');
264
265 /*
266 * Réinjection des clés en html dans l'admin r19561
267 */
268 if (strpos($_SERVER['REQUEST_URI'], "ecrire/") !== false){
269 $zzzz = implode("", array_keys($_REQUEST));
270 if (strlen($zzzz) != strcspn($zzzz, '<>"\''))
271 $ecran_securite_raison = 'Cle incorrecte en $_REQUEST';
272 }
273
274 /*
275 * Injection par connect
276 */
277 if (isset($_REQUEST['connect'])
278 and
279 // cas qui permettent de sortir d'un commentaire PHP
280 (strpos($_REQUEST['connect'], "?") !== false
281 or strpos($_REQUEST['connect'], "<") !== false
282 or strpos($_REQUEST['connect'], ">") !== false
283 or strpos($_REQUEST['connect'], "\n") !== false
284 or strpos($_REQUEST['connect'], "\r") !== false)
285 ) {
286 $ecran_securite_raison = "malformed connect argument";
287 }
288
289 /*
290 * S'il y a une raison de mourir, mourons
291 */
292 if (isset($ecran_securite_raison)) {
293 header("HTTP/1.0 403 Forbidden");
294 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
295 header("Cache-Control: no-cache, must-revalidate");
296 header("Pragma: no-cache");
297 header("Content-Type: text/html");
298 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>");
299 }
300
301 /*
302 * Un filtre filtrer_entites securise
303 */
304 if (!function_exists('filtre_filtrer_entites_dist')) {
305 function filtre_filtrer_entites_dist($t) {
306 include_spip('inc/texte');
307 return interdire_scripts(filtrer_entites($t));
308 }
309 }
310
311
312 /*
313 * Fin sécurité
314 */
315
316
317
318 /*
319 * Bloque les bots quand le load déborde
320 */
321 if (!defined('_ECRAN_SECURITE_LOAD'))
322 define('_ECRAN_SECURITE_LOAD', 4);
323
324 if (
325 defined('_ECRAN_SECURITE_LOAD')
326 and _ECRAN_SECURITE_LOAD > 0
327 and _IS_BOT
328 and $_SERVER['REQUEST_METHOD'] === 'GET'
329 and (
330 (function_exists('sys_getloadavg')
331 and $load = sys_getloadavg()
332 and is_array($load)
333 and $load = array_shift($load)
334 )
335 or
336 (@is_readable('/proc/loadavg')
337 and $load = file_get_contents('/proc/loadavg')
338 and $load = floatval($load)
339 )
340 )
341 and $load > _ECRAN_SECURITE_LOAD // eviter l'evaluation suivante si de toute facon le load est inferieur a la limite
342 and rand(0, $load * $load) > _ECRAN_SECURITE_LOAD * _ECRAN_SECURITE_LOAD
343 ) {
344 header("HTTP/1.0 503 Service Unavailable");
345 header("Retry-After: 300");
346 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
347 header("Cache-Control: no-cache, must-revalidate");
348 header("Pragma: no-cache");
349 header("Content-Type: text/html");
350 die("<html><title>Status 503: Site temporarily unavailable</title><body><h1>Status 503</h1><p>Site temporarily unavailable (load average $load)</p></body></html>");
351 }