2012-03-13 68 views
2

我已經用PHP創建了一個應用程序。我不是OOP開發人員,我知道的一切都是我自己學到的。我有一個登錄屏幕和一個文件,將其包含在所有文件的頂部。這些文件檢查用戶是否被授權,如果會話正常,然後授予訪問權限,否則它會重定向到登錄頁面。Websecurify應該保護的訪問頁面

我認爲這是安全的,但我使用Websecurify(鉻附加),它給了我很多安全錯誤,我必須檢查。這些錯誤來自於用戶名/密碼認證和會話cookie「保護」的php頁面。

Websecurify訪問表單,發佈數據併爲應該保護的頁面做了很多事情。我怎樣才能保護我的腳本免受抓取和漫遊?

另外websecurify關於apache authentication =「應用程序使用了WWW身份驗證,這種身份驗證通常被認爲是不安全的,容易受到一系列攻擊。」

這是真的嗎?真的,我需要你的意見如何保護我的PHP腳本免受未經授權的訪問。

的文件,我包括所有PHP腳本的頂部是這個

session_start(); 

// set timeout period in seconds 
$inactive = 3600; 

// check to see if $_SESSION['timeout'] is set 
if(isset($_SESSION['timeout'])) 
{ 
    $session_life = time() - $_SESSION['timeout']; 
    if($session_life > $inactive) 
    { 
    session_destroy(); 
    header("location:http://localhost/test/login.php"); 
    } 
} 
$_SESSION['timeout'] = time(); 



if(!isset($_SESSION['client'])) 
{ 

    header("location:http://localhost/test/login.php"); 
} 
else 
{ 
    // authorize user and store some session vars 
} 

我的登錄頁是

<?php 
session_start(); 
if($_GET['a']=="logout") {session_destroy();header("location:login.php");} 
if(!isset($_SESSION['attempts'])) {$_SESSION['attempts'] = 0; session_commit();} 
session_start(); 

?> 
<?php 
include_once("vars.php"); 
include ('mysql_connect.php'); 
$username=mysql_real_escape_string($_POST["username"]); 
$password=mysql_real_escape_string($_POST["password"]); 


if($_SESSION['attempts']==4){ 
    echo "<div class=\"error\">You can try one more time.</div>"; 
    } 

if($_SESSION['attempts']>4){ 


// check if blocked username 

$sql="SELECT * FROM isec_block WHERE username = '$username' and status=1"; 
$sql=mysql_query($sql); 
$sql_row = mysql_fetch_array($sql); 
$allrows = mysql_num_rows($sql); 
$nowdate = strtotime(date('Y-m-d H:i:s')); 
if($allrows>0){ 
$db_date = strtotime($sql_row['time_limit']); 


    if($db_date < $nowdate){ 
    //unblock user 
    $sql="UPDATE isec_block SET status=0 WHERE username = '$username'"; 
    $sql=mysql_query($sql); 
    echo "<div class=\"error\">Notice: Your account is open now.</div>"; 
    $_SESSION['attempts'] = 0; session_commit(); 
    session_start(); 
    }else{ 
    $error=1; 
    echo "<div class=\"error\">Multiple failed login attempts.</div>"; 
    } 
} 

// eof check if blocked username 

$error=1; 
if($_SESSION['attempts']>0) echo "<div class=\"error\">ERROR: Ty again in 30 minutes please.</div>"; 
$ip = $_SERVER['REMOTE_ADDR']; 



    if($_SESSION['attempts']==5){ 

    // store error login 
    $sql="INSERT INTO `isec_log` (username,ip,date,status) VALUES ('".$username."','$ip',NOW(),1)"; 
    $result=mysql_query($sql); 

    // block username for x time 
    $timeToBuildStructure = 300; // seconds 
    $now = time(); // current time (seconds since 1/1/1970) 
    $finishedBuilding = $now + $timeToBuildStructure; 
    $newdate = date("Y-m-d H:i:s",$finishedBuilding); 
    $sql="INSERT INTO isec_block (username,time_limit,status) VALUES ('".$username."','$newdate',1)"; 
    $result=mysql_query($sql); 
    } 

$_SESSION['attempts']= $_SESSION['attempts'] + 1; 
} 



