UNPKG

7.57 kBMarkdownView Raw
1# httpntlm
2
3__httpntlm__ is a Node.js library to do HTTP NTLM authentication
4
5It's a port from the Python libary [python-ntml](https://code.google.com/p/python-ntlm/) with added NTLMv2 support.
6
7## Donate
8
9Help keep my open source project alive! Your donation, no matter how small, makes a real difference.
10
11Thank you for your support!
12
13
14[![](https://neat.be/paypal-donate-button.png)](https://www.paypal.com/donate/?hosted_button_id=2CKNJLZJBW8ZC) [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/samdecrock)
15
16
17
18## Install
19
20You can install __httpntlm__ using the Node Package Manager (npm):
21
22 npm install httpntlm
23
24## How to use
25
26```js
27var httpntlm = require('httpntlm');
28
29httpntlm.get({
30 url: "https://someurl.com",
31 username: 'm$',
32 password: 'stinks',
33 workstation: 'choose.something',
34 domain: ''
35}, function (err, res){
36 if(err) return console.log(err);
37
38 console.log(res.headers);
39 console.log(res.body);
40});
41```
42
43It supports __http__ and __https__.
44
45## pre-encrypt the password
46```js
47
48var httpntlm = require('httpntlm');
49var ntlm = httpntlm.ntlm;
50var lm = ntlm.create_LM_hashed_password('Azx123456');
51var nt = ntlm.create_NT_hashed_password('Azx123456');
52console.log(lm);
53console.log(Array.prototype.slice.call(lm, 0));
54lm = Buffer.from([ 183, 180, 19, 95, 163, 5, 118, 130, 30, 146, 159, 252, 1, 57, 81, 39 ]);
55console.log(lm);
56
57console.log(nt);
58console.log(Array.prototype.slice.call(nt, 0));
59nt = Buffer.from([150, 27, 7, 219, 220, 207, 134, 159, 42, 60, 153, 28, 131, 148, 14, 1]);
60console.log(nt);
61
62
63httpntlm.get({
64 url: "https://someurl.com",
65 username: 'm$',
66 lm_password: lm,
67 nt_password: nt,
68 workstation: 'choose.something',
69 domain: ''
70}, function (err, res){
71 if(err) return console.log(err);
72
73 console.log(res.headers);
74 console.log(res.body);
75});
76
77/* you can save the array into your code and use it when you need it
78
79<Buffer b7 b4 13 5f a3 05 76 82 1e 92 9f fc 01 39 51 27>// before convert to array
80[ 183, 180, 19, 95, 163, 5, 118, 130, 30, 146, 159, 252, 1, 57, 81, 39 ]// convert to array
81<Buffer b7 b4 13 5f a3 05 76 82 1e 92 9f fc 01 39 51 27>//convert back to buffer
82
83<Buffer 96 1b 07 db dc cf 86 9f 2a 3c 99 1c 83 94 0e 01>
84[ 150, 27, 7, 219, 220, 207, 134, 159, 42, 60, 153, 28, 131, 148, 14, 1 ]
85<Buffer 96 1b 07 db dc cf 86 9f 2a 3c 99 1c 83 94 0e 01>
86*/
87
88```
89
90
91## Options
92
93- `url:` _{String}_ URL to connect. (Required)
94- `username:` _{String}_ Username (optional, default: '')
95- `password:` _{String}_ Password (optional, default: '')
96- `workstation:` _{String}_ Name of workstation (optional, default: '')
97- `domain:` _{String}_ Name of domain (optional, default: '')
98- `agent:` _{Agent}_ In case you want to reuse the keepaliveAgent over different calls (optional)
99
100if you already got the encrypted password,you should use this two param to replace the 'password' param.
101
102- `lm_password` _{Buffer}_ encrypted lm password.(Required)
103- `nt_password` _{Buffer}_ encrypted nt password. (Required)
104
105You can also pass along all other options of [httpreq](https://github.com/SamDecrock/node-httpreq), including custom headers, cookies, body data, ... and use POST, PUT or DELETE instead of GET.
106
107## NTLMv2
108
109When NTLMv2 extended security and target information can be negotiated with the server, this library assumes
110the server supports NTLMv2 and creates responses according to the NTLMv2 specification (the actually supported
111NTLM version cannot be negotiated).
112Otherwise, NTLMv1 or NTLMv1 with NTLMv2 extended security will be used.
113
114## Advanced
115
116If you want to use the NTLM-functions yourself, you can access the ntlm-library like this (https example):
117
118```js
119var ntlm = require('httpntlm').ntlm;
120var async = require('async');
121var httpreq = require('httpreq');
122var HttpsAgent = require('agentkeepalive').HttpsAgent;
123var keepaliveAgent = new HttpsAgent();
124
125var options = {
126 url: "https://someurl.com",
127 username: 'm$',
128 password: 'stinks',
129 workstation: 'choose.something',
130 domain: ''
131};
132
133async.waterfall([
134 function (callback){
135 var type1msg = ntlm.createType1Message(options);
136
137 httpreq.get(options.url, {
138 headers:{
139 'Connection' : 'keep-alive',
140 'Authorization': type1msg
141 },
142 agent: keepaliveAgent
143 }, callback);
144 },
145
146 function (res, callback){
147 if(!res.headers['www-authenticate'])
148 return callback(new Error('www-authenticate not found on response of second request'));
149
150 var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
151 var type3msg = ntlm.createType3Message(type2msg, options);
152
153 setImmediate(function() {
154 httpreq.get(options.url, {
155 headers:{
156 'Connection' : 'Close',
157 'Authorization': type3msg
158 },
159 allowRedirects: false,
160 agent: keepaliveAgent
161 }, callback);
162 });
163 }
164], function (err, res) {
165 if(err) return console.log(err);
166
167 console.log(res.headers);
168 console.log(res.body);
169});
170```
171
172## Download binary files
173
174```javascript
175httpntlm.get({
176 url: "https://someurl.com/file.xls",
177 username: 'm$',
178 password: 'stinks',
179 workstation: 'choose.something',
180 domain: '',
181 binary: true
182}, function (err, response) {
183 if(err) return console.log(err);
184 fs.writeFile("file.xls", response.body, function (err) {
185 if(err) return console.log("error writing file");
186 console.log("file.xls saved!");
187 });
188});
189```
190
191## More information
192
193* [python-ntlm](https://code.google.com/p/python-ntlm/)
194* [NTLM Authentication Scheme for HTTP](https://web.archive.org/web/20200724074947/https://www.innovation.ch/personal/ronald/ntlm.html)
195* [LM hash on Wikipedia](http://en.wikipedia.org/wiki/LM_hash)
196
197## Contributing
198
199Running tests in an open source package is crucial for ensuring the quality and reliability of the codebase. When you submit code changes, it's essential to ensure that these changes don't break existing functionality or introduce new bugs.
200
201To run the unit tests, simply run
202
203 node ./tests/unittests.js
204
205All tests should return `true`
206
207To run the integration tests, first start the NTLM server with
208
209 node ./tests/integrationtests-server.js
210
211Next, run the integration tests with:
212
213 node ./tests/integrationtests.js
214
215
216## License (MIT)
217
218Copyright (c) Sam Decrock <https://github.com/SamDecrock/>
219
220Permission is hereby granted, free of charge, to any person obtaining a copy
221of this software and associated documentation files (the "Software"), to deal
222in the Software without restriction, including without limitation the rights
223to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
224copies of the Software, and to permit persons to whom the Software is
225furnished to do so, subject to the following conditions:
226
227The above copyright notice and this permission notice shall be included in
228all copies or substantial portions of the Software.
229
230THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
232FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
233AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
234LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
235OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
236THE SOFTWARE.