123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- (require 'md4)
- (defun ntlm-build-auth-request (user &optional domain)
- "Return the NTLM authentication request string for USER and DOMAIN.
- USER is a string representing a user name to be authenticated and
- DOMAIN is a NT domain. USER can include a NT domain part as in
- user@domain where the string after @ is used as the domain if DOMAIN
- is not given."
- (interactive)
- (let ((request-ident (concat "NTLMSSP" (make-string 1 0)))
- (request-msgType (concat (make-string 1 1) (make-string 3 0)))
-
- (request-flags (concat (make-string 1 7) (make-string 1 178)
- (make-string 2 0)))
-
- lu ld off-d off-u)
- (when (string-match "@" user)
- (unless domain
- (setq domain (substring user (1+ (match-beginning 0)))))
- (setq user (substring user 0 (match-beginning 0))))
-
- (setq lu (length user))
- (setq ld (length domain))
- (setq off-u 32)
- (setq off-d (+ 32 lu))
-
- (concat request-ident
- request-msgType
- request-flags
- (md4-pack-int16 lu)
- (md4-pack-int16 lu)
- (md4-pack-int32 (cons 0 off-u))
- (md4-pack-int16 ld)
- (md4-pack-int16 ld)
- (md4-pack-int32 (cons 0 off-d))
- user
- domain
- )))
- (eval-when-compile
- (defmacro ntlm-string-as-unibyte (string)
- (if (fboundp 'string-as-unibyte)
- `(string-as-unibyte ,string)
- string)))
- (defun ntlm-build-auth-response (challenge user password-hashes)
- "Return the response string to a challenge string CHALLENGE given by
- the NTLM based server for the user USER and the password hash list
- PASSWORD-HASHES. NTLM uses two hash values which are represented
- by PASSWORD-HASHES. PASSWORD-HASHES should be a return value of
- (list (ntlm-smb-passwd-hash password) (ntlm-md4hash password))"
- (let* ((rchallenge (ntlm-string-as-unibyte challenge))
-
-
-
- (uDomain (substring rchallenge 12 20))
- (flags (substring rchallenge 20 24))
- (challengeData (substring rchallenge 24 32))
- uDomain-len uDomain-offs
-
- lmRespData
- ntRespData
- domain
- lu ld off-lm off-nt off-d off-u off-w off-s)
-
- (setq uDomain-len (md4-unpack-int16 (substring uDomain 0 2)))
- (setq uDomain-offs (md4-unpack-int32 (substring uDomain 4 8)))
- (setq domain
- (ntlm-unicode2ascii (substring challenge
- (cdr uDomain-offs)
- (+ (cdr uDomain-offs) uDomain-len))
- (/ uDomain-len 2)))
-
- (when (string-match "@" user)
- (setq domain (substring user (1+ (match-beginning 0))))
- (setq user (substring user 0 (match-beginning 0))))
-
- (setq lmRespData
- (ntlm-smb-owf-encrypt (car password-hashes) challengeData))
- (setq ntRespData
- (ntlm-smb-owf-encrypt (cadr password-hashes) challengeData))
-
- (setq lu (length user))
- (setq ld (length domain))
- (setq off-lm 64)
- (setq off-nt (+ 64 24))
- (setq off-d (+ 64 48))
- (setq off-u (+ 64 48 (* 2 ld)))
- (setq off-w (+ 64 48 (* 2 (+ ld lu))))
- (setq off-s (+ 64 48 (* 2 (+ ld lu lu))))
-
- (concat "NTLMSSP\0"
- (md4-pack-int32 '(0 . 3))
-
-
- (md4-pack-int16 24)
- (md4-pack-int16 24)
- (md4-pack-int32 (cons 0 off-lm))
-
-
- (md4-pack-int16 24)
- (md4-pack-int16 24)
- (md4-pack-int32 (cons 0 off-nt))
-
-
-
- (md4-pack-int16 (* 2 ld))
- (md4-pack-int16 (* 2 ld))
- (md4-pack-int32 (cons 0 off-d))
-
-
-
- (md4-pack-int16 (* 2 lu))
- (md4-pack-int16 (* 2 lu))
- (md4-pack-int32 (cons 0 off-u))
-
-
- (md4-pack-int16 (* 2 lu))
- (md4-pack-int16 (* 2 lu))
- (md4-pack-int32 (cons 0 off-w))
-
-
- (md4-pack-int16 0)
- (md4-pack-int16 0)
- (md4-pack-int32 (cons 0 (- off-s off-lm)))
-
- flags
-
- lmRespData
- ntRespData
- (ntlm-ascii2unicode domain
- (length domain))
- (ntlm-ascii2unicode user
- (length user))
- (ntlm-ascii2unicode user
- (length user))
- )))
- (defun ntlm-get-password-hashes (password)
- "Return a pair of SMB hash and NT MD4 hash of the given password PASSWORD."
- (list (ntlm-smb-passwd-hash password)
- (ntlm-md4hash password)))
- (defun ntlm-ascii2unicode (str len)
- "Convert an ASCII string into a NT Unicode string, which is
- little-endian utf16."
- (let ((utf (make-string (* 2 len) 0)) (i 0) val)
- (while (and (< i len)
- (not (zerop (setq val (aref str i)))))
- (aset utf (* 2 i) val)
- (aset utf (1+ (* 2 i)) 0)
- (setq i (1+ i)))
- utf))
- (defun ntlm-unicode2ascii (str len)
- "Extract 7 bits ASCII part of a little endian utf16 string STR of length LEN."
- (let ((buf (make-string len 0)) (i 0) (j 0))
- (while (< i len)
- (aset buf i (logand (aref str j) 127))
- (setq i (1+ i)
- j (+ 2 j)))
- buf))
- (defun ntlm-smb-passwd-hash (passwd)
- "Return the SMB password hash string of 16 bytes long for the given password
- string PASSWD. PASSWD is truncated to 14 bytes if longer."
- (let ((len (min (length passwd) 14)))
- (ntlm-smb-des-e-p16
- (concat (substring (upcase passwd) 0 len)
- (make-string (- 15 len) 0)))))
- (defun ntlm-smb-owf-encrypt (passwd c8)
- "Return the response string of 24 bytes long for the given password
- string PASSWD based on the DES encryption. PASSWD is of at most 14
- bytes long and the challenge string C8 of 8 bytes long."
- (let ((len (min (length passwd) 16)) p22)
- (setq p22 (concat (substring passwd 0 len)
- (make-string (- 22 len) 0)))
- (ntlm-smb-des-e-p24 p22 c8)))
- (defun ntlm-smb-des-e-p24 (p22 c8)
- "Return a 24 bytes hashed string for a 21 bytes string P22 and a 8 bytes
- string C8."
- (concat (ntlm-smb-hash c8 p22 t)
- (ntlm-smb-hash c8 (substring p22 7) t)
- (ntlm-smb-hash c8 (substring p22 14) t)))
- (defconst ntlm-smb-sp8 [75 71 83 33 64 35 36 37])
- (defun ntlm-smb-des-e-p16 (p15)
- "Return a 16 bytes hashed string for a 15 bytes string P15."
- (concat (ntlm-smb-hash ntlm-smb-sp8 p15 t)
- (ntlm-smb-hash ntlm-smb-sp8
- (substring p15 7) t)))
- (defun ntlm-smb-hash (in key forw)
- "Return the hash string of length 8 for a string IN of length 8 and
- a string KEY of length 8. FORW is t or nil."
- (let ((out (make-string 8 0))
- outb
- (inb (make-string 64 0))
- (keyb (make-string 64 0))
- (key2 (ntlm-smb-str-to-key key))
- (i 0) aa)
- (while (< i 64)
- (unless (zerop (logand (aref in (/ i 8)) (lsh 1 (- 7 (% i 8)))))
- (aset inb i 1))
- (unless (zerop (logand (aref key2 (/ i 8)) (lsh 1 (- 7 (% i 8)))))
- (aset keyb i 1))
- (setq i (1+ i)))
- (setq outb (ntlm-smb-dohash inb keyb forw))
- (setq i 0)
- (while (< i 64)
- (unless (zerop (aref outb i))
- (setq aa (aref out (/ i 8)))
- (aset out (/ i 8)
- (logior aa (lsh 1 (- 7 (% i 8))))))
- (setq i (1+ i)))
- out))
- (defun ntlm-smb-str-to-key (str)
- "Return a string of length 8 for the given string STR of length 7."
- (let ((key (make-string 8 0))
- (i 7))
- (aset key 0 (lsh (aref str 0) -1))
- (aset key 1 (logior
- (lsh (logand (aref str 0) 1) 6)
- (lsh (aref str 1) -2)))
- (aset key 2 (logior
- (lsh (logand (aref str 1) 3) 5)
- (lsh (aref str 2) -3)))
- (aset key 3 (logior
- (lsh (logand (aref str 2) 7) 4)
- (lsh (aref str 3) -4)))
- (aset key 4 (logior
- (lsh (logand (aref str 3) 15) 3)
- (lsh (aref str 4) -5)))
- (aset key 5 (logior
- (lsh (logand (aref str 4) 31) 2)
- (lsh (aref str 5) -6)))
- (aset key 6 (logior
- (lsh (logand (aref str 5) 63) 1)
- (lsh (aref str 6) -7)))
- (aset key 7 (logand (aref str 6) 127))
- (while (>= i 0)
- (aset key i (lsh (aref key i) 1))
- (setq i (1- i)))
- key))
- (defconst ntlm-smb-perm1 [57 49 41 33 25 17 9
- 1 58 50 42 34 26 18
- 10 2 59 51 43 35 27
- 19 11 3 60 52 44 36
- 63 55 47 39 31 23 15
- 7 62 54 46 38 30 22
- 14 6 61 53 45 37 29
- 21 13 5 28 20 12 4])
- (defconst ntlm-smb-perm2 [14 17 11 24 1 5
- 3 28 15 6 21 10
- 23 19 12 4 26 8
- 16 7 27 20 13 2
- 41 52 31 37 47 55
- 30 40 51 45 33 48
- 44 49 39 56 34 53
- 46 42 50 36 29 32])
- (defconst ntlm-smb-perm3 [58 50 42 34 26 18 10 2
- 60 52 44 36 28 20 12 4
- 62 54 46 38 30 22 14 6
- 64 56 48 40 32 24 16 8
- 57 49 41 33 25 17 9 1
- 59 51 43 35 27 19 11 3
- 61 53 45 37 29 21 13 5
- 63 55 47 39 31 23 15 7])
- (defconst ntlm-smb-perm4 [32 1 2 3 4 5
- 4 5 6 7 8 9
- 8 9 10 11 12 13
- 12 13 14 15 16 17
- 16 17 18 19 20 21
- 20 21 22 23 24 25
- 24 25 26 27 28 29
- 28 29 30 31 32 1])
- (defconst ntlm-smb-perm5 [16 7 20 21
- 29 12 28 17
- 1 15 23 26
- 5 18 31 10
- 2 8 24 14
- 32 27 3 9
- 19 13 30 6
- 22 11 4 25])
- (defconst ntlm-smb-perm6 [40 8 48 16 56 24 64 32
- 39 7 47 15 55 23 63 31
- 38 6 46 14 54 22 62 30
- 37 5 45 13 53 21 61 29
- 36 4 44 12 52 20 60 28
- 35 3 43 11 51 19 59 27
- 34 2 42 10 50 18 58 26
- 33 1 41 9 49 17 57 25])
- (defconst ntlm-smb-sc [1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1])
- (defconst ntlm-smb-sbox [[[14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7]
- [ 0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8]
- [ 4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0]
- [15 12 8 2 4 9 1 7 5 11 3 14 10 0 6 13]]
- [[15 1 8 14 6 11 3 4 9 7 2 13 12 0 5 10]
- [ 3 13 4 7 15 2 8 14 12 0 1 10 6 9 11 5]
- [ 0 14 7 11 10 4 13 1 5 8 12 6 9 3 2 15]
- [13 8 10 1 3 15 4 2 11 6 7 12 0 5 14 9]]
- [[10 0 9 14 6 3 15 5 1 13 12 7 11 4 2 8]
- [13 7 0 9 3 4 6 10 2 8 5 14 12 11 15 1]
- [13 6 4 9 8 15 3 0 11 1 2 12 5 10 14 7]
- [ 1 10 13 0 6 9 8 7 4 15 14 3 11 5 2 12]]
- [[ 7 13 14 3 0 6 9 10 1 2 8 5 11 12 4 15]
- [13 8 11 5 6 15 0 3 4 7 2 12 1 10 14 9]
- [10 6 9 0 12 11 7 13 15 1 3 14 5 2 8 4]
- [ 3 15 0 6 10 1 13 8 9 4 5 11 12 7 2 14]]
- [[ 2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9]
- [14 11 2 12 4 7 13 1 5 0 15 10 3 9 8 6]
- [ 4 2 1 11 10 13 7 8 15 9 12 5 6 3 0 14]
- [11 8 12 7 1 14 2 13 6 15 0 9 10 4 5 3]]
- [[12 1 10 15 9 2 6 8 0 13 3 4 14 7 5 11]
- [10 15 4 2 7 12 9 5 6 1 13 14 0 11 3 8]
- [ 9 14 15 5 2 8 12 3 7 0 4 10 1 13 11 6]
- [ 4 3 2 12 9 5 15 10 11 14 1 7 6 0 8 13]]
- [[ 4 11 2 14 15 0 8 13 3 12 9 7 5 10 6 1]
- [13 0 11 7 4 9 1 10 14 3 5 12 2 15 8 6]
- [ 1 4 11 13 12 3 7 14 10 15 6 8 0 5 9 2]
- [ 6 11 13 8 1 4 10 7 9 5 0 15 14 2 3 12]]
- [[13 2 8 4 6 15 11 1 10 9 3 14 5 0 12 7]
- [ 1 15 13 8 10 3 7 4 12 5 6 11 0 14 9 2]
- [ 7 11 4 1 9 12 14 2 0 6 10 13 15 3 5 8]
- [ 2 1 14 7 4 10 8 13 15 12 9 0 3 5 6 11]]])
- (defsubst ntlm-string-permute (in perm n)
- "Return a string of length N for a string IN and a permutation vector
- PERM of size N. The length of IN should be height of PERM."
- (let ((i 0) (out (make-string n 0)))
- (while (< i n)
- (aset out i (aref in (- (aref perm i) 1)))
- (setq i (1+ i)))
- out))
- (defsubst ntlm-string-lshift (str count len)
- "Return a string by circularly shifting a string STR by COUNT to the left.
- length of STR is LEN."
- (let ((c (% count len)))
- (concat (substring str c len) (substring str 0 c))))
- (defsubst ntlm-string-xor (in1 in2 n)
- "Return exclusive-or of sequences in1 and in2"
- (let ((w (make-string n 0)) (i 0))
- (while (< i n)
- (aset w i (logxor (aref in1 i) (aref in2 i)))
- (setq i (1+ i)))
- w))
- (defun ntlm-smb-dohash (in key forw)
- "Return the hash value for a string IN and a string KEY.
- Length of IN and KEY are 64. FORW non-nil means forward, nil means
- backward."
- (let (pk1
- c
- d
- cd
- (ki (make-vector 16 0))
- pd1
- l
- r
- rl
- (i 0) (j 0) (k 0))
- (setq pk1 (ntlm-string-permute key ntlm-smb-perm1 56))
- (setq c (substring pk1 0 28))
- (setq d (substring pk1 28 56))
- (setq i 0)
- (while (< i 16)
- (setq c (ntlm-string-lshift c (aref ntlm-smb-sc i) 28))
- (setq d (ntlm-string-lshift d (aref ntlm-smb-sc i) 28))
- (setq cd (concat (substring c 0 28) (substring d 0 28)))
- (aset ki i (ntlm-string-permute cd ntlm-smb-perm2 48))
- (setq i (1+ i)))
- (setq pd1 (ntlm-string-permute in ntlm-smb-perm3 64))
- (setq l (substring pd1 0 32))
- (setq r (substring pd1 32 64))
- (setq i 0)
- (let (er
- erk
- (b (make-vector 8 0))
- cb
- pcb
- r2
- jj m n bj sbox-jmn)
- (while (< i 16)
- (setq er (ntlm-string-permute r ntlm-smb-perm4 48))
- (setq erk (ntlm-string-xor er
- (aref ki (if forw i (- 15 i)))
- 48))
- (setq j 0)
- (while (< j 8)
- (setq jj (* 6 j))
- (aset b j (substring erk jj (+ jj 6)))
- (setq j (1+ j)))
- (setq j 0)
- (while (< j 8)
- (setq bj (aref b j))
- (setq m (logior (lsh (aref bj 0) 1) (aref bj 5)))
- (setq n (logior (lsh (aref bj 1) 3)
- (lsh (aref bj 2) 2)
- (lsh (aref bj 3) 1)
- (aref bj 4)))
- (setq k 0)
- (setq sbox-jmn (aref (aref (aref ntlm-smb-sbox j) m) n))
- (while (< k 4)
- (aset bj k
- (if (zerop (logand sbox-jmn (lsh 1 (- 3 k))))
- 0 1))
- (setq k (1+ k)))
- (setq j (1+ j)))
- (setq j 0)
- (setq cb nil)
- (while (< j 8)
- (setq cb (concat cb (substring (aref b j) 0 4)))
- (setq j (1+ j)))
- (setq pcb (ntlm-string-permute cb ntlm-smb-perm5 32))
- (setq r2 (ntlm-string-xor l pcb 32))
- (setq l r)
- (setq r r2)
- (setq i (1+ i))))
- (setq rl (concat r l))
- (ntlm-string-permute rl ntlm-smb-perm6 64)))
- (defun ntlm-md4hash (passwd)
- "Return the 16 bytes MD4 hash of a string PASSWD after converting it
- into a Unicode string. PASSWD is truncated to 128 bytes if longer."
- (let (len wpwd)
-
- (setq len (length passwd))
- (if (> len 128)
- (setq len 128))
-
- (setq wpwd (ntlm-ascii2unicode passwd len))
-
- (setq len (* len 2))
- (md4 wpwd len)))
- (provide 'ntlm)
|