1414 * Copyright (C) 2012 Joshua M. Clulow <[email protected] > 1515 */
1616
17- var log = console . log ;
18- var crypto = require ( 'crypto' ) ;
1917var $ = require ( './common' ) ;
20- var lmhashbuf = require ( './smbhash' ) . lmhashbuf ;
21- var nthashbuf = require ( './smbhash' ) . nthashbuf ;
22-
18+ var { lmhashbuf, nthashbuf } = require ( './smbhash' ) ;
19+ var { URL } = require ( 'url' ) ;
2320
2421function encodeType1 ( hostname , ntdomain ) {
2522 hostname = hostname . toUpperCase ( ) ;
@@ -28,7 +25,7 @@ function encodeType1(hostname, ntdomain) {
2825 var ntdomainlen = Buffer . byteLength ( ntdomain , 'ascii' ) ;
2926
3027 var pos = 0 ;
31- var buf = new Buffer ( 32 + hostnamelen + ntdomainlen ) ;
28+ var buf = Buffer . alloc ( 32 + hostnamelen + ntdomainlen ) ;
3229
3330 buf . write ( 'NTLMSSP' , pos , 7 , 'ascii' ) ; // byte protocol[8];
3431 pos += 7 ;
@@ -76,7 +73,6 @@ function encodeType1(hostname, ntdomain) {
7673 return buf ;
7774}
7875
79-
8076/*
8177 *
8278 */
@@ -102,15 +98,10 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
10298 hostname = hostname . toUpperCase ( ) ;
10399 ntdomain = ntdomain . toUpperCase ( ) ;
104100
105- var lmh = new Buffer ( 21 ) ;
106- lmhashbuf ( password ) . copy ( lmh ) ;
107- lmh . fill ( 0x00 , 16 ) ; // null pad to 21 bytes
108- var nth = new Buffer ( 21 ) ;
109- nthashbuf ( password ) . copy ( nth ) ;
110- nth . fill ( 0x00 , 16 ) ; // null pad to 21 bytes
101+ const challenge = new Buffer . from ( nonce , 'ascii' )
111102
112- var lmr = makeResponse ( lmh , nonce ) ;
113- var ntr = makeResponse ( nth , nonce ) ;
103+ var lmr = makeResponse ( lmhashbuf ( password ) , challenge ) ;
104+ var ntr = makeResponse ( nthashbuf ( password ) , challenge ) ;
114105
115106 var usernamelen = Buffer . byteLength ( username , 'ucs2' ) ;
116107 var hostnamelen = Buffer . byteLength ( hostname , 'ucs2' ) ;
@@ -126,7 +117,7 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
126117
127118 var pos = 0 ;
128119 var msg_len = 64 + ntdomainlen + usernamelen + hostnamelen + lmrlen + ntrlen ;
129- var buf = new Buffer ( msg_len ) ;
120+ var buf = Buffer . alloc ( msg_len ) ;
130121
131122 buf . write ( 'NTLMSSP' , pos , 7 , 'ascii' ) ; // byte protocol[8];
132123 pos += 7 ;
@@ -203,16 +194,18 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
203194 return buf ;
204195}
205196
206- function makeResponse ( hash , nonce )
197+ function makeResponse ( lmhash , challenge )
207198{
208- var out = new Buffer ( 24 ) ;
209- for ( var i = 0 ; i < 3 ; i ++ ) {
210- var keybuf = $ . oddpar ( $ . expandkey ( hash . slice ( i * 7 , i * 7 + 7 ) ) ) ;
211- var des = crypto . createCipheriv ( 'DES-ECB' , keybuf , '' ) ;
212- var str = des . update ( nonce . toString ( 'binary' ) , 'binary' , 'binary' ) ;
213- out . write ( str , i * 8 , i * 8 + 8 , 'binary' ) ;
214- }
215- return out ;
199+ let buf = new Buffer . alloc ( 24 ) ,
200+ pwBuffer = new Buffer . alloc ( 21 ) . fill ( 0 ) ;
201+
202+ lmhash . copy ( pwBuffer ) ;
203+
204+ $ . calculateDES ( pwBuffer . slice ( 0 , 7 ) , challenge ) . copy ( buf ) ;
205+ $ . calculateDES ( pwBuffer . slice ( 7 , 14 ) , challenge ) . copy ( buf , 8 ) ;
206+ $ . calculateDES ( pwBuffer . slice ( 14 ) , challenge ) . copy ( buf , 16 ) ;
207+
208+ return buf ;
216209}
217210
218211exports . encodeType1 = encodeType1 ;
@@ -226,9 +219,9 @@ exports.challengeHeader = function (hostname, domain) {
226219} ;
227220
228221exports . responseHeader = function ( res , url , domain , username , password ) {
229- var serverNonce = new Buffer ( ( res . headers [ 'www-authenticate' ] . match ( / ^ N T L M \s + ( .+ ?) ( , | \s + | $ ) / ) || [ ] ) [ 1 ] , 'base64' ) ;
230- var hostname = require ( ' url' ) . parse ( url ) . hostname ;
231- return 'NTLM ' + exports . encodeType3 ( username , hostname , domain , exports . decodeType2 ( serverNonce ) , password ) . toString ( 'base64' )
222+ const serverNonce = Buffer . from ( ( res . headers [ 'www-authenticate' ] . match ( / ^ N T L M \s + ( .+ ?) ( , | \s + | $ ) / ) || [ ] ) [ 1 ] , 'base64' ) ;
223+ const host = new URL ( url ) . host ;
224+ return 'NTLM ' + exports . encodeType3 ( username , host , domain , exports . decodeType2 ( serverNonce ) , password ) . toString ( 'base64' ) ;
232225} ;
233226
234227// Import smbhash module.
0 commit comments