2008-10-15 19:38:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2008-10-15 19:56:32 +04:00
|
|
|
$Id$
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-01-11 18:59:46 +04:00
|
|
|
Copyright (c) 2006-2012 sqlmap developers (http://www.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
|
|
|
|
2011-04-14 19:54:00 +04:00
|
|
|
import binascii
|
2010-04-12 13:35:20 +04:00
|
|
|
import pickle
|
2011-04-10 20:46:33 +04:00
|
|
|
import re
|
2010-02-23 11:54:33 +03:00
|
|
|
import sys
|
2011-04-10 11:16:19 +04:00
|
|
|
import string
|
2008-10-15 19:38:22 +04:00
|
|
|
import struct
|
|
|
|
import urllib
|
|
|
|
|
2011-04-15 17:51:06 +04:00
|
|
|
from extra.safe2bin.safe2bin import safecharencode
|
|
|
|
from extra.safe2bin.safe2bin import safechardecode
|
2010-03-27 02:23:25 +03:00
|
|
|
from lib.core.data import conf
|
2011-07-08 14:03:14 +04:00
|
|
|
from lib.core.data import kb
|
2011-03-09 12:36:56 +03:00
|
|
|
from lib.core.data import logger
|
2011-04-18 01:39:00 +04:00
|
|
|
from lib.core.enums import PLACE
|
2011-03-03 13:39:04 +03:00
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
2011-03-09 12:36:56 +03:00
|
|
|
from lib.core.settings import URLENCODE_CHAR_LIMIT
|
|
|
|
from lib.core.settings import URLENCODE_FAILSAFE_CHARS
|
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):
|
|
|
|
return base64encode(pickle.dumps(value))
|
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):
|
|
|
|
return value.encode("hex")
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
def md5hash(value):
|
2010-02-23 11:54:33 +03:00
|
|
|
if sys.modules.has_key('hashlib'):
|
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):
|
2010-02-23 11:54:33 +03:00
|
|
|
if sys.modules.has_key('hashlib'):
|
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-03-03 13:39:04 +03:00
|
|
|
def urldecode(value, encoding=None):
|
2010-01-02 05:02:12 +03:00
|
|
|
result = None
|
2010-10-01 12:03:39 +04:00
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
if value:
|
2011-02-26 01:43:01 +03:00
|
|
|
try:
|
|
|
|
# for cases like T%C3%BCrk%C3%A7e
|
|
|
|
value = str(value)
|
|
|
|
except ValueError:
|
2011-03-03 13:39:04 +03:00
|
|
|
pass
|
|
|
|
finally:
|
2011-02-26 01:43:01 +03:00
|
|
|
result = urllib.unquote_plus(value)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-03-03 13:39:04 +03:00
|
|
|
if isinstance(result, str):
|
2011-05-16 01:43:38 +04:00
|
|
|
result = unicode(result, encoding or UNICODE_ENCODING, errors="replace")
|
2011-03-03 13:39:04 +03:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
return result
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-07-08 14:03:14 +04:00
|
|
|
def urlencode(value, safe="%&=", convall=False, limit=False):
|
2011-04-18 01:39:00 +04:00
|
|
|
if conf.direct or PLACE.SOAP in conf.paramDict:
|
2011-01-15 15:13:45 +03:00
|
|
|
return value
|
2010-03-27 02:23:25 +03:00
|
|
|
|
2011-03-11 22:57:44 +03:00
|
|
|
count = 0
|
2010-01-02 05:02:12 +03:00
|
|
|
result = None
|
|
|
|
|
2011-01-15 15:13:45 +03:00
|
|
|
if value is None:
|
2010-01-02 05:02:12 +03:00
|
|
|
return result
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-03-11 22:57:44 +03:00
|
|
|
if convall or safe is None:
|
|
|
|
safe = ""
|
|
|
|
|
2011-04-10 20:46:33 +04:00
|
|
|
# corner case when character % really needs to be
|
|
|
|
# encoded (when not representing url encoded char)
|
2011-07-08 14:03:14 +04:00
|
|
|
# except in cases when tampering scripts are used
|
|
|
|
if all(map(lambda x: '%' in x, [safe, value])) and not kb.tamperFunctions:
|
2011-04-10 20:46:33 +04:00
|
|
|
value = re.sub("%(?![0-9a-fA-F]{2})", "%25", value, re.DOTALL | re.IGNORECASE)
|
|
|
|
|
2011-03-11 22:57:44 +03:00
|
|
|
while True:
|
|
|
|
result = urllib.quote(utf8encode(value), safe)
|
2011-03-09 12:36:56 +03:00
|
|
|
|
2011-03-11 22:57:44 +03:00
|
|
|
if limit and len(result) > URLENCODE_CHAR_LIMIT:
|
|
|
|
if count >= len(URLENCODE_FAILSAFE_CHARS):
|
2011-03-09 12:36:56 +03:00
|
|
|
break
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2011-03-11 22:57:44 +03:00
|
|
|
while count < len(URLENCODE_FAILSAFE_CHARS):
|
|
|
|
safe += URLENCODE_FAILSAFE_CHARS[count]
|
|
|
|
count += 1
|
|
|
|
if safe[-1] in value:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
return result
|
2010-05-28 15:32:10 +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):
|
2011-12-21 23:40:42 +04:00
|
|
|
_ = (('&', '&'), ('<', '<'), ('>', '>'), ('"', '"'), ("'", '''), (' ', ' '))
|
|
|
|
return reduce(lambda x, y: x.replace(y[0], y[1]), _, 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):
|
2011-12-21 23:40:42 +04:00
|
|
|
_ = (('&', '&'), ('<', '<'), ('>', '>'), ('"', '"'), (' ', ' '))
|
|
|
|
retVal = reduce(lambda x, y: x.replace(y[0], y[1]), _, retVal)
|
|
|
|
retVal = re.sub('&#(\d+);', lambda x: unichr(int(x.group(1))), retVal)
|
2011-04-15 01:36:13 +04:00
|
|
|
return retVal
|