Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. <?php
  2.  
  3. $config = parse_ini_file( "config.ini", true ) or die( 'Config.ini Parse Fail' );
  4. define( 'CACHE_DIR', $config['main']['cache_dir'] );
  5.  
  6. session_start();
  7.  
  8. $allowed_methods = array_map( 'trim', explode( ',', $config['input']['allowed_methods'] ) );
  9. $proxies_headers = ( $config['proxies']['proxies_protection'] ) ? array_map( 'trim', explode( ',', $config['proxies']['proxies_headers'] ) ) : array();
  10.  
  11. $user_ip = $_SERVER['REMOTE_ADDR'];
  12. $query_string = urldecode( $_SERVER['QUERY_STRING'] );
  13.  
  14. if ( $config['resources']['cpu_loadavg_protect'] )
  15. {
  16. if ( get_server_load() >= $config['resources']['cpu_loadavg_limit'] )
  17. {
  18. echo $config['resources']['message_exit'];
  19. exit;
  20. }
  21. }
  22.  
  23. if ( $config['ddos']['protect_ddos'] )
  24. {
  25. $user_file = CACHE_DIR . $user_ip;
  26.  
  27. if ( file_exists( $user_file ) )
  28. {
  29. $flood_row = json_decode( file_get_contents( $user_file ), true );
  30.  
  31. if ( $flood_row['banned'] )
  32. {
  33. shell_exec( "sudo /sbin/iptables -A INPUT -s $user_ip -j DROP" );
  34. exit;
  35. }
  36.  
  37. if ( time() - $flood_row['last_request'] <= $config['ddos']['frequency'] )
  38. {
  39. ++$flood_row['requests'];
  40. if ( $flood_row['requests'] >= $config['ddos']['requests_limit'] )
  41. {
  42. $flood_row['banned'] = true;
  43. }
  44. $flood_row['last_request'] = time();
  45. file_put_contents( $user_file, json_encode( $flood_row ), LOCK_EX );
  46. }
  47. else
  48. {
  49. $flood_row['requests'] = 0;
  50. $flood_row['banned'] = false;
  51. $flood_row['last_request'] = time();
  52. file_put_contents( $user_file, json_encode( $flood_row ), LOCK_EX );
  53. }
  54. }
  55. else
  56. file_put_contents( $user_file, json_encode( array(
  57. 'banned' => false,
  58. 'requests' => 0,
  59. 'last_request' => time() ) ), LOCK_EX );
  60. }
  61.  
  62.  
  63. if ( $config['user_agents']['user_agent_protection'] )
  64. {
  65. if ( $config['user_agents']['block_empty_ua'] && empty( $_SERVER['HTTP_USER_AGENT'] ) )
  66. attack_found( "EMPTY USER AGENT" );
  67.  
  68. if ( preg_match( "/^(" . $config['user_agents']['user_agents'] . ").*/i", $_SERVER['HTTP_USER_AGENT'], $matched ) )
  69. attack_found( "BAD USER AGENT({$_SERVER['HTTP_USER_AGENT']})" );
  70. }
  71.  
  72.  
  73.  
  74. if ( !file_exists( CACHE_DIR . 'tor_exit_nodes' ) || time() - filemtime( CACHE_DIR . 'tor_exit_nodes' ) >= 1800 )
  75. {
  76. $source = file_get_contents( "https://check.torproject.org/exit-addresses" );
  77. if ( preg_match_all( "/ExitAddress (.*?)\s/", $source, $matches ) )
  78. $ips = $matches[1];
  79.  
  80. file_put_contents( CACHE_DIR . 'tor_exit_nodes', implode( "\n", $ips ) );
  81. }
  82. else
  83. $ips = array_map( 'trim', file( CACHE_DIR . 'tor_exit_nodes' ) );
  84.  
  85. if ( in_array( $user_ip, $ips ) )
  86. attack_found( "TOR FOUND ON $user_ip" );
  87.  
  88.  
  89. /* Proxy Access Prtoection */
  90. foreach ( $proxies_headers as $x )
  91. {
  92. if ( !empty( $_SERVER[$x] ) )
  93. attack_found( "PROXIES NOT ALLOWED ($x is SET on \$_SERVER)" );
  94. }
  95.  
  96. if ( strlen( $query_string ) >= $config['input']['query_string_max'] )
  97. attack_found( "MAX INPUT REACHED! (QUERY STRING: $query_string)" );
  98.  
  99. /* Method Request */
  100. if ( !in_array( $_SERVER['REQUEST_METHOD'], $allowed_methods ) )
  101. attack_found( "METHOD ({$_SERVER['REQUEST_METHOD']}) NOT ALLOWED" );
  102.  
  103. /* HTTPS Limitation */
  104. if ( $config['https']['use_only_https'] && $_SERVER['REQUEST_SCHEME'] != 'https' )
  105. {
  106.  
  107. if ( $config['https']['redirect'] )
  108. {
  109. header( 'Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true, 301 );
  110. exit;
  111. }
  112.  
  113. attack_found( "NO HTTPS SCHEME FOUND" );
  114. }
  115.  
  116.  
  117. /* Protect Files/Folders With Extra Password */
  118. if ( $config['protect_files']['enable_file_protection'] && !empty( $_SERVER['SCRIPT_NAME'] ) && stristr( $_SERVER['SCRIPT_NAME'], '/' ) )
  119. {
  120. $auth = false;
  121. if ( isset( $_SERVER['PHP_AUTH_USER'] ) )
  122. {
  123. $username = @$_SERVER['PHP_AUTH_USER'];
  124. $password = @$_SERVER['PHP_AUTH_PW'];
  125.  
  126. if ( $username == $config['protect_files']['username'] && $password == $config['protect_files']['password'] )
  127. {
  128. $auth = true;
  129. }
  130. }
  131.  
  132. if ( !$auth )
  133. {
  134. $files = explode( '/', $_SERVER['SCRIPT_NAME'] );
  135.  
  136. foreach ( $files as $file )
  137. {
  138. $file = pathinfo( $file )['filename'];
  139.  
  140. if ( preg_match( "/^\b(" . $config['protect_files']['files'] . ")\b/i", $file, $matched ) )
  141. {
  142. header( 'WWW-Authenticate: Basic realm="WAFFLE Protection"' );
  143. header( 'HTTP/1.0 401 Unauthorized' );
  144. exit;
  145. }
  146. }
  147. }
  148.  
  149. }
  150.  
  151.  
  152. if ( empty( $_COOKIE ) )
  153. $_COOKIE = array();
  154.  
  155. if ( empty( $_SESSION ) )
  156. $_SESSION = array();
  157.  
  158. $exclude_keys = array(
  159. '__utmz',
  160. '__utma',
  161. '__cfduid',
  162. '_ga' );
  163.  
  164. $WAF_array = array(
  165. '_GET' => &$_GET,
  166. '_POST' => &$_POST,
  167. '_REQUEST' => &$_REQUEST,
  168. '_COOKIE' => &$_COOKIE,
  169. '_SESSION' => &$_SESSION,
  170. '_SERVER' => array( 'HTTP_USER_AGENT' => &$_SERVER['HTTP_USER_AGENT'], 'HTTP_REFERER' => &$_SERVER['HTTP_REFERER'] ),
  171. 'HTTP_RAW_POST_DATA' => array( file_get_contents( 'php://input' ) ) );
  172.  
  173. foreach ( $WAF_array as $key => $array )
  174. {
  175. if ( count( $array ) > $config['input']['max_input_elements'] )
  176. attack_found( "MAX INPUT VARS ON $key ARRAY REACHED!" );
  177.  
  178.  
  179. foreach ( $array as $k => $v )
  180. {
  181. if ( in_array( $k, $exclude_keys, true ) )
  182. {
  183. continue;
  184. }
  185.  
  186. if ( $config['input']['max_strlen_var'] != 0 && strlen( $v ) > $config['input']['max_strlen_var'] )
  187. attack_found( "MAX INPUT ON VAR REACHED!" );
  188.  
  189.  
  190. if ( !IsBase64( $v ) )
  191. ${$key}[SanitizeNClean( $k )] = SanitizeNClean( $v );
  192. else
  193. ${$key}[SanitizeNClean( $k )] = base64_encode( SanitizeNClean( base64_decode( $v ) ) );
  194.  
  195. }
  196. }
  197.  
  198. /*
  199. Get CPU Load Average on Linux/Windows
  200. Warning: On Windows might take some time
  201. */
  202. function get_server_load()
  203. {
  204. if ( stristr( PHP_OS, 'win' ) )
  205. {
  206.  
  207. $wmi = new COM( "Winmgmts://" );
  208. $server = $wmi->execquery( "SELECT LoadPercentage FROM Win32_Processor" );
  209.  
  210. $cpu_num = 0;
  211. $load_total = 0;
  212.  
  213. foreach ( $server as $cpu )
  214. {
  215. $cpu_num++;
  216. $load_total += $cpu->loadpercentage;
  217. }
  218.  
  219. $load = round( $load_total / $cpu_num );
  220.  
  221. }
  222. else
  223. {
  224.  
  225. $sys_load = sys_getloadavg();
  226. $load = $sys_load[0];
  227.  
  228. }
  229.  
  230. return ( int )$load;
  231.  
  232. }
  233.  
  234.  
  235. function attack_found( $match )
  236. {
  237. file_put_contents( CACHE_DIR . 'attacks.txt', "[WARNING] Possible Threat Found ( => '" . str_replace( "\n", "\\n", $match ) . "' <= ) @ " . date( "F j, Y, g:i a" ) . "\n", FILE_APPEND );
  238. exit( 'Hacking Attempt Detected & Eliminated' );
  239. }
  240.  
  241. function SanitizeNClean( $string )
  242. {
  243. return htmlentities( str_replace( array(
  244. '(',
  245. ')',
  246. '=',
  247. ',',
  248. '|',
  249. '$',
  250. '`',
  251. '/',
  252. '\\' ), array(
  253. '&#40;',
  254. '&#41;',
  255. '&#61;',
  256. '&#44;',
  257. '&#124;',
  258. '&#36;',
  259. '&#96;',
  260. '&#47;',
  261. '&#92;' ), urldecode( $string ) ), ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE, ini_get( "default_charset" ), false );
  262. }
  263.  
  264. function IsBase64( $string )
  265. {
  266. $d = base64_decode( $string, true );
  267. return ( !empty( $d ) ) ? isAscii( $d ) : false;
  268.  
  269. }
  270.  
  271. function isAscii( $str )
  272. {
  273. return preg_match( '/^([\x00-\x7F])*$/', $str );
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement