Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 1x 11x 11x 6x 5x 5x 5x 5x 5x 5x 5x 5x 1x 3x 3x 9x 9x 55x 50x 5x 5x 5x 5x 9x 5x 5x 7x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 1x 8x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 5x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 1x | import fetch, { RequestInit, Response } from 'node-fetch'; import dns from 'dns'; import { sysinfo, sspi, InitializeSecurityContextInput, AcquireCredHandleInput, SecuritySupportProvider, } from '../../lib/api'; import {} from './domain'; import { encode, decode } from 'base64-arraybuffer'; import dbg from 'debug'; import { CookieList } from './interfaces'; const debug = dbg('node-expose-sspi:client'); // Thanks to : // - // - /** * Get the SPN the same way Chrome/Firefox or IE does. * * Links: * - getting the domain name: https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string * - algo of IE : https://support.microsoft.com/en-us/help/4551934/kerberos-failures-in-internet-explorer * * @param {string} url * @returns {string} */ export async function getSPNFromURI(url: string): Promise<string> { const msDomainName = sysinfo.GetComputerNameEx('ComputerNameDnsDomain'); Iif (msDomainName.length === 0) { debug('Client running on a host that is not part of a Microsoft domain'); return 'whatever'; } const matches = /^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/i.exec(url); const urlDomain = matches && matches[1]; if (!urlDomain) { throw new Error('url is not well parsed. url=' + url); } debug('urlDomain: ', urlDomain); if (['localhost', '127.0.0.1'].includes(urlDomain)) { return 'HTTP/localhost'; } // needs urlFQDN for the DNS resolver. const urlFQDN = urlDomain.includes('.') ? urlDomain : urlDomain + '.' + msDomainName; let hostname = urlFQDN; try { while (Itrue) { const records = await dns.promises.resolve(hostname, 'CNAME'); debug('records', records); if (records.length === 0) { break; } hostname = records[0]; } } catch (e) { debug('DNS error', e); } const result = 'HTTP/' + hostname; debug('result: ', result); return result; } /** * Allow to fetch url with a system that uses the negotiate protocol. * Cookies are managed if necessary during the process. * * @export * @class Client */ export class Client { private cookieList: CookieList = {}; private domain!: string; private user!: string; private password!: string; private targetName!: string; private ssp: SecuritySupportProvider = 'Negotiate'; private saveCookies(response: Response): void { response.headers.forEach((value, name) => { if (name !== 'Set-Cookie'.toLowerCase()) { return; } // parse something like <key>=<val>[; Expires=xxxxx;] const [key, val] = value.split(/[=;]/g); debug('val: ', val); debug('key: ', key); this.cookieList[key] = val; }); debug('cookieList: ', this.cookieList); } private restituteCookies(requestInit: RequestInit): void { const cookieStr = Object.keys(this.cookieList) .map((key) => key + '=' + this.cookieList[key]) .join('; '); Iif (cookieStr.length === 0) { return; } Object.assign(requestInit.headers, { cookie: cookieStr }); } /** * Set the credentials for running the client as another user. * * By default, the credentials are the logged windows account. * * @param {string} domain * @param {string} user * @param {string} password * @memberof Client */ setCredentials(domain: string, user: string, password: string): void { this.domain = domain; this.user = user; this.password = password; } /** * Force the targetName to a value. * * For Kerberos, the targetName is the SPN (Service Principal Name). * * @param {string} targetName * @memberof Client */ setTargetName(targetName: string): void { this.targetName = targetName; } /** * Set the Security Support Provider (NTLM, Kerberos, Negotiate) * * @param {SecuritySupportProvider} ssp * @memberof Client */ setSSP(ssp: SecuritySupportProvider): void { this.ssp = ssp; } /** * Works as the fetch function of node-fetch node module. * This function can handle the negotiate protocol with SPNEGO tokens. * * @param {string} resource - the URL to fetch * @param {RequestInit} [init] - the options (headers, body, etc.) * @returns {Promise<Response>} a promise with the HTTP response. * @memberof Client */ async fetch(resource: string, init?: RequestInit): Promise<Response> { const response = await fetch(resource, init); const result = await this.handleAuth(response, resource, init); return result; } /** * The authentication negotiate protocol is handled by this function. * It is called by `Client.fetch`. * * @private * @param {Response} response * @param {string} resource * @param {RequestInit} [init={}] * @returns {Promise<Response>} * @memberof Client */ private async handleAuth( response: Response, resource: string, Einit: RequestInit = {} ): Promise<Response> { debug('start response.headers', response.headers); // has cookies ? this.saveCookies(response); Iif (!response.headers.has('www-authenticate')) { debug('no header www-authenticate'); return response; } Iif (!response.headers.get('www-authenticate')?.startsWith('Negotiate')) { debug( 'no header www-authenticate with Negotiate:', response.headers.get('www-authenticate') ); return response; } Iif (response.status !== 401) { debug('no status 401'); return response; } debug('starting negotiate auth'); const credInput = { packageName: this.ssp, credentialUse: 'SECPKG_CRED_OUTBOUND', } as AcquireCredHandleInput; if (this.user) { credInput.authData = { domain: this.domain, user: this.user, password: this.password, }; } const clientCred = sspi.AcquireCredentialsHandle(credInput); const packageInfo = sspi.QuerySecurityPackageInfo(this.ssp); const targetName = Ithis.targetName || (await getSPNFromURI(resource)); let input: InitializeSecurityContextInput = { credential: clientCred.credential, targetName, cbMaxToken: packageInfo.cbMaxToken, targetDataRep: 'SECURITY_NATIVE_DREP', }; debug('input: ', input); let clientSecurityContext = sspi.InitializeSecurityContext(input); // encode to Base64 and send via HTTP let base64 = encode(clientSecurityContext.SecBufferDesc.buffers[0]); let requestInit: RequestInit = { ...init }; requestInit.headers = { ...init.headers, Authorization: 'Negotiate ' + base64, }; // cookies case this.restituteCookies(requestInit); debug('first requestInit.headers', requestInit.headers); response = await fetch(resource, requestInit); debug('first response.headers', response.headers); this.saveCookies(response); while ( response.headers.has('www-authenticate') && response.status === 401 && response.headers.get('www-authenticate')?.startsWith('Negotiate ') ) { const buffer = decode( (response.headers.get('www-authenticate') as string).substring( 'Negotiate '.length ) ); input = { credential: clientCred.credential, targetName, cbMaxToken: packageInfo.cbMaxToken, SecBufferDesc: { ulVersion: 0, buffers: [buffer], }, contextHandle: clientSecurityContext.contextHandle, targetDataRep: 'SECURITY_NATIVE_DREP', }; clientSecurityContext = sspi.InitializeSecurityContext(input); base64 = encode(clientSecurityContext.SecBufferDesc.buffers[0]); requestInit = { ...init }; requestInit.headers = { ...init.headers, Authorization: 'Negotiate ' + base64, }; this.restituteCookies(requestInit); debug('other requestInit.headers', requestInit.headers); response = await fetch(resource, requestInit); debug('other response.headers', response.headers); this.saveCookies(response); } debug('handleAuth: end'); return response; } } |