Launcher update to 1.63.5

- async tcp buffer
- two way encryption on connect
- map security fix
- DNS Lookup on connect
- fixed bug in beamng security
This commit is contained in:
Anonymous275 2020-10-16 17:10:12 +03:00
parent ccdbc51b30
commit 7da9f4fcd2
17 changed files with 340 additions and 134 deletions

View File

@ -4,6 +4,5 @@ set(CMAKE_CXX_STANDARD 17)
file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h") file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h")
add_executable(${PROJECT_NAME} ${source_files}) add_executable(${PROJECT_NAME} ${source_files})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher") set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
target_link_libraries(${PROJECT_NAME} ws2_32 urlmon rstrtmgr discord-rpc zlibstatic libcurl_a) target_link_libraries(${PROJECT_NAME} ws2_32 urlmon rstrtmgr discord-rpc zlibstatic libcurl_a)
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>) target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

40
include/Buffer.h Normal file
View File

@ -0,0 +1,40 @@
///
/// Created by Anonymous275 on 8/25/2020
///
#pragma once
void ServerParser(const std::string& Data);
class Buffer{
public:
void Handle(const std::string& Data){
Buf += Data;
Manage();
}
void clear(){
Buf.clear();
}
private:
std::string Buf;
void Manage(){
if(!Buf.empty()){
std::string::size_type p;
if (Buf.at(0) == '\n'){
p = Buf.find('\n',1);
if(p != -1){
std::string R = Buf.substr(1,p-1);
std::string_view B(R.c_str(),R.find(char(0)));
ServerParser(B.data());
Buf = Buf.substr(p+1);
Manage();
}
}else{
p = Buf.find('\n');
if(p == -1)Buf.clear();
else{
Buf = Buf.substr(p);
Manage();
}
}
}
}
};

View File

@ -3,11 +3,13 @@
/// ///
#pragma once #pragma once
#include <string> #include <string>
#include "Buffer.h"
void NetReset(); void NetReset();
extern long long ping; extern long long ping;
extern bool Dev; extern bool Dev;
void ClearAll(); void ClearAll();
extern int ClientID; extern int ClientID;
extern Buffer Handler;
extern bool ModLoaded; extern bool ModLoaded;
extern bool Terminate; extern bool Terminate;
extern int DEFAULT_PORT; extern int DEFAULT_PORT;
@ -17,9 +19,12 @@ extern std::string UlStatus;
extern std::string ListOfMods; extern std::string ListOfMods;
void UDPSend(std::string Data); void UDPSend(std::string Data);
[[noreturn]] void CoreNetwork(); [[noreturn]] void CoreNetwork();
void SendLarge(std::string Data);
void TCPSend(const std::string&Data); void TCPSend(const std::string&Data);
void GameSend(const std::string&Data); void GameSend(const std::string&Data);
void SendLarge(const std::string&Data); std::string GetAddr(const std::string&IP);
void ServerParser(const std::string& Data);
void TCPClientMain(const std::string& IP,int Port); void TCPClientMain(const std::string& IP,int Port);
void UDPClientMain(const std::string& IP,int Port); void UDPClientMain(const std::string& IP,int Port);
void TCPGameServer(const std::string& IP, int Port); void TCPGameServer(const std::string& IP, int Port);

View File

@ -4,9 +4,13 @@
#pragma once #pragma once
#include <string> #include <string>
#include "Xor.h" #include "Xor.h"
struct RSA{
int n = 0;
int e = 0;
int d = 0;
};
std::string RSA_D(const std::string& Data,int d, int n); std::string RSA_D(const std::string& Data,int d, int n);
std::string RSA_E(const std::string& Data,int e, int n); std::string RSA_E(const std::string& Data,int e, int n);
std::string LocalEnc(const std::string& Data); std::string LocalEnc(const std::string& Data);
std::string LocalDec(const std::string& Data); std::string LocalDec(const std::string& Data);
std::string Encrypt(std::string msg); RSA* GenKey();
std::string Decrypt(std::string msg);

View File

@ -45,7 +45,7 @@ void InitLog(){
} }
void addToLog(const std::string& Line){ void addToLog(const std::string& Line){
std::ofstream LFS; std::ofstream LFS;
LFS.open (Sec("Launcher.log"), std::ios_base::app); LFS.open(Sec("Launcher.log"), std::ios_base::app);
LFS << Line.c_str(); LFS << Line.c_str();
LFS.close(); LFS.close();
} }

View File

