/**
* AMA DataSet Limited
* Simon Mather
* 2006-10-13
*
* This is the CMS bootstrap
*
* 2008-03-18 -- Created MAN10 to WEB work
* 2008-10-21 -- Updated to include PAGassign via the dn control
* 2011-02-21 -- Update to bootstrap for all servers
* 2011-03-12 -- Applied to CMS40
* 2011-12-19 -- Added anti hacking
* 2012-01-19 -- Add facility to switch version on the fly
* 2012-01-20 -- Made into an Generic Index
* 2012-01-21 -- Create auto insert of VERSIONing within the call in links
* 2013-02-16 -- SLM - Add AMA Signature
* 2013-03-10 -- Added Click for domains
* 2013-05-01 -- SLM - Added to avoid iFrame
* 2013-10-26 -- Added check for SQL Injection and XSS
* 2014-03-01 -- SLM - Tidy, Added: Error STrings; Check for stystem directories
* 2014-03-03 -- SLM - Added "_INDEX_CALL_CHECK" forlockdown of calling pages
* 2014-04-17 -- SLM - Pulled out the System vars into `_config.php`
* 2014-05-13 -- SLM - Added SCC blocker
* 2015-02-01 -- SLM - Updated to take into account click timestamp and click web server
* 2015-08-02 -- SLM - Added http_500 and http_404 functions
* 2015-09-13 -- SLM - Updated as per AMA CMS2
* 2015-11-22 -- SLM - Tidy up
* 2015-12-12 -- Additional Security and move 404/500 logs to security location
* 2016-06-30 -- SLM - Added DN redirect and SLL redirect as 301 rather than the default 302
* 2017-03-31 -- SLM - Add exetension `php` to omittion of SSL redirect
* 2017-06-29 -- SLM - Convert `+` in URI to URL encode %2B
* 2017-11-20 -- SLM - Extra security around `cms\..\..\..\` since it was possible to list files due to the way files are included
* 2017-11-21 -- SLM - Removed --> str_replace("'", "|x|", $_SERVER['REQUEST_URI']);
* 2018-06-04 -- SLM - Added omission for redirect if the calling server is a PAG
* 2018-08-14 -- SLM - Updated security files to have `.log` as an extension
* 2021-03-10 -- SLM - Stop double encoded URL i.e. `../` --> `%252e%252e%252f`; `%253C`; `%253E`
* 2022-04-01 -- SLM - Remove all <> from GET's for %3C `<` and %3E `>`
* 2022-04-01 -- SLM - Remove any open/close on the URI MRS PEN test 07-03-2018
* 2022-08-26 -- SLM - Remove any UTF8 encoding from the URL for the SSL redirect
* 2022-08-26 -- SLM - Add a Dynamic URL generator -- https://dev-42-glowm_www4.cms10.ama.uk.com --> dev-[VERSION]-[SITENAME].[SERVER].ama.uk.com
* 2023-01-22 -- SLM - Add NECRO to the system; Line 2 - 2023-03-15 -- IB
* 2023-05-11 -- IB - Added new log function and more filters for url security
* 2023-09-29 -- SLM - Reduce PHP Errors -- Changed `root_bb_real_escape_string($_SERVER['HTTP_USER_AGENT'])` for
* `htmlspecialchars($_SERVER['HTTP_USER_AGENT'])` within the fn `bootstrap_mail_withauth`
* 2023-10-24 -- SLM - Added `date("Y-m-d").` to the Security logging filename
* 2024-04-16 -- SLM - Check if the query is an array, if so, null the var
* 2024-04-23 -- SLM - Add fn `getallheaders` for PHP-FPM setup on new Servers
* 2024-04-23 -- SLM - 2.00 - Generalise all bootloader to version 2.0
* 2024-04-29 -- SLM - 2.01 - Updatede `http_sanitizer` to `http_sanitizer_log`, so to file contents and for MARK II to monitor
* 2024-10-08 -- IB - Added date prefix to url security logs
* 2025-04-08 -- SLM - Added option for `web50` to be added to `Dynamic URL generator`
* 2026-04-25 -- SLM - Added Virgin and local ip to stop any rediredt when proofing
*/
##
## Setup contacts
##
define("_INDEX_CALL_CHECK", "okay-to-go");
date_default_timezone_set('UTC');
##
## Top Level Blocker
##
include("/var/www/cms/_sys/http_necro/http_necro.php");
##
## Error Listing
##
$_SESSION['error']['001'] = "001 - Connect Error - Primary bootstrap";
$_SESSION['error']['002'] = "002 - Select Error - Strata Database controller";
$_SESSION['error']['003'] = "003 - Query Error - DN Controller";
$_SESSION['error']['004'] = "004 - Connect Error - Access to Site Name not found. Check for OLD pwd";
$_SESSION['error']['005'] = "005 - Select Error - Site Name";
$_SESSION['error']['006'] = "006 - Connect Error to `globals` [inc_db]";
$_SESSION['error']['007'] = "007 - Sorry this IP has been blocked:";
##
## Function
## 2014-05-13 - SLM
## 2017-11-21 - SLM - update to include alternative mailing address
function bootstrap_mail_withauth($mail_subject, $mail_body, $mail_address="errorlist@ama.uk.com") { ## 2017-11-21 - SLM - Added `$mail_address`
include_once "/var/www/cms/_sys/smtp/class.phpmailer.php";
include_once "/var/www/cms/_sys/smtp/class.smtp.php";
$mail_body .= "
".print_r($_SERVER, true)."
".print_r($_POST, true)."
".print_r($_GET, true)."
";
$text .= "{$date} {$_SERVER['REQUEST_METHOD']} IP:{$_SERVER['REMOTE_ADDR']} URL:{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}\n";
$text .= "\$_SESSION['_SQL_SERVER']:'{$_SESSION['_SQL_SERVER']}'\n";
$text .= "\$_SESSION['_SQL_SERVER_CMS']:'{$_SESSION['_SQL_SERVER_CMS']}'\n";
$text .= "\$_SESSION['_SITE_NAME']:'{$_SESSION['_SITE_NAME']}'\n";
$text .= "\$_SESSION['_SQL_SERVER_PWD']:'{$_SESSION['_SQL_SERVER_PWD']}'\n";
$text .= "\$_SESSION['_SQL_TYPE']:'{$_SESSION['_SQL_TYPE']}'\n";
$text .= "__MYSQL_PORT__:'".__MYSQL_PORT__."'\n";
$text .= "message:{$string}\n";
$text .= "mysql_error:".root_bb_error();
$text .= "\n\n";
$text .= "";
file_put_contents($file, $text, FILE_APPEND);
bootstrap_mail_withauth($string, $text);
exit;
}
function http_sanitizer_log($string=NULL) {
## Build filename
$filePathName = __PATH_LOG_SANITIZER__.date("Y-m-d")."-"."{$_SERVER['HTTP_HOST']}.log";
## Create File with correct perms
if(!file_exists($filePath)) {
touch($filePathName);
chmod($filePathName, 0660);
chown($filePathName, 33);
chgrp($filePathName, 5001);
}
## Build contents
$line = str_pad( $_SERVER['REMOTE_ADDR'], 16)."| ";
$line .= str_pad( $_SERVER['SERVER_PORT'], 4, " ")."| ";
$line .= str_pad( $_SERVER['REQUEST_METHOD'], 5, " ")."| ";
### $line .= str_pad( $_SERVER['HTTP_HOST'], 32, " ")."| ";
$line .= str_pad( $string, 48, " ")."| ";
$line .= $_SERVER['REQUEST_URI']."| ";
$line .= $_SERVER['HTTP_USER_AGENT'];
## Save all feeds
file_put_contents($filePathName, date("Y-m-d\TH:i:s")." | ".$line."\n", FILE_APPEND);
}
## 2023-05-11 - IB -- added for pen test
function url_replacement_log($matched_string, $URL){
$dateTime = date("Y-m-d");
$text = date("Y-m-d H:i:s")." - URL SECURITY ISSUE: matched - {$matched_string} : URL - {$URL} : IP - {$_SERVER['REMOTE_ADDR']}\n";
file_put_contents(__PATH_LOG_SECURITY__."{$dateTime}--BL-URL-replacement--".$_SERVER['HTTP_HOST'].".log", $text, FILE_APPEND);
}
##
## Bring in Config File
##
global $_SITE_INDEX, $_SQL_SERVER, $_PAG_SERVER, $_WEB_SERVER, $SERVER_NAME; ## Legacy
include_once("/var/www/cms/_config.php"); ## Load Config Details
## Deafault the Error logs prior to the SITENAME
$dateTime = date("Y-m-d");
$path = __PATH_LOG_PHP_ERROR__."{$dateTime}--BOOTLOADER.log";
ini_set("error_log", __PATH_LOG_PHP_ERROR__."{$dateTime}--BOOTLOADER.log");
include_once("/var/www/cms/_sys/sql_blackbox/root.php"); ## Load SQL commands for Bootloader
include_once("/var/www/cms/_sys/error_handler.php"); ## Load PHP Error Logger
$_SESSION['_SQL_SERVER'] = $_SQL_SERVER; ## 2011-02-21 - SLM -- Default SQL, used to get DN controller (removed web20.ama.uk.com)
$_SESSION['_SQL_SERVER_CMS'] = $_SQL_SERVER_CMS;
$_SESSION['_SQL_SERVER_PWD'] = $_SQL_SERVER_PWD; ## 2011-02-21 - SLM -- Default SQL since place PWD
$_SESSION['_PAG_SERVER'] = $_PAG_SERVER; ## Default added 2008-10-21 --- London 9 pagination server
$_SESSION['_WEB_SERVER'] = $_WEB_SERVER; ## 2011-02-21 - SLM -- Change for the working server, WEBXX
##
## Check for SQL Injection and XSS
##
## 2021-03-10 -- SLM - Stop double encoded URL i.e. `../` --> `%252e%252e%252f`; `%253C`; `%253E`
if(strpos( strtoupper($_SERVER['REQUEST_URI']), "%252E%252E%252F")) {$security_error='Double encoded `../`';}
if(strpos( strtoupper($_SERVER['REQUEST_URI']), "%253C")) {$security_error='Double encoded `<`';}
if(strpos( strtoupper($_SERVER['REQUEST_URI']), "%253E")) {$security_error='Double encoded `>`';}
$_SERVER['REQUEST_URI'] = str_replace("+", "%2B", $_SERVER['REQUEST_URI']); ## 2017-06-29 - SLM -- Convert `+` in URI to URL encode %2B
$_SERVER['REQUEST_URI'] = urldecode($_SERVER['REQUEST_URI']);
## 2021-03-18 - SLM -- Remove all <> from GET's for %3C `<` and %3E `>`
## e.g. https://dev-mrs.dev10.ama.uk.com/?test=%3Cimg%20src%3da%20onerror%3dalert(123)%3E
## 2022-03-30 - SLM Remove all <> from KEY for %3C `<` and %3E `>` and rebuild REQUEST_URI
## e.g. https://www.research-live.com/?%22%3e%3ciYaJs%3c=1
## 2024-06-24 - SLM -- Check for an Ajax call, allow `parameters` since this is an array
foreach( $_GET AS $name => $value) {
## 2024-04-16 - SLM -- Check if the query is an array, if so null the var
if(is_array($value)) {
## Check that is not from AMA Ajax
if($name <> 'parameters') unset($_GET[$name]);
}
else {
$value = urldecode($value);
if(preg_match("#<(.*?)>#", $value)) {
$valueNew = preg_replace("#<(.*?)>#", "$1", $value);
http_sanitizer_log("{$name}={$value}->{$valueNew}");
$_GET[$name] = $valueNew;
}
}
}
## $_SERVER['REQUEST_URI'] = str_replace("'", "|x|", $_SERVER['REQUEST_URI']); ## 2015-12-09 - SLM -- Remove Quote -- 2017-11-21 - SLM -- Removed
## 2018-03-22 Line 1; 2022-04-01 Line 2 - SLM -- Remove any open/close on the URI MRS PEN test 07-03-2018
$_SERVER['REQUEST_URI'] = preg_replace("#<(.*?)>#", "$1", $_SERVER['REQUEST_URI']);
## 2023-05-11 - IB -- Added for pen test
if(preg_match("#<|>#", $_SERVER['REQUEST_URI'])){
url_replacement_log("#<|>#", $_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = preg_replace("#<|>#", "", $_SERVER['REQUEST_URI']);
}
## 2023-05-11 - IB -- Added for pen test
if(preg_match("#'#", $_SERVER['REQUEST_URI'])){
url_replacement_log("#'#", $_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = preg_replace("#'#", "", $_SERVER['REQUEST_URI']);
}
$boot_inj_URI = strtoupper(urldecode($_SERVER['REQUEST_URI']));
$boot_inj_URI_strip = str_replace(" ", "", $boot_inj_URI);
if(strpos($boot_inj_URI_strip, 'INFORMATION_SCHEMA')) {$security_error='INFORMATION_SCHEMA';} else
if(strpos($boot_inj_URI_strip, 'DELAY(')) {$security_error='DELAY(';}
if(strpos($boot_inj_URI_strip, 'SLEEP(')) {$security_error='SLEEP(';}
if(strpos($boot_inj_URI_strip, 'CONCAT(')) {$security_error='CONCAT(';}
if(strpos($boot_inj_URI_strip, 'BENCHMARK(')) {$security_error='BENCHMARK(';}
if(strpos($boot_inj_URI_strip, 'UNION/*')) {$security_error='UNION/*';}
if(strpos($boot_inj_URI_strip, 'SELECT/*')) {$security_error='SELECT/*';}
#if(strpos($boot_inj_URI, 'DELAY ')) {$security_error='DELAY ';}
#if(strpos($boot_inj_URI, 'WAITFOR ')) {$security_error='WAITFOR ';}
if(strpos($boot_inj_URI, 'SELECT ')) {$security_error='SELECT ';}
if(strpos($boot_inj_URI, 'UNION ')) {$security_error='UNION *';}
if(strpos($boot_inj_URI, '