PDA

View Full Version : Cách chống ddos / attack ?



pengocdl
02-02-11, 10:57 PM
Các pro về MU Server cho em hỏi có cách nào để chống ddos không.Mới đầu mình dùng kiwiguard mà nghe bác Mr.Quy nói là chẳng tác dụng (mình cũng thấy thế) nên ai có cách nào giảm hoặc chống ddos thì giúp em với.Thanks

Alexandria
03-02-11, 07:50 AM
Chống ddos bạn trên localhost trước ... Mình có 1 số code gà
Mình cũng có một đoạn code, nó phức tạp hơn một chút:
* Tạo các file rỗng: blockedips.txt,log.csv,blank.htm
* Tạo các file: admin.php, logviewer.php, sitefirewall.php với nội dung như sau:
-admin.php:

<?
session_start();
$login=$_POST["login"];
$savelist=$_POST["savelist"];
$pwd=$_POST["pwd"];
if($savelist=="yes"){
$thelist=split(",", $_POST["thelist"]);
$filelist=fopen("blockedips.txt", "w");
for($i=0;$i<count($thelist);$i++){
fputs($filelist, $thelist[$i]."\r\n");
}
fclose($filelist);
}
if($login=="yes"){
$pwdfile=fopen("password.txt", "r");
$thepwd=fread($pwdfile, filesize("password.txt"));
fclose($pwdfile);
if(md5($pwd)==$thepwd){
session_register("logged");
$_SESSION["logged"]="yes";
}
}
elseif($setup=="yes"){
$pwdfile=fopen("password.txt", "w");
fwrite($pwdfile, md5($pwd));
fclose($pwdfile);
}
?>
<html>

