SIGN IN SIGN UP
sqlmapproject / sqlmap UNCLAIMED

Automatic SQL injection and database takeover tool

0 0 3 Python
#!/usr/bin/env python
2008-10-15 15:38:22 +00:00
"""
2014-01-13 17:24:49 +00:00
Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
2010-10-14 23:18:29 +00:00
See the file 'doc/COPYING' for copying permission
2008-10-15 15:38:22 +00:00
"""
import json
import pickle
import sys
2008-10-15 15:38:22 +00:00
2012-07-31 11:03:44 +02:00
from lib.core.settings import IS_WIN
2011-03-03 10:39:04 +00:00
from lib.core.settings import UNICODE_ENCODING
def base64decode(value):
2013-03-11 14:58:05 +01:00
"""
Decodes string value from Base64 to plain format
>>> base64decode('Zm9vYmFy')
'foobar'
"""
return value.decode("base64")
2008-10-15 15:38:22 +00:00
def base64encode(value):
2013-03-11 14:58:05 +01:00
"""
Encodes string value from plain to Base64 format
>>> base64encode('foobar')
'Zm9vYmFy'
"""
return value.encode("base64")[:-1].replace("\n", "")
def base64pickle(value):
2013-03-11 14:58:05 +01:00
"""
Serializes (with pickle) and encodes to Base64 format supplied (binary) value
>>> base64pickle('foobar')
'gAJVBmZvb2JhcnEALg=='
"""
2012-11-26 11:16:59 +01: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
def base64unpickle(value):
2013-03-11 14:58:05 +01:00
"""
2013-03-26 14:11:17 +01:00
Decodes value from Base64 to plain format and deserializes (with pickle) its content
2013-03-11 14:58:05 +01:00
>>> base64unpickle('gAJVBmZvb2JhcnEALg==')
'foobar'
"""
return pickle.loads(base64decode(value))
2008-10-15 15:38:22 +00:00
def hexdecode(value):
2013-03-11 14:58:05 +01:00
"""
Decodes string value from hex to plain format
>>> hexdecode('666f6f626172')
'foobar'
"""
value = value.lower()
2011-12-21 19:40:42 +00:00
return (value[2:] if value.startswith("0x") else value).decode("hex")
def hexencode(value):
2013-03-11 14:58:05 +01:00
"""
Encodes string value from plain to hex format
>>> hexencode('foobar')
'666f6f626172'
"""
2012-10-30 00:59:31 +01:00
return utf8encode(value).encode("hex")
2008-10-15 15:38:22 +00:00
2011-04-29 15:22:32 +00:00
def unicodeencode(value, encoding=None):
"""
2013-03-11 14:58:05 +01:00
Returns 8-bit string representation of the supplied unicode value
2011-04-29 15:22:32 +00:00
2013-03-11 14:58:05 +01:00
>>> unicodeencode(u'foobar')
'foobar'
2011-04-29 15:22:32 +00:00
"""
retVal = value
if isinstance(value, unicode):
try:
retVal = value.encode(encoding or UNICODE_ENCODING)
except UnicodeEncodeError:
retVal = value.encode(UNICODE_ENCODING, "replace")
2011-04-29 15:22:32 +00:00
return retVal
def utf8encode(value):
2013-03-11 14:58:05 +01:00
"""
Returns 8-bit string representation of the supplied UTF-8 value
>>> utf8encode(u'foobar')
'foobar'
"""
2011-04-29 15:22:32 +00:00
return unicodeencode(value, "utf-8")
def utf8decode(value):
2013-03-11 14:58:05 +01:00
"""
Returns UTF-8 representation of the supplied 8-bit string representation
2013-03-11 14:58:05 +01:00
>>> utf8decode('foobar')
u'foobar'
"""
return value.decode("utf-8")
def htmlunescape(value):
2013-03-11 14:58:05 +01:00
"""
Returns (basic conversion) HTML unescaped value
>>> htmlunescape('a<b')
'a<b'
"""
2011-12-21 14:25:39 +00:00
retVal = value
if value and isinstance(value, basestring):
2012-07-23 15:06:49 +02:00
codes = (('&lt;', '<'), ('&gt;', '>'), ('&quot;', '"'), ('&nbsp;', ' '), ('&amp;', '&'))
retVal = reduce(lambda x, y: x.replace(y[0], y[1]), codes, retVal)
return retVal
2012-07-31 11:03:44 +02:00
def singleTimeWarnMessage(message): # Cross-linked function
2013-08-12 14:25:51 +02:00
raise NotImplementedError
2012-07-31 11:03:44 +02:00
def stdoutencode(data):
retVal = None
try:
# Reference: http://bugs.python.org/issue1602
if IS_WIN:
2013-04-06 01:48:23 +02:00
output = data.encode("ascii", "replace")
2012-07-31 11:03:44 +02:00
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
def jsonize(data):
2013-03-11 14:58:05 +01:00
"""
Returns JSON serialized data
>>> jsonize({'foo':'bar'})
'{\\n "foo": "bar"\\n}'
"""
return json.dumps(data, sort_keys=False, indent=4)
def dejsonize(data):
2013-03-11 14:58:05 +01:00
"""
Returns JSON deserialized data
>>> dejsonize('{\\n "foo": "bar"\\n}')
{u'foo': u'bar'}
"""
return json.loads(data)