diff options
| -rw-r--r-- | docs/lib/passlib.hash.hex_digests.rst | 6 | ||||
| -rw-r--r-- | docs/lib/passlib.hash.oracle10.rst | 116 | ||||
| -rw-r--r-- | docs/lib/passlib.hash.oracle11.rst | 81 | ||||
| -rw-r--r-- | docs/lib/passlib.hash.postgres_md5.rst | 2 | ||||
| -rw-r--r-- | docs/lib/passlib.hash.rst | 3 | ||||
| -rw-r--r-- | docs/lib/passlib.hash.sun_md5_crypt.rst | 2 | ||||
| -rw-r--r-- | passlib/base.py | 2 | ||||
| -rw-r--r-- | passlib/drivers/oracle.py | 186 | ||||
| -rw-r--r-- | passlib/sqldb.py | 3 | ||||
| -rw-r--r-- | passlib/tests/test_drivers.py | 69 |
10 files changed, 463 insertions, 7 deletions
diff --git a/docs/lib/passlib.hash.hex_digests.rst b/docs/lib/passlib.hash.hex_digests.rst index ff4c00f..cbd1b1a 100644 --- a/docs/lib/passlib.hash.hex_digests.rst +++ b/docs/lib/passlib.hash.hex_digests.rst @@ -1,6 +1,6 @@ -================================================================== -:samp:`passlib.hash.hex_{digest}` - Hexdecimal Standard Digests -================================================================== +============================================================== +:samp:`passlib.hash.hex_{digest}` - Generic Hexdecimal Digests +============================================================== .. currentmodule:: passlib.hash diff --git a/docs/lib/passlib.hash.oracle10.rst b/docs/lib/passlib.hash.oracle10.rst new file mode 100644 index 0000000..45dfa4b --- /dev/null +++ b/docs/lib/passlib.hash.oracle10.rst @@ -0,0 +1,116 @@ +================================================================== +:class:`passlib.hash.oracle10` - Oracle 10g password hash +================================================================== + +.. currentmodule:: passlib.hash + +This class implements the hash algorithm used by the Oracle Database up to +version 10g Rel.2. It was superceded by a newer algorithm in :class:`Oracle 11 <passlib.hash.oracle11>`. + +.. warning:: + + This hash is not secure, and should not be used for any purposes + besides manipulating existing Oracle 10 password hashes. + +.. warning:: + + This implementation has not been compared + very carefully against the official implementation or reference documentation, + and it's behavior may not match under various border cases. + It should not be relied on for anything but novelty purposes + for the time being. + +Usage +===== +This class can be used directly as follows (note that this class requires +a username for all encrypt/verify operations):: + + >>> from passlib.hash import oracle10 as or10 + + >>> #encrypt password using specified username + >>> h = or10.encrypt("password", "username") + >>> h + '872805F3F4C83365' + + >>> or10.identify(h) #check if hash is recognized + True + >>> or10.identify('$1$3azHgidD$SrJPt7B.9rekpmwJwtON31') #check if some other hash is recognized + False + + >>> or10.verify("password", h, "username") #verify correct password + True + >>> or10.verify("password", h, "somebody") #verify correct password w/ wrong username + False + >>> or10.verify("password", h, "username") #verify incorrect password + False + +Interface +========= +.. autoclass:: oracle10() + +Format +====== +Oracle10 hashes all consist of a series of 16 hexidecimal digits, +representing the resulting checksum. + +.. rst-class:: html-toggle + +Algorithm +========= +Oracle10 hashes are formed by: + +1. Concatenate the username and password together. +2. Convert the result to upper case +3. Encoding the result in a multi-byte format [#enc]_ such that ascii characters (eg: ``user``) are represented + with additional null bytes inserted (eg: ``\x00u\x00s\x00e\x00r``). +4. Right-pad the result with null bytes to bring the size to an integer multiple of 8. + this is the final input string. +5. The input string is then encoded using DES-CBC, + using the key ``\x01\x23\x45\x67\x89\xAB\xCD\xEF``, + and a null initialization vector. +6. The input string is then run through DES-CBC a second time, + using the last block of ciphertext from step 5 + as the key for the second round. +7. The last block of ciphertext of step 6 is converted + to a hexdecimal string, and returned as the checksum. + +Security Issues +=============== +This algorithm it not suitable for *any* use besides manipulating existing +Oracle10 account passwords, due to the following flaws: + +* It's use of the username as a salt value means that common usernames + (eg ``system``) will occur more frequently as salts, + weakening the effectiveness of the salt in foiling pre-computed tables. + +* The fact that is it case insensitive, and simply concatenates the username + and password, greatly reduces the requirements for brute-force + or pre-computed attacks. + +* It's simplicity makes high-speed brute force attacks much more feasible. + +Deviations +========== +PassLib's implementation of the Oracle10g hash may deviate from the official +implementation in unknown ways, as there is no official documentation. +There is only one known issue: + +* Unicode Policy + Lack of testing (and test vectors) leaves it unclear + as to how Oracle 11g handles passwords containing non-7bit ascii. + + In order to provide support for unicode strings, + PassLib will encode unicode passwords using ``utf-16-be`` + before running them through Oracle11. + This behavior may be altered in the future, if further testing + reveals another behavior is more in line with the official representation. + + This note applies as well to any provided username, + as they are run through the same policy. + +References +========== +.. [#enc] The exact encoded used in the algorithm is not clear from known references (see below). + +.. [#] Description of Oracle10g and Oracle11g algorithms - + `<http://www.notesbit.com/index.php/scripts-oracle/oracle-11g-new-password-algorithm-is-revealed-by-seclistsorg/>`_. diff --git a/docs/lib/passlib.hash.oracle11.rst b/docs/lib/passlib.hash.oracle11.rst new file mode 100644 index 0000000..0398ae6 --- /dev/null +++ b/docs/lib/passlib.hash.oracle11.rst @@ -0,0 +1,81 @@ +================================================================== +:class:`passlib.hash.oracle11` - Oracle 11g password hash +================================================================== + +.. currentmodule:: passlib.hash + +This class implements the hash algorithm introduced in version 11g of the Oracle Database. +It supercedes the :class:`Oracle 10 <passlib.hash.oracle10>` password hash. + +.. warning:: + + This implementation has not been compared + very carefully against the official implementation or reference documentation, + and it's behavior may not match under various border cases. + It should not be relied on for anything but novelty purposes + for the time being. + +Usage +===== +PassLib provides an oracle11 class, which can be can be used directly as follows:: + + >>> from passlib.hash import oracle11 as or11 + + >>> #generate new salt, encrypt password + >>> h = or11.encrypt("password") + >>> h + 'S:4143053633E59B4992A8EA17D2FF542C9EDEB335C886EED9C80450C1B4E6' + + >>> or11.identify(h) #check if hash is recognized + True + >>> or11.identify('JQMuyS6H.AGMo') #check if some other hash is recognized + False + + >>> or11.verify("password", h) #verify correct password + True + >>> or11.verify("secret", h) #verify incorrect password + False + +Interface +========= +.. autoclass:: oracle11(checksum=None, salt=None, strict=False) + +Format & Algorithm +================== +An example oracle11 hash (of the string ``password``) +is ``'S:4143053633E59B4992A8EA17D2FF542C9EDEB335C886EED9C80450C1B4E6'``. + +An oracle11 hash string has the format :samp:`S:{checksum}{salt}`, where: + +* ``S:`` is the prefix used to identify oracle11 hashes + (as distinct from oracle10 hashes, which have no constant prefix). +* :samp:`{checksum}` is 40 hexidecimal characters; + encoding a 160-bit checksum (``4143053633E59B4992A8EA17D2FF542C9EDEB335`` in the example). +* :samp:`{salt}` is 20 hexidecimal characters; + providing a 80-bit salt (``C886EED9C80450C1B4E6`` in the example). + +The Oracle 11 hash has a very simple algorithm: The salt is decoded +from it's hexidecimal representation into binary, and the SHA-1 digest +of :samp:`{password}+{raw_salt}` is then encoded into hexidecimal, and returned as the checksum. + +Deviations +========== +PassLib's implementation of the Oracle11g hash may deviate from the official +implementation in unknown ways, as there is no official documentation. +There is only one known issue: + +* Unicode Policy + Lack of testing (and test vectors) leaves it unclear + as to how Oracle 11g handles passwords containing non-7bit ascii. + + In order to provide support for unicode strings, + PassLib will encode unicode passwords using ``utf-8`` + before running them through Oracle11. + + This behavior may be altered in the future, if further testing + reveals another behavior is more in line with the official representation. + +References +========== +.. [#] Description of Oracle10g and Oracle11g algorithms - + `<http://www.notesbit.com/index.php/scripts-oracle/oracle-11g-new-password-algorithm-is-revealed-by-seclistsorg/>`_. diff --git a/docs/lib/passlib.hash.postgres_md5.rst b/docs/lib/passlib.hash.postgres_md5.rst index 8a3aa60..9cc7249 100644 --- a/docs/lib/passlib.hash.postgres_md5.rst +++ b/docs/lib/passlib.hash.postgres_md5.rst @@ -4,7 +4,7 @@ .. currentmodule:: passlib.hash -This class implemented the md5-based hash algorithm used by PostgreSQL to store +This class implements the md5-based hash algorithm used by PostgreSQL to store it's user account passwords. This scheme was introduced in PostgreSQL 7.2; prior to this PostgreSQL stored it's password in plain text. diff --git a/docs/lib/passlib.hash.rst b/docs/lib/passlib.hash.rst index f163679..0611574 100644 --- a/docs/lib/passlib.hash.rst +++ b/docs/lib/passlib.hash.rst @@ -78,7 +78,8 @@ not seen outside those specific contexts: passlib.hash.mysql323 passlib.hash.mysql41 passlib.hash.postgres_md5 - + passlib.hash.oracle10 + passlib.hash.oracle11 Other Schemes ------------- diff --git a/docs/lib/passlib.hash.sun_md5_crypt.rst b/docs/lib/passlib.hash.sun_md5_crypt.rst index 38ed905..e326672 100644 --- a/docs/lib/passlib.hash.sun_md5_crypt.rst +++ b/docs/lib/passlib.hash.sun_md5_crypt.rst @@ -1,5 +1,5 @@ ================================================================= -:class:`passlib.hash.sun_md5_crypt` - Sun MD5 Crypt password hash +:class:`passlib.hash.sun_md5_crypt` - Sun MD5 Crypt ================================================================= .. currentmodule:: passlib.hash diff --git a/passlib/base.py b/passlib/base.py index 29fb45a..ba75ba8 100644 --- a/passlib/base.py +++ b/passlib/base.py @@ -106,6 +106,8 @@ _driver_locations = { "mysql323": ("passlib.drivers.mysql", "mysql323"), "mysql41": ("passlib.drivers.mysql", "mysql41"), "nthash": ("passlib.drivers.nthash", "nthash"), + "oracle10": ("passlib.drivers.oracle", "oracle10"), + "oracle11": ("passlib.drivers.oracle", "oracle11"), "phpass": ("passlib.drivers.phpass", "phpass"), "plaintext": ("passlib.drivers.misc", "plaintext"), "postgres_md5": ("passlib.drivers.postgres", "postgres_md5"), diff --git a/passlib/drivers/oracle.py b/passlib/drivers/oracle.py new file mode 100644 index 0000000..d5437ed --- /dev/null +++ b/passlib/drivers/oracle.py @@ -0,0 +1,186 @@ +"""passlib.drivers.oracle - Oracle DB Password Hashes""" +#========================================================= +#imports +#========================================================= +#core +from binascii import hexlify, unhexlify +from hashlib import sha1 +import re +import logging; log = logging.getLogger(__name__) +from warnings import warn +#site +#libs +#pkg +from passlib.utils import xor_bytes +from passlib.utils.des import des_encrypt_block +from passlib.utils.drivers import ExtHash +#local +__all__ = [ + "oracle10g", + "oracle11g" +] + +#========================================================= +#oracle10 +#========================================================= +def des_cbc_encrypt(key, value, iv='\x00' * 8, pad='\x00'): + """performs des-cbc encryption, returns only last block. + + this performs a specific DES-CBC encryption implementation + as needed by the Oracle10 hash. it probably won't be useful for + other purposes as-is. + + input value is null-padded to multiple of 8 bytes. + + :arg key: des key as bytes + :arg value: value to encrypt, as bytes. + :param iv: optional IV + :param pad: optional pad byte + + :returns: last block of DES-CBC encryption of all ``value``'s byte blocks. + """ + value += pad * (-len(value) % 8) #null pad to multiple of 8 + hash = iv #start things off + for offset in xrange(0,len(value),8): + chunk = xor_bytes(hash, value[offset:offset+8]) + hash = des_encrypt_block(key, chunk) + return hash + +class oracle10(object): + """This class implements the password hash used by Oracle up to version 10g, and follows the :ref:`password-hash-api`. + + It has no salt and a single fixed round. + + The :meth:`encrypt()` and :meth:`genconfig` methods accept no optional keywords. + + The :meth:`encrypt()`, :meth:`genhash()`, and :meth:`verify()` methods all require the + following additional contextual keywords: + + :param user: string containing name of oracle user account this password is associated with. + """ + #========================================================= + #algorithm information + #========================================================= + name = "oracle10" + setting_kwds = () + context_kwds = ("user",) + + #========================================================= + #formatting + #========================================================= + _pat = re.compile(r"^[0-9a-fA-F]{16}$") + + @classmethod + def identify(cls, hash): + return bool(hash and cls._pat.match(hash)) + + #========================================================= + #primary interface + #========================================================= + @classmethod + def genconfig(cls): + return None + + @classmethod + def genhash(cls, secret, config, user): + if config and not cls.identify(config): + raise ValueError, "not a oracle10g hash" + return cls.encrypt(secret, user) + + #========================================================= + #secondary interface + #========================================================= + @classmethod + def encrypt(cls, secret, user): + if secret is None: + raise TypeError, "secret must be specified" + if not user: + raise ValueError, "user keyword must be specified for this algorithm" + + #FIXME: not sure how oracle handles unicode. + # online docs about 10g hash indicate it puts ascii chars + # in a 2-byte encoding w/ the high bytenull. + # they don't say how it handles other chars, + # or what encoding. + # + # so for now, encoding secret & user to utf-16-be, + # since that fits, + # and if secret/user is bytes, inserting artificial + # null bytes in between. + # + # this whole mess really needs someone w/ an oracle system, + # and some answers :) + + def encode(value): + "encode according to guess at how oracle encodes strings (see note above)" + if not isinstance(value, unicode): + value = value.decode("ascii") + return value.upper().encode("utf-16-be") + + input = encode(user) + encode(secret) + hash = des_cbc_encrypt("\x01\x23\x45\x67\x89\xAB\xCD\xEF", input) + hash = des_cbc_encrypt(hash, input) + return hexlify(hash).upper() + + @classmethod + def verify(cls, secret, hash, user): + if not hash: + raise ValueError, "no hash specified" + return cls.genhash(secret, hash, user) == hash.upper() + + #========================================================= + #eoc + #========================================================= + +#========================================================= +#oracle11 +#========================================================= +class oracle11(ExtHash): + """This class implements the Oracle11g password hash, and follows the :ref:`password-hash-api`. + + It supports a fixed-length salt. + + The :meth:`encrypt()` and :meth:`genconfig` methods accept the following optional keywords: + + :param salt: + Optional salt string. + If not specified, one will be autogenerated (this is recommended). + If specified, it must be 20 hexidecimal characters. + """ + + name = "oracle11" + setting_kwds = ("salt",) + + min_salt_chars = max_salt_chars = 20 + salt_charset = checksum_charset = "0123456789ABCDEF" + checksum_chars = 40 + + _pat = re.compile("^S:(?P<chk>[0-9a-f]{40})(?P<salt>[0-9a-f]{20})$", re.I) + + @classmethod + def identify(cls, hash): + return bool(hash and cls._pat.match(hash)) + + @classmethod + def from_string(cls, hash): + if not hash: + raise ValueError, "no hash provided" + m = cls._pat.match(hash) + if not m: + raise ValueError, "invalid oracle11g hash" + salt, chk = m.group("salt", "chk") + return cls(salt=salt, checksum=chk.upper(), strict=True) + + _stub_checksum = '0' * 40 + + def to_string(self): + return "S:%s%s" % ((self.checksum or self._stub_checksum).upper(), self.salt.upper()) + + def calc_checksum(self, secret): + if isinstance(secret, unicode): + secret = secret.encode("utf-8") + return sha1(secret + unhexlify(self.salt)).hexdigest().upper() + +#========================================================= +#eof +#========================================================= diff --git a/passlib/sqldb.py b/passlib/sqldb.py index 9208dad..dc4f6cd 100644 --- a/passlib/sqldb.py +++ b/passlib/sqldb.py @@ -38,7 +38,8 @@ mysql_context = CryptContext([mysql41, mysql323]) #========================================================= #TODO: #========================================================= -#oracle - http://www.notesbit.com/index.php/scripts-oracle/oracle-11g-new-password-algorithm-is-revealed-by-seclistsorg/ +#oracle +#mssql #========================================================= # eof diff --git a/passlib/tests/test_drivers.py b/passlib/tests/test_drivers.py index e5affc1..160b0b7 100644 --- a/passlib/tests/test_drivers.py +++ b/passlib/tests/test_drivers.py @@ -258,6 +258,75 @@ class NTHashTest(HandlerCase): ] #========================================================= +#oracle 10 & 11 +#========================================================= +from passlib.drivers.oracle import oracle10, oracle11 + +class Oracle10Test(HandlerCase): + handler = oracle10 + + known_correct_hashes = [ + # ((secret,user),hash) + (('tiger', 'scott'), 'F894844C34402B67'), + ((u'ttTiGGeR', u'ScO'), '7AA1A84E31ED7771'), + (("d_syspw", "SYSTEM"), '1B9F1F9A5CB9EB31'), + (("strat_passwd", "strat_user"), 'AEBEDBB4EFB5225B'), + #TODO: get more test vectors (especially ones which properly test unicode / non-ascii) + #existing vectors taken from - http://www.petefinnigan.com/default/default_password_list.htm + ] + + known_unidentified_hashes = [ + #bad 'z' char in otherwise correct hash + 'F894844C34402B6Z', + ] + + def test_user(self): + "check user kwd is required for encrypt/verify" + self.assertRaises(TypeError, self.handler.encrypt, 'mypass') + self.assertRaises(TypeError, self.handler.verify, 'mypass', 'CC60FA650C497E52') + + #NOTE: all of the methods below are merely to override + # the default test harness in order to insert a default username + # when encrypt/verify/etc are called. + + def create_mismatch(self, secret): + if isinstance(secret, tuple): + secret, user = secret + return 'x' + secret, user + else: + return 'x' + secret + + def do_encrypt(self, secret, **kwds): + if isinstance(secret, tuple): + secret, user = secret + else: + user = 'default' + assert 'user' not in kwds + kwds['user'] = user + return self.handler.encrypt(secret, **kwds) + + def do_verify(self, secret, hash): + if isinstance(secret, tuple): + secret, user = secret + else: + user = 'default' + return self.handler.verify(secret, hash, user=user) + + def do_genhash(self, secret, config): + if isinstance(secret, tuple): + secret, user = secret + else: + user = 'default' + return self.handler.genhash(secret, config, user=user) + +class Oracle11Test(HandlerCase): + handler = oracle11 + known_correct_hashes = [ + ("SHAlala", "S:2BFCFDF5895014EE9BB2B9BA067B01E0389BB5711B7B5F82B7235E9E182C"), + #TODO: find more test vectors + ] + +#========================================================= #PHPass Portable Crypt #========================================================= from passlib.drivers.phpass import phpass |