<head>
<meta <b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>"Content-Type" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Tường lửa Website</title>
<style>
<!--
.fieldstyle { font-family: Arial; font-size: 12px; border: 1px solid #000000 }
-->
</style>
</head>

<body>
<p><font face="Arial" size="6"><font color="#FF6600">Quản lý</font> <font
color="#0033CC">Tường lửa</font></font></p>
<?
if(!file_exists("password.txt")){
?>
<script language="JavaScript">
<!--
function CheckPasswords(){
var form=document.forms.setup;
if(form.pwd.value!=form.pwd2.value){
window.alert("Passwords don't match, check them");
return false;
}
return true;
}
//-->
</script>
<p><font face="Arial" size="4">Khởi tạo Password:</font></p>
<form name="setup" method="POST" action="admin.php" onSubmit="return CheckPasswords();">
<p><font face="Arial" size="3">Password: <input type="password" name="pwd"
size="20" class="fieldstyle"></font></p>
<p><font face="Arial" size="3">Nhập lại Password: <input type="password"
name="pwd2" size="20" class="fieldstyle"></font></p>
<p><font face="Arial" size="3"><input type="submit" value="Setup Password"
name="B1" class="fieldstyle"></font></p>
<input type="hidden" name="setup" value="yes">
</form>
<?
}
else{
if($_SESSION["logged"]=="yes"){
?>
<script language="JavaScript">
<!--
function AddIPAddress(form){
var ip=form.newipaddress.value;
var chars="1234567890.- ";
if(ip=="") return;
if(ip.split(".").length<4){
window.alert("Please add a valid IP address to the list");
return;
}
for(i=0;i<ip.length;i++){
if(chars.indexOf(ip.charAt(i))==-1){
window.alert("Please add a valid IP address to the list");
return;
}
}
form.blockedips.options[form.blockedips.options.length]=new Option(ip, ip, true, false);
form.newipaddress.value="";
}
function RemoveItem(form){
if(form.blockedips.options.length==0) return;
if(form.blockedips.options.selectedIndex!=-1){
form.blockedips.options[form.blockedips.options.selectedIndex]=null;
}
else window.alert("Please select an item from the blocked IP address list");
}

function SaveIPAddressList(form){
var iplist=form.blockedips.options;
var list=new Array();
for(i=0;i<iplist.length;i++){
list
[list.length]=iplist[i].value;
}
form.thelist.value=list.join(",");
form.submit();
}
//-->
</script>
<form method="POST" action="admin.php">
<p><a href="logviewer.php" target="_blank"><font face="Arial" size="3"><strong>Xem thông tin Tường lửa</strong></font></a></p>
<p><b><font face="Arial" size="3">Khoá IP</font></b></p>
<table border="0" cellpadding="2" width="100%" cellspacing="4">
<tr>
<td width="100%"><font face="Arial" size="3">Bạn lưu lý: Nếu muốn khoá 1 dãy IP thì được cách nhau bằng &quot;-&quot; của IP đầu và cuối</font>
<p><font size="3"><font face="Arial">IP Khoá:</font> </font><input
type="text" name="newipaddress" size="20"> <input type="button"
value="Chấp nhận vô hiệu IP" name="B1" class="fieldstyle"
onclick="AddIPAddress(this.form);"></td>
</tr>
<tr>
<td width="100%"><select size="5" name="blockedips" class="fieldstyle"
style="width:300px">
<?
$filelist=fopen("blockedips.txt", "r");
while(!feof($filelist)){
$currline=join("-", fgetcsv($filelist, 1024, "-"));
if(trim($currline)!=""){
?>
<option value="<?echo $currline;?>"><?echo $currline;?></option>
<?
}
}
fclose($filelist);
?>
</select></td>
</tr>
<tr>
<td width="100%"><input type="button" value="Huỷ bỏ"
name="B2" class="fieldstyle" onClick="RemoveItem(this.form);"> <input
type="button" value="Lưu thông tin xác lập" name="B3" class="fieldstyle"
onclick="SaveIPAddressList(this.form);"></td>
</tr>
</table>
<input type="hidden" name="savelist" value="yes"><input type="hidden"
name="thelist" value>
</form>
<?
}
else{
?>
<form method="POST" action="admin.php">
<table border="0" cellpadding="0" width="100%" style="border: 1 solid #000000"
bgcolor="#CCCCCC">
<tr>
<td width="100%">
<p align="center">&nbsp;</p>
<p align="center"><font face="Arial" size="2">Nhập Password: </font><input
type="password" name="pwd" size="20" class="fieldstyle"> <input type="submit"
value=" Enter " name="B1" class="fieldstyle"></p>
<p align="center"></td>
</tr>
</table>
<input type="hidden" name="login" value="yes">
</form>
<?
}
}
?>
</body>

</html>

-logviewer.php:

<?
session_start();
if($_SESSION["logged"]=="yes"){
?>
<html>

<head>
<meta <b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>"Content-Type" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Thông tin Log site</title>
</head>

<body>

<p><font face="Arial" size="6"><font color="#FF6600">Thông tin</font> <font
color="#0033CC">Tường lửa</font></font></p>

<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td width="20%" align="center" style="border: 1px outset" bgcolor="#CCCCCC"><font
face="Arial" size="2"><b>Sự kiện</b></font></td>
<td width="20%" align="center" style="border: 1px outset" bgcolor="#CCCCCC"><font
face="Arial" size="2"><b>Ngày/Giờ</b></font></td>
<td width="20%" align="center" style="border: 1px outset" bgcolor="#CCCCCC"><font
face="Arial" size="2"><b>Gây ra bởi</b></font></td>
<td width="20%" align="center" style="border: 1px outset" bgcolor="#CCCCCC"><font
face="Arial" size="2"><b>Địa chỉ IP</b></font></td>
<td width="20%" align="center" style="border: 1px outset" bgcolor="#CCCCCC"><font
face="Arial" size="2"><b>File yêu cầu</b></font></td>
</tr>
<?
$logfile=fopen("log.csv", "r");
while(!feof($logfile)){
$curritem=fgetcsv($logfile, 1024);
?>
<tr>
<td width="20%" style="border: 1 solid #CCCCCC"><font
face="Arial" size="2"><?echo $curritem[0];?></font></td>
<td width="20%" style="border: 1 solid #CCCCCC"><font
face="Arial" size="2"><?echo $curritem[1];?></font></td>
<td width="20%" style="border: 1 solid #CCCCCC"><font
face="Arial" size="2"><?echo $curritem[2];?></font></td>
<td width="20%" style="border: 1 solid #CCCCCC"><font
face="Arial" size="2"><?echo $curritem[3];?></font></td>
<td width="20%" style="border: 1 solid #CCCCCC"><font
face="Arial" size="2"><?echo $curritem[4];?></font></td>
<?
}
fclose($logfile);
?>
</tr>
</table>

</body>

</html>
<?
}
else header("Location: admin.php");
?>

-sitefirewall.php:

<?
function isspamprogram($agent){
$agentslist="CherryPicker,Crescent Internet ToolPak,ExtractorPro,EmailCollector,EmailSiphon,Em ailWolf,Mozilla/2.0 (compatible; NEWT ActiveX; Win32)";
$agentsarray=split(",", $agentslist);
if($agent=="") return true;
$isp=false;
for($i=0;$i<count($agentsarray);$i++){
if(stristr($agent, $agentsarray[$i])){
$isp=true;
last;
}
}
return $isp;
}

$browseragent=getenv("<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>");
$userip=getenv("REMOTE_ADDR");
$thedate=date("l")." ".date("F")." ".date("j")." ".date("Y")." - ".date("g").":".date("i").":".date("s")." ".date("A");
$requestedpage=getenv("SCRIPT_NAME");
$requestedpage=substr($requestedpage, 1, strlen($requestedpage)-1);
$iparray3=explode(".", $userip);
$locksite=false;
if(isspamprogram($browseragent)){
$locksite=true;
$thealert="Spam program blocked";
}
if(!$locksite){
$iplist=fopen("blockedips.txt", "r");
while(!feof($iplist)){
$currip=fgetcsv($iplist, 1024, "-");
if(trim($currip[0])!=""){
if(count($currip)>1){
$iparray1=explode(".", trim($currip[0]));
$iparray2=explode(".", trim($currip[1]));
$cnt=0;
for($i=0;$i<4;$i++){
if($iparray3[$i]<=$iparray2[$i] && $iparray3[$i]>=$iparray1[$i]) $cnt++;
}
if($iparray3[0]<$iparray2[0] && $iparray3[0]>=$iparray1[0]) $cnt=4;
if($cnt==4){
$thealert="IP address blocked";
$locksite=true;
last;
}
}
else{
if($currip[0]==$userip){
$thealert="IP address blocked";
$locksite=true;
last;
}
}
}
}
fclose($iplist);
}
if($locksite){
$logfile=fopen("log.csv", "a");
fputs($logfile,"$thealert,$thedate,$browseragent,$userip,$requeste dpage\r\n");
fclose($logfile);
header("Location: blank.htm");
}
?>
<script language="JavaScript">
<!--
//Get browser name
var nav=navigator.appName;
//Determine whether browser is Internet Explorer or Netscape
var ie=(nav.indexOf("Microsoft")!=-1);
var ns=(nav.indexOf("Netscape")!=-1);

//Disables right click in IE
function nrcIE(){
return false;
}

//Disables right click in NS versions 4 and up
function nrcNS(e){
//Check if mouse button pressed is the right one
if(e.which==2 || e.which==3){
return false;
}
}

//If browser is IE, set the right click event to don't show the context menu when clicking
if(ie){
document.oncontextmenu=nrcIE;
}

//If browser is NS4, capture the right click event and set it to don't show the context menu when clicking
if(ns){
if(document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=nrcNS;
}

//If browser is NS6 capture the right click event and set it to don't show the context menu when clicking
if(document.getElementById){
document.onmouseup=nrcNS;
}
}

//Disable drag & drop
document.ondragstart=new Function("return false;");

//Disable text selecting and copy-paste functions
document.onselectstart=new Function("return false;");

//Disables showing URL of links in status bar, it works by showing a custom message in the statusbar when the mouse is moving, you can customize the message to your own
document.onmousemove=new Function("window.status='';");

//Disable offline use by detecting whether the URL of the webpage is not an HTTP protocol
if(window.self.location.href.indexOf("<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>")==-1) window.location="";

//Disable printing of page
if(document.all) window.onbeforeprint=new Function("window.location='';");

//Keep page out of frames
if(top.location!=self.location) top.location=self.location;

//This function clears the clipboard data (text or pictures)
function ccb(){
if(clipboardData){
clipboardData.clearData();
}
}

//This code triggers the interval for deleting clipboard contents and also it will set to don't show error messages in case of bugs with browser, so the it don't shows any alert of error
window.onerror=new Function("return true;");
setInterval("ccb();", 1000);
//-->
</script>
* Và dưới đây là hướng dẫn:
* Tường lửa website - Chỉ áp dụng cho HTML và PHP
Chống việc DDOS, Cấm truy cập của một hay một dãy IP
* Cài đặt:

1.Upload toàn bộ các file: sitefirewall.php, admin.php, logviewer.php, log.txt, and blockedips.txt cùng thư mục với website
2. Thêm đoạn mã phía dưới vào trước tag <html> đầu tiên của file muốn bảo vệ ( index).

<?include("sitefirewall.php");?>

3. OK ! Bạn Upload lên Host (<b><font color=red>[Chỉ có thành viên mới xem link được. <a href="register.php"> Nhấp đây để đăng ký thành viên......</a>]</font></b>) và chạy.
* Phần quản lí
- đăng nhập vào admin.php theo đường dẫn bạn mới upload lên, khởi tạo pass và quản lí
(Password được tạo đặt tại tập tin password.txt với mật khẩu là mỗi dãy mã hóa MD5).