Advertisement
1337_Brain

Proxy Block proxy checker Script

Jun 20th, 2016
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.49 KB | None | 0 0
  1.  
  2. <?php
  3. /*
  4.     --------------------
  5.     Proxy Block Script
  6.     --------------------
  7.  
  8.     Created by 1337 Brain
  9.     Date    2011/07/08
  10.     Version 1.0.4
  11.  
  12.     Requirements:
  13.     = PHP 5.2 or higher
  14.     = MySQL 5 or higher
  15.  
  16.     License: CC BY-SA 3.0
  17. */
  18.  
  19. function check_proxy()
  20. {
  21.     /*---------------------
  22.      * Configuration start
  23.      *--------------------*/
  24.  
  25.     // Database information
  26.     $db_hostname  = 'localhost';
  27.     $db_database  = 'proxydb';
  28.     $db_username  = 'username';
  29.     $db_password  = 'password';
  30.     $db_installed = false; // change to true after executing 1st time
  31.  
  32.     // Ports to check
  33.     $check_ports = true;
  34.     $ports = array(3128,8080);
  35.  
  36.     // Proxy headers
  37.     $check_headers = true;
  38.     $headers = array('HTTP_VIA', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP', 'HTTP_FORWARDED_FOR_IP', 'VIA', 'X_FORWARDED_FOR', 'FORWARDED_FOR', 'X_FORWARDED', 'FORWARDED', 'CLIENT_IP', 'FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION');
  39.  
  40.     // Banned
  41.     $banned_ips = array('193.200.150.');
  42.     $banned_useragents = array();
  43.  
  44.     // Allowed
  45.     $allowed_ips = array('127.0.0.');
  46.     $allowed_useragents = array('Googlebot','msnbot','Slurp');
  47.  
  48.     // Notes:
  49.     // You are able to ban/allow an IP range such as 1.0.0.0 -> 1.0.0.255
  50.     // by banning/allowing the IP "1.0.0."
  51.  
  52.     /*---------------------
  53.      * Configuration end
  54.      *--------------------*/
  55.  
  56.     // Init
  57.     error_reporting(0);
  58.     ini_set("default_socket_timeout",1);
  59.     $proxy     = false;
  60.     $userip    = (string) $_SERVER['REMOTE_ADDR'];
  61.     $useragent = (string) $_SERVER["HTTP_USER_AGENT"];
  62.  
  63.     // Fix configuration
  64.     if(!$check_ports)
  65.     {
  66.         $ports = array();
  67.     }
  68.     if(!$check_headers)
  69.     {
  70.         $headers = array();
  71.     }
  72.  
  73.     // Ban certain IPs
  74.     if( count($banned_ips) )
  75.     {
  76.         foreach($banned_ips as $ip)
  77.         {
  78.             $test = strpos($userip,$ip);
  79.  
  80.             if($test !== false && $test == 0)
  81.             {
  82.                 return true;
  83.             }        
  84.         }
  85.         unset($ip);
  86.     }
  87.  
  88.     // Ban certain User-Agents
  89.     if( count($banned_useragents) )
  90.     {
  91.         foreach($banned_useragents as $ua)
  92.         {
  93.             $test = strpos($useragent,$ua);
  94.  
  95.             if($test !== false)
  96.             {
  97.                 return true;
  98.             }    
  99.         }
  100.         unset($ua);
  101.     }
  102.  
  103.     // Allow certain IPs
  104.     if( count($allowed_ips) )
  105.     {
  106.         foreach($allowed_ips as $ip)
  107.         {
  108.             $test = strpos($userip,$ip);
  109.  
  110.             if($test !== false && $test == 0)
  111.             {
  112.                 return false;
  113.             }        
  114.         }
  115.         unset($ip);
  116.     }
  117.  
  118.     // Allow certain User-Agents
  119.     if( count($allowed_useragents) )
  120.     {
  121.         foreach($allowed_useragents as $ua)
  122.         {
  123.             $test = strpos($useragent,$ua);
  124.  
  125.             if($test !== false)
  126.             {
  127.                 return false;
  128.             }
  129.         }
  130.         unset($ua);
  131.     }
  132.  
  133.     // Check for proxy
  134.     if( count($ports) || count($headers) )
  135.     {    
  136.         // Connect and select database
  137.         $db_link = mysql_connect($db_hostname,$db_username,$db_password) or die(mysql_error());
  138.         mysql_select_db($db_database) or die(mysql_error());
  139.  
  140.         $db_setup = "CREATE TABLE IF NOT EXISTS `users` ( `ip` varchar(40) CHARACTER SET latin1 NOT NULL, `proxy` tinyint(1) NOT NULL, `time` DATETIME NOT NULL, UNIQUE KEY `ip` (`ip`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
  141.         $db_query = sprintf( "SELECT * FROM `users` WHERE `ip`='%s'",mysql_real_escape_string($userip) );
  142.  
  143.         // To select records created in the last 30 minutes
  144.         $db_query .= " AND `time` > DATE_SUB( NOW(), INTERVAL 30 MINUTE)";
  145.  
  146.         // Has database been initialized?
  147.         if( !$db_installed )
  148.         {
  149.             mysql_query($db_setup) or die(mysql_error());
  150.         }
  151.  
  152.         // Now query for the IP address
  153.         $db_result = mysql_query($db_query) or die(mysql_error());
  154.  
  155.         // Have we found it?
  156.         while ($row = mysql_fetch_assoc($db_result))
  157.         {
  158.             // No need for a port scan or check for headers here
  159.             return $row['proxy'];
  160.         }
  161.  
  162.         // Check for proxy headers
  163.         if( count($headers) )
  164.         {
  165.             foreach ($headers as $header)
  166.             {
  167.                 if( isset($_SERVER[$header]) )
  168.                 {
  169.                     $proxy = true;
  170.                     break;
  171.                 }
  172.             }
  173.         }
  174.  
  175.         // Do a port scan
  176.         if( !$proxy && count($ports) )
  177.         {
  178.             foreach($ports as $port)
  179.             {
  180.                 $test = fsockopen($userip,$port);
  181.  
  182.                 if($test !== false)
  183.                 {
  184.                     fclose($test);
  185.                     $proxy = true;
  186.                     break;
  187.                 }
  188.             }
  189.         }
  190.  
  191.         // Delete older result and insert new
  192.         $proxy = intval($proxy);
  193.         $db_delete_ip = sprintf( "DELETE FROM `users` WHERE `ip`='%s'",mysql_real_escape_string($userip) );
  194.         $db_insert_ip = sprintf( "INSERT INTO `users` VALUES ('%s','{$proxy}',NOW())",mysql_real_escape_string($userip) );
  195.         mysql_query($db_delete_ip) or die(mysql_error());
  196.         mysql_query($db_insert_ip) or die(mysql_error());
  197.     }
  198.  
  199.     // Return result
  200.     return $proxy;
  201. }
  202.  
  203. if( check_proxy() )
  204. {
  205.     die("<title>403: Forbidden</title>Oops... A proxy");
  206. }
  207. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement