Codice:
#pragma comment(lib, "Ws2_32.lib")
// Define
#define MAX_SOCKET 10
#define RECV_BUFFER 512
// Includes
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#include <vector>
#include <sstream>
using namespace std;
int WsaStart(void); // Simpel WSA-Start Function
std::string DecryptSessionPacket(std::string str); // Decrypt Session Function
std::vector<std::string> split( std::string str, char delimiter ); // Split Function
std::string DecryptGamePacket2(unsigned char str[]); // DecryptGame2 Function
std::string DecryptGamePacket(int session_id, unsigned char *str, int length); // DecryptGame Function
int main()
{
SetConsoleTitle(L"Simpel Server");
int iResult;
long lRc;
SOCKET sAcceptSocket;
SOCKADDR_IN sAddr;
char cBuf[RECV_BUFFER];
char rBuf[RECV_BUFFER];
FD_SET FdSet;
SOCKET sClient[MAX_SOCKET];
lRc = WsaStart();
if(lRc != 0)
{
cout << "Error: WsaStart : " << WSAGetLastError() << endl;
return 1;
}else
{
cout << "WinSock Started" << endl;
}
sAcceptSocket = socket(AF_INET, SOCK_STREAM, NULL);
if(sAcceptSocket == INVALID_SOCKET)
{
cout << "Error: Socket can not Creat : " << WSAGetLastError() << endl;
return 1;
}else
{
cout << "AcceptSocket Creat" << endl;
}
// Socket bind
memset(&sAddr, NULL, sizeof(SOCKADDR_IN));
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(4017);
sAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
lRc = bind(sAcceptSocket, (SOCKADDR*)&sAddr, sizeof(SOCKADDR_IN));
if(lRc == SOCKET_ERROR)
{
cout << "Error: Bind : " << WSAGetLastError() << endl;
return 1;
}else
{
cout << "Socket bind on port 4017" << endl;
}
lRc = listen(sAcceptSocket, MAX_SOCKET);
if(lRc == SOCKET_ERROR)
{
cout << "Error: listen : " << WSAGetLastError() << endl;
return 1;
}else
{
cout << "sAcceptSocket on list mod" << endl;
}
for(int i = 0; i < MAX_SOCKET; i++)
{
sClient[i] = INVALID_SOCKET;
}
while(1)
{
FD_ZERO(&FdSet);
FD_SET(sAcceptSocket, &FdSet);
for(int i = 0; i < MAX_SOCKET; i++)
{
if(sClient[i] != INVALID_SOCKET)
{
FD_SET(sClient[i], &FdSet);
}
}
lRc = select(0, &FdSet, NULL, NULL, NULL);
if(lRc == SOCKET_ERROR)
{
cout << "Error: select : " << WSAGetLastError() << endl;
}
if(FD_ISSET(sAcceptSocket, &FdSet))
{
for(int i = 0; i < MAX_SOCKET; i++)
{
if(sClient[i] == INVALID_SOCKET)
{
sClient[i] = accept(sAcceptSocket, NULL, NULL);
cout << "New Client" << endl;
iResult = recv(sClient[i], rBuf, RECV_BUFFER, NULL);
string sSessionID = DecryptSessionPacket(rBuf);
vector<string> pSessionID = split(sSessionID.c_str(), ' ');
stringstream tSessionID(pSessionID[0].c_str());
int iSessionID;
tSessionID >> iSessionID;
cout << "Session-ID: " << iSessionID << "\n" << endl;
cout << "Size: " << iResult << endl;
iResult = recv(sClient[i], rBuf, RECV_BUFFER, NULL);
cout << "DecryptGamePacket: " << DecryptGamePacket(iSessionID, (unsigned char*)rBuf, iResult) << endl;
cout << "Size: " << iResult << endl;
break;
}
}
}
for(int i = 0; i < MAX_SOCKET; i++)
{
if(sClient[i] == INVALID_SOCKET)
{
continue;
}
if(FD_ISSET(sClient[i], &FdSet))
{
lRc = recv(sClient[i], rBuf, RECV_BUFFER, NULL);
if(lRc == 0 || lRc == SOCKET_ERROR)
{
cout << "Client " << i << " have close connection" << endl;
closesocket(sClient[i]);
sClient[i] = INVALID_SOCKET;
}else
{
// Recv
}
}
}
}
return 0;
}
int WsaStart(void)
{
WSADATA dWsa;
return WSAStartup(MAKEWORD(2, 0), &dWsa);
}
std::string DecryptSessionPacket(std::string str)
{
std::string encrypted_string;
for (int i = 1; i < str.length(); i++)
{
if (str[i] == 0xE) { return encrypted_string; }
unsigned char firstbyte = str[i] - 0xF;
unsigned char secondbyte = firstbyte;
secondbyte &= 0xF0;
firstbyte = firstbyte - secondbyte;
secondbyte >>= 0x4;
switch (secondbyte)
{
case 0:
encrypted_string += ' ';
break;
case 1:
encrypted_string += ' ';
break;
case 2:
encrypted_string += '-';
break;
case 3:
encrypted_string += '.';
break;
default:
secondbyte += 0x2C;
encrypted_string += secondbyte;
break;
}
switch (firstbyte)
{
case 0:
encrypted_string += '\0';
break;
case 1:
encrypted_string += ' ';
break;
case 2:
encrypted_string += '-';
break;
case 3:
encrypted_string += '.';
break;
default:
firstbyte += 0x2C;
encrypted_string += firstbyte;
break;
}
}
return encrypted_string;
}
std::vector<std::string> split( std::string str, char delimiter )
{
std::vector<std::string> ret;
size_t pos = str.find_first_of( delimiter );
while ( !str.empty() )
{
std::string cur = str.substr( 0, pos );
if ( !cur.empty() )
ret.push_back( cur );
if ( pos == std::string::npos )
break;
str = str.substr( pos + 1 );
pos = str.find_first_of( delimiter );
}
return ret;
}
std::string DecryptGamePacket2(unsigned char str[])
{
std::string decrypted_string;
char table[] = {' ','-','.','0','1','2','3','4','5','6','7','8','9','n'};
int count = 0;
for (count = 0; count < strlen((const char*)str); )
{
if (str[count] <= 0x7A)
{
unsigned char len = str[count];
for (int i = 0; i < (int)len; i++)
{
count++;
decrypted_string += str[count] ^ 0xFF;
}
count++;
} else
{
unsigned char len = str[count];
len &= 0x7F;
for (int i = 0; i < (int)len;)
{
count++;
unsigned char highbyte = str[count];
highbyte &= 0xF0;
highbyte >>= 0x4;
unsigned char lowbyte = str[count];
lowbyte &= 0x0F;
if (highbyte != 0x0 && highbyte != 0xF)
{
decrypted_string += table[highbyte-1];
i++;
}
if (lowbyte != 0x0 && lowbyte != 0xF)
{
decrypted_string += table[lowbyte-1];
i++;
}
}
count ++;
}
}
return decrypted_string;
}
std::string DecryptGamePacket(int session_id, unsigned char *str, int length)
{
std::string encrypted_string = "";
int session_key = session_id & 0xFF;
unsigned char session_number = session_id >> 6;
session_number &= 0xFF;
session_number &= 0x80000003;
switch (session_number)
{
case 0:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] - firstbyte;
encrypted_string += highbyte;
}
break;
case 1:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] + firstbyte;
encrypted_string += highbyte;
}
break;
case 2:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] - firstbyte ^ 0xC3;
encrypted_string += highbyte;
}
break;
case 3:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] + firstbyte ^ 0xC3;
encrypted_string += highbyte;
}
break;
default:
encrypted_string += 0xF;
break;
}
std::vector<std::string> temp = split(encrypted_string, 0xFF);
std::string save;
for (int i = 0; i < temp.size(); i++)
{
save += DecryptGamePacket2((unsigned char*) temp[i].c_str());
save += 0xFF;
}
return save;
}