2011-01-28 80 views
2

我還在學習有關SQL注入,但總是對我來說最好的辦法是使用的例子,所以這是我的代碼部分:我可以對此代碼執行SQL注入嗎?

$sql = "INSERT INTO `comments` (`id`, `idpost`, `comment`, `datetime`, `author`, `active`) 
     VALUES (NULL, '" . addslashes($_POST['idcomment']) . "', '" . 
     addslashes($_POST['comment']) . "', NOW(), '" . 
     addslashes($_POST['name']) . "', '1');"; 

    mysql_query($sql); 

知道了所有的POST變量是由用戶輸入,可以你向我展示瞭如何爲這個腳本注入?所以我可以更多地瞭解這個漏洞。謝謝!

我的數據庫服務器是MySQL。

+2

請讀得好的問題,是如何使注射,而不是如何防止它 – DomingoSL 2011-01-28 18:31:26

+0

請使用[PDO ](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)來保護自己免受sql注入。 PDO準備好的聲明(請參閱PDO鏈接)是安全的。 – Alfred 2011-01-28 22:17:07

回答

3

大多數其他答案似乎完全錯過了這個問題的觀點。

也就是說,根據上面的示例(儘管代碼不遵循最佳實踐使用mysql_real_escape_string()),但使用addslashes()時無法注入任何真正有害的東西。

但是,如果你忽略它,用戶可以輸入一個字符串到name場,看起來像:

some name'; DROP TABLE comments; -- 

的目標是結束當前的語句,然後執行自己。 --是一個註釋,用於確保處理注入的字符串後通常不會出現任何內容。

但是(再次),我的理解是,默認情況下MySQL在單個語句執行結束時自動關閉數據庫連接。所以,即使我嘗試刪除一個表,MySQL也會導致第二條語句失敗。

但是,這不是唯一的SQL注入類型,我建議讀一些關於該主題的內容。我的研究從dev.mysql.com打開了這個文件,這是相當不錯的:http://dev.mysql.com/tech-resources/articles/guide-to-php-security-ch3.pdf


編輯,又一想:

根據一旦進入到數據庫中發生了什麼數據,我可能不會想要注入任何SQL。我可能希望注入一些在將數據發佈回Cross-Site Scripting (XSS)攻擊中的網頁時運行的HTML/JavaScript。這也是需要注意的事情。

5

請勿使用addslashes(),請始終使用mysql_real_escape_string()。有已知的edge cases where addslashes() is not enough

如果從頭開始新的東西,最好使用支持預先準備好的語句(如PDO或mysqli)的數據庫包裝器。

+2

+ PDO/mysqli +準備的statemets建議 - 絕對是一個很好的方法來做到這一點。 – mfonda 2011-01-28 18:07:20

1

正如之前所說,字符串,使用mysql_real_escape_string()代替addslashes()爲整數,使用intval()

/* little code cleanup */ 

$idcomment = intval($_POST['idcomment']); 
$comment = mysql_real_escape_string($_POST['comment']); 
$name = mysql_real_escape_string($_POST['name']); 

$sql = "INSERT INTO comments (idpost, comment, datetime, author, active) 
     VALUES ($idcomment, '$comment', NOW(), '$name', 1)"; 

mysql_query($sql); 
0

Addslashes只處理引號。

但這裏也有一些比較重要的情況:

Be careful on whether you use double or single quotes when creating the string to be escaped: 

$test = 'This is one line\r\nand this is another\r\nand this line has\ta tab'; 

echo $test; 
echo "\r\n\r\n"; 
echo addslashes($test); 

$test = "This is one line\r\nand this is another\r\nand this line has\ta tab"; 

echo $test; 
echo "\r\n\r\n"; 
echo addslashes($test); 

還有一句:

In particular, MySQL wants \n, \r and \x1a escaped which addslashes does NOT do. Therefore relying on addslashes is not a good idea at all and may make your code vulnerable to security risks. 

還有一:

Be very careful when using addslashes and stripslashes in combination with regular expression that will be stored in a MySQL database. Especially when the regular expression contain escape characters! 

To store a regular expression with escape characters in a MySQL database you use addslashes. For example: 

$l_reg_exp = addslashes(�[\x00-\x1F]�); 

After this the variable $l_reg_exp will contain: [\\x00-\\x1F]. 

When you store this regular expression in a MySQL database, the regular expression in the database becomes [\x00-\x1F]. 

When you retrieve the regular expression from the MySQL database and apply the PHP function stripslashes(), the single backslashes will be gone! 

The regular expression will become [x00-x1F] and your regular expression might not work! 

記住,魔術可能會發生:

  • addslashes從數據庫

你的範例檢索後可能會錯過一些

  • 添加到數據庫
  • 之前只是一個摘錄。真正的問題可能在此處不可見還有


    (基於php.net這是非常往往比手動本身多提寶貴意見)

  • 相關問題