2013-02-10 79 views
0

每次我將這個圖像查看器上傳到我的網站時,我就會被黑客入侵,即每次我上傳這個文件時,有人會攻擊我的網站任何更改密碼。黑客總是上傳php shell。但沒有辦法上傳文件。我的文件中的安全問題在哪裏

include "./config.php"; 

@ $db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error()); 

if ($_GET['id']) 
{ 
    $id = $_GET['id']; 
} 
else 
{ 
    header('Location: http://404.html') ; 
    exit; 
} 

$id = mysql_real_escape_string($id, $db); 

//IT"S NOT WORKING! 
if (!$db) 
{ 
    die("error"); 
} 
mysql_select_db($mysql['db']) or die(mysql_error()); 

$query = "SELECT * FROM `images` WHERE id='" . $id . "'"; 

$result = mysql_query($query) or die(mysql_error()); 

if (!$result) 
{ 
    die("MySQL Select error"); 
} 

$num_results = mysql_num_rows($result); 
if ($num_results ==0) 
{ 
header('Location: http:///404.html'); 
exit; 
} 
else{ 
    $row = mysql_fetch_array($result); 
    $downloads = $row['downloads'] + 1; 

    $lastuse = time(); 

    $ss = mysql_query("select downloads from `images` where id='".$id."'") or die(mysql_error()); 
    $rr = mysql_fetch_array($ss); 

    $query = "update `images` set downloads=downloads+1, lastuse='" . $lastuse . "' where id='".$id."'"; 
    $result = mysql_query($query); 
    if (!$result) 
    { 
     die("MySQL update error"); 
    } 


    //get current stats 
    $query = "SELECT * FROM `stat_cache` WHERE 1"; 

    $result = mysql_query($query); 

    if (!$result) 
    { 
     die("MySQL Select error"); 
    } 
    $stat = mysql_fetch_array($result); 


    //downloads update 
    $downloads = $stat['downloads'] + 1; 
    $query = "UPDATE `stat_cache` SET downloads='" . $downloads . "' WHERE 1"; 
    $result = mysql_query($query); 
    if (!$result) 
    { 
     die("MySQL Update error"); 
    } 
} 
//Lets create the image, now. 
if(!file_exists('./images/' . $id)) { 
header('Location: http:///404.html') ; 
    exit; 
} 
header('Content-type: image/jpeg'); 

$fp = fopen('./images/' . $id, 'r'); 

$contents = fread($fp, $maxfilesize); 
fclose($fp); 

echo $contents; 

任何人都可以告訴我這裏的安全問題在哪裏?

+1

定義「我被黑客攻擊」 – Popnoodles 2013-02-10 06:24:46

+1

你想知道的究竟是什麼? SQL注入? – 2013-02-10 06:24:53

+3

Obligatory Bobby Tables:http://xkcd.com/327/並查找SQL注入和php準備好的語句 – Patashu 2013-02-10 06:25:21

回答

-1

Mysql()已被棄用,是您現在最關心的問題之一。考慮使用mysqliPDO。瞭解更多here

這是可能的「黑客」在圖像中使用Web彈,閱讀更多有關here

我能想到的另一種方法是不是通過PHP而是通過你的數據庫。自您使用mysql()確保爲每個用戶設置了正確的權限(特別是在存在root或匿名的情況下),每個用戶的通用權限最低。如果有任何未經授權的訪問,我建議更改密碼。

檢查您的ftp日誌和服務器日誌,以查看是否有任何圖像或文件已被更改,請查看是否有未經授權的登錄。不要像在註釋中提到的那樣將IP地址用作參考,而應仔細查看編輯過的文件和密碼嘗試。

+3

他最關心的問題當然不是他的數據庫API,而是這個特定的腳本包含某種類型的漏洞。改變API並不是一個神奇的子彈。 – Lusitanian 2013-02-10 06:31:25

+0

然後擔心sql注入。如果我正在黑客使用該腳本。瞭解更多[here](http://www.veracode.com/security/sql-injection) – 2013-02-10 06:33:17

+4

我知道SQL注入是什麼,這個代碼實際上似乎可以防止它在表面上。如果您發現漏洞,請解釋它! – Lusitanian 2013-02-10 06:33:44