2008-10-15 19:38:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2012-07-12 21:38:03 +04:00
|
|
|
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
|
2010-02-23 11:54:33 +03:00
|
|
|
try:
|
|
|
|
import hashlib
|
|
|
|
except:
|
|
|
|
import md5
|
|
|
|
import sha
|
2010-04-12 13:35:20 +04:00
|
|
|
|
|
|
|
import pickle
|
2010-02-23 11:54:33 +03:00
|
|
|
import sys
|
2008-10-15 19:38:22 +04:00
|
|
|
import struct
|
|
|
|
|
2012-07-31 13:03:44 +04:00
|
|
|
from lib.core.settings import IS_WIN
|
2011-03-03 13:39:04 +03:00
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
2010-03-27 02:23:25 +03:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def base64decode(value):
|
|
|
|
return value.decode("base64")
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def base64encode(value):
|
|
|
|
return value.encode("base64")[:-1].replace("\n", "")
|
2010-04-12 13:35:20 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def base64pickle(value):
|
2012-11-26 14:16:59 +04:00
|
|
|
retVal = None
|
|
|
|
try:
|
|
|
|
retVal = base64encode(pickle.dumps(value, pickle.HIGHEST_PROTOCOL))
|
|
|
|
except:
|
|
|
|
warnMsg = "problem occurred while serializing "
|
|
|
|
warnMsg += "instance of a type '%s'" % type(value)
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
|
|
|
|
|
|
|
retVal = base64encode(pickle.dumps(str(value), pickle.HIGHEST_PROTOCOL))
|
|
|
|
return retVal
|
2010-04-12 13:35:20 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def base64unpickle(value):
|
|
|
|
return pickle.loads(base64decode(value))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def hexdecode(value):
|
|
|
|
value = value.lower()
|
2011-12-21 23:40:42 +04:00
|
|
|
return (value[2:] if value.startswith("0x") else value).decode("hex")
|
2010-11-03 13:08:27 +03:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def hexencode(value):
|
2012-10-30 03:59:31 +04:00
|
|
|
return utf8encode(value).encode("hex")
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def md5hash(value):
|
2013-01-04 02:28:07 +04:00
|
|
|
if "hashlib" in sys.modules:
|
2011-01-15 15:13:45 +03:00
|
|
|
return hashlib.md5(value).hexdigest()
|
2010-02-23 11:54:33 +03:00
|
|
|
else:
|
2011-01-15 15:13:45 +03:00
|
|
|
return md5.new(value).hexdigest()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def orddecode(value):
|
|
|
|
packedString = struct.pack("!"+"I" * len(value), *value)
|
2011-11-21 00:14:47 +04:00
|
|
|
return "".join(chr(char) for char in struct.unpack("!"+"I"*(len(packedString)/4), packedString))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def ordencode(value):
|
2011-11-21 00:14:47 +04:00
|
|
|
return tuple(ord(char) for char in value)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def sha1hash(value):
|
2013-01-04 02:28:07 +04:00
|
|
|
if "hashlib" in sys.modules:
|
2011-01-15 15:13:45 +03:00
|
|
|
return hashlib.sha1(value).hexdigest()
|
2010-02-23 11:54:33 +03:00
|
|
|
else:
|
2011-01-15 15:13:45 +03:00
|
|
|
return sha.new(value).hexdigest()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-04-29 19:22:32 +04:00
|
|
|
def unicodeencode(value, encoding=None):
|
|
|
|
"""
|
|
|
|
Return 8-bit string representation of the supplied unicode value:
|
|
|
|
|
|
|
|
>>> unicodeencode(u'test')
|
|
|
|
'test'
|
|
|
|
"""
|
|
|
|
|
|
|
|
retVal = value
|
|
|
|
if isinstance(value, unicode):
|
|
|
|
try:
|
|
|
|
retVal = value.encode(encoding or UNICODE_ENCODING)
|
|
|
|
except UnicodeEncodeError:
|
2011-05-16 01:43:38 +04:00
|
|
|
retVal = value.encode(UNICODE_ENCODING, "replace")
|
2011-04-29 19:22:32 +04:00
|
|
|
return retVal
|
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def utf8encode(value):
|
2011-04-29 19:22:32 +04:00
|
|
|
return unicodeencode(value, "utf-8")
|
2010-05-28 15:32:10 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def utf8decode(value):
|
|
|
|
return value.decode("utf-8")
|
2010-10-01 12:03:39 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def htmlescape(value):
|
2012-07-23 17:06:49 +04:00
|
|
|
codes = (('&', '&'), ('<', '<'), ('>', '>'), ('"', '"'), ("'", '''), (' ', ' '))
|
|
|
|
return reduce(lambda x, y: x.replace(y[0], y[1]), codes, value)
|
2010-10-01 12:03:39 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def htmlunescape(value):
|
2011-12-21 18:25:39 +04:00
|
|
|
retVal = value
|
|
|
|
if value and isinstance(value, basestring):
|
2012-07-23 17:06:49 +04:00
|
|
|
codes = (('<', '<'), ('>', '>'), ('"', '"'), (' ', ' '), ('&', '&'))
|
|
|
|
retVal = reduce(lambda x, y: x.replace(y[0], y[1]), codes, retVal)
|
2011-04-15 01:36:13 +04:00
|
|
|
return retVal
|
2012-07-31 13:03:44 +04:00
|
|
|
|
|
|
|
def singleTimeWarnMessage(message): # Cross-linked function
|
|
|
|
pass
|
|
|
|
|
|
|
|
def stdoutencode(data):
|
|
|
|
retVal = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Reference: http://bugs.python.org/issue1602
|
|
|
|
if IS_WIN:
|
|
|
|
output = data.encode('ascii', "replace")
|
|
|
|
|
|
|
|
if output != data:
|
|
|
|
warnMsg = "cannot properly display Unicode characters "
|
|
|
|
warnMsg += "inside Windows OS command prompt "
|
|
|
|
warnMsg += "(http://bugs.python.org/issue1602). All "
|
|
|
|
warnMsg += "unhandled occurances will result in "
|
|
|
|
warnMsg += "replacement with '?' character. Please, find "
|
|
|
|
warnMsg += "proper character representation inside "
|
|
|
|
warnMsg += "corresponding output files. "
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
|
|
|
|
|
|
|
retVal = output
|
|
|
|
else:
|
|
|
|
retVal = data.encode(sys.stdout.encoding)
|
|
|
|
except:
|
|
|
|
retVal = data.encode(UNICODE_ENCODING)
|
|
|
|
|
|
|
|
return retVal
|