forked from hugo-marello/ntlm-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntlm-socket.js
288 lines (234 loc) · 9.12 KB
/
ntlm-socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const Socket = require('net').Socket;
const { Duplex } = require('stream');
const util = require('./utils');
module.exports = class NtlmSocket extends Duplex {
_negotiationStarted = false;
_socket = new Socket();
_optionalHeaders = '';
_firstPayload = '';
customEvents = ['ntlm-error', 'ntlm-data', 'ntlm-authorized', 'ntlm-authenticate', 'ntlm-challenge', 'ntlm-negotiate'];
socketEvents = ['close', 'connect', 'data', 'drain', 'end', 'error', 'lookup', 'ready', 'timeout'];
constructor(user, options) {
super();
this._user = user;
this._options = options;
//prepare future optional headers
for(let header of this._options.headers){
this._optionalHeaders += header + '\r\n';
}
//re-throw all socket events
for(let event of this.socketEvents){
this._socket.on(event, (...args) => this.emit(event, ...args));
}
this._socket.once('data', (data) => this._parseChallenge(data));
this.on('ntlm-data', (data) => {
this.push(data);
});
}
connect(port, host, cb) {
this._socket.connect(port, host, cb);
}
_parseChallenge(data) {
let headers = data.toString().split('\r\n');
if(headers.length < 2) {
return this.emit('ntlm-error', 'Received an invalid response from proxy.\n'+data);
}
let httpMethod = headers.shift();
let statusCode = httpMethod.match(/HTTP\/\d\.\d (.*) /);
if(!statusCode || (statusCode[1] != '407' && statusCode[1] != '401')) {
return this.emit('ntlm-error', 'Proxy challenge answer mismatch 407(Authentication Required).\n'+data);
}
headers.filter(header => header.startsWith('Proxy-Authenticate: NTLM ')).forEach(value => this.challenge = value.slice(25));
if(!this.challenge) {
return this.emit('ntlm-error', 'No ntlm challenge received.\n'+data.toString());
}
let buf = Buffer.from(this.challenge, 'base64');
let pos = 8;
let signature = buf.slice(0, pos);
if(signature.toString() !== 'NTLMSSP\0') {
return this.emit('ntlm-error', `Proxy didn't send a signature in the challenge.\n`+this.challenge);
}
let messageType = buf.readUInt32LE(pos);
pos += 4;
if(messageType !== 0x02) {
return this.emit('ntlm-error', `Proxy didn't send a valid message type in the challenge.\n`+this.challenge);
}
pos += 8; //ignoring the TargetName as suggested on the documentation
this._receivedFlags = buf.readUInt32LE(pos);
pos += 4;
this._challengeNonce = buf.slice(pos, pos+8);
pos += 16; // also ignoring other 8 reserved bytes
this.emit('ntlm-challenge', this.challenge);
this._socket.once('data', (arg) => this._parseAuthorized(arg));
this._writeAuthenticate();
}
_parseAuthorized(data) {
if(data.toString().match(/^HTTP\/\d\.\d 2\d\d .*/)) {
this._socket.on('data', (chunk) => this.emit('ntlm-data', chunk));
this.emit('ntlm-authorized');
} else {
return this.emit('ntlm-error', 'Proxy response was different than authorized.\n'+data.toString());
}
}
write(chunk, encoding, cb) {
let _cb = cb;
if(typeof encoding == 'function' && !cb){
_cb = encoding;
}
if(this._negotiationStarted){
this._socket.write(chunk, (...args)=> _cb(args));
} else {
this._writeNegotiate(chunk.toString(), encoding, _cb);
}
}
_ntlmNegotiateMsg(){
let domain = this._user.domain.toUpperCase();
let user = this._user.hostname.toUpperCase();
let userlen = Buffer.byteLength(user, 'ascii');
let domainlen = Buffer.byteLength(domain, 'ascii');
let buf = Buffer.alloc(32 + userlen + domainlen);
let pos = 0;
buf.write('NTLMSSP\0', pos, 7, 'ascii'); //Signature
pos += 8;
buf.writeUInt32LE(1, pos); //Message type
pos += 4;
buf.writeUInt32LE(0xb207,pos);// flags
pos += 4;
buf.writeUInt16LE(domainlen, pos); //Domain
pos += 2;
buf.writeUInt16LE(domainlen, pos);
pos += 2;
buf.writeUInt32LE(0x20, pos); //Domain offset, from start of package
pos += 4;
buf.writeUInt16LE(userlen, pos); //Username
pos += 2;
buf.writeUInt16LE(userlen, pos);
pos += 2;
buf.writeUInt32LE(0x20+domainlen, pos); //Username offset, also from start of package
pos += 4;
//Payload
buf.write(domain, pos, domainlen, 'ascii');
pos += domainlen;
buf.write(user, pos, userlen, 'ascii');
pos += userlen;
return buf.toString('base64');
}
_writeNegotiate(chunk, cb) {
let httpEnd = chunk.indexOf('\r\n\r\n');
if(httpEnd === -1){
return this.emit('ntlm-error', 'Invalid first HTTP request.\n'+chunk);
}
this._firstMessage = chunk.slice(0, httpEnd+2); //also removing last terminator of http request
this._firstCb = cb;
if(chunk.length > httpEnd + 4) { // has payload
this._firstPayload = chunk.slice(httpEnd+4);
}
let NTLM = this._ntlmNegotiateMsg();
let request = this._firstMessage + this._optionalHeaders + 'Proxy-Authorization: NTLM '+NTLM+'\r\n\r\n'+this._firstPayload;
this._negotiationStarted = true;
this._socket.write(request, () => {
this.emit('ntlm-negotiate', NTLM);
});
}
_writeAuthenticate() {
let NTLM = this._ntlmAuthenticateMsg();
let request = this._firstMessage + this._optionalHeaders + 'Proxy-Authorization: NTLM '+NTLM+'\r\n\r\n'+this._firstPayload;
this._socket.write(request, () => {
if(this._firstCb) {
this._firstCb();
}
this.emit('ntlm-authenticate', NTLM);
});
}
_ntlmAuthenticateMsg() {
let lmr = util.resolveChallenge(this._challengeNonce, this._user.LTKeys);
let ntr = util.resolveChallenge(this._challengeNonce, this._user.NTKeys);
let hostnameLength = this._user.hostname.length*2;
let domainLength = this._user.domain.length*2;
let userLength = this._user.user.length*2;
let totalLength = 64 +
domainLength +
userLength +
hostnameLength +
lmr.length +
ntr.length;
let response = Buffer.allocUnsafe(totalLength);
let pos = 0;
let payloadOffset = 64;
response.write('NTLMSSP\0', pos, 8, 'ascii'); //signature
pos += 8;
response.writeUInt32LE(0x03, pos); //message type
pos += 4;
// LM challenge
response.writeUInt16LE(lmr.length, pos);
pos += 2;
response.writeUInt16LE(lmr.length, pos);
pos += 2;
response.writeUInt32LE(payloadOffset, pos);
pos += 4;
payloadOffset += lmr.length;
// NTLM challenge
response.writeUInt16LE(ntr.length, pos);
pos += 2;
response.writeUInt16LE(ntr.length, pos);
pos += 2;
response.writeUInt32LE(payloadOffset, pos);
pos += 4;
payloadOffset += ntr.length;
// domain
response.writeUInt16LE(domainLength, pos);
pos += 2;
response.writeUInt16LE(domainLength, pos);
pos += 2;
response.writeUInt32LE(payloadOffset, pos);
pos += 4;
payloadOffset += domainLength;
// username
response.writeUInt16LE(userLength, pos);
pos += 2;
response.writeUInt16LE(userLength, pos);
pos += 2;
response.writeUInt32LE(payloadOffset, pos);
pos += 4;
payloadOffset += userLength;
// workstation
response.writeUInt16LE(hostnameLength, pos);
pos += 2;
response.writeUInt16LE(hostnameLength, pos);
pos += 2;
response.writeUInt32LE(payloadOffset, pos);
pos += 4;
payloadOffset += hostnameLength;
// encrypted session
response.writeUInt16LE(0, pos);
pos += 2;
response.writeUInt16LE(0, pos);
pos += 2;
response.writeUInt32LE(payloadOffset, pos);
pos += 4;
payloadOffset += 0;
// flags
response.writeUInt32LE(this._receivedFlags, pos);
pos += 4;
// Payload
lmr.copy(response, pos, 0, lmr.length);
pos += lmr.length;
ntr.copy(response, pos, 0, ntr.length);
pos += ntr.length;
response.write(this._user.domain.toUpperCase(), pos, domainLength, 'ucs2');
pos += domainLength;
response.write(this._user.user.toUpperCase(), pos, userLength, 'ucs2');
pos += userLength;
response.write(this._user.hostname.toUpperCase(), pos, hostnameLength, 'ucs2');
pos += hostnameLength;
return response.toString('base64');
}
_write(chunk, encoding, callback) {
this.write(chunk, encoding, callback);
}
_read(size) {
}
_final(cb) {
this._socket.end(cb);
}
}