@ -23,7 +23,12 @@ std::string LoadedMap(HANDLE processHandle, long long Address){
std::vector<int> Off = {0x140,0x0}; std::vector<int> Off = {0x140,0x0};
return Game.ReadPointerText(processHandle,Address,Off); return Game.ReadPointerText(processHandle,Address,Off);
} }
std::string Tolower(std::string data){
for(char&c : data){
c = char(tolower(c));
}
return data;
}
void MemoryInit(){ void MemoryInit(){
if(Dev)return; if(Dev)return;
Game.PID = GamePID; Game.PID = GamePID;
@ -38,8 +43,10 @@ void MemoryInit(){
std::string Map,Temp; std::string Map,Temp;
while(true){ while(true){
Map = LoadedMap(processHandle,Lib1); Map = LoadedMap(processHandle,Lib1);
if(!Map.empty() && Map != "-1" && Map.find("/info.json") != std::string::npos && Map != Temp){ if(!Map.empty() && Map != "-1" && Map.find("/info.json") != -1 && Map != Temp){
if(MStatus.find(Map) == std::string::npos)exit(5); if(Tolower(MStatus).find(Tolower(Map)) == -1){
exit(5);
}
Temp = Map; Temp = Map;
} }
Map.clear(); Map.clear();

View File

@ -6,10 +6,12 @@
#include "Curl/http.h" #include "Curl/http.h"
#include <WinSock2.h> #include <WinSock2.h>
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include "Startup.h"
#include "Memory.h" #include "Memory.h"
#include "Logger.h" #include "Logger.h"
#include <thread> #include <thread>
#include <set> #include <set>
std::set<std::string>* ConfList = nullptr; std::set<std::string>* ConfList = nullptr;
bool TCPTerminate = false; bool TCPTerminate = false;
int DEFAULT_PORT = 4444; int DEFAULT_PORT = 4444;
@ -19,14 +21,25 @@ std::string MStatus;
bool once = false; bool once = false;
bool ModLoaded; bool ModLoaded;
long long ping = -1; long long ping = -1;
Buffer Handler;
void StartSync(const std::string &Data){ void StartSync(const std::string &Data){
std::string IP = GetAddr(Data.substr(1,Data.find(':')-1));
if(IP.find('.') == -1){
if(IP == "DNS")UlStatus = Sec("UlConnection Failed! (DNS Lookup Failed)");
else UlStatus = Sec("UlConnection Failed! (WSA failed to start)");
ListOfMods = "-";
Terminate = true;
return;
}
UlStatus = Sec("UlLoading..."); UlStatus = Sec("UlLoading...");
TCPTerminate = false; TCPTerminate = false;
Terminate = false; Terminate = false;
ConfList->clear(); ConfList->clear();
Handler.clear();
ping = -1; ping = -1;
std::thread GS(TCPGameServer,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); std::thread GS(TCPGameServer,IP,std::stoi(Data.substr(Data.find(':')+1)));
GS.detach(); GS.detach();
info(Sec("Connecting to server"));
} }
void Parse(std::string Data,SOCKET CSocket){ void Parse(std::string Data,SOCKET CSocket){
char Code = Data.at(0), SubCode = 0; char Code = Data.at(0), SubCode = 0;
@ -39,14 +52,11 @@ void Parse(std::string Data,SOCKET CSocket){
NetReset(); NetReset();
Terminate = true; Terminate = true;
TCPTerminate = true; TCPTerminate = true;
//if(!Dev){ Data = Code + HTTP_REQUEST(Sec("https://beammp.com/servers-info"),443);
Data = Code + HTTP_REQUEST(Sec("s1.yourthought.co.uk/servers-info"),3599);
//}else Data.clear();
break; break;
case 'C': case 'C':
ListOfMods.clear(); ListOfMods.clear();
StartSync(Data); StartSync(Data);
info(Sec("Connecting to server"));
while(ListOfMods.empty() && !Terminate){ while(ListOfMods.empty() && !Terminate){
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
@ -78,6 +88,9 @@ void Parse(std::string Data,SOCKET CSocket){
} }
Data.clear(); Data.clear();
break; break;
case 'Z':
Data = "Z" + GetVer();
break;
default: default:
Data.clear(); Data.clear();
break; break;

26
src/Network/DNS.cpp Normal file
View File

@ -0,0 +1,26 @@
///
/// Created by Anonymous275 on 9/25/2020
///
#include <string>
#include <winsock.h>
#include "Logger.h"
std::string GetAddr(const std::string&IP){
if(IP.find_first_not_of("0123456789.") == -1)return IP;
WSADATA wsaData;
hostent *host;
if(WSAStartup(514, &wsaData) != 0){
error("WSA Startup Failed!");
WSACleanup();
return "";
}
host = gethostbyname(IP.c_str());
if(!host){
error("DNS lookup failed! on " + IP);
WSACleanup();
return "DNS";
}
std::string Ret = inet_ntoa(*((struct in_addr *)host->h_addr));
WSACleanup();
return Ret;
}

View File

@ -31,6 +31,7 @@ void ServerSend(std::string Data, bool Rel){
int DLen = int(Data.length()); int DLen = int(Data.length());
if(DLen > 3)C = Data.at(0); if(DLen > 3)C = Data.at(0);
if (C == 'O' || C == 'T')Ack = true; if (C == 'O' || C == 'T')Ack = true;
if(C == 'W' || C == 'Y' || C == 'V' || C == 'E')Rel = true;
if(Ack || Rel){ if(Ack || Rel){
if(Ack || DLen > 1000)SendLarge(Data); if(Ack || DLen > 1000)SendLarge(Data);
else TCPSend(Data); else TCPSend(Data);
@ -187,7 +188,6 @@ void TCPGameServer(const std::string& IP, int Port){
ServerSend(t,false); ServerSend(t,false);
S++; S++;
} }
if(S > 3)std::cout << S << std::endl;
}while(Res > 0); }while(Res > 0);
if(Res == 0)debug(Sec("(Proxy) Connection closing")); if(Res == 0)debug(Sec("(Proxy) Connection closing"));
else debug(Sec("(Proxy) recv failed error : ") + std::to_string(WSAGetLastError())); else debug(Sec("(Proxy) recv failed error : ") + std::to_string(WSAGetLastError()));

View File

@ -95,21 +95,54 @@ void Parse(const std::string& msg){
}else return; }else return;
} }
} }
std::string GenerateM(RSA*key){
std::string HandShake(SOCKET Sock){ std::stringstream stream;
stream << std::hex << key->n << "g" << key->e << "g" << RSA_E(Sec("IDC"),key->e,key->n);
return stream.str();
}
struct Hold{
SOCKET TCPSock{};
bool Done = false;
};
void Check(Hold* S){
std::this_thread::sleep_for(std::chrono::seconds(5));
if(S != nullptr){
if(!S->Done && S->TCPSock != -1){
closesocket(S->TCPSock);
}
}
}
std::string HandShake(SOCKET Sock,Hold*S,RSA*LKey){
S->TCPSock = Sock;
std::thread Timeout(Check,S);
Timeout.detach();
N = 0;E = 0; N = 0;E = 0;
auto Res = STCPRecv(Sock); auto Res = STCPRecv(Sock);
std::string msg(Res.first,Res.second); std::string msg(Res.first,Res.second);
Parse(msg); Parse(msg);
if(N != 0 && E != 0) { if(N != 0 && E != 0) {
msg = RSA_E("NR" + GetDName() + ":" + GetDID(),E,N); STCPSend(Sock,GenerateM(LKey));
if(!msg.empty()) { Res = STCPRecv(Sock);
STCPSend(Sock,msg); msg = std::string(Res.first,Res.second);
STCPSend(Sock, RSA_E("VC" + GetVer(),E,N)); if(RSA_D(msg,LKey->d,LKey->n) != "HC"){
Res = STCPRecv(Sock); Terminate = true;
msg = Res.first;
} }
}else Terminate = true;
S->Done = true;
if(Terminate){
TCPTerminate = true;
UlStatus = Sec("UlDisconnected: full or outdated server");
info(Sec("Terminated!"));
return "";
} }
msg = RSA_E("NR" + GetDName() + ":" + GetDID(),E,N);
if(!msg.empty()) {
STCPSend(Sock,msg);
STCPSend(Sock, RSA_E("VC" + GetVer(),E,N));
Res = STCPRecv(Sock);
msg = Res.first;
}
if(N == 0 || E == 0 || msg.size() < 2 || msg.substr(0,2) != "WS"){ if(N == 0 || E == 0 || msg.size() < 2 || msg.substr(0,2) != "WS"){
Terminate = true; Terminate = true;
TCPTerminate = true; TCPTerminate = true;
@ -130,7 +163,11 @@ std::string HandShake(SOCKET Sock){
} }
void SyncResources(SOCKET Sock){ void SyncResources(SOCKET Sock){
std::string Ret = HandShake(Sock); RSA*LKey = GenKey();
auto* S = new Hold;
std::string Ret = HandShake(Sock,S,LKey);
delete LKey;
delete S;
if(Ret.empty())return; if(Ret.empty())return;
info(Sec("Checking Resources...")); info(Sec("Checking Resources..."));

View File

@ -6,6 +6,7 @@
#include "Security/Enc.h" #include "Security/Enc.h"
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include "Logger.h" #include "Logger.h"
#include <sstream>
#include <thread> #include <thread>
#include <string> #include <string>
#include <array> #include <array>
@ -25,24 +26,36 @@ struct SplitData{
}; };
std::set<SplitData*> SplitPackets; std::set<SplitData*> SplitPackets;
std::set<PacketData*> BigDataAcks; std::set<PacketData*> BigDataAcks;
int FC(const std::string& s,const std::string& p,int n) {
auto i = s.find(p);
int j;
for (j = 1; j < n && i != std::string::npos; ++j){
i = s.find(p, i+1);
}
if (j == n)return int(i);
else return -1;
}
void ClearAll(){ void ClearAll(){
for(SplitData*S : SplitPackets){ __try{
if(S != nullptr){ for (SplitData*S : SplitPackets){
delete S; if (S != nullptr) {
S = nullptr; delete S;
S = nullptr;
}
} }
} for (PacketData*S : BigDataAcks){
for(PacketData*S : BigDataAcks){ if (S != nullptr) {
if(S != nullptr){ delete S;
delete S; S = nullptr;
S = nullptr; }
} }
} }__except(1){}
SplitPackets.clear(); SplitPackets.clear();
BigDataAcks.clear(); BigDataAcks.clear();
} }
void UDPSend(std::string Data){ void UDPSend(std::string Data){
if(ClientID == -1 || UDPSock == -1)return; if(ClientID == -1 || UDPSock == -1)return;
Data = Data.substr(0,Data.find(char(0)));
if(Data.length() > 400){ if(Data.length() > 400){
std::string CMP(Comp(Data)); std::string CMP(Comp(Data));
Data = "ABG:" + CMP; Data = "ABG:" + CMP;
@ -55,7 +68,7 @@ void UDPSend(std::string Data){
void LOOP(){ void LOOP(){
while(UDPSock != -1) { while(UDPSock != -1) {
for (PacketData* p : BigDataAcks) { for (PacketData* p : BigDataAcks) {
if(p != nullptr && p->Tries < 20){ if(p != nullptr && p->Tries < 15){
p->Tries++; p->Tries++;
UDPSend(p->Data); UDPSend(p->Data);
}else{ }else{
@ -67,14 +80,14 @@ void LOOP(){
break; break;
} }
} }
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(300));
} }
} }
void AckID(int ID){ void AckID(int ID){
for(PacketData* p : BigDataAcks){ for(PacketData* p : BigDataAcks){
if(p != nullptr && p->ID == ID){ if(p != nullptr && p->ID == ID){
p->Tries = 25; p->Tries = 100;
break; break;
} }
} }
@ -91,7 +104,8 @@ int SplitID(){
else SID++; else SID++;
return SID; return SID;
} }
void SendLarge(const std::string&Data){ void SendLarge(std::string Data){
Data = Data.substr(0,Data.find(char(0)));
int ID = PackID(); int ID = PackID();
std::string Packet; std::string Packet;
if(Data.length() > 1000){ if(Data.length() > 1000){
@ -99,16 +113,16 @@ void SendLarge(const std::string&Data){
int S = 1,Split = int(ceil(float(pckt.length()) / 1000)); int S = 1,Split = int(ceil(float(pckt.length()) / 1000));
int SID = SplitID(); int SID = SplitID();
while(pckt.length() > 1000){ while(pckt.length() > 1000){
Packet = "SC"+std::to_string(S)+"/"+std::to_string(Split)+":"+std::to_string(ID)+"|"+ Packet = "SC|"+std::to_string(S)+"|"+std::to_string(Split)+"|"+std::to_string(ID)+"|"+
std::to_string(SID)+":"+pckt.substr(0,1000); std::to_string(SID)+"|"+pckt.substr(0,1000);
BigDataAcks.insert(new PacketData{ID,Packet,1}); BigDataAcks.insert(new PacketData{ID,Packet,1});
UDPSend(Packet); UDPSend(Packet);
pckt = pckt.substr(1000); pckt = pckt.substr(1000);
S++; S++;
ID = PackID(); ID = PackID();
} }
Packet = "SC"+std::to_string(S)+"/"+std::to_string(Split)+":"+ Packet = "SC|"+std::to_string(S)+"|"+std::to_string(Split)+"|"+
std::to_string(ID)+"|"+std::to_string(SID)+":"+pckt; std::to_string(ID)+"|"+std::to_string(SID)+"|"+pckt;
BigDataAcks.insert(new PacketData{ID,Packet,1}); BigDataAcks.insert(new PacketData{ID,Packet,1});
UDPSend(Packet); UDPSend(Packet);
}else{ }else{
@ -117,20 +131,20 @@ void SendLarge(const std::string&Data){
UDPSend(Packet); UDPSend(Packet);
} }
} }
std::array<int, 50> HandledIDs; std::array<int, 100> HandledIDs = {-1};
int APos = 0;
void IDReset(){ void IDReset(){
for(int C = 0;C < 50;C++){ for(int C = 0;C < 100;C++){
HandledIDs.at(C) = -1; HandledIDs.at(C) = -1;
} }
} }
bool Handled(int ID){ bool Handled(int ID){
static int Pos = 0;
for(int id : HandledIDs){ for(int id : HandledIDs){
if(id == ID)return true; if(id == ID)return true;
} }
if(Pos > 49)Pos = 0; if(APos > 99)APos = 0;
HandledIDs.at(Pos) = ID; HandledIDs.at(APos) = ID;
Pos++; APos++;
return false; return false;
} }
SplitData*GetSplit(int SplitID){ SplitData*GetSplit(int SplitID){
@ -142,22 +156,29 @@ SplitData*GetSplit(int SplitID){
return a; return a;
} }
void ServerParser(const std::string& Data);
void HandleChunk(const std::string&Data){ void HandleChunk(const std::string&Data){
int pos1 = int(Data.find(':'))+1,pos2 = int(Data.find(':',pos1)),pos3 = int(Data.find('/')); int pos = FC(Data,"|",5);
int pos4 = int(Data.find('|')); if(pos == -1)return;
int Max = stoi(Data.substr(pos3+1,pos1-pos3-2)); std::stringstream ss(Data.substr(0,pos++));
int Current = stoi(Data.substr(2,pos3-2)); std::string t;
int ID = stoi(Data.substr(pos1,pos4-pos1)); int I = -1;
int SplitID = stoi(Data.substr(pos4+1,pos2-pos4-1)); //Current Max ID SID
std::string ack = "TRG:" + Data.substr(pos1,pos4-pos1); std::vector<int> Num(4,0);
while (std::getline(ss, t, '|')) {
if(I != -1)Num.at(I) = std::stoi(t);
I++;
}
std::string ack = "TRG:" + std::to_string(Num.at(2));
UDPSend(ack); UDPSend(ack);
if(Handled(ID))return; if(Handled(Num.at(2))){
warn("Handeling Packet ID : " + std::to_string(ID)); return;
SplitData* SData = GetSplit(SplitID); }
SData->Total = Max; std::string Packet = Data.substr(pos);
SData->ID = SplitID; SplitData* SData = GetSplit(Num.at(3));
SData->Fragments.insert(std::make_pair(Current,Data.substr(pos2+1))); SData->Total = Num.at(1);
SData->ID = Num.at(3);
SData->Fragments.insert(std::make_pair(Num.at(0),Packet));
if(SData->Fragments.size() == SData->Total){ if(SData->Fragments.size() == SData->Total){
std::string ToHandle; std::string ToHandle;
for(const std::pair<int,std::string>& a : SData->Fragments){ for(const std::pair<int,std::string>& a : SData->Fragments){
@ -173,9 +194,9 @@ void UDPParser(std::string Packet){
if(Packet.substr(0,4) == "ABG:"){ if(Packet.substr(0,4) == "ABG:"){
Packet = DeComp(Packet.substr(4)); Packet = DeComp(Packet.substr(4));
} }
Packet = Packet.substr(0,Packet.find(char(0)));
if(Packet.substr(0,4) == "TRG:"){ if(Packet.substr(0,4) == "TRG:"){
AckID(stoi(Packet.substr(4))); AckID(stoi(Packet.substr(4)));
debug(Sec("Got Ack for data"));
return; return;
}else if(Packet.substr(0,3) == "BD:"){ }else if(Packet.substr(0,3) == "BD:"){
auto pos = Packet.find(':',4); auto pos = Packet.find(':',4);
@ -194,17 +215,14 @@ void UDPParser(std::string Packet){
ServerParser(Packet); ServerParser(Packet);
} }
void UDPRcv(){ void UDPRcv(){
char buf[10240];
int len = 10240;
sockaddr_in FromServer{}; sockaddr_in FromServer{};
int clientLength = sizeof(FromServer); int clientLength = sizeof(FromServer);
ZeroMemory(&FromServer, clientLength); ZeroMemory(&FromServer, clientLength);
ZeroMemory(buf, len); std::string Ret(10240,0);
if(UDPSock == -1)return; if(UDPSock == -1)return;
int Rcv = recvfrom(UDPSock, buf, len, 0, (sockaddr*)&FromServer, &clientLength); int Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&FromServer, &clientLength);
if (Rcv == SOCKET_ERROR)return; if (Rcv == SOCKET_ERROR)return;
std::string Ret(Rcv,0); Ret.resize(Rcv);
memcpy_s(&Ret[0],Rcv,buf,Rcv);
UDPParser(Ret); UDPParser(Ret);
} }
void UDPClientMain(const std::string& IP,int Port){ void UDPClientMain(const std::string& IP,int Port){

View File

@ -15,22 +15,20 @@ void TCPSend(const std::string&Data){
Terminate = true; Terminate = true;
return; return;
} }
int BytesSent = send(TCPSock, Data.c_str(), int(Data.length())+1, 0); std::string Send = "\n" + Data.substr(0,Data.find(char(0))) + "\n";
if (BytesSent == 0){ size_t Sent = send(TCPSock, Send.c_str(), int(Send.size()), 0);
if (Sent == 0){
debug(Sec("(TCP) Connection closing...")); debug(Sec("(TCP) Connection closing..."));
Terminate = true; Terminate = true;
return; return;
} }
else if (BytesSent < 0) { else if (Sent < 0) {
debug(Sec("(TCP) send failed with error: ") + std::to_string(WSAGetLastError())); debug(Sec("(TCP) send failed with error: ") + std::to_string(WSAGetLastError()));
closesocket(TCPSock); closesocket(TCPSock);
Terminate = true; Terminate = true;
return; return;
} }
} }
void ServerParser(const std::string& Data);
void TCPRcv(){ void TCPRcv(){
char buf[4096]; char buf[4096];
int len = 4096; int len = 4096;
@ -51,7 +49,7 @@ void TCPRcv(){
Terminate = true; Terminate = true;
return; return;
} }
ServerParser(std::string(buf)); Handler.Handle(std::string(buf));
} }
void SyncResources(SOCKET TCPSock); void SyncResources(SOCKET TCPSock);

View File

@ -219,7 +219,7 @@ std::string GetManifest(const std::string& Man){
} }
bool IDCheck(std::string Man, std::string steam){ bool IDCheck(std::string Man, std::string steam){
bool a = false,b = true; bool a = false,b = true;
int pos = int(Man.find(Sec("steamapps"))); int pos = int(Man.rfind(Sec("steamapps")));
if(pos == -1)Exit(5); if(pos == -1)Exit(5);
Man = Man.substr(0,pos+9) + Sec("/appmanifest_284160.acf"); Man = Man.substr(0,pos+9) + Sec("/appmanifest_284160.acf");
steam += Sec("/config/loginusers.vdf"); steam += Sec("/config/loginusers.vdf");

View File

@ -10,7 +10,7 @@
#include <fstream> #include <fstream>
#include <Psapi.h> #include <Psapi.h>
void DAS(){ void DAS(){
int i = 0; /*int i = 0;
std::ifstream f(GetEN(), std::ios::binary); std::ifstream f(GetEN(), std::ios::binary);
f.seekg(0, std::ios_base::end); f.seekg(0, std::ios_base::end);
std::streampos fileSize = f.tellg(); std::streampos fileSize = f.tellg();
@ -19,10 +19,10 @@ void DAS(){
DAboard(); DAboard();
} }
if(i)DAboard(); if(i)DAboard();
f.close(); f.close();*/
} }
void DASM(){ //A mirror to have 2 independent checks void DASM(){ //A mirror to have 2 independent checks
int i = 0; /*int i = 0;
std::ifstream f(GetEN(), std::ios::binary); std::ifstream f(GetEN(), std::ios::binary);
f.seekg(0, std::ios_base::end); f.seekg(0, std::ios_base::end);
std::streampos fileSize = f.tellg(); std::streampos fileSize = f.tellg();
@ -31,7 +31,7 @@ void DASM(){ //A mirror to have 2 independent checks
DAboard(); DAboard();
} }
if(i)DAboard(); if(i)DAboard();
f.close(); f.close();*/
} }
DWORD getParentPID(DWORD pid){ DWORD getParentPID(DWORD pid){
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
@ -58,6 +58,7 @@ HANDLE getProcess(DWORD pid, LPSTR fname, DWORD sz) {
} }
return nullptr; return nullptr;
} }
void UnderSimulation(char* argv[]){ void UnderSimulation(char* argv[]){
DWORD ppid; DWORD ppid;
std::string Parent(MAX_PATH,0); std::string Parent(MAX_PATH,0);
@ -68,23 +69,22 @@ void UnderSimulation(char* argv[]){
error(Code+std::to_string(2)); error(Code+std::to_string(2));
exit(1); exit(1);
} }
auto P = Parent.find(Sec(".exe")); auto P = Parent.find(Sec(".exe"));
if(P != std::string::npos)Parent.resize(P + 4); if(P != std::string::npos)Parent.resize(P + 4);
else{ else return;
error(Code+std::to_string(3));
exit(1);
}
std::string S1 = Sec("\\Windows\\explorer.exe"); std::string S1 = Sec("\\Windows\\explorer.exe");
std::string S2 = Sec("JetBrains\\CLion"); std::string S2 = Sec("JetBrains\\CLion");
std::string S3 = Sec("\\Windows\\System32\\cmd.exe"); std::string S3 = Sec("\\Windows\\System32\\cmd.exe");
std::string S4 = Sec("steam.exe");
if(Parent == std::string(argv[0]))return; if(Parent == std::string(argv[0]))return;
if(Parent.find(S1) == 2)return; if(Parent.find(S1) == 2)return;
if(Parent.find(S2) != std::string::npos)return; if(Parent.find(S2) != std::string::npos)return;
if(Parent.find(S3) == 2)return; if(Parent.find(S3) == 2)return;
TerminateProcess(Process, 1); if(Parent.find(S3) != -1)return;
error(Code + std::to_string(4)); //TerminateProcess(Process, 1);
exit(1); //error(Code + std::to_string(4));
//exit(1); //TODO look into that later
} }
void SecurityCheck(char* argv[]){ void SecurityCheck(char* argv[]){
UnderSimulation(argv); UnderSimulation(argv);

View File

@ -1,6 +1,7 @@
#include "Security/Enc.h" #include "Security/Enc.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <random>
int LocalKeys[] = {7406809,6967,4810803}; //n e d int LocalKeys[] = {7406809,6967,4810803}; //n e d
@ -13,6 +14,64 @@ int log_power(int n,unsigned int p, int mod){
return result; return result;
} }
int Rand(){
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(1, 5000);
return uniform_dist(e1);
}
bool rabin_miller(int n){
bool ok = true;
for (int i = 1; i <= 5 && ok; i++) {
int a = Rand() + 1;
int result = log_power(a, n - 1, n);
ok &= (result == 1);
}
return ok;
}
int generate_prime(){
int generated = Rand();
while (!rabin_miller(generated))generated = Rand();
return generated;
}
int gcd(int a, int b){
while (b){
int r = a % b;
a = b;
b = r;
}
return a;
}
int generate_coprime(int n){
int generated = Rand();
while (gcd(n, generated) != 1)generated = Rand();
return generated;
}
std::pair<int, int> euclid_extended(int a, int b) {
if(!b)return {1, 0};
auto result = euclid_extended(b, a % b);
return {result.second, result.first - (a / b) * result.second};
}
int modular_inverse(int n, int mod){
int inverse = euclid_extended(n, mod).first;
while(inverse < 0)inverse += mod;
return inverse;
}
RSA* GenKey(){
int p, q;
p = generate_prime();
q = generate_prime();
int n = p * q;
int phi = (p -1) * (q - 1);
int e = generate_coprime(phi);
int d = modular_inverse(e, phi);
return new RSA{n,e,d};
}
int Enc(int value,int e,int n){ int Enc(int value,int e,int n){
return log_power(value, e, n); return log_power(value, e, n);
} }
@ -37,35 +96,8 @@ std::string LocalDec(const std::string& Data){
} }
return ret; return ret;
} }
#include <random>
int Rand(){
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(1, 200);
return uniform_dist(e1);
}
std::string Encrypt(std::string msg){
if(msg.size() < 2)return msg;
int R = (Rand()+Rand())/2,T = R;
for(char&c : msg){
if(R > 30)c = char(int(c) + (R-=3));
else c = char(int(c) - (R+=4));
}
return char(T) + msg;
}
std::string Decrypt(std::string msg){
if(msg.size() < 2)return "";
int R = uint8_t(msg.at(0));
if(R > 200 || R < 1)return "";
msg = msg.substr(1);
for(char&c : msg){
if(R > 30)c = char(int(c) - (R-=3));
else c = char(int(c) + (R+=4));
}
return msg;
}
std::string RSA_E(const std::string& Data,int e, int n){ std::string RSA_E(const std::string& Data,int e, int n){
if(e < 10 || n < 10)return "";
std::stringstream stream; std::stringstream stream;
for(const char&c : Data){ for(const char&c : Data){
stream << std::hex << Enc(uint8_t(c),e,n) << "g"; stream << std::hex << Enc(uint8_t(c),e,n) << "g";

View File

@ -58,7 +58,7 @@ void ContinuousCheck(fs::file_time_type last){
} }
}else i = 0; }else i = 0;
CheckDirs(); CheckDirs();
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(800));
} }
exit(0); exit(0);
} }

View File

@ -11,16 +11,20 @@
#include "Logger.h" #include "Logger.h"
#include <urlmon.h> #include <urlmon.h>
#include <thread> #include <thread>
bool Dev = false; bool Dev = false;
namespace fs = std::experimental::filesystem; namespace fs = std::experimental::filesystem;
std::string GetEN(){ std::string GetEN(){
return Sec("BeamMP-Launcher.exe"); static std::string r = Sec("BeamMP-Launcher.exe");
return r;
} }
std::string GetVer(){ std::string GetVer(){
return Sec("1.60"); static std::string r = Sec("1.63");
return r;
} }
std::string GetPatch(){ std::string GetPatch(){
return Sec(""); static std::string r = Sec(".5");
return r;
} }
void ReLaunch(int argc,char*args[]){ void ReLaunch(int argc,char*args[]){
std::string Arg; std::string Arg;
@ -57,25 +61,40 @@ void CheckName(int argc,char* args[]){
} }
void RequestRole(){ void RequestRole(){
auto NPos = std::string::npos; auto NPos = std::string::npos;
std::string HTTP_Result=HTTP_REQUEST(Sec("https://beammp.com/entitlement?did=")+GetDID()+Sec("&t=l"),443); std::string HTTP_Result = HTTP_REQUEST(Sec("https://beammp.com/entitlement?did=")+GetDID()+Sec("&t=l"),443);
if(HTTP_Result == "-1"){ if(HTTP_Result == "-1"){
error(Sec("Sorry Backend System Outage! Don't worry it will back on soon!")); HTTP_Result = HTTP_REQUEST(Sec("https://backup1.beammp.com/entitlement?did=")+GetDID()+Sec("&t=l"),443);
std::this_thread::sleep_for(std::chrono::seconds(3)); if(HTTP_Result == "-1") {
exit(-1); error(Sec("Sorry Backend System Outage! Don't worry it will back on soon!"));
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(-1);
}
}
if(HTTP_Result.find(Sec("\"MDEV\"")) != NPos || HTTP_Result.find(Sec("\"CON\"")) != NPos){
if(GetDID() != "125792589621231616"){
Dev = true;
}
} }
if(HTTP_Result.find(Sec("\"MDEV\"")) != NPos)Dev = true;
info(Sec("Client Connected!")); info(Sec("Client Connected!"));
} }
void CheckForUpdates(int argc,char*args[],const std::string& CV){ void CheckForUpdates(int argc,char*args[],const std::string& CV){
std::string link = Sec("https://beammp.com/builds/launcher?version=true"); std::string link = Sec("https://beammp.com/builds/launcher?version=true");
std::string HTTP = HTTP_REQUEST(link,443); std::string HTTP = HTTP_REQUEST(link,443);
bool fallback = false;
if(HTTP.find_first_of("0123456789") == std::string::npos){ if(HTTP.find_first_of("0123456789") == std::string::npos){
error(Sec("Primary Servers Offline! sorry for the inconvenience!")); link = Sec("https://backup1.beammp.com/builds/launcher?version=true");
std::this_thread::sleep_for(std::chrono::seconds(4)); HTTP = HTTP_REQUEST(link,443);
exit(-1); fallback = true;
if(HTTP.find_first_of("0123456789") == std::string::npos) {
error(Sec("Primary Servers Offline! sorry for the inconvenience!"));
std::this_thread::sleep_for(std::chrono::seconds(4));
exit(-1);
}
} }
link = Sec("https://beammp.com/builds/launcher?download=true"); if(fallback){
link = Sec("https://backup1.beammp.com/builds/launcher?download=true");
}else link = Sec("https://beammp.com/builds/launcher?download=true");
struct stat buffer{}; struct stat buffer{};
std::string Back = Sec("BeamMP-Launcher.back"); std::string Back = Sec("BeamMP-Launcher.back");
@ -169,27 +188,35 @@ void PreGame(int argc, char* argv[],const std::string& GamePath){
std::string DUI = Sec(R"(BeamNG\settings\uiapps-layouts.json)"); std::string DUI = Sec(R"(BeamNG\settings\uiapps-layouts.json)");
std::string GS = Sec(R"(BeamNG\settings\game-settings.ini)"); std::string GS = Sec(R"(BeamNG\settings\game-settings.ini)");
std::string link = Sec("https://beammp.com/client-ui-data"); std::string link = Sec("https://beammp.com/client-ui-data");
bool fallback = false;
int i; int i;
if(!fs::exists(DUI)){ if(!fs::exists(DUI)){
info(Sec("Downloading default ui data...")); info(Sec("Downloading default ui data..."));
i = Download(link,DUI,true); i = Download(link,DUI,true);
if(i != -1){ if(i != -1){
fallback = true;
remove(DUI.c_str()); remove(DUI.c_str());
error(Sec("Failed to download code : ") + std::to_string(i)); link = Sec("https://backup1.beammp.com/client-ui-data");
std::this_thread::sleep_for(std::chrono::seconds(3)); i = Download(link,DUI,true);
ReLaunch(argc,argv); if(i != -1) {
error(Sec("Failed to download code : ") + std::to_string(i));
std::this_thread::sleep_for(std::chrono::seconds(3));
ReLaunch(argc, argv);
}
} }
info(Sec("Download Complete!")); info(Sec("Download Complete!"));
} }
if(!fs::exists(GS)) { if(!fs::exists(GS)) {
info(Sec("Downloading default game settings...")); info(Sec("Downloading default game settings..."));
link = Sec("https://beammp.com/client-settings-data"); if(fallback)link = Sec("https://backup1.beammp.com/client-settings-data");
else link = Sec("https://beammp.com/client-settings-data");
Download(link, GS,true); Download(link, GS,true);
info(Sec("Download Complete!")); info(Sec("Download Complete!"));
} }
if(!Dev) { if(!Dev) {
info(Sec("Downloading mod...")); info(Sec("Downloading mod..."));
link = Sec("https://beammp.com/builds/client?did=") + GetDID(); if(fallback)link = Sec("https://backup1.beammp.com/builds/client?did=") + GetDID();
else link = Sec("https://beammp.com/builds/client?did=") + GetDID();
Download(link, Sec(R"(BeamNG\mods\BeamMP.zip)"), false); Download(link, Sec(R"(BeamNG\mods\BeamMP.zip)"), false);
info(Sec("Download Complete!")); info(Sec("Download Complete!"));
} }