if($username!=="" && $password!=="" && $error<>1) 
{ 
    $sql="SELECT * FROM isec_usertable WHERE username='$username' AND password='$password'"; 
    $result=mysql_query($sql); 
    $row_result= mysql_fetch_assoc($result); 
    $authenticated = $row_result['username']; 
    $authenticatedid = $row_result['id']; 
    $authenitcatedate = $row_result['Lastvisit']; 
    $authenticatedtype = $row_result['rights']; 
    $authenticatestatus = $row_result['status']; 
    $rows=mysql_num_rows($result); 


     if ($rows==1 and $authenticatestatus==1){ 
     $_SESSION['client']=$authenticated; 
     $_SESSION['id']=$authenticatedid; 
     $_SESSION['ldate'] = $authenitcatedate; 
     $_SESSION['rights'] = $authenticatedtype; 
     $_SESSION['client_id'] = $row_result['client']; 
     $_SESSION['isLoggedIn'] = true; 
     $_SESSION['imagemanager.filesystem.rootpath'] = "../../../../../UserFiles/".$authenticatedid; 

     // add visit data 
     $ip = $_SERVER['REMOTE_ADDR']; 
     $visitdate="UPDATE `usertable` SET Lastvisit=NOW(), visits=visits+1 WHERE id='$authenticatedid'"; 
     $result=mysql_query($visitdate); 
     // eof visit date 

     // store error login 
     $sql="INSERT INTO isec_log (username,ip,date,status) VALUES ('$username','$ip',NOW(),0)"; 
     $result=mysql_query($sql); 
     header("location:index.php"); 
     } else { 
     $_SESSION['attempts']= $_SESSION['attempts'] + 1; 
     //header("location:login.php?er=1"); 
     echo "<div class=\"error\">ERROR: Wrong passoword or inactive account</div>"; 
     $error=1; } 
} 


?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Login</title> 
<link href="general_css.css" rel="stylesheet" type="text/css" /> 
</head> 
<body><?php if($_GET['er']==1) {echo "<div class=\"error\">ERROR: Wrong password or inactive account</div>";} ?> 
<div id="container"> 
    <div id="logo"><img src="template/isec-logogif.gif" width="285" height="64" /></div> 
    <?php include_once("header-icons.php");?> 
    <div id="main"> 
<div class="actionsblock"> 
      <div class="actionheader">Login</div> 
       <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
       <table width="100%" border="0" cellspacing="5" cellpadding="5"> 
        <tr> 
        <td width="17%" class="menublock"><div align="right"><a href="pages/clients-add.php"></a><a href="pages/clients.php"></a>Username</div></td> 
        <td width="17%" class="menublock"><label> 
         <input name="username" type="text" class="formfield_client" id="username" value="<?php echo $_POST['username'];?>" /> 
        </label></td> 
        </tr> 
        <tr> 
        <td class="menublock"><div align="right">Password</div></td> 
        <td class="menublock"><input name="password" type="password" class="formfield_client" id="password" /></td> 
        </tr> 
        <tr> 
        <td class="menublock"><div align="right"><a href="myip.php?ip=<?php echo $_SERVER['REMOTE_ADDR'];?>" target="_blank"><img src="template/dot.gif" alt="ip" width="10" height="9" /></a></div></td> 
        <td class="menublock"><label> 
         <input type="submit" name="submit" id="submit" value="Connect" /> 
        </label></td> 
        </tr> 
       </table> 
     </form> 
     </div> 
    </div> 

</div> 
</body> 
</html> 

<?php 
mysql_close($dbc); 
?> 
+0

您的登錄腳本是什麼樣子的? – afuzzyllama 2012-03-13 13:28:06

+0

我在哪裏可以複製粘貼我的登錄頁面的php代碼? – 2012-03-13 14:16:54

+0

只需將其編輯到您的問題。 – afuzzyllama 2012-03-13 14:18:46

回答

0

這是非常不安全的代碼。你從來沒有阻止訪問任何頁面。你不用散列密碼,它容易受到XSS的攻擊。

讓我們從訪問控制開始: header()函數爲響應添加一個任意的http頭,但PHP代碼正常執行。

並不阻止訪問到任何東西,它只是瀏覽器重定向: header("location:http://localhost/test/login.php");

這就好比說,這行代碼阻止訪問:

header("Message: Go away!");

這樣可以防止一個訪問頁面調用die():

header("location:http://localhost/test/login.php"); 
die(); 

xss vectors:

echo $ _POST ['username'];

echo $ _SERVER ['PHP_SELF'];

修補:

回波用htmlspecialchars($ _ POST [ '用戶名'],ENT_QUOTES);

echo htmlspecialchars($ _ SERVER ['PHP_SELF'],ENT_QUOTES);

+0

對於變量「username」,mysql_real_escape_string命令是不夠的? – 2012-03-14 08:22:12

+0

@George D.你正確地解決了SQL注入問題,但就是這樣。幾乎一切都有問題。 – rook 2012-03-14 15:38:25

+0

我糾正了這些問題。謝謝你的幫助。 – 2012-03-14 16:57:47