Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

1688 rader
61 KiB

  1. """passlib.tests.test_handlers - tests for passlib hash algorithms"""
  2. #=============================================================================
  3. # imports
  4. #=============================================================================
  5. from __future__ import with_statement
  6. # core
  7. import logging; log = logging.getLogger(__name__)
  8. import os
  9. import sys
  10. import warnings
  11. # site
  12. # pkg
  13. from passlib import hash
  14. from passlib.utils import repeat_string
  15. from passlib.utils.compat import irange, PY3, u, get_method_function
  16. from passlib.tests.utils import TestCase, HandlerCase, skipUnless, \
  17. TEST_MODE, UserHandlerMixin, EncodingHandlerMixin
  18. # module
  19. #=============================================================================
  20. # constants & support
  21. #=============================================================================
  22. # some common unicode passwords which used as test cases
  23. UPASS_WAV = u('\u0399\u03c9\u03b1\u03bd\u03bd\u03b7\u03c2')
  24. UPASS_USD = u("\u20AC\u00A5$")
  25. UPASS_TABLE = u("t\u00e1\u0411\u2113\u0259")
  26. PASS_TABLE_UTF8 = b't\xc3\xa1\xd0\x91\xe2\x84\x93\xc9\x99' # utf-8
  27. # handlers which support multiple backends, but don't have multi-backend tests.
  28. _omitted_backend_tests = ["django_bcrypt", "django_bcrypt_sha256", "django_argon2"]
  29. #: modules where get_handler_case() should search for test cases.
  30. _handler_test_modules = [
  31. "test_handlers",
  32. "test_handlers_argon2",
  33. "test_handlers_bcrypt",
  34. "test_handlers_cisco",
  35. "test_handlers_django",
  36. "test_handlers_pbkdf2",
  37. "test_handlers_scrypt",
  38. ]
  39. def get_handler_case(scheme):
  40. """return HandlerCase instance for scheme, used by other tests"""
  41. from passlib.registry import get_crypt_handler
  42. handler = get_crypt_handler(scheme)
  43. if hasattr(handler, "backends") and scheme not in _omitted_backend_tests:
  44. # NOTE: will throw MissingBackendError if none are installed.
  45. backend = handler.get_backend()
  46. name = "%s_%s_test" % (scheme, backend)
  47. else:
  48. name = "%s_test" % scheme
  49. for module in _handler_test_modules:
  50. modname = "passlib.tests." + module
  51. __import__(modname)
  52. mod = sys.modules[modname]
  53. try:
  54. return getattr(mod, name)
  55. except AttributeError:
  56. pass
  57. raise KeyError("test case %r not found" % name)
  58. #: hashes which there may not be a backend available for,
  59. #: and get_handler_case() may (correctly) throw a MissingBackendError
  60. conditionally_available_hashes = ["argon2", "bcrypt", "bcrypt_sha256"]
  61. #=============================================================================
  62. # apr md5 crypt
  63. #=============================================================================
  64. class apr_md5_crypt_test(HandlerCase):
  65. handler = hash.apr_md5_crypt
  66. known_correct_hashes = [
  67. #
  68. # http://httpd.apache.org/docs/2.2/misc/password_encryptions.html
  69. #
  70. ('myPassword', '$apr1$r31.....$HqJZimcKQFAMYayBlzkrA/'),
  71. #
  72. # custom
  73. #
  74. # ensures utf-8 used for unicode
  75. (UPASS_TABLE, '$apr1$bzYrOHUx$a1FcpXuQDJV3vPY20CS6N1'),
  76. ]
  77. known_malformed_hashes = [
  78. # bad char in otherwise correct hash ----\/
  79. '$apr1$r31.....$HqJZimcKQFAMYayBlzkrA!'
  80. ]
  81. #=============================================================================
  82. # bigcrypt
  83. #=============================================================================
  84. class bigcrypt_test(HandlerCase):
  85. handler = hash.bigcrypt
  86. # TODO: find an authoritative source of test vectors
  87. known_correct_hashes = [
  88. #
  89. # various docs & messages on the web.
  90. #
  91. ("passphrase", "qiyh4XPJGsOZ2MEAyLkfWqeQ"),
  92. ("This is very long passwd", "f8.SVpL2fvwjkAnxn8/rgTkwvrif6bjYB5c"),
  93. #
  94. # custom
  95. #
  96. # ensures utf-8 used for unicode
  97. (UPASS_TABLE, 'SEChBAyMbMNhgGLyP7kD1HZU'),
  98. ]
  99. known_unidentified_hashes = [
  100. # one char short (10 % 11)
  101. "qiyh4XPJGsOZ2MEAyLkfWqe"
  102. # one char too many (1 % 11)
  103. "f8.SVpL2fvwjkAnxn8/rgTkwvrif6bjYB5cd"
  104. ]
  105. # omit des_crypt from known_other since it's a valid bigcrypt hash too.
  106. known_other_hashes = [row for row in HandlerCase.known_other_hashes
  107. if row[0] != "des_crypt"]
  108. def test_90_internal(self):
  109. # check that _norm_checksum() also validates checksum size.
  110. # (current code uses regex in parser)
  111. self.assertRaises(ValueError, hash.bigcrypt, use_defaults=True,
  112. checksum=u('yh4XPJGsOZ'))
  113. #=============================================================================
  114. # bsdi crypt
  115. #=============================================================================
  116. class _bsdi_crypt_test(HandlerCase):
  117. """test BSDiCrypt algorithm"""
  118. handler = hash.bsdi_crypt
  119. known_correct_hashes = [
  120. #
  121. # from JTR 1.7.9
  122. #
  123. ('U*U*U*U*', '_J9..CCCCXBrJUJV154M'),
  124. ('U*U***U', '_J9..CCCCXUhOBTXzaiE'),
  125. ('U*U***U*', '_J9..CCCC4gQ.mB/PffM'),
  126. ('*U*U*U*U', '_J9..XXXXvlzQGqpPPdk'),
  127. ('*U*U*U*U*', '_J9..XXXXsqM/YSSP..Y'),
  128. ('*U*U*U*U*U*U*U*U', '_J9..XXXXVL7qJCnku0I'),
  129. ('*U*U*U*U*U*U*U*U*', '_J9..XXXXAj8cFbP5scI'),
  130. ('ab1234567', '_J9..SDizh.vll5VED9g'),
  131. ('cr1234567', '_J9..SDizRjWQ/zePPHc'),
  132. ('zxyDPWgydbQjgq', '_J9..SDizxmRI1GjnQuE'),
  133. ('726 even', '_K9..SaltNrQgIYUAeoY'),
  134. ('', '_J9..SDSD5YGyRCr4W4c'),
  135. #
  136. # custom
  137. #
  138. (" ", "_K1..crsmZxOLzfJH8iw"),
  139. ("my", '_KR/.crsmykRplHbAvwA'), # <-- to detect old 12-bit rounds bug
  140. ("my socra", "_K1..crsmf/9NzZr1fLM"),
  141. ("my socrates", '_K1..crsmOv1rbde9A9o'),
  142. ("my socrates note", "_K1..crsm/2qeAhdISMA"),
  143. # ensures utf-8 used for unicode
  144. (UPASS_TABLE, '_7C/.ABw0WIKy0ILVqo2'),
  145. ]
  146. known_unidentified_hashes = [
  147. # bad char in otherwise correctly formatted hash
  148. # \/
  149. "_K1.!crsmZxOLzfJH8iw"
  150. ]
  151. platform_crypt_support = [
  152. ("freebsd|openbsd|netbsd|darwin", True),
  153. ("linux|solaris", False),
  154. ]
  155. def test_77_fuzz_input(self, **kwds):
  156. # we want to generate even rounds to verify it's correct, but want to ignore warnings
  157. warnings.filterwarnings("ignore", "bsdi_crypt rounds should be odd.*")
  158. super(_bsdi_crypt_test, self).test_77_fuzz_input(**kwds)
  159. def test_needs_update_w_even_rounds(self):
  160. """needs_update() should flag even rounds"""
  161. handler = self.handler
  162. even_hash = '_Y/../cG0zkJa6LY6k4c'
  163. odd_hash = '_Z/..TgFg0/ptQtpAgws'
  164. secret = 'test'
  165. # don't issue warning
  166. self.assertTrue(handler.verify(secret, even_hash))
  167. self.assertTrue(handler.verify(secret, odd_hash))
  168. # *do* signal as needing updates
  169. self.assertTrue(handler.needs_update(even_hash))
  170. self.assertFalse(handler.needs_update(odd_hash))
  171. # new hashes shouldn't have even rounds
  172. new_hash = handler.hash("stub")
  173. self.assertFalse(handler.needs_update(new_hash))
  174. # create test cases for specific backends
  175. bsdi_crypt_os_crypt_test = _bsdi_crypt_test.create_backend_case("os_crypt")
  176. bsdi_crypt_builtin_test = _bsdi_crypt_test.create_backend_case("builtin")
  177. #=============================================================================
  178. # crypt16
  179. #=============================================================================
  180. class crypt16_test(HandlerCase):
  181. handler = hash.crypt16
  182. # TODO: find an authortative source of test vectors
  183. known_correct_hashes = [
  184. #
  185. # from messages around the web, including
  186. # http://seclists.org/bugtraq/1999/Mar/76
  187. #
  188. ("passphrase", "qi8H8R7OM4xMUNMPuRAZxlY."),
  189. ("printf", "aaCjFz4Sh8Eg2QSqAReePlq6"),
  190. ("printf", "AA/xje2RyeiSU0iBY3PDwjYo"),
  191. ("LOLOAQICI82QB4IP", "/.FcK3mad6JwYt8LVmDqz9Lc"),
  192. ("LOLOAQICI", "/.FcK3mad6JwYSaRHJoTPzY2"),
  193. ("LOLOAQIC", "/.FcK3mad6JwYelhbtlysKy6"),
  194. ("L", "/.CIu/PzYCkl6elhbtlysKy6"),
  195. #
  196. # custom
  197. #
  198. # ensures utf-8 used for unicode
  199. (UPASS_TABLE, 'YeDc9tKkkmDvwP7buzpwhoqQ'),
  200. ]
  201. #=============================================================================
  202. # des crypt
  203. #=============================================================================
  204. class _des_crypt_test(HandlerCase):
  205. """test des-crypt algorithm"""
  206. handler = hash.des_crypt
  207. known_correct_hashes = [
  208. #
  209. # from JTR 1.7.9
  210. #
  211. ('U*U*U*U*', 'CCNf8Sbh3HDfQ'),
  212. ('U*U***U', 'CCX.K.MFy4Ois'),
  213. ('U*U***U*', 'CC4rMpbg9AMZ.'),
  214. ('*U*U*U*U', 'XXxzOu6maQKqQ'),
  215. ('', 'SDbsugeBiC58A'),
  216. #
  217. # custom
  218. #
  219. ('', 'OgAwTx2l6NADI'),
  220. (' ', '/Hk.VPuwQTXbc'),
  221. ('test', 'N1tQbOFcM5fpg'),
  222. ('Compl3X AlphaNu3meric', 'um.Wguz3eVCx2'),
  223. ('4lpHa N|_|M3r1K W/ Cur5Es: #$%(*)(*%#', 'sNYqfOyauIyic'),
  224. ('AlOtBsOl', 'cEpWz5IUCShqM'),
  225. # ensures utf-8 used for unicode
  226. (u('hell\u00D6'), 'saykDgk3BPZ9E'),
  227. ]
  228. known_unidentified_hashes = [
  229. # bad char in otherwise correctly formatted hash
  230. #\/
  231. '!gAwTx2l6NADI',
  232. # wrong size
  233. 'OgAwTx2l6NAD',
  234. 'OgAwTx2l6NADIj',
  235. ]
  236. platform_crypt_support = [
  237. ("freebsd|openbsd|netbsd|linux|solaris|darwin", True),
  238. ]
  239. # create test cases for specific backends
  240. des_crypt_os_crypt_test = _des_crypt_test.create_backend_case("os_crypt")
  241. des_crypt_builtin_test = _des_crypt_test.create_backend_case("builtin")
  242. #=============================================================================
  243. # fshp
  244. #=============================================================================
  245. class fshp_test(HandlerCase):
  246. """test fshp algorithm"""
  247. handler = hash.fshp
  248. known_correct_hashes = [
  249. #
  250. # test vectors from FSHP reference implementation
  251. # https://github.com/bdd/fshp-is-not-secure-anymore/blob/master/python/test.py
  252. #
  253. ('test', '{FSHP0|0|1}qUqP5cyxm6YcTAhz05Hph5gvu9M='),
  254. ('test',
  255. '{FSHP1|8|4096}MTIzNDU2NzjTdHcmoXwNc0f'
  256. 'f9+ArUHoN0CvlbPZpxFi1C6RDM/MHSA=='
  257. ),
  258. ('OrpheanBeholderScryDoubt',
  259. '{FSHP1|8|4096}GVSUFDAjdh0vBosn1GUhz'
  260. 'GLHP7BmkbCZVH/3TQqGIjADXpc+6NCg3g=='
  261. ),
  262. ('ExecuteOrder66',
  263. '{FSHP3|16|8192}0aY7rZQ+/PR+Rd5/I9ss'
  264. 'RM7cjguyT8ibypNaSp/U1uziNO3BVlg5qPU'
  265. 'ng+zHUDQC3ao/JbzOnIBUtAeWHEy7a2vZeZ'
  266. '7jAwyJJa2EqOsq4Io='
  267. ),
  268. #
  269. # custom
  270. #
  271. # ensures utf-8 used for unicode
  272. (UPASS_TABLE, '{FSHP1|16|16384}9v6/l3Lu/d9by5nznpOS'
  273. 'cqQo8eKu/b/CKli3RCkgYg4nRTgZu5y659YV8cCZ68UL'),
  274. ]
  275. known_unidentified_hashes = [
  276. # incorrect header
  277. '{FSHX0|0|1}qUqP5cyxm6YcTAhz05Hph5gvu9M=',
  278. 'FSHP0|0|1}qUqP5cyxm6YcTAhz05Hph5gvu9M=',
  279. ]
  280. known_malformed_hashes = [
  281. # bad base64 padding
  282. '{FSHP0|0|1}qUqP5cyxm6YcTAhz05Hph5gvu9M',
  283. # wrong salt size
  284. '{FSHP0|1|1}qUqP5cyxm6YcTAhz05Hph5gvu9M=',
  285. # bad rounds
  286. '{FSHP0|0|A}qUqP5cyxm6YcTAhz05Hph5gvu9M=',
  287. ]
  288. def test_90_variant(self):
  289. """test variant keyword"""
  290. handler = self.handler
  291. kwds = dict(salt=b'a', rounds=1)
  292. # accepts ints
  293. handler(variant=1, **kwds)
  294. # accepts bytes or unicode
  295. handler(variant=u('1'), **kwds)
  296. handler(variant=b'1', **kwds)
  297. # aliases
  298. handler(variant=u('sha256'), **kwds)
  299. handler(variant=b'sha256', **kwds)
  300. # rejects None
  301. self.assertRaises(TypeError, handler, variant=None, **kwds)
  302. # rejects other types
  303. self.assertRaises(TypeError, handler, variant=complex(1,1), **kwds)
  304. # invalid variant
  305. self.assertRaises(ValueError, handler, variant='9', **kwds)
  306. self.assertRaises(ValueError, handler, variant=9, **kwds)
  307. #=============================================================================
  308. # hex digests
  309. #=============================================================================
  310. class hex_md4_test(HandlerCase):
  311. handler = hash.hex_md4
  312. known_correct_hashes = [
  313. ("password", '8a9d093f14f8701df17732b2bb182c74'),
  314. (UPASS_TABLE, '876078368c47817ce5f9115f3a42cf74'),
  315. ]
  316. class hex_md5_test(HandlerCase):
  317. handler = hash.hex_md5
  318. known_correct_hashes = [
  319. ("password", '5f4dcc3b5aa765d61d8327deb882cf99'),
  320. (UPASS_TABLE, '05473f8a19f66815e737b33264a0d0b0'),
  321. ]
  322. class hex_sha1_test(HandlerCase):
  323. handler = hash.hex_sha1
  324. known_correct_hashes = [
  325. ("password", '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'),
  326. (UPASS_TABLE, 'e059b2628e3a3e2de095679de9822c1d1466e0f0'),
  327. ]
  328. class hex_sha256_test(HandlerCase):
  329. handler = hash.hex_sha256
  330. known_correct_hashes = [
  331. ("password", '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'),
  332. (UPASS_TABLE, '6ed729e19bf24d3d20f564375820819932029df05547116cfc2cc868a27b4493'),
  333. ]
  334. class hex_sha512_test(HandlerCase):
  335. handler = hash.hex_sha512
  336. known_correct_hashes = [
  337. ("password", 'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c'
  338. '706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cac'
  339. 'bc86'),
  340. (UPASS_TABLE, 'd91bb0a23d66dca07a1781fd63ae6a05f6919ee5fc368049f350c9f'
  341. '293b078a18165d66097cf0d89fdfbeed1ad6e7dba2344e57348cd6d51308c843a06f'
  342. '29caf'),
  343. ]
  344. #=============================================================================
  345. # htdigest hash
  346. #=============================================================================
  347. class htdigest_test(UserHandlerMixin, HandlerCase):
  348. handler = hash.htdigest
  349. known_correct_hashes = [
  350. # secret, user, realm
  351. # from RFC 2617
  352. (("Circle Of Life", "Mufasa", "testrealm@host.com"),
  353. '939e7578ed9e3c518a452acee763bce9'),
  354. # custom
  355. ((UPASS_TABLE, UPASS_USD, UPASS_WAV),
  356. '4dabed2727d583178777fab468dd1f17'),
  357. ]
  358. known_unidentified_hashes = [
  359. # bad char \/ - currently rejecting upper hex chars, may change
  360. '939e7578edAe3c518a452acee763bce9',
  361. # bad char \/
  362. '939e7578edxe3c518a452acee763bce9',
  363. ]
  364. def test_80_user(self):
  365. raise self.skipTest("test case doesn't support 'realm' keyword")
  366. def populate_context(self, secret, kwds):
  367. """insert username into kwds"""
  368. if isinstance(secret, tuple):
  369. secret, user, realm = secret
  370. else:
  371. user, realm = "user", "realm"
  372. kwds.setdefault("user", user)
  373. kwds.setdefault("realm", realm)
  374. return secret
  375. #=============================================================================
  376. # ldap hashes
  377. #=============================================================================
  378. class ldap_md5_test(HandlerCase):
  379. handler = hash.ldap_md5
  380. known_correct_hashes = [
  381. ("helloworld", '{MD5}/F4DjTilcDIIVEHn/nAQsA=='),
  382. (UPASS_TABLE, '{MD5}BUc/ihn2aBXnN7MyZKDQsA=='),
  383. ]
  384. class ldap_sha1_test(HandlerCase):
  385. handler = hash.ldap_sha1
  386. known_correct_hashes = [
  387. ("helloworld", '{SHA}at+xg6SiyUovktq1redipHiJpaE='),
  388. (UPASS_TABLE, '{SHA}4FmyYo46Pi3glWed6YIsHRRm4PA='),
  389. ]
  390. class ldap_salted_md5_test(HandlerCase):
  391. handler = hash.ldap_salted_md5
  392. known_correct_hashes = [
  393. ("testing1234", '{SMD5}UjFY34os/pnZQ3oQOzjqGu4yeXE='),
  394. (UPASS_TABLE, '{SMD5}Z0ioJ58LlzUeRxm3K6JPGAvBGIM='),
  395. # alternate salt sizes (8, 15, 16)
  396. ('test', '{SMD5}LnuZPJhiaY95/4lmVFpg548xBsD4P4cw'),
  397. ('test', '{SMD5}XRlncfRzvGi0FDzgR98tUgBg7B3jXOs9p9S615qTkg=='),
  398. ('test', '{SMD5}FbAkzOMOxRbMp6Nn4hnZuel9j9Gas7a2lvI+x5hT6j0='),
  399. ]
  400. known_malformed_hashes = [
  401. # salt too small (3)
  402. '{SMD5}IGVhwK+anvspmfDt2t0vgGjt/Q==',
  403. # incorrect base64 encoding
  404. '{SMD5}LnuZPJhiaY95/4lmVFpg548xBsD4P4c',
  405. '{SMD5}LnuZPJhiaY95/4lmVFpg548xBsD4P4cw'
  406. '{SMD5}LnuZPJhiaY95/4lmVFpg548xBsD4P4cw=',
  407. '{SMD5}LnuZPJhiaY95/4lmV=pg548xBsD4P4cw',
  408. '{SMD5}LnuZPJhiaY95/4lmVFpg548xBsD4P===',
  409. ]
  410. class ldap_salted_sha1_test(HandlerCase):
  411. handler = hash.ldap_salted_sha1
  412. known_correct_hashes = [
  413. ("testing123", '{SSHA}0c0blFTXXNuAMHECS4uxrj3ZieMoWImr'),
  414. ("secret", "{SSHA}0H+zTv8o4MR4H43n03eCsvw1luG8LdB7"),
  415. (UPASS_TABLE, '{SSHA}3yCSD1nLZXznra4N8XzZgAL+s1sQYsx5'),
  416. # alternate salt sizes (8, 15, 16)
  417. ('test', '{SSHA}P90+qijSp8MJ1tN25j5o1PflUvlqjXHOGeOckw=='),
  418. ('test', '{SSHA}/ZMF5KymNM+uEOjW+9STKlfCFj51bg3BmBNCiPHeW2ttbU0='),
  419. ('test', '{SSHA}Pfx6Vf48AT9x3FVv8znbo8WQkEVSipHSWovxXmvNWUvp/d/7'),
  420. ]
  421. known_malformed_hashes = [
  422. # salt too small (3)
  423. '{SSHA}ZQK3Yvtvl6wtIRoISgMGPkcWU7Nfq5U=',
  424. # incorrect base64 encoding
  425. '{SSHA}P90+qijSp8MJ1tN25j5o1PflUvlqjXHOGeOck',
  426. '{SSHA}P90+qijSp8MJ1tN25j5o1PflUvlqjXHOGeOckw=',
  427. '{SSHA}P90+qijSp8MJ1tN25j5o1Pf=UvlqjXHOGeOckw==',
  428. '{SSHA}P90+qijSp8MJ1tN25j5o1PflUvlqjXHOGeOck===',
  429. ]
  430. class ldap_plaintext_test(HandlerCase):
  431. # TODO: integrate EncodingHandlerMixin
  432. handler = hash.ldap_plaintext
  433. known_correct_hashes = [
  434. ("password", 'password'),
  435. (UPASS_TABLE, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
  436. (PASS_TABLE_UTF8, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
  437. ]
  438. known_unidentified_hashes = [
  439. "{FOO}bar",
  440. # NOTE: this hash currently rejects the empty string.
  441. "",
  442. ]
  443. known_other_hashes = [
  444. ("ldap_md5", "{MD5}/F4DjTilcDIIVEHn/nAQsA==")
  445. ]
  446. class FuzzHashGenerator(HandlerCase.FuzzHashGenerator):
  447. def random_password(self):
  448. # NOTE: this hash currently rejects the empty string.
  449. while True:
  450. pwd = super(ldap_plaintext_test.FuzzHashGenerator, self).random_password()
  451. if pwd:
  452. return pwd
  453. class _ldap_md5_crypt_test(HandlerCase):
  454. # NOTE: since the ldap_{crypt} handlers are all wrappers, don't need
  455. # separate test; this is just to test the codebase end-to-end
  456. handler = hash.ldap_md5_crypt
  457. known_correct_hashes = [
  458. #
  459. # custom
  460. #
  461. ('', '{CRYPT}$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.'),
  462. (' ', '{CRYPT}$1$m/5ee7ol$bZn0kIBFipq39e.KDXX8I0'),
  463. ('test', '{CRYPT}$1$ec6XvcoW$ghEtNK2U1MC5l.Dwgi3020'),
  464. ('Compl3X AlphaNu3meric', '{CRYPT}$1$nX1e7EeI$ljQn72ZUgt6Wxd9hfvHdV0'),
  465. ('4lpHa N|_|M3r1K W/ Cur5Es: #$%(*)(*%#', '{CRYPT}$1$jQS7o98J$V6iTcr71CGgwW2laf17pi1'),
  466. ('test', '{CRYPT}$1$SuMrG47N$ymvzYjr7QcEQjaK5m1PGx1'),
  467. # ensures utf-8 used for unicode
  468. (UPASS_TABLE, '{CRYPT}$1$d6/Ky1lU$/xpf8m7ftmWLF.TjHCqel0'),
  469. ]
  470. known_malformed_hashes = [
  471. # bad char in otherwise correct hash
  472. '{CRYPT}$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o!',
  473. ]
  474. # create test cases for specific backends
  475. ldap_md5_crypt_os_crypt_test =_ldap_md5_crypt_test.create_backend_case("os_crypt")
  476. ldap_md5_crypt_builtin_test =_ldap_md5_crypt_test.create_backend_case("builtin")
  477. class _ldap_sha1_crypt_test(HandlerCase):
  478. # NOTE: this isn't for testing the hash (see ldap_md5_crypt note)
  479. # but as a self-test of the os_crypt patching code in HandlerCase.
  480. handler = hash.ldap_sha1_crypt
  481. known_correct_hashes = [
  482. ('password', '{CRYPT}$sha1$10$c.mcTzCw$gF8UeYst9yXX7WNZKc5Fjkq0.au7'),
  483. (UPASS_TABLE, '{CRYPT}$sha1$10$rnqXlOsF$aGJf.cdRPewJAXo1Rn1BkbaYh0fP'),
  484. ]
  485. def populate_settings(self, kwds):
  486. kwds.setdefault("rounds", 10)
  487. super(_ldap_sha1_crypt_test, self).populate_settings(kwds)
  488. def test_77_fuzz_input(self):
  489. raise self.skipTest("unneeded")
  490. # create test cases for specific backends
  491. ldap_sha1_crypt_os_crypt_test = _ldap_sha1_crypt_test.create_backend_case("os_crypt")
  492. #=============================================================================
  493. # lanman
  494. #=============================================================================
  495. class lmhash_test(EncodingHandlerMixin, HandlerCase):
  496. handler = hash.lmhash
  497. secret_case_insensitive = True
  498. known_correct_hashes = [
  499. #
  500. # http://msdn.microsoft.com/en-us/library/cc245828(v=prot.10).aspx
  501. #
  502. ("OLDPASSWORD", "c9b81d939d6fd80cd408e6b105741864"),
  503. ("NEWPASSWORD", '09eeab5aa415d6e4d408e6b105741864'),
  504. ("welcome", "c23413a8a1e7665faad3b435b51404ee"),
  505. #
  506. # custom
  507. #
  508. ('', 'aad3b435b51404eeaad3b435b51404ee'),
  509. ('zzZZZzz', 'a5e6066de61c3e35aad3b435b51404ee'),
  510. ('passphrase', '855c3697d9979e78ac404c4ba2c66533'),
  511. ('Yokohama', '5ecd9236d21095ce7584248b8d2c9f9e'),
  512. # ensures cp437 used for unicode
  513. (u('ENCYCLOP\xC6DIA'), 'fed6416bffc9750d48462b9d7aaac065'),
  514. (u('encyclop\xE6dia'), 'fed6416bffc9750d48462b9d7aaac065'),
  515. # test various encoding values
  516. ((u("\xC6"), None), '25d8ab4a0659c97aaad3b435b51404ee'),
  517. ((u("\xC6"), "cp437"), '25d8ab4a0659c97aaad3b435b51404ee'),
  518. ((u("\xC6"), "latin-1"), '184eecbbe9991b44aad3b435b51404ee'),
  519. ((u("\xC6"), "utf-8"), '00dd240fcfab20b8aad3b435b51404ee'),
  520. ]
  521. known_unidentified_hashes = [
  522. # bad char in otherwise correct hash
  523. '855c3697d9979e78ac404c4ba2c6653X',
  524. ]
  525. def test_90_raw(self):
  526. """test lmhash.raw() method"""
  527. from binascii import unhexlify
  528. from passlib.utils.compat import str_to_bascii
  529. lmhash = self.handler
  530. for secret, hash in self.known_correct_hashes:
  531. kwds = {}
  532. secret = self.populate_context(secret, kwds)
  533. data = unhexlify(str_to_bascii(hash))
  534. self.assertEqual(lmhash.raw(secret, **kwds), data)
  535. self.assertRaises(TypeError, lmhash.raw, 1)
  536. #=============================================================================
  537. # md5 crypt
  538. #=============================================================================
  539. class _md5_crypt_test(HandlerCase):
  540. handler = hash.md5_crypt
  541. known_correct_hashes = [
  542. #
  543. # from JTR 1.7.9
  544. #
  545. ('U*U*U*U*', '$1$dXc3I7Rw$ctlgjDdWJLMT.qwHsWhXR1'),
  546. ('U*U***U', '$1$dXc3I7Rw$94JPyQc/eAgQ3MFMCoMF.0'),
  547. ('U*U***U*', '$1$dXc3I7Rw$is1mVIAEtAhIzSdfn5JOO0'),
  548. ('*U*U*U*U', '$1$eQT9Hwbt$XtuElNJD.eW5MN5UCWyTQ0'),
  549. ('', '$1$Eu.GHtia$CFkL/nE1BYTlEPiVx1VWX0'),
  550. #
  551. # custom
  552. #
  553. # NOTE: would need to patch HandlerCase to coerce hashes
  554. # to native str for this first one to work under py3.
  555. ## ('', b('$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.')),
  556. ('', '$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.'),
  557. (' ', '$1$m/5ee7ol$bZn0kIBFipq39e.KDXX8I0'),
  558. ('test', '$1$ec6XvcoW$ghEtNK2U1MC5l.Dwgi3020'),
  559. ('Compl3X AlphaNu3meric', '$1$nX1e7EeI$ljQn72ZUgt6Wxd9hfvHdV0'),
  560. ('4lpHa N|_|M3r1K W/ Cur5Es: #$%(*)(*%#', '$1$jQS7o98J$V6iTcr71CGgwW2laf17pi1'),
  561. ('test', '$1$SuMrG47N$ymvzYjr7QcEQjaK5m1PGx1'),
  562. (b'test', '$1$SuMrG47N$ymvzYjr7QcEQjaK5m1PGx1'),
  563. (u('s'), '$1$ssssssss$YgmLTApYTv12qgTwBoj8i/'),
  564. # ensures utf-8 used for unicode
  565. (UPASS_TABLE, '$1$d6/Ky1lU$/xpf8m7ftmWLF.TjHCqel0'),
  566. ]
  567. known_malformed_hashes = [
  568. # bad char in otherwise correct hash \/
  569. '$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o!',
  570. # too many fields
  571. '$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.$',
  572. ]
  573. platform_crypt_support = [
  574. ("freebsd|openbsd|netbsd|linux|solaris", True),
  575. ("darwin", False),
  576. ]
  577. # create test cases for specific backends
  578. md5_crypt_os_crypt_test = _md5_crypt_test.create_backend_case("os_crypt")
  579. md5_crypt_builtin_test = _md5_crypt_test.create_backend_case("builtin")
  580. #=============================================================================
  581. # msdcc 1 & 2
  582. #=============================================================================
  583. class msdcc_test(UserHandlerMixin, HandlerCase):
  584. handler = hash.msdcc
  585. user_case_insensitive = True
  586. known_correct_hashes = [
  587. #
  588. # http://www.jedge.com/wordpress/windows-password-cache/
  589. #
  590. (("Asdf999", "sevans"), "b1176c2587478785ec1037e5abc916d0"),
  591. #
  592. # http://infosecisland.com/blogview/12156-Cachedump-for-Meterpreter-in-Action.html
  593. #
  594. (("ASDqwe123", "jdoe"), "592cdfbc3f1ef77ae95c75f851e37166"),
  595. #
  596. # http://comments.gmane.org/gmane.comp.security.openwall.john.user/1917
  597. #
  598. (("test1", "test1"), "64cd29e36a8431a2b111378564a10631"),
  599. (("test2", "test2"), "ab60bdb4493822b175486810ac2abe63"),
  600. (("test3", "test3"), "14dd041848e12fc48c0aa7a416a4a00c"),
  601. (("test4", "test4"), "b945d24866af4b01a6d89b9d932a153c"),
  602. #
  603. # http://ciscoit.wordpress.com/2011/04/13/metasploit-hashdump-vs-cachedump/
  604. #
  605. (("1234qwer!@#$", "Administrator"), "7b69d06ef494621e3f47b9802fe7776d"),
  606. #
  607. # http://www.securiteam.com/tools/5JP0I2KFPA.html
  608. #
  609. (("password", "user"), "2d9f0b052932ad18b87f315641921cda"),
  610. #
  611. # from JTR 1.7.9
  612. #
  613. (("", "root"), "176a4c2bd45ac73687676c2f09045353"),
  614. (("test1", "TEST1"), "64cd29e36a8431a2b111378564a10631"),
  615. (("okolada", "nineteen_characters"), "290efa10307e36a79b3eebf2a6b29455"),
  616. ((u("\u00FC"), u("\u00FC")), "48f84e6f73d6d5305f6558a33fa2c9bb"),
  617. ((u("\u00FC\u00FC"), u("\u00FC\u00FC")), "593246a8335cf0261799bda2a2a9c623"),
  618. ((u("\u20AC\u20AC"), "user"), "9121790702dda0fa5d353014c334c2ce"),
  619. #
  620. # custom
  621. #
  622. # ensures utf-8 used for unicode
  623. ((UPASS_TABLE, 'bob'), 'fcb82eb4212865c7ac3503156ca3f349'),
  624. ]
  625. known_alternate_hashes = [
  626. # check uppercase accepted.
  627. ("B1176C2587478785EC1037E5ABC916D0", ("Asdf999", "sevans"),
  628. "b1176c2587478785ec1037e5abc916d0"),
  629. ]
  630. class msdcc2_test(UserHandlerMixin, HandlerCase):
  631. handler = hash.msdcc2
  632. user_case_insensitive = True
  633. known_correct_hashes = [
  634. #
  635. # from JTR 1.7.9
  636. #
  637. (("test1", "test1"), "607bbe89611e37446e736f7856515bf8"),
  638. (("qerwt", "Joe"), "e09b38f84ab0be586b730baf61781e30"),
  639. (("12345", "Joe"), "6432f517a900b3fc34ffe57f0f346e16"),
  640. (("", "bin"), "c0cbe0313a861062e29f92ede58f9b36"),
  641. (("w00t", "nineteen_characters"), "87136ae0a18b2dafe4a41d555425b2ed"),
  642. (("w00t", "eighteencharacters"), "fc5df74eca97afd7cd5abb0032496223"),
  643. (("longpassword", "twentyXXX_characters"), "cfc6a1e33eb36c3d4f84e4c2606623d2"),
  644. (("longpassword", "twentyoneX_characters"), "99ff74cea552799da8769d30b2684bee"),
  645. (("longpassword", "twentytwoXX_characters"), "0a721bdc92f27d7fb23b87a445ec562f"),
  646. (("test2", "TEST2"), "c6758e5be7fc943d00b97972a8a97620"),
  647. (("test3", "test3"), "360e51304a2d383ea33467ab0b639cc4"),
  648. (("test4", "test4"), "6f79ee93518306f071c47185998566ae"),
  649. ((u("\u00FC"), "joe"), "bdb80f2c4656a8b8591bd27d39064a54"),
  650. ((u("\u20AC\u20AC"), "joe"), "1e1e20f482ff748038e47d801d0d1bda"),
  651. ((u("\u00FC\u00FC"), "admin"), "0839e4a07c00f18a8c65cf5b985b9e73"),
  652. #
  653. # custom
  654. #
  655. # custom unicode test
  656. ((UPASS_TABLE, 'bob'), 'cad511dc9edefcf69201da72efb6bb55'),
  657. ]
  658. #=============================================================================
  659. # mssql 2000 & 2005
  660. #=============================================================================
  661. class mssql2000_test(HandlerCase):
  662. handler = hash.mssql2000
  663. secret_case_insensitive = "verify-only"
  664. # FIXME: fix UT framework - this hash is sensitive to password case, but verify() is not
  665. known_correct_hashes = [
  666. #
  667. # http://hkashfi.blogspot.com/2007/08/breaking-sql-server-2005-hashes.html
  668. #
  669. ('Test', '0x010034767D5C0CFA5FDCA28C4A56085E65E882E71CB0ED2503412FD54D6119FFF04129A1D72E7C3194F7284A7F3A'),
  670. ('TEST', '0x010034767D5C2FD54D6119FFF04129A1D72E7C3194F7284A7F3A2FD54D6119FFF04129A1D72E7C3194F7284A7F3A'),
  671. #
  672. # http://www.sqlmag.com/forums/aft/68438
  673. #
  674. ('x', '0x010086489146C46DD7318D2514D1AC706457CBF6CD3DF8407F071DB4BBC213939D484BF7A766E974F03C96524794'),
  675. #
  676. # http://stackoverflow.com/questions/173329/how-to-decrypt-a-password-from-sql-server
  677. #
  678. ('AAAA', '0x0100CF465B7B12625EF019E157120D58DD46569AC7BF4118455D12625EF019E157120D58DD46569AC7BF4118455D'),
  679. #
  680. # http://msmvps.com/blogs/gladchenko/archive/2005/04/06/41083.aspx
  681. #
  682. ('123', '0x01002D60BA07FE612C8DE537DF3BFCFA49CD9968324481C1A8A8FE612C8DE537DF3BFCFA49CD9968324481C1A8A8'),
  683. #
  684. # http://www.simple-talk.com/sql/t-sql-programming/temporarily-changing-an-unknown-password-of-the-sa-account-/
  685. #
  686. ('12345', '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3'),
  687. #
  688. # XXX: sample is incomplete, password unknown
  689. # https://anthonystechblog.wordpress.com/2011/04/20/password-encryption-in-sql-server-how-to-tell-if-a-user-is-using-a-weak-password/
  690. # (????, '0x0100813F782D66EF15E40B1A3FDF7AB88B322F51401A87D8D3E3A8483C4351A3D96FC38499E6CDD2B6F?????????'),
  691. #
  692. #
  693. # from JTR 1.7.9
  694. #
  695. ('foo', '0x0100A607BA7C54A24D17B565C59F1743776A10250F581D482DA8B6D6261460D3F53B279CC6913CE747006A2E3254'),
  696. ('bar', '0x01000508513EADDF6DB7DDD270CCA288BF097F2FF69CC2DB74FBB9644D6901764F999BAB9ECB80DE578D92E3F80D'),
  697. ('canard', '0x01008408C523CF06DCB237835D701C165E68F9460580132E28ED8BC558D22CEDF8801F4503468A80F9C52A12C0A3'),
  698. ('lapin', '0x0100BF088517935FC9183FE39FDEC77539FD5CB52BA5F5761881E5B9638641A79DBF0F1501647EC941F3355440A2'),
  699. #
  700. # custom
  701. #
  702. # ensures utf-8 used for unicode
  703. (UPASS_USD, '0x0100624C0961B28E39FEE13FD0C35F57B4523F0DA1861C11D5A5B28E39FEE13FD0C35F57B4523F0DA1861C11D5A5'),
  704. (UPASS_TABLE, '0x010083104228FAD559BE52477F2131E538BE9734E5C4B0ADEFD7F6D784B03C98585DC634FE2B8CA3A6DFFEC729B4'),
  705. ]
  706. known_alternate_hashes = [
  707. # lower case hex
  708. ('0x01005b20054332752e1bc2e7c5df0f9ebfe486e9bee063e8d3b332752e1bc2e7c5df0f9ebfe486e9bee063e8d3b3',
  709. '12345', '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3'),
  710. ]
  711. known_unidentified_hashes = [
  712. # malformed start
  713. '0X01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3',
  714. # wrong magic value
  715. '0x02005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3',
  716. # wrong size
  717. '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3',
  718. '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3AF',
  719. # mssql2005
  720. '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3',
  721. ]
  722. known_malformed_hashes = [
  723. # non-hex char -----\/
  724. b'0x01005B200543327G2E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3',
  725. u('0x01005B200543327G2E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3'),
  726. ]
  727. class mssql2005_test(HandlerCase):
  728. handler = hash.mssql2005
  729. known_correct_hashes = [
  730. #
  731. # http://hkashfi.blogspot.com/2007/08/breaking-sql-server-2005-hashes.html
  732. #
  733. ('TEST', '0x010034767D5C2FD54D6119FFF04129A1D72E7C3194F7284A7F3A'),
  734. #
  735. # http://www.openwall.com/lists/john-users/2009/07/14/2
  736. #
  737. ('toto', '0x01004086CEB6BF932BC4151A1AF1F13CD17301D70816A8886908'),
  738. #
  739. # http://msmvps.com/blogs/gladchenko/archive/2005/04/06/41083.aspx
  740. #
  741. ('123', '0x01004A335DCEDB366D99F564D460B1965B146D6184E4E1025195'),
  742. ('123', '0x0100E11D573F359629B344990DCD3D53DE82CF8AD6BBA7B638B6'),
  743. #
  744. # XXX: password unknown
  745. # http://www.simple-talk.com/sql/t-sql-programming/temporarily-changing-an-unknown-password-of-the-sa-account-/
  746. # (???, '0x01004086CEB6301EEC0A994E49E30DA235880057410264030797'),
  747. #
  748. #
  749. # http://therelentlessfrontend.com/2010/03/26/encrypting-and-decrypting-passwords-in-sql-server/
  750. #
  751. ('AAAA', '0x010036D726AE86834E97F20B198ACD219D60B446AC5E48C54F30'),
  752. #
  753. # from JTR 1.7.9
  754. #
  755. ("toto", "0x01004086CEB6BF932BC4151A1AF1F13CD17301D70816A8886908"),
  756. ("titi", "0x01004086CEB60ED526885801C23B366965586A43D3DEAC6DD3FD"),
  757. ("foo", "0x0100A607BA7C54A24D17B565C59F1743776A10250F581D482DA8"),
  758. ("bar", "0x01000508513EADDF6DB7DDD270CCA288BF097F2FF69CC2DB74FB"),
  759. ("canard", "0x01008408C523CF06DCB237835D701C165E68F9460580132E28ED"),
  760. ("lapin", "0x0100BF088517935FC9183FE39FDEC77539FD5CB52BA5F5761881"),
  761. #
  762. # adapted from mssql2000.known_correct_hashes (above)
  763. #
  764. ('Test', '0x010034767D5C0CFA5FDCA28C4A56085E65E882E71CB0ED250341'),
  765. ('Test', '0x0100993BF2315F36CC441485B35C4D84687DC02C78B0E680411F'),
  766. ('x', '0x010086489146C46DD7318D2514D1AC706457CBF6CD3DF8407F07'),
  767. ('AAAA', '0x0100CF465B7B12625EF019E157120D58DD46569AC7BF4118455D'),
  768. ('123', '0x01002D60BA07FE612C8DE537DF3BFCFA49CD9968324481C1A8A8'),
  769. ('12345', '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3'),
  770. #
  771. # custom
  772. #
  773. # ensures utf-8 used for unicode
  774. (UPASS_USD, '0x0100624C0961B28E39FEE13FD0C35F57B4523F0DA1861C11D5A5'),
  775. (UPASS_TABLE, '0x010083104228FAD559BE52477F2131E538BE9734E5C4B0ADEFD7'),
  776. ]
  777. known_alternate_hashes = [
  778. # lower case hex
  779. ('0x01005b20054332752e1bc2e7c5df0f9ebfe486e9bee063e8d3b3',
  780. '12345', '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3'),
  781. ]
  782. known_unidentified_hashes = [
  783. # malformed start
  784. '0X010036D726AE86834E97F20B198ACD219D60B446AC5E48C54F30',
  785. # wrong magic value
  786. '0x020036D726AE86834E97F20B198ACD219D60B446AC5E48C54F30',
  787. # wrong size
  788. '0x010036D726AE86834E97F20B198ACD219D60B446AC5E48C54F',
  789. '0x010036D726AE86834E97F20B198ACD219D60B446AC5E48C54F3012',
  790. # mssql2000
  791. '0x01005B20054332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B332752E1BC2E7C5DF0F9EBFE486E9BEE063E8D3B3',
  792. ]
  793. known_malformed_hashes = [
  794. # non-hex char --\/
  795. '0x010036D726AE86G34E97F20B198ACD219D60B446AC5E48C54F30',
  796. ]
  797. #=============================================================================
  798. # mysql 323 & 41
  799. #=============================================================================
  800. class mysql323_test(HandlerCase):
  801. handler = hash.mysql323
  802. known_correct_hashes = [
  803. #
  804. # from JTR 1.7.9
  805. #
  806. ('drew', '697a7de87c5390b2'),
  807. ('password', "5d2e19393cc5ef67"),
  808. #
  809. # custom
  810. #
  811. ('mypass', '6f8c114b58f2ce9e'),
  812. # ensures utf-8 used for unicode
  813. (UPASS_TABLE, '4ef327ca5491c8d7'),
  814. ]
  815. known_unidentified_hashes = [
  816. # bad char in otherwise correct hash
  817. '6z8c114b58f2ce9e',
  818. ]
  819. def test_90_whitespace(self):
  820. """check whitespace is ignored per spec"""
  821. h = self.do_encrypt("mypass")
  822. h2 = self.do_encrypt("my pass")
  823. self.assertEqual(h, h2)
  824. class FuzzHashGenerator(HandlerCase.FuzzHashGenerator):
  825. def accept_password_pair(self, secret, other):
  826. # override to handle whitespace
  827. return secret.replace(" ","") != other.replace(" ","")
  828. class mysql41_test(HandlerCase):
  829. handler = hash.mysql41
  830. known_correct_hashes = [
  831. #
  832. # from JTR 1.7.9
  833. #
  834. ('verysecretpassword', '*2C905879F74F28F8570989947D06A8429FB943E6'),
  835. ('12345678123456781234567812345678', '*F9F1470004E888963FB466A5452C9CBD9DF6239C'),
  836. ("' OR 1 /*'", '*97CF7A3ACBE0CA58D5391AC8377B5D9AC11D46D9'),
  837. #
  838. # custom
  839. #
  840. ('mypass', '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'),
  841. # ensures utf-8 used for unicode
  842. (UPASS_TABLE, '*E7AFE21A9CFA2FC9D15D942AE8FB5C240FE5837B'),
  843. ]
  844. known_unidentified_hashes = [
  845. # bad char in otherwise correct hash
  846. '*6Z8989366EAF75BB670AD8EA7A7FC1176A95CEF4',
  847. ]
  848. #=============================================================================
  849. # NTHASH
  850. #=============================================================================
  851. class nthash_test(HandlerCase):
  852. handler = hash.nthash
  853. known_correct_hashes = [
  854. #
  855. # http://msdn.microsoft.com/en-us/library/cc245828(v=prot.10).aspx
  856. #
  857. ("OLDPASSWORD", u("6677b2c394311355b54f25eec5bfacf5")),
  858. ("NEWPASSWORD", u("256781a62031289d3c2c98c14f1efc8c")),
  859. #
  860. # from JTR 1.7.9
  861. #
  862. # ascii
  863. ('', '31d6cfe0d16ae931b73c59d7e0c089c0'),
  864. ('tigger', 'b7e0ea9fbffcf6dd83086e905089effd'),
  865. # utf-8
  866. (b'\xC3\xBC', '8bd6e4fb88e01009818749c5443ea712'),
  867. (b'\xC3\xBC\xC3\xBC', 'cc1260adb6985ca749f150c7e0b22063'),
  868. (b'\xE2\x82\xAC', '030926b781938db4365d46adc7cfbcb8'),
  869. (b'\xE2\x82\xAC\xE2\x82\xAC','682467b963bb4e61943e170a04f7db46'),
  870. #
  871. # custom
  872. #
  873. ('passphrase', '7f8fe03093cc84b267b109625f6bbf4b'),
  874. ]
  875. known_unidentified_hashes = [
  876. # bad char in otherwise correct hash
  877. '7f8fe03093cc84b267b109625f6bbfxb',
  878. ]
  879. class bsd_nthash_test(HandlerCase):
  880. handler = hash.bsd_nthash
  881. known_correct_hashes = [
  882. ('passphrase', '$3$$7f8fe03093cc84b267b109625f6bbf4b'),
  883. (b'\xC3\xBC', '$3$$8bd6e4fb88e01009818749c5443ea712'),
  884. ]
  885. known_unidentified_hashes = [
  886. # bad char in otherwise correct hash --\/
  887. '$3$$7f8fe03093cc84b267b109625f6bbfxb',
  888. ]
  889. #=============================================================================
  890. # oracle 10 & 11
  891. #=============================================================================
  892. class oracle10_test(UserHandlerMixin, HandlerCase):
  893. handler = hash.oracle10
  894. secret_case_insensitive = True
  895. user_case_insensitive = True
  896. # TODO: get more test vectors (especially ones which properly test unicode)
  897. known_correct_hashes = [
  898. # ((secret,user),hash)
  899. #
  900. # http://www.petefinnigan.com/default/default_password_list.htm
  901. #
  902. (('tiger', 'scott'), 'F894844C34402B67'),
  903. ((u('ttTiGGeR'), u('ScO')), '7AA1A84E31ED7771'),
  904. (("d_syspw", "SYSTEM"), '1B9F1F9A5CB9EB31'),
  905. (("strat_passwd", "strat_user"), 'AEBEDBB4EFB5225B'),
  906. #
  907. # http://openwall.info/wiki/john/sample-hashes
  908. #
  909. (('#95LWEIGHTS', 'USER'), '000EA4D72A142E29'),
  910. (('CIAO2010', 'ALFREDO'), 'EB026A76F0650F7B'),
  911. #
  912. # from JTR 1.7.9
  913. #
  914. (('GLOUGlou', 'Bob'), 'CDC6B483874B875B'),
  915. (('GLOUGLOUTER', 'bOB'), 'EF1F9139DB2D5279'),
  916. (('LONG_MOT_DE_PASSE_OUI', 'BOB'), 'EC8147ABB3373D53'),
  917. #
  918. # custom
  919. #
  920. ((UPASS_TABLE, 'System'), 'B915A853F297B281'),
  921. ]
  922. known_unidentified_hashes = [
  923. # bad char in hash --\
  924. 'F894844C34402B6Z',
  925. ]
  926. class oracle11_test(HandlerCase):
  927. handler = hash.oracle11
  928. # TODO: find more test vectors (especially ones which properly test unicode)
  929. known_correct_hashes = [
  930. #
  931. # from JTR 1.7.9
  932. #
  933. ("abc123", "S:5FDAB69F543563582BA57894FE1C1361FB8ED57B903603F2C52ED1B4D642"),
  934. ("SyStEm123!@#", "S:450F957ECBE075D2FA009BA822A9E28709FBC3DA82B44D284DDABEC14C42"),
  935. ("oracle", "S:3437FF72BD69E3FB4D10C750B92B8FB90B155E26227B9AB62D94F54E5951"),
  936. ("11g", "S:61CE616647A4F7980AFD7C7245261AF25E0AFE9C9763FCF0D54DA667D4E6"),
  937. ("11g", "S:B9E7556F53500C8C78A58F50F24439D79962DE68117654B6700CE7CC71CF"),
  938. #
  939. # source?
  940. #
  941. ("SHAlala", "S:2BFCFDF5895014EE9BB2B9BA067B01E0389BB5711B7B5F82B7235E9E182C"),
  942. #
  943. # custom
  944. #
  945. (UPASS_TABLE, 'S:51586343E429A6DF024B8F242F2E9F8507B1096FACD422E29142AA4974B0'),
  946. ]
  947. #=============================================================================
  948. # PHPass Portable Crypt
  949. #=============================================================================
  950. class phpass_test(HandlerCase):
  951. handler = hash.phpass
  952. known_correct_hashes = [
  953. #
  954. # from official 0.3 implementation
  955. # http://www.openwall.com/phpass/
  956. #
  957. ('test12345', '$P$9IQRaTwmfeRo7ud9Fh4E2PdI0S3r.L0'), # from the source
  958. #
  959. # from JTR 1.7.9
  960. #
  961. ('test1', '$H$9aaaaaSXBjgypwqm.JsMssPLiS8YQ00'),
  962. ('123456', '$H$9PE8jEklgZhgLmZl5.HYJAzfGCQtzi1'),
  963. ('123456', '$H$9pdx7dbOW3Nnt32sikrjAxYFjX8XoK1'),
  964. ('thisisalongertestPW', '$P$912345678LIjjb6PhecupozNBmDndU0'),
  965. ('JohnRipper', '$P$612345678si5M0DDyPpmRCmcltU/YW/'),
  966. ('JohnRipper', '$H$712345678WhEyvy1YWzT4647jzeOmo0'),
  967. ('JohnRipper', '$P$B12345678L6Lpt4BxNotVIMILOa9u81'),
  968. #
  969. # custom
  970. #
  971. ('', '$P$7JaFQsPzJSuenezefD/3jHgt5hVfNH0'),
  972. ('compL3X!', '$P$FiS0N5L672xzQx1rt1vgdJQRYKnQM9/'),
  973. # ensures utf-8 used for unicode
  974. (UPASS_TABLE, '$P$7SMy8VxnfsIy2Sxm7fJxDSdil.h7TW.'),
  975. ]
  976. known_malformed_hashes = [
  977. # bad char in otherwise correct hash
  978. # ---\/
  979. '$P$9IQRaTwmfeRo7ud9Fh4E2PdI0S3r!L0',
  980. ]
  981. #=============================================================================
  982. # plaintext
  983. #=============================================================================
  984. class plaintext_test(HandlerCase):
  985. # TODO: integrate EncodingHandlerMixin
  986. handler = hash.plaintext
  987. accepts_all_hashes = True
  988. known_correct_hashes = [
  989. ('',''),
  990. ('password', 'password'),
  991. # ensure unicode uses utf-8
  992. (UPASS_TABLE, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
  993. (PASS_TABLE_UTF8, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
  994. ]
  995. #=============================================================================
  996. # postgres_md5
  997. #=============================================================================
  998. class postgres_md5_test(UserHandlerMixin, HandlerCase):
  999. handler = hash.postgres_md5
  1000. known_correct_hashes = [
  1001. # ((secret,user),hash)
  1002. #
  1003. # generated using postgres 8.1
  1004. #
  1005. (('mypass', 'postgres'), 'md55fba2ea04fd36069d2574ea71c8efe9d'),
  1006. (('mypass', 'root'), 'md540c31989b20437833f697e485811254b'),
  1007. (("testpassword",'testuser'), 'md5d4fc5129cc2c25465a5370113ae9835f'),
  1008. #
  1009. # custom
  1010. #
  1011. # verify unicode->utf8
  1012. ((UPASS_TABLE, 'postgres'), 'md5cb9f11283265811ce076db86d18a22d2'),
  1013. ]
  1014. known_unidentified_hashes = [
  1015. # bad 'z' char in otherwise correct hash
  1016. 'md54zc31989b20437833f697e485811254b',
  1017. ]
  1018. #=============================================================================
  1019. # (netbsd's) sha1 crypt
  1020. #=============================================================================
  1021. class _sha1_crypt_test(HandlerCase):
  1022. handler = hash.sha1_crypt
  1023. known_correct_hashes = [
  1024. #
  1025. # custom
  1026. #
  1027. ("password", "$sha1$19703$iVdJqfSE$v4qYKl1zqYThwpjJAoKX6UvlHq/a"),
  1028. ("password", "$sha1$21773$uV7PTeux$I9oHnvwPZHMO0Nq6/WgyGV/tDJIH"),
  1029. (UPASS_TABLE, '$sha1$40000$uJ3Sp7LE$.VEmLO5xntyRFYihC7ggd3297T/D'),
  1030. ]
  1031. known_malformed_hashes = [
  1032. # bad char in otherwise correct hash
  1033. '$sha1$21773$u!7PTeux$I9oHnvwPZHMO0Nq6/WgyGV/tDJIH',
  1034. # zero padded rounds
  1035. '$sha1$01773$uV7PTeux$I9oHnvwPZHMO0Nq6/WgyGV/tDJIH',
  1036. # too many fields
  1037. '$sha1$21773$uV7PTeux$I9oHnvwPZHMO0Nq6/WgyGV/tDJIH$',
  1038. # empty rounds field
  1039. '$sha1$$uV7PTeux$I9oHnvwPZHMO0Nq6/WgyGV/tDJIH$',
  1040. ]
  1041. platform_crypt_support = [
  1042. ("netbsd", True),
  1043. ("freebsd|openbsd|linux|solaris|darwin", False),
  1044. ]
  1045. # create test cases for specific backends
  1046. sha1_crypt_os_crypt_test = _sha1_crypt_test.create_backend_case("os_crypt")
  1047. sha1_crypt_builtin_test = _sha1_crypt_test.create_backend_case("builtin")
  1048. #=============================================================================
  1049. # roundup
  1050. #=============================================================================
  1051. # NOTE: all roundup hashes use PrefixWrapper,
  1052. # so there's nothing natively to test.
  1053. # so we just have a few quick cases...
  1054. class RoundupTest(TestCase):
  1055. def _test_pair(self, h, secret, hash):
  1056. self.assertTrue(h.verify(secret, hash))
  1057. self.assertFalse(h.verify('x'+secret, hash))
  1058. def test_pairs(self):
  1059. self._test_pair(
  1060. hash.ldap_hex_sha1,
  1061. "sekrit",
  1062. '{SHA}8d42e738c7adee551324955458b5e2c0b49ee655')
  1063. self._test_pair(
  1064. hash.ldap_hex_md5,
  1065. "sekrit",
  1066. '{MD5}ccbc53f4464604e714f69dd11138d8b5')
  1067. self._test_pair(
  1068. hash.ldap_des_crypt,
  1069. "sekrit",
  1070. '{CRYPT}nFia0rj2TT59A')
  1071. self._test_pair(
  1072. hash.roundup_plaintext,
  1073. "sekrit",
  1074. '{plaintext}sekrit')
  1075. self._test_pair(
  1076. hash.ldap_pbkdf2_sha1,
  1077. "sekrit",
  1078. '{PBKDF2}5000$7BvbBq.EZzz/O0HuwX3iP.nAG3s$g3oPnFFaga2BJaX5PoPRljl4XIE')
  1079. #=============================================================================
  1080. # sha256-crypt
  1081. #=============================================================================
  1082. class _sha256_crypt_test(HandlerCase):
  1083. handler = hash.sha256_crypt
  1084. known_correct_hashes = [
  1085. #
  1086. # from JTR 1.7.9
  1087. #
  1088. ('U*U*U*U*', '$5$LKO/Ute40T3FNF95$U0prpBQd4PloSGU0pnpM4z9wKn4vZ1.jsrzQfPqxph9'),
  1089. ('U*U***U', '$5$LKO/Ute40T3FNF95$fdgfoJEBoMajNxCv3Ru9LyQ0xZgv0OBMQoq80LQ/Qd.'),
  1090. ('U*U***U*', '$5$LKO/Ute40T3FNF95$8Ry82xGnnPI/6HtFYnvPBTYgOL23sdMXn8C29aO.x/A'),
  1091. ('*U*U*U*U', '$5$9mx1HkCz7G1xho50$O7V7YgleJKLUhcfk9pgzdh3RapEaWqMtEp9UUBAKIPA'),
  1092. ('', '$5$kc7lRD1fpYg0g.IP$d7CMTcEqJyTXyeq8hTdu/jB/I6DGkoo62NXbHIR7S43'),
  1093. #
  1094. # custom tests
  1095. #
  1096. ('', '$5$rounds=10428$uy/jIAhCetNCTtb0$YWvUOXbkqlqhyoPMpN8BMe.ZGsGx2aBvxTvDFI613c3'),
  1097. (' ', '$5$rounds=10376$I5lNtXtRmf.OoMd8$Ko3AI1VvTANdyKhBPavaRjJzNpSatKU6QVN9uwS9MH.'),
  1098. ('test', '$5$rounds=11858$WH1ABM5sKhxbkgCK$aTQsjPkz0rBsH3lQlJxw9HDTDXPKBxC0LlVeV69P.t1'),
  1099. ('Compl3X AlphaNu3meric', '$5$rounds=10350$o.pwkySLCzwTdmQX$nCMVsnF3TXWcBPOympBUUSQi6LGGloZoOsVJMGJ09UB'),
  1100. ('4lpHa N|_|M3r1K W/ Cur5Es: #$%(*)(*%#', '$5$rounds=11944$9dhlu07dQMRWvTId$LyUI5VWkGFwASlzntk1RLurxX54LUhgAcJZIt0pYGT7'),
  1101. (u('with unic\u00D6de'), '$5$rounds=1000$IbG0EuGQXw5EkMdP$LQ5AfPf13KufFsKtmazqnzSGZ4pxtUNw3woQ.ELRDF4'),
  1102. ]
  1103. if TEST_MODE("full"):
  1104. # builtin alg was changed in 1.6, and had possibility of fencepost
  1105. # errors near rounds that are multiples of 42. these hashes test rounds
  1106. # 1004..1012 (42*24=1008 +/- 4) to ensure no mistakes were made.
  1107. # (also relying on fuzz testing against os_crypt backend).
  1108. known_correct_hashes.extend([
  1109. ("secret", '$5$rounds=1004$nacl$oiWPbm.kQ7.jTCZoOtdv7/tO5mWv/vxw5yTqlBagVR7'),
  1110. ("secret", '$5$rounds=1005$nacl$6Mo/TmGDrXxg.bMK9isRzyWH3a..6HnSVVsJMEX7ud/'),
  1111. ("secret", '$5$rounds=1006$nacl$I46VwuAiUBwmVkfPFakCtjVxYYaOJscsuIeuZLbfKID'),
  1112. ("secret", '$5$rounds=1007$nacl$9fY4j1AV3N/dV/YMUn1enRHKH.7nEL4xf1wWB6wfDD4'),
  1113. ("secret", '$5$rounds=1008$nacl$CiFWCfn8ODmWs0I1xAdXFo09tM8jr075CyP64bu3by9'),
  1114. ("secret", '$5$rounds=1009$nacl$QtpFX.CJHgVQ9oAjVYStxAeiU38OmFILWm684c6FyED'),
  1115. ("secret", '$5$rounds=1010$nacl$ktAwXuT5WbjBW/0ZU1eNMpqIWY1Sm4twfRE1zbZyo.B'),
  1116. ("secret", '$5$rounds=1011$nacl$QJWLBEhO9qQHyMx4IJojSN9sS41P1Yuz9REddxdO721'),
  1117. ("secret", '$5$rounds=1012$nacl$mmf/k2PkbBF4VCtERgky3bEVavmLZKFwAcvxD1p3kV2'),
  1118. ])
  1119. known_malformed_hashes = [
  1120. # bad char in otherwise correct hash
  1121. '$5$rounds=10428$uy/:jIAhCetNCTtb0$YWvUOXbkqlqhyoPMpN8BMeZGsGx2aBvxTvDFI613c3',
  1122. # zero-padded rounds
  1123. '$5$rounds=010428$uy/jIAhCetNCTtb0$YWvUOXbkqlqhyoPMpN8BMe.ZGsGx2aBvxTvDFI613c3',
  1124. # extra "$"
  1125. '$5$rounds=10428$uy/jIAhCetNCTtb0$YWvUOXbkqlqhyoPMpN8BMe.ZGsGx2aBvxTvDFI613c3$',
  1126. ]
  1127. known_correct_configs = [
  1128. # config, secret, result
  1129. #
  1130. # taken from official specification at http://www.akkadia.org/drepper/SHA-crypt.txt
  1131. #
  1132. ( "$5$saltstring", "Hello world!",
  1133. "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" ),
  1134. ( "$5$rounds=10000$saltstringsaltstring", "Hello world!",
  1135. "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2."
  1136. "opqey6IcA" ),
  1137. ( "$5$rounds=5000$toolongsaltstring", "This is just a test",
  1138. "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8"
  1139. "mGRcvxa5" ),
  1140. ( "$5$rounds=1400$anotherlongsaltstring",
  1141. "a very much longer text to encrypt. This one even stretches over more"
  1142. "than one line.",
  1143. "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12"
  1144. "oP84Bnq1" ),
  1145. ( "$5$rounds=77777$short",
  1146. "we have a short salt string but not a short password",
  1147. "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/" ),
  1148. ( "$5$rounds=123456$asaltof16chars..", "a short string",
  1149. "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/"
  1150. "cZKmF/wJvD" ),
  1151. ( "$5$rounds=10$roundstoolow", "the minimum number is still observed",
  1152. "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97"
  1153. "2bIC" ),
  1154. ]
  1155. filter_config_warnings = True # rounds too low, salt too small
  1156. platform_crypt_support = [
  1157. ("freebsd(9|1\d)|linux", True),
  1158. ("freebsd8", None), # added in freebsd 8.3
  1159. ("freebsd|openbsd|netbsd|darwin", False),
  1160. # solaris - depends on policy
  1161. ]
  1162. # create test cases for specific backends
  1163. sha256_crypt_os_crypt_test = _sha256_crypt_test.create_backend_case("os_crypt")
  1164. sha256_crypt_builtin_test = _sha256_crypt_test.create_backend_case("builtin")
  1165. #=============================================================================
  1166. # test sha512-crypt
  1167. #=============================================================================
  1168. class _sha512_crypt_test(HandlerCase):
  1169. handler = hash.sha512_crypt
  1170. known_correct_hashes = [
  1171. #
  1172. # from JTR 1.7.9
  1173. #
  1174. ('U*U*U*U*', "$6$LKO/Ute40T3FNF95$6S/6T2YuOIHY0N3XpLKABJ3soYcXD9mB7uVbtEZDj/LNscVhZoZ9DEH.sBciDrMsHOWOoASbNLTypH/5X26gN0"),
  1175. ('U*U***U', "$6$LKO/Ute40T3FNF95$wK80cNqkiAUzFuVGxW6eFe8J.fSVI65MD5yEm8EjYMaJuDrhwe5XXpHDJpwF/kY.afsUs1LlgQAaOapVNbggZ1"),
  1176. ('U*U***U*', "$6$LKO/Ute40T3FNF95$YS81pp1uhOHTgKLhSMtQCr2cDiUiN03Ud3gyD4ameviK1Zqz.w3oXsMgO6LrqmIEcG3hiqaUqHi/WEE2zrZqa/"),
  1177. ('*U*U*U*U', "$6$OmBOuxFYBZCYAadG$WCckkSZok9xhp4U1shIZEV7CCVwQUwMVea7L3A77th6SaE9jOPupEMJB.z0vIWCDiN9WLh2m9Oszrj5G.gt330"),
  1178. ('', "$6$ojWH1AiTee9x1peC$QVEnTvRVlPRhcLQCk/HnHaZmlGAAjCfrAN0FtOsOnUk5K5Bn/9eLHHiRzrTzaIKjW9NTLNIBUCtNVOowWS2mN."),
  1179. #
  1180. # custom tests
  1181. #
  1182. ('', '$6$rounds=11021$KsvQipYPWpr93wWP$v7xjI4X6vyVptJjB1Y02vZC5SaSijBkGmq1uJhPr3cvqvvkd42Xvo48yLVPFt8dvhCsnlUgpX.//Cxn91H4qy1'),
  1183. (' ', '$6$rounds=11104$ED9SA4qGmd57Fq2m$q/.PqACDM/JpAHKmr86nkPzzuR5.YpYa8ZJJvI8Zd89ZPUYTJExsFEIuTYbM7gAGcQtTkCEhBKmp1S1QZwaXx0'),
  1184. ('test', '$6$rounds=11531$G/gkPn17kHYo0gTF$Kq.uZBHlSBXyzsOJXtxJruOOH4yc0Is13uY7yK0PvAvXxbvc1w8DO1RzREMhKsc82K/Jh8OquV8FZUlreYPJk1'),
  1185. ('Compl3X AlphaNu3meric', '$6$rounds=10787$wakX8nGKEzgJ4Scy$X78uqaX1wYXcSCtS4BVYw2trWkvpa8p7lkAtS9O/6045fK4UB2/Jia0Uy/KzCpODlfVxVNZzCCoV9s2hoLfDs/'),
  1186. ('4lpHa N|_|M3r1K W/ Cur5Es: #$%(*)(*%#', '$6$rounds=11065$5KXQoE1bztkY5IZr$Jf6krQSUKKOlKca4hSW07MSerFFzVIZt/N3rOTsUgKqp7cUdHrwV8MoIVNCk9q9WL3ZRMsdbwNXpVk0gVxKtz1'),
  1187. # ensures utf-8 used for unicode
  1188. (UPASS_TABLE, '$6$rounds=40000$PEZTJDiyzV28M3.m$GTlnzfzGB44DGd1XqlmC4erAJKCP.rhvLvrYxiT38htrNzVGBnplFOHjejUGVrCfusGWxLQCc3pFO0A/1jYYr0'),
  1189. ]
  1190. known_malformed_hashes = [
  1191. # zero-padded rounds
  1192. '$6$rounds=011021$KsvQipYPWpr93wWP$v7xjI4X6vyVptJjB1Y02vZC5SaSijBkGmq1uJhPr3cvqvvkd42Xvo48yLVPFt8dvhCsnlUgpX.//Cxn91H4qy1',
  1193. # bad char in otherwise correct hash
  1194. '$6$rounds=11021$KsvQipYPWpr9:wWP$v7xjI4X6vyVptJjB1Y02vZC5SaSijBkGmq1uJhPr3cvqvvkd42Xvo48yLVPFt8dvhCsnlUgpX.//Cxn91H4qy1',
  1195. ]
  1196. known_correct_configs = [
  1197. # config, secret, result
  1198. #
  1199. # taken from official specification at http://www.akkadia.org/drepper/SHA-crypt.txt
  1200. #
  1201. ("$6$saltstring", "Hello world!",
  1202. "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
  1203. "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1" ),
  1204. ( "$6$rounds=10000$saltstringsaltstring", "Hello world!",
  1205. "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb"
  1206. "HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." ),
  1207. ( "$6$rounds=5000$toolongsaltstring", "This is just a test",
  1208. "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ"
  1209. "zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" ),
  1210. ( "$6$rounds=1400$anotherlongsaltstring",
  1211. "a very much longer text to encrypt. This one even stretches over more"
  1212. "than one line.",
  1213. "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP"
  1214. "vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" ),
  1215. ( "$6$rounds=77777$short",
  1216. "we have a short salt string but not a short password",
  1217. "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g"
  1218. "ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" ),
  1219. ( "$6$rounds=123456$asaltof16chars..", "a short string",
  1220. "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc"
  1221. "elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" ),
  1222. ( "$6$rounds=10$roundstoolow", "the minimum number is still observed",
  1223. "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x"
  1224. "hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." ),
  1225. ]
  1226. filter_config_warnings = True # rounds too low, salt too small
  1227. platform_crypt_support = _sha256_crypt_test.platform_crypt_support
  1228. # create test cases for specific backends
  1229. sha512_crypt_os_crypt_test = _sha512_crypt_test.create_backend_case("os_crypt")
  1230. sha512_crypt_builtin_test = _sha512_crypt_test.create_backend_case("builtin")
  1231. #=============================================================================
  1232. # sun md5 crypt
  1233. #=============================================================================
  1234. class sun_md5_crypt_test(HandlerCase):
  1235. handler = hash.sun_md5_crypt
  1236. # TODO: this scheme needs some real test vectors, especially due to
  1237. # the "bare salt" issue which plagued the official parser.
  1238. known_correct_hashes = [
  1239. #
  1240. # http://forums.halcyoninc.com/showthread.php?t=258
  1241. #
  1242. ("Gpcs3_adm", "$md5$zrdhpMlZ$$wBvMOEqbSjU.hu5T2VEP01"),
  1243. #
  1244. # http://www.c0t0d0s0.org/archives/4453-Less-known-Solaris-features-On-passwords-Part-2-Using-stronger-password-hashing.html
  1245. #
  1246. ("aa12345678", "$md5$vyy8.OVF$$FY4TWzuauRl4.VQNobqMY."),
  1247. #
  1248. # http://www.cuddletech.com/blog/pivot/entry.php?id=778
  1249. #
  1250. ("this", "$md5$3UqYqndY$$6P.aaWOoucxxq.l00SS9k0"),
  1251. #
  1252. # http://compgroups.net/comp.unix.solaris/password-file-in-linux-and-solaris-8-9
  1253. #
  1254. ("passwd", "$md5$RPgLF6IJ$WTvAlUJ7MqH5xak2FMEwS/"),
  1255. #
  1256. # source: http://solaris-training.com/301_HTML/docs/deepdiv.pdf page 27
  1257. # FIXME: password unknown
  1258. # "$md5,rounds=8000$kS9FT1JC$$mnUrRO618lLah5iazwJ9m1"
  1259. #
  1260. # source: http://www.visualexams.com/310-303.htm
  1261. # XXX: this has 9 salt chars unlike all other hashes. is that valid?
  1262. # FIXME: password unknown
  1263. # "$md5,rounds=2006$2amXesSj5$$kCF48vfPsHDjlKNXeEw7V."
  1264. #
  1265. #
  1266. # custom
  1267. #
  1268. # ensures utf-8 used for unicode
  1269. (UPASS_TABLE, '$md5,rounds=5000$10VYDzAA$$1arAVtMA3trgE1qJ2V0Ez1'),
  1270. ]
  1271. known_correct_configs = [
  1272. # (config, secret, hash)
  1273. #---------------------------
  1274. # test salt string handling
  1275. #
  1276. # these tests attempt to verify that passlib is handling
  1277. # the "bare salt" issue (see sun md5 crypt docs)
  1278. # in a sane manner
  1279. #---------------------------
  1280. # config with "$" suffix, hash strings with "$$" suffix,
  1281. # should all be treated the same, with one "$" added to salt digest.
  1282. ("$md5$3UqYqndY$",
  1283. "this", "$md5$3UqYqndY$$6P.aaWOoucxxq.l00SS9k0"),
  1284. ("$md5$3UqYqndY$$.................DUMMY",
  1285. "this", "$md5$3UqYqndY$$6P.aaWOoucxxq.l00SS9k0"),
  1286. # config with no suffix, hash strings with "$" suffix,
  1287. # should all be treated the same, and no suffix added to salt digest.
  1288. # NOTE: this is just a guess re: config w/ no suffix,
  1289. # but otherwise there's no sane way to encode bare_salt=False
  1290. # within config string.
  1291. ("$md5$3UqYqndY",
  1292. "this", "$md5$3UqYqndY$HIZVnfJNGCPbDZ9nIRSgP1"),
  1293. ("$md5$3UqYqndY$.................DUMMY",
  1294. "this", "$md5$3UqYqndY$HIZVnfJNGCPbDZ9nIRSgP1"),
  1295. ]
  1296. known_malformed_hashes = [
  1297. # unexpected end of hash
  1298. "$md5,rounds=5000",
  1299. # bad rounds
  1300. "$md5,rounds=500A$xxxx",
  1301. "$md5,rounds=0500$xxxx",
  1302. "$md5,rounds=0$xxxx",
  1303. # bad char in otherwise correct hash
  1304. "$md5$RPgL!6IJ$WTvAlUJ7MqH5xak2FMEwS/",
  1305. # digest too short
  1306. "$md5$RPgLa6IJ$WTvAlUJ7MqH5xak2FMEwS",
  1307. # digest too long
  1308. "$md5$RPgLa6IJ$WTvAlUJ7MqH5xak2FMEwS/.",
  1309. # 2+ "$" at end of salt in config
  1310. # NOTE: not sure what correct behavior is, so forbidding format for now.
  1311. "$md5$3UqYqndY$$",
  1312. # 3+ "$" at end of salt in hash
  1313. # NOTE: not sure what correct behavior is, so forbidding format for now.
  1314. "$md5$RPgLa6IJ$$$WTvAlUJ7MqH5xak2FMEwS/",
  1315. ]
  1316. platform_crypt_support = [
  1317. ("solaris", True),
  1318. ("freebsd|openbsd|netbsd|linux|darwin", False),
  1319. ]
  1320. def do_verify(self, secret, hash):
  1321. # Override to fake error for "$..." hash string listed in known_correct_configs (above)
  1322. # These have to be hash strings, in order to test bare salt issue.
  1323. if isinstance(hash, str) and hash.endswith("$.................DUMMY"):
  1324. raise ValueError("pretending '$...' stub hash is config string")
  1325. return self.handler.verify(secret, hash)
  1326. #=============================================================================
  1327. # unix disabled / fallback
  1328. #=============================================================================
  1329. class unix_disabled_test(HandlerCase):
  1330. handler = hash.unix_disabled
  1331. # accepts_all_hashes = True # TODO: turn this off.
  1332. known_correct_hashes = [
  1333. # everything should hash to "!" (or "*" on BSD),
  1334. # and nothing should verify against either string
  1335. ("password", "!"),
  1336. (UPASS_TABLE, "*"),
  1337. ]
  1338. known_unidentified_hashes = [
  1339. # should never identify anything crypt() could return...
  1340. "$1$xxx",
  1341. "abc",
  1342. "./az",
  1343. "{SHA}xxx",
  1344. ]
  1345. def test_76_hash_border(self):
  1346. # so empty strings pass
  1347. self.accepts_all_hashes = True
  1348. super(unix_disabled_test, self).test_76_hash_border()
  1349. def test_90_special(self):
  1350. """test marker option & special behavior"""
  1351. warnings.filterwarnings("ignore", "passing settings to .*.hash\(\) is deprecated")
  1352. handler = self.handler
  1353. # preserve hash if provided
  1354. self.assertEqual(handler.genhash("stub", "!asd"), "!asd")
  1355. # use marker if no hash
  1356. self.assertEqual(handler.genhash("stub", ""), handler.default_marker)
  1357. self.assertEqual(handler.hash("stub"), handler.default_marker)
  1358. self.assertEqual(handler.using().default_marker, handler.default_marker)
  1359. # custom marker
  1360. self.assertEqual(handler.genhash("stub", "", marker="*xxx"), "*xxx")
  1361. self.assertEqual(handler.hash("stub", marker="*xxx"), "*xxx")
  1362. self.assertEqual(handler.using(marker="*xxx").hash("stub"), "*xxx")
  1363. # reject invalid marker
  1364. self.assertRaises(ValueError, handler.genhash, 'stub', "", marker='abc')
  1365. self.assertRaises(ValueError, handler.hash, 'stub', marker='abc')
  1366. self.assertRaises(ValueError, handler.using, marker='abc')
  1367. class unix_fallback_test(HandlerCase):
  1368. handler = hash.unix_fallback
  1369. accepts_all_hashes = True
  1370. known_correct_hashes = [
  1371. # *everything* should hash to "!", and nothing should verify
  1372. ("password", "!"),
  1373. (UPASS_TABLE, "!"),
  1374. ]
  1375. # silence annoying deprecation warning
  1376. def setUp(self):
  1377. super(unix_fallback_test, self).setUp()
  1378. warnings.filterwarnings("ignore", "'unix_fallback' is deprecated")
  1379. def test_90_wildcard(self):
  1380. """test enable_wildcard flag"""
  1381. h = self.handler
  1382. self.assertTrue(h.verify('password','', enable_wildcard=True))
  1383. self.assertFalse(h.verify('password',''))
  1384. for c in "!*x":
  1385. self.assertFalse(h.verify('password',c, enable_wildcard=True))
  1386. self.assertFalse(h.verify('password',c))
  1387. def test_91_preserves_existing(self):
  1388. """test preserves existing disabled hash"""
  1389. handler = self.handler
  1390. # use marker if no hash
  1391. self.assertEqual(handler.genhash("stub", ""), "!")
  1392. self.assertEqual(handler.hash("stub"), "!")
  1393. # use hash if provided and valid
  1394. self.assertEqual(handler.genhash("stub", "!asd"), "!asd")
  1395. #=============================================================================
  1396. # eof
  1397. #=============================================================================