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
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
|
|
|
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
|
|
|
|
import urllib
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
from lib.core.data import conf
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def base64decode(string):
|
|
|
|
return string.decode("base64")
|
|
|
|
|
|
|
|
def base64encode(string):
|
2010-04-12 13:35:20 +04:00
|
|
|
return string.encode("base64")[:-1].replace("\n", "")
|
|
|
|
|
|
|
|
def base64pickle(string):
|
|
|
|
return base64encode(pickle.dumps(string))
|
|
|
|
|
|
|
|
def base64unpickle(string):
|
|
|
|
return pickle.loads(base64decode(string))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def hexdecode(string):
|
|
|
|
string = string.lower()
|
|
|
|
|
|
|
|
if string.startswith("0x"):
|
|
|
|
string = string[2:]
|
|
|
|
|
|
|
|
return string.decode("hex")
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def hexencode(string):
|
|
|
|
return string.encode("hex")
|
|
|
|
|
|
|
|
def md5hash(string):
|
2010-02-23 11:54:33 +03:00
|
|
|
if sys.modules.has_key('hashlib'):
|
|
|
|
return hashlib.md5(string).hexdigest()
|
|
|
|
else:
|
|
|
|
return md5.new(string).hexdigest()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def orddecode(string):
|
|
|
|
packedString = struct.pack("!"+"I" * len(string), *string)
|
|
|
|
return "".join([chr(char) for char in struct.unpack("!"+"I"*(len(packedString)/4), packedString)])
|
|
|
|
|
|
|
|
def ordencode(string):
|
|
|
|
return tuple([ord(char) for char in string])
|
|
|
|
|
|
|
|
def sha1hash(string):
|
2010-02-23 11:54:33 +03:00
|
|
|
if sys.modules.has_key('hashlib'):
|
|
|
|
return hashlib.sha1(string).hexdigest()
|
|
|
|
else:
|
|
|
|
return sha.new(string).hexdigest()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
def urldecode(string):
|
2010-01-02 05:02:12 +03:00
|
|
|
result = None
|
2010-10-01 12:03:39 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if string:
|
|
|
|
result = urllib.unquote_plus(string)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
return result
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def urlencode(string, safe=":/?%&=", convall=False):
|
2010-06-30 01:07:23 +04:00
|
|
|
if conf.direct or "POSTxml" in conf.paramDict:
|
2010-03-27 02:23:25 +03:00
|
|
|
return string
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
result = None
|
|
|
|
|
|
|
|
if string is None:
|
|
|
|
return result
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if convall:
|
2010-06-30 01:07:23 +04:00
|
|
|
result = urllib.quote(utf8encode(string)) # Reference: http://old.nabble.com/Re:-Problem:-neither-urllib2.quote-nor-urllib.quote-encode-the--unicode-strings-arguments-p19823144.html
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
2010-05-28 18:07:48 +04:00
|
|
|
result = urllib.quote(utf8encode(string), safe)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
|
|
|
return result
|
2010-05-28 15:32:10 +04:00
|
|
|
|
|
|
|
def utf8encode(string):
|
|
|
|
return string.encode("utf-8")
|
|
|
|
|
|
|
|
def utf8decode(string):
|
|
|
|
return string.decode("utf-8")
|
2010-10-01 12:03:39 +04:00
|
|
|
|
|
|
|
def htmlescape(string):
|
|
|
|
return string.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
|
|
|
|
|
|
|
|
def htmlunescape(string):
|
|
|
|
return string.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace(''', "'")
|