Advertisement
rhiby

anti ddos

Feb 12th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.33 KB | None | 0 0
  1. <?php
  2.  
  3. ################################################## ###########################
  4. # #
  5. # Cafe Contrainteleigencia PHP Website Security Script 1.8 #
  6. # 123
  7. # =)#
  8.  
  9. ################################################## ###########################
  10.  
  11. ##################
  12. #
  13. # ANTI DDOS,DOS and BOTNET Attacks!
  14. #
  15. ##################
  16.  
  17. $usehtaccessbans = 1; # 1 = modify .htaccess to ban IPs, 0 = don't ban IPs.
  18. $filterGETvars = 1; # 1 = sterilize HTML tags in GET variables, 0 = don't
  19. $filterCOOKIEvars = 1; # 1 = sterilize HTML tags in COOKIE variables, 0 = don't
  20. $filterPOSTvars = 0; # 1 = sterilize HTML tags in POST variables, 0 = don't
  21. $extraPOSTprotection = 0; # 1 = use the extra POST protection, 0 = don't
  22. $extraGETprotection = 0; # 1 = use the extra GET protection, 0 = don't (not recommended!)
  23. $checkmultiPOST = 1; # 1 = only allow maxmultiPOST number of successive POSTs, 0 = don't care
  24. $maxmultiPOST = 5; # Maximum number of POST operations in a row, if checkmultipost is on.
  25. $zipcompress = 0; # 1 = Compress pages using GZIP library (lower bandwidth, higher CPU), 0 = don't
  26. $compresslevel = 9; # Compression level for zipcompressing, from 1 (low) to 9 (maximum)
  27. $cpuloadmonitor = 0; # 1 = block access if over a certain system load, 0 = don't
  28. $cpumaxload = 10.0; # Maximum 5 minute system load average before blocking access
  29. $ccisessionpath = ""; # if not blank, sets a directory path to store session files.
  30.  
  31. ##### Encriptacion/En codigo de Variables
  32.  
  33. $javababble = 0; # 1 = Use Encoding/Encrypting (Must be on for any), 0 = Don't
  34. $javaencrypt = 0; # Do actual encrypting of HTML, not just escaping (warning: may slow display)
  35. $preservehead = 0; # 1 = Only encode/encrypt between BODY tags, 0 = encode/encrypt whole document
  36.  
  37. ##################
  38. #
  39. # verificar para in-script overrides
  40. #
  41. ##################
  42.  
  43. if (isset($zipoverride)) {
  44. if (!isset($_REQUEST["zipoverride"])) {
  45. $zipcompress = $zipoverride;
  46. unset($zipoverride);
  47. }
  48. }
  49.  
  50. if (isset($babbleoverride)) {
  51. if (!isset($_REQUEST["babbleoverride"])) {
  52. $javababble = $babbleoverride;
  53. unset($babbleoverride);
  54. }
  55. }
  56.  
  57. ##################
  58. #
  59. # Funcion: CCIJavaBabble
  60. #
  61. #
  62. ##################
  63.  
  64. function CCIJavaBabble($myoutput) {
  65. global $mycrypto, $myalpha2, $javaencrypt, $preservehead;
  66. $s = $myoutput;
  67. $s = ereg_replace("\n","",$s);
  68.  
  69. if ($preservehead) {
  70. eregi("(^.+<body[^>]*>)",$s,$chunks);
  71. $outputstring = $chunks[1];
  72. eregi_replace($headpart,"",$s);
  73.  
  74. eregi("(</body[^>]*>.*)",$s,$chunks);
  75. $outputend = $chunks[1];
  76. eregi_replace($footpart,"",$s);
  77. } else {
  78. $outputstring = "";
  79. $outputend = "";
  80. }
  81.  
  82. if ($javaencrypt) {
  83. $s = strtr($s,$myalpha2,$mycrypto);
  84. $s = rawurlencode($s);
  85. $outputstring .= "<script>var cc=unescape('$s'); ";
  86. $outputstring .= "var index = document.cookie.indexOf('" . md5($_SERVER["REMOTE_ADDR"] . $_SERVER["SERVER_ADDR"]) . "='); " .
  87. "var aa = '$myalpha2'; " .
  88. "if (index > -1) { " .
  89. " index = document.cookie.indexOf('=', index) + 1; " .
  90. " var endstr = document.cookie.indexOf(';', index); " .
  91. " if (endstr == -1) endstr = document.cookie.length; " .
  92. " var bb = unescape(document.cookie.substring(index, endstr)); " .
  93. "} " .
  94. "cc = cc.replace(/[$myalpha2]/g,function(str) { return aa.substr(bb.indexOf(str),1) }); document.write(cc);";
  95. } else {
  96. $outputstring .= "<script>document.write(unescape('" . rawurlencode($s) . "'));";
  97. }
  98. $outputstring .= "</script><noscript>You must enable Javascript in order to view this webpage.</noscript>" . $outputend;
  99.  
  100. return $outputstring;
  101. }
  102.  
  103. ##################
  104. #
  105. # Funcion: CCIClearSession
  106. #
  107. # Formato: CCIClearSession()
  108. # Retornos: Nothing
  109. #
  110. # Usage: Clears all the data out of the session record other than data used for this script
  111. #
  112. ##################
  113.  
  114. function CCIClearSession() {
  115. $getvariables = array_keys($_SESSION);
  116. $count = 0;
  117. while($count < count($getvariables)) {
  118. if (substr($getvariables[$count],0,7) != "ccisec-") {
  119. session_unregister($getvariables[$count]);
  120. if (ini_get('register_globals')) unset($$getvariables[$count]);
  121. }
  122. $count++;
  123. }
  124. }
  125.  
  126. ##################
  127. #
  128. # Funcion: CCIBanIP
  129. #
  130. # Formato: CCIBanIP(IPAddress)
  131. # Retornos: Nothing
  132. #
  133. #
  134. #
  135. ##################
  136.  
  137. function CCIBanIP($banip) {
  138. $filelocation = ".htaccess";
  139. $limitend = "# End of CCI Security Section\n";
  140. $newline = "deny from $banip\n";
  141. if (file_exists($filelocation)) {
  142. $mybans = file($filelocation);
  143. $lastline = "";
  144. if (in_array($newline,$mybans)) exit();
  145. if (in_array($limitend,$mybans)) {
  146. $i = count($mybans)-1;
  147. while ($mybans[$i] != $limitend) {
  148. $lastline = array_pop($mybans) . $lastline;
  149. $i--;
  150. }
  151. $lastline = array_pop($mybans) . $lastline;
  152. $lastline = array_pop($mybans) . $lastline;
  153. $lastline = array_pop($mybans) . $lastline;
  154. array_push($mybans,$newline,$lastline);
  155. } else {
  156. array_push($mybans,"\n\n# CCI Security Script\n","<Limit GET POST>\n","order allow,deny\n",$newline,"allow from all\n","</Limit>\n",$limitend);
  157. }
  158. } else {
  159. $mybans = array("# CCI Security Script\n","<Limit GET POST>\n","order allow,deny\n",$newline,"allow from all\n","</Limit>\n",$limitend);
  160. }
  161. $myfile = fopen($filelocation,"w");
  162. fwrite($myfile,implode($mybans,""));
  163. fclose($myfile);
  164.  
  165. }
  166.  
  167. ##################
  168. #
  169. # Function: CCIFloodCheck
  170. #
  171. # Formato: CCIFloodCheck("identifier",interval,threshold)
  172. #
  173. #
  174. ##################
  175.  
  176. function CCIFloodCheck($identifier,$interval,$threshold=1) {
  177. $myresult = 0;
  178. if (isset($_SESSION["ccisec-" . $identifier])) {
  179. if ($_SESSION["ccisec-" . $identifier] > (time()-$interval)) {
  180. if ($threshold<2) {
  181. $myresult = 1;
  182. } else {
  183. if (!isset($_SESSION["ccisec-" . $identifier . "-counter"])) {
  184. $_SESSION["ccisec-" . $identifier . "-counter"] = 1;
  185. } else {
  186. $_SESSION["ccisec-" . $identifier . "-counter"]++;
  187. if ($_SESSION["ccisec-" . $identifier . "-counter"] >= $threshold) {
  188. $myresult = 1;
  189. }
  190. }
  191. }
  192. }
  193. $_SESSION["ccisec-" . $identifier] = time();
  194. }
  195. return $myresult;
  196. }
  197.  
  198. ################################################## ##############################
  199.  
  200. srand(time());
  201. if (eregi("ccisecurity\.php",$_SERVER["SCRIPT_NAME"])) exit();
  202.  
  203. if ($ccisessionpath != "") session_save_path($ccisessionpath);
  204. session_name(md5($_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_HOST"] . "CCI"));
  205.  
  206. ini_set("session.use_only_cookies","1");
  207. ini_set("session.use_trans_sid","0");
  208.  
  209. if (($zipcompress) && (eregi("gzip",$_SERVER["HTTP_ACCEPT_ENCODING"]))) {
  210. ini_set("zlib.output_compression","On");
  211. ini_set("zlib.output_compression_level",$compressl evel);
  212. ob_start("ob_gzhandler");
  213. }
  214. if ($javababble) {
  215. if ($javaencrypt) {
  216. $myalpha = array_merge(range("a","z"),range("A","Z"),range("0 ","9"));
  217. $myalpha2 = implode("",$myalpha);
  218. shuffle($myalpha);
  219. $mycrypto = implode("",$myalpha);
  220. setcookie(md5($_SERVER["REMOTE_ADDR"] . $_SERVER["SERVER_ADDR"]),$mycrypto);
  221. unset($myalpha);
  222. }
  223. ob_start("cciJavaBabble");
  224. }
  225.  
  226. if (substr_count($_SERVER["SERVER_NAME"],".")>1) {
  227. $cookiedomain = eregi_replace("^[^\.]+\.",".",$_SERVER["SERVER_NAME"]);
  228. } else $cookiedomain = "." . $_SERVER["SERVER_NAME"];
  229.  
  230. $ip = $_SERVER["REMOTE_ADDR"];
  231. $mykeyname = md5($_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_HOST"] . $_SERVER["DOCUMENT_ROOT"] . "CCI");
  232. $myposthashname = md5($_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_HOST"] . $_SERVER["PATH"] . "CCI");
  233.  
  234. $myhash = md5($_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_USER_AGENT"] .
  235. $_SERVER["HTTP_HOST"] . $_SERVER["DOCUMENT_ROOT"] .
  236. $_SERVER["SERVER_SOFTWARE"] . $_SERVER["PATH"] . "X");
  237.  
  238. $mysession = md5($_SERVER["REMOTE_ADDR"] . $_SERVER["HTTP_HOST"]);
  239. session_id($mysession);
  240. session_start();
  241.  
  242.  
  243. # Sneaky cookie-storing flooding programs tend to trip this - a cookie not meant to be returned.
  244.  
  245. if ((isset($_SESSION["ccisec-tripwire"])) && (isset($_COOKIE[$_SESSION["ccisec-tripwire"]]))) {
  246. CCIBanIP($ip);
  247. exit();
  248. }
  249. $tripwire = md5(uniqid(time()));
  250. setcookie($tripwire,md5(uniqid(time())),time()-999999,"/",$cookiedomain);
  251. $_SESSION["ccisec-tripwire"]=$tripwire;
  252.  
  253. # End of the tripwire routine
  254.  
  255.  
  256. if (!isset($_SESSION["ccisec-errors"])) $_SESSION["ccisec-errors"] = 0;
  257. if ($_SESSION["ccisec-errors"]>=10) {
  258. CCIBanIP($ip);
  259. exit();
  260. }
  261.  
  262. if ($_SESSION["ccisec-myhash"] != $myhash) {
  263. $_SESSION["ccisec-myhash"] = $myhash;
  264. $_SESSION["ccisec-errors"]++;
  265. session_write_close();
  266. Header("Location: http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
  267. exit();
  268. }
  269.  
  270. if ((!isset($_COOKIE[$mykeyname])) || ($_COOKIE[$mykeyname] != $myhash)) {
  271.  
  272. if (!isset($_SESSION["ccisec-nocookie"])) {
  273. $_SESSION["ccisec-nocookie"] = 1;
  274. } else {
  275. $_SESSION["ccisec-nocookie"]++;
  276. }
  277.  
  278. if (($usehtaccessbans) && ($_SESSION["ccisec-nocookie"]>10)) CCIBanIP($ip);
  279.  
  280. setcookie($mykeyname,$myhash,0,"/",$cookiedomain);
  281.  
  282. if ($_SESSION["ccisec-nocookie"]>2) {
  283. echo "<b><h1>Access Denied</h1><br><br>You must enable cookies in order to access this website. Please do so before returning, as continued attempts to access without cookies may result in a banning of this ip ($ip).</b>";
  284. session_write_close();
  285. exit();
  286. }
  287. if ($extraGETprotection) {
  288. $_SESSION["ccisec-hash"] = md5(uniqid(time()));
  289. setcookie($myposthashname,$_SESSION["ccisec-hash"],0,"/",$cookiedomain);
  290. }
  291. CCIClearSession();
  292. session_write_close();
  293. Header("Location: http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
  294. exit();
  295. } else $_SESSION["ccisec-nocookie"] = 0;
  296.  
  297. if (($usehtaccessbans) && ($_SESSION["ccisec-fastaccesses"]>40)) CCIBanIP($ip);
  298.  
  299. if ($_SESSION["ccisec-fastaccesses"]>10) {
  300. if ((time()-60) < $_SESSION["ccisec-lastaccess"]) {
  301. echo "<b><h1>Access Denied</h1><br><br>There have been too many rapid requests from this IP address ($ip). You must now wait a full 60 seconds before accessing this site again.</b>";
  302. $_SESSION["ccisec-fastaccesses"]++;
  303. $_SESSION["ccisec-lastaccess"]=time();
  304. exit();
  305. }
  306. }
  307.  
  308. if (!isset($_SESSION["ccisec-lastaccess"])) {
  309. $_SESSION["ccisec-lastaccess"]=time();
  310. } else {
  311. if ((time()-2) < $_SESSION["ccisec-lastaccess"]) {
  312. if (!isset($_SESSION["ccisec-fastaccesses"])) $_SESSION["ccisec-fastaccesses"] = 0;
  313. $_SESSION["ccisec-fastaccesses"]++;
  314. } else {
  315. $_SESSION["ccisec-fastaccesses"] = 0;
  316. }
  317. $_SESSION["ccisec-lastaccess"]=time();
  318. }
  319.  
  320. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  321.  
  322. if ($checkmultiPOST) {
  323. if (($_SESSION["ccisec-lastoperation"] == "POST") && ($_SESSION["ccisec-opcount"] >= $maxmultiPOST)) {
  324. echo "<b><h1>Access Denied</h1><br><br>You may not make multiple POST operations in sequence - please return to the website and try again.</b>";
  325. $_SESSION["ccisec-errors"]++;
  326. exit();
  327. }
  328. }
  329.  
  330. if ($extraPOSTprotection) {
  331. if ((!isset($_COOKIE[$myposthashname])) || ($_COOKIE[$myposthashname] != $_SESSION["ccisec-hash"])) {
  332. echo "<b><h1>Access Denied</h1><br><br>Your browser did not send the correct security data needed to complete a POST operation. Make sure that you have cookies enabled and then try again, or contact the administration if you feel you are receiving this message in error.</b>";
  333. $_SESSION["ccisec-errors"]++;
  334. exit();
  335. }
  336. }
  337. } else if (($extraGETprotection) && ($_SERVER["REQUEST_METHOD"] == "GET")) {
  338. if ((!isset($_COOKIE[$myposthashname])) || ($_COOKIE[$myposthashname] != $_SESSION["ccisec-hash"])) {
  339. echo "<b><h1>Access Denied</h1><br><br>Your browser did not send the correct security data needed to complete a GET operation. Make sure that you have cookies enabled and then try again, or contact the administration if you feel you are receiving this message in error.</b>";
  340. $_SESSION["ccisec-errors"]++;
  341. exit();
  342. }
  343. } else if ($_SERVER["REQUEST_METHOD"] != "GET") {
  344. exit();
  345. }
  346.  
  347. if (($extraPOSTprotection) || ($extraGETprotection)) {
  348. srand(time());
  349. $_SESSION["ccisec-hash"] = md5(uniqid(time()));
  350. setcookie($myposthashname,$_SESSION["ccisec-hash"],0,"/",$cookiedomain);
  351. }
  352.  
  353. if ($_SESSION["ccisec-lastoperation"] == $_SERVER["REQUEST_METHOD"]) {
  354. if (!isset($_SESSION["ccisec-opcount"])) {
  355. $_SESSION["ccisec-opcount"] = 1;
  356. } else {
  357. $_SESSION["ccisec-opcount"]++;
  358. }
  359. } else $_SESSION["ccisec-lastoperation"] = $_SERVER["REQUEST_METHOD"];
  360.  
  361. # Make special characters safe in any GET based cgi variables.
  362.  
  363. if ($filterGETvars) {
  364. $getvariables = array_keys($_GET);
  365. $count = 0;
  366. while($count < count($getvariables)) {
  367. $_GET[$getvariables[$count]] = htmlspecialchars($_GET[$getvariables[$count]]);
  368. if (ini_get('register_globals')) $$getvariables[$count] = $_GET[$getvariables[$count]];
  369. $count++;
  370. }
  371. }
  372.  
  373. if ($filterPOSTvars) {
  374. $getvariables = array_keys($_POST);
  375. $count = 0;
  376. while($count < count($getvariables)) {
  377. $_POST[$getvariables[$count]] = htmlspecialchars($_POST[$getvariables[$count]]);
  378. if (ini_get('register_globals')) $$getvariables[$count] = $_POST[$getvariables[$count]];
  379. $count++;
  380. }
  381. }
  382.  
  383. if ($filterCOOKIEvars) {
  384. $getvariables = array_keys($_COOKIE);
  385. $count = 0;
  386. while($count < count($getvariables)) {
  387. $_COOKIE[$getvariables[$count]] = htmlspecialchars($_COOKIE[$getvariables[$count]]);
  388. if (ini_get('register_globals')) $$getvariables[$count] = $_COOKIE[$getvariables[$count]];
  389. $count++;
  390. }
  391. }
  392.  
  393. if ($cpuloadmonitor) {
  394. $myshelldata = shell_exec("uptime");
  395. $myshelldata = eregi_replace(".*average.*: ","",$myshelldata);
  396. $myshelldata = eregi_replace(", .*","",$myshelldata);
  397. if ($myshelldata >= $cpumaxload) {
  398. echo "<b><h1>Access Denied</h1><br><br>The server is currently too busy to serve your request. We apologize for the inconvenience.</b>";
  399. exit();
  400. }
  401. unset($myshelldata);
  402. }
  403.  
  404. unset($count);
  405. unset($getvariables);
  406. unset($ip);
  407. unset($cookiedomain);
  408. unset($mykeyname);
  409. unset($myposthashname);
  410. unset($myhash);
  411. unset($mysession);
  412.  
  413. $_SESSION["ccisec-errors"] = 0;
  414. if (connection_aborted()) exit();
  415.  
  416. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement