1414 * Copyright (C) 2012 Joshua M. Clulow <[email protected] > 1515 */
1616
17- var log = console . log ;
18- var crypto = require ( 'crypto' ) ;
17+ const { url } = require ( 'inspector' ) ;
1918var $ = require ( './common' ) ;
20- var lmhashbuf = require ( './smbhash' ) . lmhashbuf ;
21- var nthashbuf = require ( './smbhash' ) . nthashbuf ;
22-
19+ var { lmhashbuf, nthashbuf } = require ( './smbhash' ) ;
20+ var { URL } = require ( 'url' ) ;
2321
2422function encodeType1 ( hostname , ntdomain ) {
2523 hostname = hostname . toUpperCase ( ) ;
@@ -28,7 +26,7 @@ function encodeType1(hostname, ntdomain) {
2826 var ntdomainlen = Buffer . byteLength ( ntdomain , 'ascii' ) ;
2927
3028 var pos = 0 ;
31- var buf = new Buffer ( 32 + hostnamelen + ntdomainlen ) ;
29+ var buf = Buffer . alloc ( 32 + hostnamelen + ntdomainlen ) ;
3230
3331 buf . write ( 'NTLMSSP' , pos , 7 , 'ascii' ) ; // byte protocol[8];
3432 pos += 7 ;
@@ -102,15 +100,10 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
102100 hostname = hostname . toUpperCase ( ) ;
103101 ntdomain = ntdomain . toUpperCase ( ) ;
104102
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
103+ const challenge = new Buffer . from ( nonce , 'ascii' )
111104
112- var lmr = makeResponse ( lmh , nonce ) ;
113- var ntr = makeResponse ( nth , nonce ) ;
105+ var lmr = makeResponse ( lmhashbuf ( password ) , challenge ) ;
106+ var ntr = makeResponse ( nthashbuf ( password ) , challenge ) ;
114107
115108 var usernamelen = Buffer . byteLength ( username , 'ucs2' ) ;
116109 var hostnamelen = Buffer . byteLength ( hostname , 'ucs2' ) ;
@@ -126,7 +119,7 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
126119
127120 var pos = 0 ;
128121 var msg_len = 64 + ntdomainlen + usernamelen + hostnamelen + lmrlen + ntrlen ;
129- var buf = new Buffer ( msg_len ) ;
122+ var buf = Buffer . alloc ( msg_len ) ;
130123
131124 buf . write ( 'NTLMSSP' , pos , 7 , 'ascii' ) ; // byte protocol[8];
132125 pos += 7 ;
@@ -203,16 +196,18 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
203196 return buf ;
204197}
205198
206- function makeResponse ( hash , nonce )
199+ function makeResponse ( lmhash , challenge )
207200{
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 ;
201+ let buf = new Buffer . alloc ( 24 ) ,
202+ pwBuffer = new Buffer . alloc ( 21 ) . fill ( 0 ) ;
203+
204+ lmhash . copy ( pwBuffer ) ;
205+
206+ $ . calculateDES ( pwBuffer . slice ( 0 , 7 ) , challenge ) . copy ( buf ) ;
207+ $ . calculateDES ( pwBuffer . slice ( 7 , 14 ) , challenge ) . copy ( buf , 8 ) ;
208+ $ . calculateDES ( pwBuffer . slice ( 14 ) , challenge ) . copy ( buf , 16 ) ;
209+
210+ return buf ;
216211}
217212
218213exports . encodeType1 = encodeType1 ;
@@ -226,9 +221,9 @@ exports.challengeHeader = function (hostname, domain) {
226221} ;
227222
228223exports . 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' )
224+ const serverNonce = Buffer . from ( ( res . headers [ 'www-authenticate' ] . match ( / ^ N T L M \s + ( .+ ?) ( , | \s + | $ ) / ) || [ ] ) [ 1 ] , 'base64' ) ;
225+ const host = new URL ( url ) . host ;
226+ return 'NTLM ' + exports . encodeType3 ( username , host , domain , exports . decodeType2 ( serverNonce ) , password ) . toString ( 'base64' ) ;
232227} ;
233228
234229// Import smbhash module.
0 commit comments