sqlmap/lib/core/compat.py

260 lines
7.3 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2019-03-27 17:17:11 +03:00
"""
2020-01-01 15:25:15 +03:00
Copyright (c) 2006-2020 sqlmap developers (http://sqlmap.org/)
2019-03-27 17:17:11 +03:00
See the file 'LICENSE' for copying permission
"""
2019-06-04 13:15:39 +03:00
from __future__ import division
2019-03-27 17:17:11 +03:00
import binascii
2019-05-09 15:10:18 +03:00
import functools
2019-05-03 00:51:54 +03:00
import math
2019-03-27 17:17:11 +03:00
import os
import random
2019-03-28 18:04:38 +03:00
import sys
2019-08-30 15:43:56 +03:00
import time
2019-06-04 15:48:51 +03:00
import uuid
2019-03-27 17:17:11 +03:00
class WichmannHill(random.Random):
"""
Reference: https://svn.python.org/projects/python/trunk/Lib/random.py
"""
VERSION = 1 # used by getstate/setstate
def seed(self, a=None):
"""Initialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
If a is not None or an int or long, hash(a) is used instead.
If a is an int or long, a is used directly. Distinct values between
0 and 27814431486575L inclusive are guaranteed to yield distinct
internal states (this guarantee is specific to the default
Wichmann-Hill generator).
"""
if a is None:
try:
a = int(binascii.hexlify(os.urandom(16)), 16)
except NotImplementedError:
2019-04-19 14:54:48 +03:00
a = int(time.time() * 256) # use fractional seconds
2019-03-27 17:17:11 +03:00
if not isinstance(a, int):
a = hash(a)
a, x = divmod(a, 30268)
a, y = divmod(a, 30306)
a, z = divmod(a, 30322)
2019-04-19 14:54:48 +03:00
self._seed = int(x) + 1, int(y) + 1, int(z) + 1
2019-03-27 17:17:11 +03:00
self.gauss_next = None
def random(self):
"""Get the next random number in the range [0.0, 1.0)."""
# Wichman-Hill random number generator.
#
# Wichmann, B. A. & Hill, I. D. (1982)
# Algorithm AS 183:
# An efficient and portable pseudo-random number generator
# Applied Statistics 31 (1982) 188-190
#
# see also:
# Correction to Algorithm AS 183
# Applied Statistics 33 (1984) 123
#
# McLeod, A. I. (1985)
# A remark on Algorithm AS 183
# Applied Statistics 34 (1985),198-200
# This part is thread-unsafe:
# BEGIN CRITICAL SECTION
x, y, z = self._seed
x = (171 * x) % 30269
y = (172 * y) % 30307
z = (170 * z) % 30323
self._seed = x, y, z
# END CRITICAL SECTION
# Note: on a platform using IEEE-754 double arithmetic, this can
# never return 0.0 (asserted by Tim; proof too long for a comment).
2019-04-19 14:54:48 +03:00
return (x / 30269.0 + y / 30307.0 + z / 30323.0) % 1.0
2019-03-27 17:17:11 +03:00
def getstate(self):
"""Return internal state; can be passed to setstate() later."""
return self.VERSION, self._seed, self.gauss_next
def setstate(self, state):
"""Restore internal state from object returned by getstate()."""
version = state[0]
if version == 1:
version, self._seed, self.gauss_next = state
else:
raise ValueError("state with version %s passed to "
"Random.setstate() of version %s" %
(version, self.VERSION))
def jumpahead(self, n):
"""Act as if n calls to random() were made, but quickly.
n is an int, greater than or equal to 0.
Example use: If you have 2 threads and know that each will
consume no more than a million random numbers, create two Random
objects r1 and r2, then do
r2.setstate(r1.getstate())
r2.jumpahead(1000000)
Then r1 and r2 will use guaranteed-disjoint segments of the full
period.
"""
2019-05-29 17:42:04 +03:00
if n < 0:
2019-03-27 17:17:11 +03:00
raise ValueError("n must be >= 0")
x, y, z = self._seed
x = int(x * pow(171, n, 30269)) % 30269
y = int(y * pow(172, n, 30307)) % 30307
z = int(z * pow(170, n, 30323)) % 30323
self._seed = x, y, z
def __whseed(self, x=0, y=0, z=0):
"""Set the Wichmann-Hill seed from (x, y, z).
These must be integers in the range [0, 256).
"""
if not type(x) == type(y) == type(z) == int:
raise TypeError('seeds must be integers')
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
raise ValueError('seeds must be in range(0, 256)')
if 0 == x == y == z:
# Initialize from current time
t = int(time.time() * 256)
2019-04-19 14:54:48 +03:00
t = int((t & 0xffffff) ^ (t >> 24))
2019-03-27 17:17:11 +03:00
t, x = divmod(t, 256)
t, y = divmod(t, 256)
t, z = divmod(t, 256)
# Zero is a poor seed, so substitute 1
self._seed = (x or 1, y or 1, z or 1)
self.gauss_next = None
def whseed(self, a=None):
"""Seed from hashable object's hash code.
None or no argument seeds from current time. It is not guaranteed
that objects with distinct hash codes lead to distinct internal
states.
This is obsolete, provided for compatibility with the seed routine
used prior to Python 2.1. Use the .seed() method instead.
"""
if a is None:
self.__whseed()
return
a = hash(a)
a, x = divmod(a, 256)
a, y = divmod(a, 256)
a, z = divmod(a, 256)
x = (x + a) % 256 or 1
y = (y + a) % 256 or 1
z = (z + a) % 256 or 1
self.__whseed(x, y, z)
2019-03-27 18:36:32 +03:00
2019-05-02 17:54:54 +03:00
def patchHeaders(headers):
2019-05-03 00:51:54 +03:00
if headers is not None and not hasattr(headers, "headers"):
2019-05-02 17:54:54 +03:00
headers.headers = ["%s: %s\r\n" % (header, headers[header]) for header in headers]
2019-05-03 00:51:54 +03:00
def cmp(a, b):
"""
>>> cmp("a", "b")
-1
>>> cmp(2, 1)
1
"""
if a < b:
return -1
elif a > b:
return 1
else:
return 0
2019-03-27 18:36:32 +03:00
# Reference: https://github.com/urllib3/urllib3/blob/master/src/urllib3/filepost.py
def choose_boundary():
2020-12-07 13:22:34 +03:00
"""
>>> len(choose_boundary()) == 32
True
"""
retval = ""
try:
retval = uuid.uuid4().hex
except AttributeError:
retval = "".join(random.sample("0123456789abcdef", 1)[0] for _ in xrange(32))
return retval
2019-03-28 18:04:38 +03:00
2019-05-03 00:51:54 +03:00
# Reference: http://python3porting.com/differences.html
def round(x, d=0):
"""
>>> round(2.0)
2.0
>>> round(2.5)
3.0
"""
p = 10 ** d
if x > 0:
2019-05-09 16:47:23 +03:00
return float(math.floor((x * p) + 0.5)) / p
2019-05-03 00:51:54 +03:00
else:
2019-05-09 16:47:23 +03:00
return float(math.ceil((x * p) - 0.5)) / p
2019-05-03 00:51:54 +03:00
2019-08-30 15:43:56 +03:00
# Reference: https://code.activestate.com/recipes/576653-convert-a-cmp-function-to-a-key-function/
2019-05-09 15:10:18 +03:00
def cmp_to_key(mycmp):
"""Convert a cmp= function into a key= function"""
class K(object):
__slots__ = ['obj']
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __init__(self, obj, *args):
self.obj = obj
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
def __hash__(self):
raise TypeError('hash not implemented')
2019-05-09 16:47:23 +03:00
2019-05-09 15:10:18 +03:00
return K
# Note: patch for Python 2.6
if not hasattr(functools, "cmp_to_key"):
functools.cmp_to_key = cmp_to_key
2019-05-03 00:51:54 +03:00
2019-03-29 00:54:05 +03:00
if sys.version_info >= (3, 0):
2019-03-28 18:04:38 +03:00
xrange = range
2019-05-03 16:33:32 +03:00
buffer = memoryview
2019-03-28 18:04:38 +03:00
else:
xrange = xrange
2019-05-03 16:33:32 +03:00
buffer = buffer