Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.57 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. RealmSocket::RealmSocket(SocketHandler &h) : TcpSocket(h)
  4. {
  5.     N.SetHexStr("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7");
  6.     g.SetDword(7);
  7.     _authed = false;
  8.     acct = NULL;
  9. }
  10.  
  11. RealmSocket::~RealmSocket()
  12. {
  13.  
  14. }
  15.  
  16. void RealmSocket::OnAccept()
  17. {
  18.     TcpSocket::OnAccept();
  19.     Log.Text("[REALMLIST] Connection accepted from %s", GetRemoteAddress().c_str());
  20.     s.SetRand(s_BYTE_SIZE * 8);
  21. }
  22.  
  23. void RealmSocket::OnRead()
  24. {
  25.     TcpSocket::OnRead();
  26.  
  27.     if(!ibuf.GetLength())
  28.         return; // abort on no data
  29.  
  30.     unsigned char cmdid = 0;
  31.     ibuf.SoftRead((char*)&cmdid, sizeof(unsigned char));
  32.  
  33.     switch(cmdid)
  34.     {
  35.     case 0x00:  // CLIENT LOGON CHALLENGE
  36.         this->_HandleLogonChallenge();
  37.         break;
  38.     case 0x01:  // CLIENT LOGON PROOF
  39.         this->_HandleLogonProof();
  40.         break;
  41.     case 0x10:  // CLIENT REALM LIST
  42.         this->_HandleRealmList();
  43.         break;
  44.     default:
  45.         Log.Text("[REALMLIST] Received unknown cmdid: %d of size %u", cmdid, ibuf.GetLength());
  46.         break;
  47.     }
  48. }
  49.  
  50. void RealmSocket::_HandleLogonChallenge()
  51. {
  52.     if(ibuf.GetLength() < 4)
  53.         return;     // wtf?
  54.  
  55.     std::vector<uint8> buf;
  56.     buf.resize(4);
  57.     ibuf.Read((char*)&buf[0], 4);
  58.  
  59.     uint16 r = ((sAuthLogonChallenge_C *)&buf[0])->size;
  60.     Log.Text("[REALMLIST] Got Auth Challenge Header. Body is %u bytes.", r);
  61.  
  62.     if(ibuf.GetLength() < r)
  63.         return; // incomplete packet
  64.    
  65.     buf.resize(r+ buf.size() + 1);
  66.     buf[buf.size() - 1] = 0;
  67.     ibuf.Read((char *)&buf[4], r);
  68.  
  69.     sAuthLogonChallenge_C *ch = (sAuthLogonChallenge_C*)&buf[0];
  70.  
  71.     Log.Text("[REALMLIST] Got full packet. Client is build %u.", ch->build);
  72.  
  73.     Log.Text("            Username: %s", (const char*)ch->I);
  74.  
  75.     acct = AccountMgr.GetAccount((const char*)ch->I);
  76.     if(!acct)
  77.     {
  78.         acct = AccountMgr.AddAccount((const char*)ch->I, "NOPASS");
  79.     }
  80.     ByteBuffer outpacket;
  81.  
  82.     if(!acct)
  83.     {
  84.         outpacket << uint8(AUTH_LOGON_CHALLENGE);
  85.         outpacket << uint8(0);
  86.         outpacket << uint8(0x04);
  87.     }
  88.     else
  89.     {
  90.         outpacket << uint8(AUTH_LOGON_CHALLENGE);
  91.         outpacket << uint8(0);
  92.         outpacket << uint8(0x00);
  93.  
  94.         password = acct->password;
  95.         username = acct->username;
  96.  
  97.         Sha1Hash I;
  98.         std::string sI = username + ":" + password;
  99.         I.UpdateData(sI);
  100.         I.Finalize();
  101.  
  102.         Sha1Hash sha;
  103.         sha.UpdateData(s.AsByteArray(), s.GetNumBytes());
  104.         sha.UpdateData(I.GetDigest(), SHA_DIGEST_LENGTH);
  105.         sha.Finalize();
  106.  
  107.         BigNumber x;
  108.         x.SetBinary(sha.GetDigest(), sha.GetLength());
  109.        
  110.         v = g.ModExp(x, N);
  111.         b.SetRand(19 * 8);
  112.  
  113.         BigNumber gmod = g.ModExp(b, N);
  114.         B = ((v * 3) + gmod) % N;
  115.         ASSERT(gmod.GetNumBytes() <= 32);
  116.  
  117.         BigNumber unk3;
  118.         unk3.SetRand(16*8);
  119.  
  120.         outpacket.append(B.AsByteArray(), 32);
  121.         outpacket // g_len, g
  122.             << (uint8)1;
  123.         outpacket.append(g.AsByteArray(), 1);
  124.         // N_len, N
  125.         outpacket << (uint8)32;
  126.         outpacket.append(N.AsByteArray(), 32);
  127.  
  128.         outpacket.append(s.AsByteArray(), s.GetNumBytes());
  129.         outpacket.append(unk3.AsByteArray(), 16);
  130.         outpacket << uint8(0x00);     // new in 1.11
  131.     }
  132.  
  133.     SendBuf(((char*)outpacket.contents()), outpacket.size());
  134. }
  135.  
  136. void RealmSocket::_HandleLogonProof()
  137. {
  138.     Log.Text("[REALMLIST] Got Logon Proof. Checking.");
  139.  
  140.     sAuthLogonProof_C lp;
  141.     ibuf.Read((char *)&lp, sizeof(sAuthLogonProof_C));
  142.  
  143.     BigNumber A;
  144.     A.SetBinary(lp.A, 32);
  145.  
  146.     Sha1Hash sha;
  147.     sha.UpdateBigNumbers(&A, &B, NULL);
  148.     sha.Finalize();
  149.     BigNumber u;
  150.     u.SetBinary(sha.GetDigest(), 20);
  151.     BigNumber S = (A * (v.ModExp(u, N))).ModExp(b, N);
  152.  
  153.     uint8 t[32];
  154.     uint8 t1[16];
  155.     uint8 vK[40];
  156.     memcpy(t, S.AsByteArray(), 32);
  157.     for (int i = 0; i < 16; i++)
  158.     {
  159.         t1[i] = t[i*2];
  160.     }
  161.     sha.Initialize();
  162.     sha.UpdateData(t1, 16);
  163.     sha.Finalize();
  164.     for (int i = 0; i < 20; i++)
  165.     {
  166.         vK[i*2] = sha.GetDigest()[i];
  167.     }
  168.     for (int i = 0; i < 16; i++)
  169.     {
  170.         t1[i] = t[i*2+1];
  171.     }
  172.     sha.Initialize();
  173.     sha.UpdateData(t1, 16);
  174.     sha.Finalize();
  175.     for (int i = 0; i < 20; i++)
  176.     {
  177.         vK[i*2+1] = sha.GetDigest()[i];
  178.     }
  179.     K.SetBinary(vK, 40);
  180.  
  181.     uint8 hash[20];
  182.  
  183.     sha.Initialize();
  184.     sha.UpdateBigNumbers(&N, NULL);
  185.     sha.Finalize();
  186.     memcpy(hash, sha.GetDigest(), 20);
  187.     sha.Initialize();
  188.     sha.UpdateBigNumbers(&g, NULL);
  189.     sha.Finalize();
  190.     for (int i = 0; i < 20; i++)
  191.     {
  192.         hash[i] ^= sha.GetDigest()[i];
  193.     }
  194.     BigNumber t3;
  195.     t3.SetBinary(hash, 20);
  196.  
  197.     sha.Initialize();
  198.     sha.UpdateData(username);
  199.     sha.Finalize();
  200.     BigNumber t4;
  201.     t4.SetBinary(sha.GetDigest(), 20);
  202.  
  203.     sha.Initialize();
  204.     sha.UpdateBigNumbers(&t3, &t4, &s, &A, &B, &K, NULL);
  205.     sha.Finalize();
  206.     BigNumber M;
  207.     M.SetBinary(sha.GetDigest(), 20);
  208.  
  209.     if (!memcmp(M.AsByteArray(), lp.M1, 20))
  210.     {
  211.         Log.Text("           Successful.");
  212.         //memcpy(acct->sessionkey, K.AsHexStr(), 40);
  213.         acct->sessionkey = K.AsHexStr();
  214.  
  215.         sha.Initialize();
  216.         sha.UpdateBigNumbers(&A, &M, &K, NULL);
  217.         sha.Finalize();
  218.  
  219.         sAuthLogonProof_S proof;
  220.         memcpy(proof.M2, sha.GetDigest(), 20);
  221.         proof.cmd = AUTH_LOGON_PROOF;
  222.         proof.error = 0;
  223.         proof.unk2 = 0;
  224.         proof.unk203 = 0;
  225.         proof.unk203_more = 0;
  226.  
  227.         SendBuf((char *)&proof, sizeof(proof));
  228.     }
  229.     else
  230.     {
  231.         ByteBuffer pkt;
  232.         pkt << (uint8) AUTH_LOGON_PROOF;
  233.         pkt << (uint8) 4; // bad password
  234.         pkt << (uint32) 3;
  235.  
  236.         SendBuf((char *)pkt.contents(), pkt.size());
  237.     }
  238. }
  239.  
  240. void RealmSocket::_HandleRealmList()
  241. {
  242.     // static realmlist for now.
  243.     ByteBuffer pkt;
  244.  
  245.     if (ibuf.GetLength() < 5)
  246.         return;
  247.     ibuf.Remove(5);
  248.  
  249.     pkt << uint8(REALM_LIST);
  250.     pkt << uint16(0);   // size placeholder
  251.  
  252.     pkt << uint32(0);
  253.     pkt << uint16(1);
  254.  
  255.     pkt << uint8(1);//type
  256.     pkt << uint8(0);//lock
  257.     pkt << uint8(0);//offline
  258.     pkt << "Burlex WotLK Sandbox";
  259.     pkt << "127.0.0.1:3725";
  260.     pkt << float(1.6); //pop
  261.     pkt << uint8(1); //ch count
  262.     pkt << uint8(1); //realm category
  263.     pkt << uint8(6); //unk
  264.     pkt << uint8(0); //unk
  265.     pkt << uint8(0x17);//unk
  266.  
  267.     *(uint16*)&pkt.contents()[1] = (pkt.size() - 3);
  268.  
  269.     SendBuf((char*)pkt.contents(), pkt.size());
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement