Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Spotify Protocol Connection Demo
- //N.B. Don't forget to install the dependencies
- var crypto = require('crypto');
- var fs = require('fs');
- var net = require('net');
- var protobuf = require('protocol-buffers');
- var HOST = 'lon6-accesspoint-a19.ap.spotify.com';
- var PORT = 4070;
- var client = new net.Socket();
- var messages = protobuf(fs.readFileSync('keyexchange.proto'))
- var prime = Buffer([
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
- 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,
- 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e,
- 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
- 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e,
- 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a,
- 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14,
- 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
- 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4,
- 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ]);
- var dh = crypto.createDiffieHellman(prime);
- var dhLocalKeys = dh.generateKeys();
- var clientHelloMessage = messages.ClientHello.encode({
- build_info: {
- product: messages.Product.PRODUCT_PARTNER,
- platform: messages.Platform.PLATFORM_OSX_X86_64,
- version: 105600451,
- },
- cryptosuites_supported: [messages.Cryptosuite.CRYPTO_SUITE_SHANNON],
- login_crypto_hello: {
- diffie_hellman: {
- gc: dhLocalKeys,
- server_keys_known: 1
- }
- },
- client_nonce: crypto.randomBytes(16),
- });
- var bufHeader = new Buffer([0x00, 0x04]);
- var bufLength = Buffer.alloc(4);
- bufLength.writeUInt32BE('0x' + (2 + 4 + clientHelloMessage.length).toString(16));
- var clientHelloBuf = Buffer.concat([bufHeader, bufLength, clientHelloMessage]);
- var isInitialPacket = true;
- client.connect(PORT, HOST, function() {
- console.log('CONNECTED TO: ' + HOST + ':' + PORT);
- client.write(clientHelloBuf);
- });
- client.on('data', function(data) {
- var response = new Buffer(data, "hex");
- if (isInitialPacket) {
- var sharedSecret = dh.computeSecret(messages.APResponseMessage.decode(response.slice(4, 2 * (response.readUInt32BE(0, 7)))).challenge.login_crypto_challenge.diffie_hellman.gs);
- var shannonKeyBuf = new Buffer(100);
- for (var i = 1; i <= 5; i++) {
- var hmacSha1 = crypto.createHmac('sha1', sharedSecret);
- var buf = Buffer.concat([clientHelloBuf, response]);
- buf.write(i.toString(), buf.length - 1);
- hmacSha1.update(buf);
- shannonKeyBuf.write(hmacSha1.digest('hex'), (i - 1) * 20, 20, 'hex');
- if (shannonKeyBuf[99] !== 00) {
- console.log('\nKey String: ' + shannonKeyBuf.toString('hex'));
- console.log('\nChallenge: ' + shannonKeyBuf.slice(0, 20).toString('hex'));
- console.log('\nSend Key: ' + shannonKeyBuf.slice(20, 52).toString('hex'));
- console.log('\nRecieve Key: ' + shannonKeyBuf.slice(52, 82).toString('hex'));
- var clientResponsePlaintext = messages.ClientResponsePlaintext.encode({
- login_crypto_response: {
- diffie_hellman: {
- hmac: shannonKeyBuf.slice(0, 20)
- }
- },
- pow_response: {},
- crypto_response: {}
- });
- client.write(clientResponsePlaintext);
- }
- }
- } else {
- console.log(response.toString('hex'));
- }
- client.destroy();
- });
- client.on('close', function() {
- console.log('Connection closed');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement