From 62e93f87fa0ca8e346997e5c171af47d05dd2d1b Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Mon, 23 Jan 2017 11:05:40 -0500 Subject: passlib.crypto.scrypt: reworked backend loader internals * reworked backends so they're more pluggable. * added _has_backend() helper for UTs to use, instead of _load_backend() hack, which wasn't respecting dryrun flag. this is mainly in preparation for issue 86: adding backend to support stdlib's hashlib.scrypt(). --- docs/lib/passlib.hash.scrypt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/lib') diff --git a/docs/lib/passlib.hash.scrypt.rst b/docs/lib/passlib.hash.scrypt.rst index 19751ca..5c3677c 100644 --- a/docs/lib/passlib.hash.scrypt.rst +++ b/docs/lib/passlib.hash.scrypt.rst @@ -55,7 +55,7 @@ Scrypt Backends This class will use the first available of two possible backends: -1. The C-accelarated `scrypt `_ package, if installed. +1. The C-accelerated `scrypt `_ package, if installed. 2. A pure-python implementation of SCrypt, built into Passlib. .. warning:: -- cgit v1.2.1 From 83d4c5b5237129e246536b7d686e7d79c4bc0fd2 Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Sun, 29 Jan 2017 16:27:43 -0500 Subject: Per issue 83, all "harden_verify" code is now deprecated & a noop. will be removed completely in 1.8. Rationale: Aside from the arguments in issue 83, performed a timing analysis, and decided harden_verify framework wasn't going to be easily workable to prevent a timing attack anyways (see attached admin/plot_verify_timing.py script). Changes: * dummy_verify() has been kept around, but now uses .verify() against a dummy hash, which is guaranteed to have correct timing (though wastes cpu cycles). * Removed most harden_verify code, treating it as NOOP just like min_verify_time. Similarly, removed most documentation references to. --- docs/lib/passlib.context.rst | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'docs/lib') diff --git a/docs/lib/passlib.context.rst b/docs/lib/passlib.context.rst index b2f7248..5ed7413 100644 --- a/docs/lib/passlib.context.rst +++ b/docs/lib/passlib.context.rst @@ -180,37 +180,21 @@ Options which directly affect the behavior of the CryptContext instance: and will be removed in version 1.8. .. versionchanged:: 1.7 - Per deprecation roadmap above, this option is now ignored. - See ``harden_verify`` below for a replacement. + Per deprecation roadmap above, this option is now ignored. .. _context-harden-verify-option: ``harden_verify`` - If set to ``true``, CryptContext will pause the first time :meth:`verify` - is called, in order to calculate the "average" time it would take - to verify a hash created using the default settings. - - Subsequent :meth:`verify` calls using will have their time padded - to this minimum time, in order to make it harder for an attacker - to guess which accounts have weak hashes. - - Applications may also wish to call :meth:`~CryptContext.dummy_verify` for login - attempts where the user does not exist, in order to mask which - users accounts have valid hashes. + Companion to ``min_verify_time``, currently ignored. - This option can be set to ``True`` or ``False`` (the default). - - The default (may) be changed in a later 2.x release. - - .. warning:: + .. versionadded:: 1.7 - This feature is new, and adjustments may need to be made - to when (and how) the code calculates what the "minimum verification time" - is supposed to be. + .. deprecated:: 1.7.1 - .. versionadded:: 1.7 + This option is ignored by 1.7.1, and will be removed in 1.8 + along with ``min_verify_time``. .. _context-algorithm-options: -- cgit v1.2.1 From 1fa1999d529392b0f7c7f8ea7ccf116abc9ae360 Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Mon, 30 Jan 2017 13:18:24 -0500 Subject: bugfix: passlib.hash.cisco_pix & cisco_asa: major overhaul of internal implementation, fixed a number of edge cases where we were generating the wrong thing. tests ----- * expanded test vectors, and ran them against an ASA 9.6 system. marked out all vectors that were verified on 9.6 system. found that user appending, and various size thresholds, weren't being performed correctly by cisco_pix & cisco_asa -- certain hashes generated prior to this will be "unverifiable" by a Cisco system. * consolidated shared pix & asa tests into common base class. * added test for "spoil_digest" codepath that's been added to cisco_pix _calc_digest() (see below). hashers ------- * cisco_pix & cisco_asa now reject too-large passwords, and silently return False when verifying them; to match Cisco behavior. updated truncate policy flags to match new behavior. * overhaul of _calc_checksum(), to fix identified issues, lay out guesses & reasoning, and added some citations. docs ---- * updated docs to list new algorithm, and conditions where the old code would generate a bad hash. * general cleanup of doc layout for these hashes * updated "Cisco Hashes" section to give better listing of known hash formats. --- docs/lib/passlib.hash.cisco_asa.rst | 12 ++-- docs/lib/passlib.hash.cisco_pix.rst | 129 +++++++++++++++++++++++------------- docs/lib/passlib.hash.rst | 55 +++++++++++++-- 3 files changed, 140 insertions(+), 56 deletions(-) (limited to 'docs/lib') diff --git a/docs/lib/passlib.hash.cisco_asa.rst b/docs/lib/passlib.hash.cisco_asa.rst index 3b9e757..5875214 100644 --- a/docs/lib/passlib.hash.cisco_asa.rst +++ b/docs/lib/passlib.hash.cisco_asa.rst @@ -1,7 +1,7 @@ .. index:: Cisco; ASA hash ================================================================== -:class:`passlib.hash.cisco_asa` - Cisco ASA hash +:class:`passlib.hash.cisco_asa` - Cisco ASA MD5 hash ================================================================== .. include:: ../_fragments/insecure_hash_warning.rst @@ -10,9 +10,11 @@ .. versionadded:: 1.7 -The :class:`!cisco_asa` handler provides support for the 2005 revision of the older :class:`!cisco_pix` hash. -The usage, functionality, and format is the same as for :class:`!cisco_pix`, +.. include:: ../_fragments/asa_verify_callout.rst -.. seealso:: +The :class:`!cisco_asa` class provides support for Cisco ASA "encrypted" hash format. +This is a revision of the older :class:`!cisco_pix` hash; +and the usage and format is the same. - :doc:`cisco_pix ` documentation page. +**See the** :doc:`cisco_pix ` **documentation page** +for combined details of both these classes. diff --git a/docs/lib/passlib.hash.cisco_pix.rst b/docs/lib/passlib.hash.cisco_pix.rst index 5b00b83..f9978f2 100644 --- a/docs/lib/passlib.hash.cisco_pix.rst +++ b/docs/lib/passlib.hash.cisco_pix.rst @@ -1,57 +1,57 @@ .. index:: Cisco; PIX hash ================================================================== -:class:`passlib.hash.cisco_pix` - Cisco PIX hash +:class:`passlib.hash.cisco_pix` - Cisco PIX MD5 hash ================================================================== +.. currentmodule:: passlib.hash + .. include:: ../_fragments/insecure_hash_warning.rst .. versionadded:: 1.6 -.. todo:: - - Passlib currently lack a thorough set of test cases for the :class:`cisco_asa` hash - For people with access to such a system, verifying passlib's reference vectors - would be a great help (see :issue:`51`). - In the mean time, there are no guarantees that its behavior correctly replicates - the official implementation. *caveat emptor*. - -.. warning:: - - This class does not correctly handle hashes generated by - Pix/ASA 7.0 (2005) or newer; particularly for passwords 13 characters or more - (:issue:`51`). A new :class:`!cisco_asa` will be added in Passlib 1.7 to support these hashes. +Overview +======== +.. include:: ../_fragments/asa_verify_callout.rst -.. currentmodule:: passlib.hash +The :class:`cisco_asa` class implements the "encrypted" password hash algorithm commonly found on Cisco +ASA systems. The companion :class:`cisco_pix` class +implements the older variant found on Cisco PIX. +Aside from internal differences, and slightly different limitations, +the two hashes have the same format, and in some cases the same output. -The :class:`cisco_pix` class implements the password hash algorithm commonly found on older Cisco -PIX firewalls. The :class:`cisco_asa` class implements a newer variant found Cisco ASA 7.0 and -newer systems. They can be used directly as follows:: +These classes can be used directly to generate or verify a hash for a specific +user. Specifying the user account name is required for this hash:: - >>> from passlib.hash import cisco_pix as pix + >>> from passlib.hash import cisco_asa >>> # hash password using specified username - >>> hash = pix.hash("password", user="user") + >>> hash = cisco_asa.hash("password", user="user") >>> hash 'A5XOy94YKDPXCo7U' >>> # verify correct password - >>> pix.verify("password", hash, user="user") + >>> cisco_asa.verify("password", hash, user="user") True + >>> # verify correct password w/ wrong username - >>> pm.verify("password", hash, user="other") + >>> cisco_asa.verify("password", hash, user="other") False + >>> # verify incorrect password - >>> pm.verify("letmein", hash, user="user") + >>> cisco_asa.verify("letmein", hash, user="user") False +The main "enable" password can be hashes / verified just by omitting +the ``user`` parameter, or setting ``user=""``:: + >>> # hash password without associated user account - >>> hash2 = pix.hash("password") + >>> hash2 = cisco_asa.hash("password") >>> hash2 'NuLKvvWGg.x9HEKO' >>> # verify password without associated user account - >>> pix.verify("password", hash2) + >>> cisco_asa.verify("password", hash2) True .. seealso:: the generic :ref:`PasswordHash usage examples ` @@ -66,8 +66,8 @@ Interface .. note:: These hash algorithms have a context-sensitive peculiarity. - It takes in an optional username, used to salt the hash, - but with specific restrictions... + They take in an optional username to salt the hash, + but have specific restrictions... * The username *must* be provided in order to correctly hash passwords associated with a user account on the Cisco device. @@ -80,15 +80,19 @@ Interface Format & Algorithm ================== -Cisco PIX / ASA hashes consist of a 12 byte digest, encoded as a 16 character +Cisco PIX & ASA hashes consist of a 12 byte digest, encoded as a 16 character :data:`HASH64 `-encoded string. An example -hash (of ``"password"``) is ``"NuLKvvWGg.x9HEKO"``. +hash (of ``"password"``, with user ``""``) is ``"NuLKvvWGg.x9HEKO"``. The PIX / ASA digests are calculated as follows: -1. The password is encoded using an ``ASCII``-compatible encoding - (all known references are strict 7-bit ascii, and Passlib uses ``UTF-8`` - to provide unicode support). +1. The password is encoded using ``UTF-8`` (though entering non-ASCII + characters is subject to interface-specific issues, and may lead + to problems such as double-encoding). + + If the result is greater than 16 bytes (for PIX), or 32 bytes (for ASA), + the password is not allowed -- it will be rejected when set, + and simplify not verify during authentication. 2. If the hash is associated with a user account, append the first four bytes of the user account name @@ -96,14 +100,18 @@ The PIX / ASA digests are calculated as follows: with a user account (e.g. it's the "enable" password), this step should be omitted. + If the user account is 1-3 bytes, it is repeated until all 4 bytes are filled + up (e.g. "usr" becomes "usru"). + For :class:`!cisco_asa`, this step is omitted if the password is 28 bytes or more. -3. The password should be truncated to 16 bytes, or the right side NULL - padded to 16 bytes, as appropriate. +3. The password+user string is truncated, or right-padded with NULLs, + until it's 16 bytes in size. For :class:`!cisco_asa`, - if the password is 13 or more bytes, the truncate/padding size is increased to 32 bytes. + if the password+user string is 16 or more bytes, + a padding size of 32 is used instead. 4. Run the result of step 3 through MD5. @@ -112,6 +120,10 @@ The PIX / ASA digests are calculated as follows: 6. Encode the 12-byte result using :data:`HASH64 `. +.. versionchanged:: 1.7.1 + + Updated to reflect current understanding of the algorithm. + Security Issues =============== This algorithm is not suitable for *any* use besides manipulating existing @@ -138,22 +150,44 @@ Cisco PIX hashes, due to the following flaws: Deviations ========== -This implementation differs from the standard in one main way: +This implementation tries to adhere to the canonical Cisco implementation, +but without an official specification, there may be other unknown deviations. +The following are known issues: * Unicode Policy: - The official Cisco PIX algorithm is primarily used with ``ascii`` passwords, - how it handles other characters is not known. + ASA documentation [#charset]_ indicates it uses UTF-8 encoding, + and Passlib does as well. However, some ASA interfaces + have issues such as: ASDM may double-encode unicode characters, + and SSH connections may drop non-ASCII characters entirely. + +* How usernames are added is not entirely pinned down. Under ASA, 3-character + usernames have their last character repeated to make a string of length 4. + It is currently assumed that a similar repetition would be applied to + usernames of 1-2 characters, and that this applies to PIX as well; + though neither assumption has been confirmed. + +* .. _passlib-asa96-bug: - In order to provide support for unicode strings, - Passlib will encode unicode passwords using ``utf-8`` - before running them through this algorithm. If a different - encoding is desired by an application, the password should be encoded - before handing it to Passlib. + **Passlib 1.7.1 Bugfix**: Prior releases of Passlib had a number of issues + with their implementation of the PIX & ASA algorithms. As of 1.7.1, + the reference vectors were greatly expanded, and then tested against + an ASA 9.6 system. This revealed a number of errors in passlib's implementation, + which under the following conditions would create hashes that were + unverifiable on a Cisco system: -* While this implementation agrees with all known references, - the actual algorithm has not been published by Cisco, so there may be other - unknown deviations. + - PIX and ASA: Usernames containing 1-3 characters were not appended correctly (step 2, above). + + - ASA omits the user entirely (step 2, above) for passwords with >= 28 characters, + not >= 27. Non-enable passwords of exactly 27 characters were previous hashed + incorrectly. + + - ASA's padding size decision (step 3, above) is made after the user + has been appended, not before. This caused prior releases to + incorrectly hash non-enable passwords of length 13-15. + + Anyone relying on cisco_asa or cisco_pix should upgrade to Passlib 1.7.1 or newer + to avoid these issues. .. rubric:: Footnotes @@ -166,3 +200,6 @@ This implementation differs from the standard in one main way: .. [#] Partial description of ASA algorithm - ``_ + +.. [#charset] Character set used by ASA 8.4 - + ``_ diff --git a/docs/lib/passlib.hash.rst b/docs/lib/passlib.hash.rst index 3211066..94580e8 100644 --- a/docs/lib/passlib.hash.rst +++ b/docs/lib/passlib.hash.rst @@ -238,20 +238,65 @@ no identifying markers, identifying them is pretty much context-dependant. Cisco Hashes ============ -The following hashes are used in various places on Cisco IOS and ASA devices: +.. + TODO: + + What was/were IOS types 1, 2, 3, and 6? Don't see many references. + Think type 6 is a reversible encryption format ala type 7, + per https://supportforums.cisco.com/discussion/11733226/when-use-type-6-encrypted-or-type-7-encrypted + + +**Cisco IOS** + +The following hashes are used in various places on Cisco IOS, and +are usually referred to by a Cisco-assigned "type" code: + +.. rst-class:: hidden .. toctree:: :maxdepth: 1 - passlib.hash.cisco_pix - passlib.hash.cisco_asa + passlib.hash.cisco_type7 + +* :doc:`passlib.hash.md5_crypt ` -- "Type 5" hashes are actually just the standard + Unix MD5-Crypt hash, the format is identical. + +* :doc:`passlib.hash.cisco_type7 ` -- "Type 7" isn't actually a hash, + but a reversible encoding designed to obscure passwords from idle view. + +* "Type 8" hashes are based on PBKDF2-HMAC-SHA256; + but not currently supported by passlib (:issue:`87`). -* **Cisco "Type 5" hashes** - these are the same as :doc:`md5_crypt ` +* "Type 9" hashes are based on scrypt; + but not currently supported by passlib (:issue:`87`). + +**Cisco PIX & ASA** + +Separately from this, Cisco PIX & ASA firewalls have their own hash formats, +generally identified by the "format" parameter in the :samp:`username {user} password {hash} {format}` config line +they occur in. The following are known & handled by passlib: + +.. rst-class:: hidden .. toctree:: :maxdepth: 1 - passlib.hash.cisco_type7 + passlib.hash.cisco_pix + passlib.hash.cisco_asa + +* :doc:`passlib.hash.cisco_pix ` -- PIX "encrypted" hashes + use a simple unsalted MD5-based algorithm. + +* :doc:`passlib.hash.cisco_asa ` -- ASA "encrypted" hashes + use a similar algorithm to PIX, with some minor improvements. + +* ASA "nt-encrypted" hashes + are the same as :class:`passlib.hash.nthash`, + except that they use base64 encoding rather than hexadecimal. + +* ASA 9.5 added support for "pbkdf2" hashes + (based on PBKDF2-HMAC-SHA512); which aren't currently supported + by passlib (:issue:`87`). .. _other-hashes: -- cgit v1.2.1 From 724af9c14f7ccee0e2f09d06600b244d79931cdf Mon Sep 17 00:00:00 2001 From: John McLaughlin Date: Sun, 29 Jan 2017 18:16:35 +0000 Subject: passlib.hash.bsdi_crypt.rst edited online with Bitbucket (grafted from 2a0ee135a56568ff8442cdbc2775adc1ae8f304f) --- docs/lib/passlib.hash.bsdi_crypt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/lib') diff --git a/docs/lib/passlib.hash.bsdi_crypt.rst b/docs/lib/passlib.hash.bsdi_crypt.rst index dde7dee..605f190 100644 --- a/docs/lib/passlib.hash.bsdi_crypt.rst +++ b/docs/lib/passlib.hash.bsdi_crypt.rst @@ -48,7 +48,7 @@ Interface Format ====== An example hash (of the string ``password``) is ``_EQ0.jzhSVeUyoSqLupI``. -A bsdi_crypt hash string consists of a 21 character string of the form :samp:`_{rounds}{salt}{checksum}`. +A bsdi_crypt hash string consists of a 20 character string of the form :samp:`_{rounds}{salt}{checksum}`. All characters except the underscore prefix are drawn from ``[./0-9A-Za-z]``. * ``_`` - the underscore is used to distinguish this scheme from others, such as des-crypt. -- cgit v1.2.1 From 39b8e96a1ff27462dcf8ed90d8747b31abc7b53f Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Mon, 30 Jan 2017 21:24:25 -0500 Subject: passlib.apache: updated internal comments --- docs/lib/passlib.apache.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/lib') diff --git a/docs/lib/passlib.apache.rst b/docs/lib/passlib.apache.rst index 6274159..b87eb63 100644 --- a/docs/lib/passlib.apache.rst +++ b/docs/lib/passlib.apache.rst @@ -16,7 +16,7 @@ htpasswd and htdigest files; though the use of two helper classes. .. versionchanged:: 1.7 - These classes will now preserve blank lines & "#" comments when updating + These classes will now preserve blank lines and "#" comments when updating htpasswd files; previous releases would throw a parse error. .. index:: Apache; htpasswd -- cgit v1.2.1