javascript - How do I code a node.js proxy to use NTLMv2 authentication -
i've tried search through stackoverflow similar question people asking client-side of ntlmv2 protocol. i'm implementing proxy performing server-side of protocol authenticate users connecting proxy. i've coded lot of protocol i'm stuck because documentation should take me further difficult understand.
this best documentation i've found far: http://www.innovation.ch/personal/ronald/ntlm.html, how deal lm , nt responses oblivious me.
the proxy located on application server. domain server different machine.
example code node proxy:
var http = require('http') , request = require('request') , proxyauth = require('./proxyauth'); function handlerproxy(req, res) { proxyauth.authorize(req, res); var options = { url: req.url, method: req.method, headers: req.headers } req.pipe(request(options)).pipe(res) } var server = http.createserver(handlerproxy); server.listen(3000, function(){ console.log('express server listening on port ' + 3000); }); proxyauth.js code:
proxyauth = { parsetype3msg: function(buf) { var lmlen = buf.readuint16le(12); var lmoff = buf.readuint16le(16); var ntlen = buf.readuint16le(20); var ntoff = buf.readuint16le(24); var dlen = buf.readuint16le(28); var doff = buf.readuint16le(32); var ulen = buf.readuint16le(36); var uoff = buf.readuint16le(40); var hlen = buf.readuint16le(44); var hoff = buf.readuint16le(48); var domain = buf.slice(doff, doff+dlen).tostring('utf8'); var user = buf.slice(uoff, uoff+ulen).tostring('utf8'); var host = buf.slice(hoff, hoff+hlen).tostring('utf8'); var lmresp = buf.slice(lmoff, lmoff+lmlen).tostring('utf8'); var ntresp = buf.slice(ntoff, ntoff+ntlen).tostring('utf8'); console.log(user, lmresp, ntresp); /* do? */ }, authorize: function(req, res) { var auth = req.headers['authorization']; if (!auth) { res.writehead(401, { 'www-authenticate': 'ntlm', }); res.end('<html><body>proxy authentication required</body></html>'); } else if(auth) { var header = auth.split(' '); var buf = new buffer(header[1], 'base64'); var msg = buf.tostring('utf8'); console.log("decoded", msg); if (header[0] == "ntlm") { if (msg.substring(0,8) != "ntlmssp\x00") { res.writehead(401, { 'www-authenticate': 'ntlm', }); res.end('<html><body>header not recognized</body></html>'); } // type 1 message if (msg[8] == "\x01") { console.log(buf.tostring('hex')); var challenge = require('crypto').randombytes(8); var type2msg = "ntlmssp\x00"+ "\x02\x00\x00\x00"+ // 8 message type "\x00\x00\x00\x00"+ // 12 target name len/alloc "\x00\x00\x00\x00"+ // 16 target name offset "\x01\x82\x00\x00"+ // 20 flags challenge.tostring('utf8')+ // 24 challenge "\x00\x00\x00\x00\x00\x00\x00\x00"+ // 32 context "\x00\x00\x00\x00\x00\x00\x00\x00"; // 40 target info len/alloc/offset type2msg = new buffer(type2msg).tostring('base64'); res.writehead(401, { 'www-authenticate': 'ntlm '+type2msg.trim(), }); res.end(); } else if (msg[8] == "\x03") { console.log(buf.tostring('hex')); proxyauth.parsetype3msg(buf); /* do? */ } } else if (header[0] == "basic") { } } } }; module.exports = proxyauth; the /* do? */ comment specifies stuck.
i hope put enough information there, let me know if else needed.
Comments
Post a Comment