Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. //Spotify Protocol Connection Demo
  2. //N.B. Don't forget to install the dependencies
  3.  
  4. var crypto = require('crypto');
  5. var fs = require('fs');
  6. var net = require('net');
  7.  
  8. var protobuf = require('protocol-buffers');
  9.  
  10. var HOST = 'lon6-accesspoint-a19.ap.spotify.com';
  11. var PORT = 4070;
  12.  
  13. var client = new net.Socket();
  14.  
  15. var messages = protobuf(fs.readFileSync('keyexchange.proto'))
  16.  
  17. var prime = Buffer([
  18. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
  19. 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,
  20. 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e,
  21. 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
  22. 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e,
  23. 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a,
  24. 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14,
  25. 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
  26. 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4,
  27. 0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff,
  28. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ]);
  29.  
  30. var dh = crypto.createDiffieHellman(prime);
  31. var dhLocalKeys = dh.generateKeys();
  32.  
  33. var clientHelloMessage = messages.ClientHello.encode({
  34. build_info: {
  35. product: messages.Product.PRODUCT_PARTNER,
  36. platform: messages.Platform.PLATFORM_OSX_X86_64,
  37. version: 105600451,
  38. },
  39. cryptosuites_supported: [messages.Cryptosuite.CRYPTO_SUITE_SHANNON],
  40. login_crypto_hello: {
  41. diffie_hellman: {
  42. gc: dhLocalKeys,
  43. server_keys_known: 1
  44. }
  45. },
  46. client_nonce: crypto.randomBytes(16),
  47. });
  48.  
  49. var bufHeader = new Buffer([0x00, 0x04]);
  50.  
  51. var bufLength = Buffer.alloc(4);
  52. bufLength.writeUInt32BE('0x' + (2 + 4 + clientHelloMessage.length).toString(16));
  53.  
  54. var clientHelloBuf = Buffer.concat([bufHeader, bufLength, clientHelloMessage]);
  55.  
  56. var isInitialPacket = true;
  57.  
  58. client.connect(PORT, HOST, function() {
  59.  
  60. console.log('CONNECTED TO: ' + HOST + ':' + PORT);
  61.  
  62. client.write(clientHelloBuf);
  63.  
  64. });
  65.  
  66. client.on('data', function(data) {
  67.  
  68. var response = new Buffer(data, "hex");
  69.  
  70. if (isInitialPacket) {
  71.  
  72. var sharedSecret = dh.computeSecret(messages.APResponseMessage.decode(response.slice(4, 2 * (response.readUInt32BE(0, 7)))).challenge.login_crypto_challenge.diffie_hellman.gs);
  73.  
  74. var shannonKeyBuf = new Buffer(100);
  75.  
  76. for (var i = 1; i <= 5; i++) {
  77. var hmacSha1 = crypto.createHmac('sha1', sharedSecret);
  78. var buf = Buffer.concat([clientHelloBuf, response]);
  79.  
  80. buf.write(i.toString(), buf.length - 1);
  81. hmacSha1.update(buf);
  82.  
  83. shannonKeyBuf.write(hmacSha1.digest('hex'), (i - 1) * 20, 20, 'hex');
  84.  
  85. if (shannonKeyBuf[99] !== 00) {
  86.  
  87. console.log('\nKey String: ' + shannonKeyBuf.toString('hex'));
  88. console.log('\nChallenge: ' + shannonKeyBuf.slice(0, 20).toString('hex'));
  89. console.log('\nSend Key: ' + shannonKeyBuf.slice(20, 52).toString('hex'));
  90. console.log('\nRecieve Key: ' + shannonKeyBuf.slice(52, 82).toString('hex'));
  91.  
  92. var clientResponsePlaintext = messages.ClientResponsePlaintext.encode({
  93. login_crypto_response: {
  94. diffie_hellman: {
  95. hmac: shannonKeyBuf.slice(0, 20)
  96. }
  97. },
  98. pow_response: {},
  99. crypto_response: {}
  100. });
  101.  
  102. client.write(clientResponsePlaintext);
  103. }
  104.  
  105. }
  106.  
  107. } else {
  108. console.log(response.toString('hex'));
  109. }
  110.  
  111. client.destroy();
  112.  
  113. });
  114.  
  115. client.on('close', function() {
  116. console.log('Connection closed');
  117. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement