Tutorial Parte III [XFACIL] Segurança - Protegendo o mainfile
Parte III: Protegendo o mainfile.php
Relembrando...
O mainfile.php é o arquivo que guarda toda a informação essencial do seu site, incluídas aí a senha e nome do usuário do seu banco de dados. E o nome do banco de dados, e o path [caminho físico] do seu site! Se você percebeu, ao editá-lo para instalar o protector, uma boa parte do que existe de fundamental a ser protegido está ali.E todos que já operaram com XOOPS SABEM que o mainfile está na raiz do seu site. Ou deveria estar. Mais uma razão para nos perguntarmos ... que tal tirar o mainfile da possibilidade de ser acessado pela WEB?
Movendo o mainfile
Na verdade é muito simples... Faça uma cópia do seu arquivo mainfile.php usado no site, e coloque essa cópia para a pasta 'xtrustpath'[veja Parte II].
Depois, substitua todo o conteúdo do mainfile.php original por :
Assim o mainfile.php que fica no xoops vai servir apenas como atalho para buscar o "verdadeiro" mainfile ... que está fora da web! Simples não?
'Evolução' de um mainfile.php de exemplo: abaixo os detalhes...
Usaremos para esse exemplo um site que está hospedado em um servidor com cPanel:
O login do cPanel será : seulogincpanel ;
Banco de Dados: nome: nomebd ;
Usuário e senha desse banco de dados: nomeusuariobd e senhausuariobd;
Path: /home/seulogincpanel/public_html
O site é: http://meusite.com.br
Mainfile antes de qualquer alteração - fizemos a instalação normal do XOOPS e ele está assim:
Código PHP:
if ( !defined("XOOPS_MAINFILE_INCLUDED") ) { define("XOOPS_MAINFILE_INCLUDED",1); // XOOPS Physical Path // Physical path to your main XOOPS directory WITHOUT trailing slash // Example: define('XOOPS_ROOT_PATH', '/home/daeqhos/public_html'); define('XOOPS_ROOT_PATH', '/home/seulogincpanel/public_html');
// XOOPS Virtual Path (URL) // Virtual path to your main XOOPS directory WITHOUT trailing slash // Example: define('XOOPS_URL', 'http://meusite.com.br'); define('XOOPS_URL', 'http://meusite.com.br'); define('XOOPS_CHECK_PATH', 1);
// Protect against external scripts execution if safe mode is not enabled if ( XOOPS_CHECK_PATH && !@ini_get('safe_mode') ) { if ( function_exists('debug_backtrace') ) { $xoopsScriptPath = debug_backtrace(); if ( !count($xoopsScriptPath) ) { die("XOOPS path check: this file cannot be requested directly"); } $xoopsScriptPath = $xoopsScriptPath[0]['file']; } else { $xoopsScriptPath = isset($_SERVER['PATH_TRANSLATED']) ? $_SERVER['PATH_TRANSLATED'] : $_SERVER['SCRIPT_FILENAME']; } if ( DIRECTORY_SEPARATOR != '/' ) { // IIS6 may double the \ chars $xoopsScriptPath = str_replace( strpos( $xoopsScriptPath, '\\\\', 2 ) ? '\\\\' : DIRECTORY_SEPARATOR, '/', $xoopsScriptPath); } if ( strcasecmp( substr($xoopsScriptPath, 0, strlen(XOOPS_ROOT_PATH)), str_replace( DIRECTORY_SEPARATOR, '/', XOOPS_ROOT_PATH)) ) { exit("XOOPS path check: Script is not inside XOOPS_ROOT_PATH and cannot run."); } } // Database // Choose the database to be used define('XOOPS_DB_TYPE', 'mysql'); // Table Prefix // This prefix will be added to all new tables created to avoid name conflict in the database. If you are unsure, just use the default 'xoops'. define('XOOPS_DB_PREFIX', 'xoops'); // Database Hostname // Hostname of the database server. If you are unsure, 'localhost' works in most cases. define('XOOPS_DB_HOST', 'localhost'); // Database Username // Your database user account on the host define('XOOPS_DB_USER', 'seulogincpanel_nomedousuariobd'); // Database Password // Password for your database user account define('XOOPS_DB_PASS', 'senhausuariobd'); // Database Name // The name of database on the host. The installer will attempt to create the database if not exist define('XOOPS_DB_NAME', 'nomebd'); // Use persistent connection? (Yes=1 No=0) // Default is 'Yes'. Choose 'Yes' if you are unsure. define('XOOPS_DB_PCONNECT', 0); define('XOOPS_GROUP_ADMIN', '1'); define('XOOPS_GROUP_USERS', '2'); define('XOOPS_GROUP_ANONYMOUS', '3'); foreach ( array('GLOBALS', '_SESSION', 'HTTP_SESSION_VARS', '_GET', 'HTTP_GET_VARS', '_POST', 'HTTP_POST_VARS', '_COOKIE', 'HTTP_COOKIE_VARS', '_REQUEST', '_SERVER', 'HTTP_SERVER_VARS', '_ENV', 'HTTP_ENV_VARS', '_FILES', 'HTTP_POST_FILES', 'xoopsDB', 'xoopsUser', 'xoopsUserId', 'xoopsUserGroups', 'xoopsUserIsAdmin', 'xoopsConfig', 'xoopsOption', 'xoopsModule', 'xoopsModuleConfig', 'xoopsRequestUri') as $bad_global ) { if ( isset( $_REQUEST[$bad_global] ) ) { header( 'Location: '.XOOPS_URL.'/' ); exit(); } } if (!isset($xoopsOption['nocommon']) && XOOPS_ROOT_PATH != '') { include XOOPS_ROOT_PATH."/include/common.php"; } } ?>
Com todas as alterações: XOOPS_TRUS_PATH criado como pasta 'aleluia' FORA da WEB...
Código PHP:
<?php
if ( !defined("XOOPS_MAINFILE_INCLUDED") ) { define("XOOPS_MAINFILE_INCLUDED",1); // XOOPS Physical Path // Physical path to your main XOOPS directory WITHOUT trailing slash // Example: define('XOOPS_ROOT_PATH', '/home/daeqhos/public_html'); define('XOOPS_ROOT_PATH', '/home/seulogincpanel/public_html'); // definindo o XOOPS_TRUST_PATH - basico para o protector define('XOOPS_TRUST_PATH','/home/seulogincpanel/aleulia'); // XOOPS Virtual Path (URL) // Virtual path to your main XOOPS directory WITHOUT trailing slash // Example: define('XOOPS_URL', 'http://meusite.com.br'); define('XOOPS_URL', 'http://meusite.com.br'); define('XOOPS_CHECK_PATH', 1); // Protect against external scripts execution if safe mode is not enabled if ( XOOPS_CHECK_PATH && !@ini_get('safe_mode') ) { if ( function_exists('debug_backtrace') ) { $xoopsScriptPath = debug_backtrace(); if ( !count($xoopsScriptPath) ) { die("XOOPS path check: this file cannot be requested directly"); } $xoopsScriptPath = $xoopsScriptPath[0]['file']; } else { $xoopsScriptPath = isset($_SERVER['PATH_TRANSLATED']) ? $_SERVER['PATH_TRANSLATED'] : $_SERVER['SCRIPT_FILENAME']; } if ( DIRECTORY_SEPARATOR != '/' ) { // IIS6 may double the \ chars $xoopsScriptPath = str_replace( strpos( $xoopsScriptPath, '\\\\', 2 ) ? '\\\\' : DIRECTORY_SEPARATOR, '/', $xoopsScriptPath); } if ( strcasecmp( substr($xoopsScriptPath, 0, strlen(XOOPS_ROOT_PATH)), str_replace( DIRECTORY_SEPARATOR, '/', XOOPS_ROOT_PATH)) ) { exit("XOOPS path check: Script is not inside XOOPS_ROOT_PATH and cannot run."); } } // Database // Choose the database to be used define('XOOPS_DB_TYPE', 'mysql'); // Table Prefix // This prefix will be added to all new tables created to avoid name conflict in the database. If you are unsure, just use the default 'xoops'. define('XOOPS_DB_PREFIX', 'xoops'); // Database Hostname // Hostname of the database server. If you are unsure, 'localhost' works in most cases. define('XOOPS_DB_HOST', 'localhost'); // Database Username // Your database user account on the host define('XOOPS_DB_USER', 'seulogincpanel_nomedousuariobd'); // Database Password // Password for your database user account define('XOOPS_DB_PASS', 'senhausuariobd'); // Database Name // The name of database on the host. The installer will attempt to create the database if not exist define('XOOPS_DB_NAME', 'nomebd'); // Use persistent connection? (Yes=1 No=0) // Default is 'Yes'. Choose 'Yes' if you are unsure. define('XOOPS_DB_PCONNECT', 0); define('XOOPS_GROUP_ADMIN', '1'); define('XOOPS_GROUP_USERS', '2'); define('XOOPS_GROUP_ANONYMOUS', '3'); foreach ( array('GLOBALS', '_SESSION', 'HTTP_SESSION_VARS', '_GET', 'HTTP_GET_VARS', '_POST', 'HTTP_POST_VARS', '_COOKIE', 'HTTP_COOKIE_VARS', '_REQUEST', '_SERVER', 'HTTP_SERVER_VARS', '_ENV', 'HTTP_ENV_VARS', '_FILES', 'HTTP_POST_FILES', 'xoopsDB', 'xoopsUser', 'xoopsUserId', 'xoopsUserGroups', 'xoopsUserIsAdmin', 'xoopsConfig', 'xoopsOption', 'xoopsModule', 'xoopsModuleConfig', 'xoopsRequestUri') as $bad_global ) { if ( isset( $_REQUEST[$bad_global] ) ) { header( 'Location: '.XOOPS_URL.'/' ); exit(); } } include XOOPS_TRUST_PATH.'/modules/protector/include/precheck.inc.php' ; if (!isset($xoopsOption['nocommon']) && XOOPS_ROOT_PATH != '' ) { include XOOPS_ROOT_PATH."/include/common.php"; } include XOOPS_TRUST_PATH.'/modules/protector/include/postcheck.inc.php' ; }} ?>
E finalmente com a transmutação do mainfile [e copia do original para a pasta 'aleluia' - NÃO ESQUEÇA DE FAZER BACKUP], temos :
"The Kingdom of God is within you and all around you, not in mansions of wood and stone. Split a piece of wood and I am there; lift a stone and you will find me."
"O reino de Deus esta dentro de você e ao seu redor, não em construções de madeira e pedra. Lasque um pedaço de madeira e estarei lá; erga uma pedra e me encontrarás"