Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: дٳŦ٭ en 6 Junio 2009, 07:15 am



Título: NST Web Based Anti-DoS v0.1
Publicado por: дٳŦ٭ en 6 Junio 2009, 07:15 am
Código
  1. <?
  2. /*
  3. /************************************************************
  4. /                                                                                                        
  5. /           NST Anti Denial of Service Script  v 0.1                                          
  6. /                                                                                                        
  7. /                                                                                                        
  8. /                                                                                                        
  9. /             Author: Link   < link07@gmail.com >                              
  10. /                                                                                                        
  11. /              ..::[N]eo [S]ecurity [T]eam::..                                          
  12. /                                                                                                          
  13. /              http://www.neosecurityteam.net    
  14. /
  15. /                   Date:  October - 2006                
  16. /                                                                                                          
  17. /                                                                                                          
  18. /*************************************************************
  19.  
  20.  
  21.      This script will prevent Dos and DDos attacks using request flood
  22.      of http packets, that causes your server to excecute to much connections
  23.      with the database and execute tha php interpreter a lot of times in a few
  24.      seconds, all you have to do to install this is include it in a script that
  25.      all your scripts use , an script for the database connection for example
  26.      and create three empty txt files on the same folder with write and read
  27.      permissions: log.txt , bans.txt , counter.txt
  28.      This script will ban users using the .htaccess file, so dont forget to put
  29.      this is script on the same folder as the .htaccess file as well
  30.      It is very configurable just modify the parameters indicated below , to
  31.      asign how sctrict the script will act.  
  32.  
  33. */
  34.  
  35. $data = "";
  36.  
  37. $ban = "";
  38.  
  39. $visits = 1;
  40.  
  41. $error = 0;
  42.  
  43. $ahora = date("Y-n-j H:i:s");
  44.  
  45. $safemode = 0;
  46.  
  47. $new = "";
  48.  
  49. $flooder =0;
  50.  
  51. $registered = 0;
  52.  
  53. $wwwdir = "/home/neosecur/public_html/";
  54.  
  55. $ahora = date("Y-n-j H:i:s");
  56.  
  57. if(!$file = @fopen($wwwdir."log.txt","r"))
  58.  
  59. $error = 1;
  60.  
  61. while (!@feof($file))
  62.  
  63. {
  64.  
  65. $data .= @fgets($file);
  66.  
  67. }
  68.  
  69. $visitor = explode("\n",$data);
  70.  
  71. $i=0;
  72.  
  73. while($visitor[$i])
  74.  
  75. {
  76.  
  77. $iptime = explode("@",$visitor[$i]);
  78.  
  79. if ($_SERVER['REMOTE_ADDR'] == $iptime[0])
  80.  
  81. {
  82.  
  83.  
  84.  
  85. $tiempo = strtotime($ahora) - $iptime[1];
  86.  
  87. if ($tiempo <= 1)  // this line controls the acces of an ip, only one request during the number of seconds you replace here
  88.  
  89. {
  90.  
  91. $safemode = 1;
  92.  
  93.  
  94. $flooder = 1;
  95.  
  96. }
  97.  
  98.  
  99. $iptime[1]=strtotime($ahora);
  100.  
  101.  
  102.  
  103.  
  104.  
  105. $registered = 1;
  106.  
  107. }
  108.  
  109. $new .=$iptime[0]."@".$iptime[1]."\n";
  110.  
  111. $i++;
  112.  
  113. }
  114.  
  115.  
  116. if (!$registered)
  117.  
  118. $new.=$_SERVER['REMOTE_ADDR']."@".strtotime($ahora)."\n";
  119.  
  120.  
  121.  
  122. if (!@fclose($file))
  123.  
  124. $error = 1;
  125.  
  126.  
  127.  
  128. if(!$file = @fopen($wwwdir."log.txt","wt"))
  129.  
  130. $error = 1;
  131.  
  132.  
  133.  
  134. @fwrite($file,$new);
  135.  
  136. if (!@fclose($file))
  137.  
  138. $error = 1;
  139.  
  140.  
  141.  
  142. if (!$fcount = @fopen($wwwdir."counter.txt","rt"))
  143.  
  144. $error = 1;
  145.  
  146. $vst = @fgets($fcount);
  147.  
  148. $count = explode("@",$vst);
  149.  
  150. if (strtotime($ahora) == $count[1])
  151.  
  152. {
  153.  
  154. $visits = $count[0] + 1;
  155.  
  156. $count[0] = $visits;
  157.  
  158. }
  159.  
  160. else
  161.  
  162. {
  163.  
  164. $count[0] = 1;
  165.  
  166. $count[1] = strtotime($ahora);
  167.  
  168. }
  169.  
  170. if (!@fclose($fcount))
  171.  
  172. $error = 1;
  173.  
  174.  
  175.  
  176. $new = $count[0]."@".$count[1];
  177.  
  178.  
  179.  
  180. if (!$fcount = @fopen($wwwdir."counter.txt","wt"))
  181.  
  182. $error = 1;
  183.  
  184.  
  185.  
  186. @fwrite($fcount,$new);
  187.  
  188.  
  189.  
  190. if (!@fclose($fcount))
  191.  
  192. $error = 1;
  193.  
  194.  
  195.  
  196.  
  197. $new = "";
  198.  
  199. $registered = 0;
  200.  
  201.  
  202.  
  203. if ($visits > 4) // this line controls visits during a second, it is used to prevent ddos attacks you can modify this depending on the capacity of your server
  204.  
  205. {
  206.  
  207. $safemode = 1;
  208.  
  209. if ($flooder)
  210.  
  211. {
  212.  
  213. if (!$fban = @fopen($wwwdir."bans.txt","rt")) $error = 1;
  214.  
  215. while (!@feof($fban))
  216.  
  217. {
  218.  
  219. $ban .= @fgets($fban);
  220.  
  221. }
  222.  
  223. $ips = explode ("\n",$ban);
  224.  
  225. $i=0;
  226.  
  227. while ($ips[$i])
  228.  
  229. {
  230.  
  231. if ($_SERVER['REMOTE_ADDR'] == $ips[$i])
  232.  
  233. $registered = 1;
  234.  
  235. $i++;
  236.  
  237. $new .=$ips[$i];
  238.  
  239. }
  240.  
  241. if (!@fclose($fban)) $error = 1;
  242.  
  243.  
  244.  
  245. if (!$registered)
  246.  
  247. {
  248.  
  249. if (!$fban = @fopen($wwwdir."bans.txt","wt")) $error = 1;
  250.  
  251. $new .=$_SERVER['REMOTE_ADDR']."\n";
  252.  
  253. @fwrite($fban,$new);
  254.  
  255. if (!@fclose($fban)) $error = 1;
  256.  
  257. }
  258.  
  259. else
  260.  
  261. {
  262.  
  263. if (!$fht = @fopen($wwwdir.".htaccess","rt"))
  264.  
  265. $error = 1;
  266.  
  267. $cont = "";
  268. $cont2 = "";
  269. $eob = 0;
  270. while (!@feof($fht))
  271.  
  272. {
  273.  
  274. $a = @fgets($fht);
  275.  
  276. if ($eob)
  277. $cont2.=$a;
  278. else
  279. {
  280. if ($a == "</Limit>\r\n" or $a == "</Limit>" or $a == "</Limit>\n")
  281. {
  282. $eob = 1;
  283. $cont2.=$a;
  284. }
  285. else
  286. $cont.=$a;
  287. }
  288.  
  289. }
  290.  
  291.  
  292.  
  293. if (!@fclose($fht))
  294.  
  295. $error = 1;
  296.  
  297. if (!$fht = @fopen($wwwdir.".htaccess","wt"))
  298.  
  299. $error = 1;
  300.  
  301.  
  302. $cont .= "Deny from ".$_SERVER['REMOTE_ADDR']."\n";
  303. $cont .= $cont2;
  304.  
  305. @fwrite($fht,$cont);
  306.  
  307. if (!@fclose($fht))
  308.  
  309. $error = 1;
  310.  
  311. }
  312.  
  313. }
  314.  
  315.  
  316. }
  317.  
  318.  
  319. if ($error)
  320.  
  321. {
  322.  
  323. echo "ocurrio un error inesperado por favor trata actualizando de nuevo o vuelve en un rato. Gracias por tu comprensión.";
  324.  
  325. exit(0);
  326.  
  327. }
  328.  
  329. if($safemode)
  330.  
  331. {
  332.  
  333. echo '<META HTTP-EQUIV="Refresh" CONTENT="2;URL=">';
  334.  
  335. exit(0);
  336.  
  337. }
  338.  
  339.  
  340.  
  341. // if no restrictions have been activated , the content of the web will be displayed normally
  342. //echo "contenido";
  343. ?>