2015-04-02 12:54:57 -04:00
|
|
|
/*
|
2014-10-28 18:33:19 +00:00
|
|
|
* Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org>
|
2018-01-01 00:40:17 +01:00
|
|
|
* Copyright (C) 2008-2018 TrinityCore <https://www.trinitycore.org/>
|
2014-10-28 18:33:19 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Helper.hpp"
|
|
|
|
|
#include "Patcher.hpp"
|
2014-11-06 18:01:46 +01:00
|
|
|
#include "Patches/Common.hpp"
|
2014-10-28 18:33:19 +00:00
|
|
|
#include "Patches/Mac.hpp"
|
|
|
|
|
#include "Patches/Windows.hpp"
|
2014-11-06 18:01:46 +01:00
|
|
|
#include "Patterns/Common.hpp"
|
2014-10-28 18:33:19 +00:00
|
|
|
#include "Patterns/Mac.hpp"
|
|
|
|
|
#include "Patterns/Windows.hpp"
|
|
|
|
|
|
2016-07-17 17:24:53 +02:00
|
|
|
#include "Banner.h"
|
2017-12-29 12:50:12 +01:00
|
|
|
#include "BigNumber.h"
|
|
|
|
|
#include "RSA.h"
|
|
|
|
|
#include "SHA256.h"
|
2014-10-28 18:33:19 +00:00
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2015-06-30 16:05:50 +02:00
|
|
|
#include <boost/program_options.hpp>
|
2014-10-28 18:33:19 +00:00
|
|
|
|
2017-03-04 18:44:13 +01:00
|
|
|
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
2014-10-28 18:33:19 +00:00
|
|
|
#include <Shlobj.h>
|
2017-03-04 18:44:13 +01:00
|
|
|
#elif TRINITY_PLATFORM == TRINITY_PLATFORM_UNIX
|
2015-08-20 07:23:47 +02:00
|
|
|
#include <pwd.h>
|
2014-10-28 18:33:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
namespace po = boost::program_options;
|
2014-12-20 22:58:21 +00:00
|
|
|
|
2014-10-28 18:33:19 +00:00
|
|
|
namespace Connection_Patcher
|
|
|
|
|
{
|
2015-06-30 16:05:50 +02:00
|
|
|
po::variables_map GetConsoleArguments(int argc, char** argv);
|
2014-12-21 14:02:03 +00:00
|
|
|
|
2014-10-28 18:33:19 +00:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
template<typename PATCH, typename PATTERN>
|
2016-03-23 18:51:50 +01:00
|
|
|
void do_patches(Patcher* patcher, boost::filesystem::path output, uint32_t buildNumber)
|
2014-10-28 18:33:19 +00:00
|
|
|
{
|
2014-11-06 18:01:46 +01:00
|
|
|
std::cout << "patching Portal\n";
|
2016-03-27 23:57:41 +02:00
|
|
|
// '.actual.battle.net' -> '' to allow for set portal 'host'
|
2014-11-06 18:01:46 +01:00
|
|
|
patcher->Patch(Patches::Common::Portal(), Patterns::Common::Portal());
|
2014-11-08 17:16:37 +01:00
|
|
|
|
2014-11-06 18:01:46 +01:00
|
|
|
std::cout << "patching redirect RSA Modulus\n";
|
2014-11-08 17:16:37 +01:00
|
|
|
// public component of connection signing key to use known key pair
|
2014-11-06 18:01:46 +01:00
|
|
|
patcher->Patch(Patches::Common::Modulus(), Patterns::Common::Modulus());
|
2014-11-08 17:16:37 +01:00
|
|
|
|
2016-03-27 23:57:41 +02:00
|
|
|
std::cout << "patching BNet certificate file location\n";
|
2017-12-29 12:50:12 +01:00
|
|
|
// replace certificate bundle url
|
|
|
|
|
patcher->Patch(Patches::Common::CertBundleUrl(), Patterns::Common::CertBundleUrl());
|
2016-03-27 23:57:41 +02:00
|
|
|
|
2017-12-29 12:50:12 +01:00
|
|
|
std::cout << "patching BNet certificate file signature\n";
|
|
|
|
|
Trinity::Crypto::RSA rsa;
|
|
|
|
|
rsa.LoadFromString(Patches::Common::CertificatePrivateKey(), Trinity::Crypto::RSA::PrivateKey{});
|
|
|
|
|
std::unique_ptr<uint8[]> modulusArray = rsa.GetModulus().AsByteArray(256);
|
|
|
|
|
patcher->Patch(std::vector<uint8>(modulusArray.get(), modulusArray.get() + 256), Patterns::Common::CertSignatureModulus());
|
2014-11-08 17:16:37 +01:00
|
|
|
|
2016-03-23 18:51:50 +01:00
|
|
|
std::cout << "patching Versions\n";
|
|
|
|
|
// sever the connection to blizzard's versions file to stop it from updating and replace with custom version
|
|
|
|
|
// this is good practice with or without the retail version, just to stop the exe from auto-patching randomly
|
|
|
|
|
// hardcode %s.patch.battle.net:1119/%s/versions to trinity6.github.io/%s/%s/build/versi
|
|
|
|
|
std::string verPatch(Patches::Common::VersionsFile());
|
|
|
|
|
std::string buildPattern = "build";
|
|
|
|
|
|
|
|
|
|
boost::algorithm::replace_all(verPatch, buildPattern, std::to_string(buildNumber));
|
|
|
|
|
std::vector<unsigned char> verVec(verPatch.begin(), verPatch.end());
|
|
|
|
|
patcher->Patch(verVec, Patterns::Common::VersionsFile());
|
2015-06-30 16:05:50 +02:00
|
|
|
|
2017-10-03 18:03:57 +02:00
|
|
|
std::cout << "patching launcher login parameters location\n";
|
|
|
|
|
// change registry/CFPreferences path
|
2017-10-08 11:15:53 +02:00
|
|
|
patcher->Patch(PATCH::LauncherLoginParametersLocation(), PATTERN::LauncherLoginParametersLocation());
|
2017-10-03 18:03:57 +02:00
|
|
|
|
2014-10-28 18:33:19 +00:00
|
|
|
patcher->Finish(output);
|
|
|
|
|
|
|
|
|
|
std::cout << "Patching done.\n";
|
|
|
|
|
}
|
2016-03-27 23:57:41 +02:00
|
|
|
|
|
|
|
|
void WriteCertificateBundle(boost::filesystem::path const& dest)
|
|
|
|
|
{
|
2017-12-29 12:50:12 +01:00
|
|
|
if (!boost::filesystem::exists(dest.parent_path()) &&
|
|
|
|
|
!boost::filesystem::create_directories(dest.parent_path()))
|
|
|
|
|
throw std::runtime_error("could not create " + dest.parent_path().string());
|
|
|
|
|
|
2016-03-27 23:57:41 +02:00
|
|
|
std::ofstream ofs(dest.string(), std::ofstream::binary);
|
|
|
|
|
if (!ofs)
|
|
|
|
|
throw std::runtime_error("could not open " + dest.string());
|
|
|
|
|
|
2017-12-29 12:50:12 +01:00
|
|
|
ofs << std::noskipws << Patches::Common::CertificateBundle() << "NGIS";
|
|
|
|
|
|
|
|
|
|
SHA256Hash signatureHash;
|
|
|
|
|
signatureHash.UpdateData(Patches::Common::CertificateBundle());
|
|
|
|
|
signatureHash.UpdateData("Blizzard Certificate Bundle");
|
|
|
|
|
signatureHash.Finalize();
|
|
|
|
|
std::array<uint8, 256> signature;
|
|
|
|
|
|
|
|
|
|
Trinity::Crypto::RSA rsa;
|
|
|
|
|
rsa.LoadFromString(Patches::Common::CertificatePrivateKey(), Trinity::Crypto::RSA::PrivateKey{});
|
|
|
|
|
rsa.Sign(signatureHash.GetDigest(), signatureHash.GetLength(), signature.data(), Trinity::Crypto::RSA::SHA256{});
|
|
|
|
|
|
|
|
|
|
ofs.write(reinterpret_cast<char const*>(signature.data()), signature.size());
|
2016-03-27 23:57:41 +02:00
|
|
|
}
|
2014-10-28 18:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
po::variables_map GetConsoleArguments(int argc, char** argv)
|
2014-10-28 18:33:19 +00:00
|
|
|
{
|
2015-06-30 16:05:50 +02:00
|
|
|
po::options_description all("Allowed options");
|
|
|
|
|
all.add_options()
|
|
|
|
|
("help,h", "print usage message")
|
|
|
|
|
("path", po::value<std::string>()->required(), "Path to the Wow.exe")
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
po::positional_options_description pos;
|
|
|
|
|
pos.add("path", 1);
|
|
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
|
try
|
2014-10-28 18:33:19 +00:00
|
|
|
{
|
2015-06-30 16:05:50 +02:00
|
|
|
po::store(po::command_line_parser(argc, argv).options(all).positional(pos).run(), vm);
|
|
|
|
|
po::notify(vm);
|
2014-10-28 18:33:19 +00:00
|
|
|
}
|
2015-06-30 16:05:50 +02:00
|
|
|
catch (std::exception& e)
|
2014-12-20 22:58:21 +00:00
|
|
|
{
|
2015-06-30 16:05:50 +02:00
|
|
|
std::cerr << e.what() << "\n";
|
2014-12-20 22:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
if (vm.count("help"))
|
|
|
|
|
std::cout << all << "\n";
|
2014-12-20 22:58:21 +00:00
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
if (!vm.count("path"))
|
|
|
|
|
throw std::invalid_argument("Wrong number of arguments: Missing client file.");
|
2014-12-20 22:58:21 +00:00
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
return vm;
|
2014-12-20 22:58:21 +00:00
|
|
|
}
|
2014-10-28 18:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
using namespace Connection_Patcher;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-17 17:24:53 +02:00
|
|
|
Trinity::Banner::Show("connection_patcher", [](char const* text) { std::cout << text << std::endl; }, nullptr);
|
|
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
auto vm = GetConsoleArguments(argc, argv);
|
2014-10-28 18:33:19 +00:00
|
|
|
|
2015-06-30 16:05:50 +02:00
|
|
|
// exit if help is enabled
|
|
|
|
|
if (vm.count("help"))
|
|
|
|
|
{
|
|
|
|
|
std::cin.get();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string const binary_path(std::move(vm["path"].as<std::string>()));
|
2014-10-28 18:33:19 +00:00
|
|
|
std::string renamed_binary_path(binary_path);
|
2017-12-29 12:50:12 +01:00
|
|
|
std::wstring appDataPath;
|
2014-10-28 18:33:19 +00:00
|
|
|
|
2017-12-29 12:50:12 +01:00
|
|
|
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
|
|
|
|
wchar_t* tempPath(nullptr);
|
|
|
|
|
SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &tempPath);
|
|
|
|
|
appDataPath = std::wstring(tempPath);
|
|
|
|
|
CoTaskMemFree(tempPath);
|
|
|
|
|
#elif TRINITY_PLATFORM == TRINITY_PLATFORM_UNIX
|
|
|
|
|
char* tempPath(nullptr);
|
|
|
|
|
if ((tempPath = getenv("HOME")) == nullptr)
|
|
|
|
|
tempPath = getpwuid(getuid())->pw_dir;
|
|
|
|
|
std::string tempPathStr(tempPath);
|
|
|
|
|
appDataPath.assign(tempPathStr.begin(), tempPathStr.end());
|
|
|
|
|
appDataPath += std::wstring(L"/.wine/drive_c/users/Public/Application Data");
|
|
|
|
|
#elif TRINITY_PLATFORM == TRINITY_PLATFORM_APPLE
|
|
|
|
|
appDataPath = L"/Users/Shared";
|
|
|
|
|
#endif
|
2015-06-30 16:05:50 +02:00
|
|
|
std::cout << "Creating patched binary..." << std::endl;
|
2014-10-28 18:33:19 +00:00
|
|
|
|
|
|
|
|
Patcher patcher(binary_path);
|
|
|
|
|
|
2016-03-23 18:51:50 +01:00
|
|
|
// always set wowBuild to current build of the .exe files
|
|
|
|
|
int wowBuild = Helper::GetBuildNumber(patcher.GetBinary());
|
2015-06-30 16:05:50 +02:00
|
|
|
|
2016-03-23 18:51:50 +01:00
|
|
|
// define logical limits in case the exe was tinkered with and the build number was changed
|
|
|
|
|
if (wowBuild == 0 || wowBuild < 10000 || wowBuild > 65535) // Build number has to be exactly 5 characters long
|
|
|
|
|
throw std::runtime_error("Build number was out of range. Build: " + std::to_string(wowBuild));
|
2015-06-30 16:05:50 +02:00
|
|
|
|
2016-03-23 18:51:50 +01:00
|
|
|
std::cout << "Determined build number: " << std::to_string(wowBuild) << std::endl;
|
2015-06-30 16:05:50 +02:00
|
|
|
|
|
|
|
|
switch (patcher.GetType())
|
2014-10-28 18:33:19 +00:00
|
|
|
{
|
|
|
|
|
case Constants::BinaryTypes::Pe32:
|
|
|
|
|
case Constants::BinaryTypes::Pe64:
|
2017-12-29 12:50:12 +01:00
|
|
|
std::cout << (patcher.GetType() == Constants::BinaryTypes::Pe64 ? "Win64" : "Win32") << " client...\n";
|
2014-10-28 18:33:19 +00:00
|
|
|
|
|
|
|
|
boost::algorithm::replace_all(renamed_binary_path, ".exe", "_Patched.exe");
|
2017-12-29 12:50:12 +01:00
|
|
|
do_patches<Patches::Windows, Patterns::Windows>
|
2016-03-23 18:51:50 +01:00
|
|
|
(&patcher, renamed_binary_path, wowBuild);
|
2017-12-29 12:50:12 +01:00
|
|
|
WriteCertificateBundle(boost::filesystem::path(appDataPath) / L"Blizzard Entertainment/Battle.net/Cache/web_cert_bundle");
|
2014-10-28 18:33:19 +00:00
|
|
|
break;
|
|
|
|
|
case Constants::BinaryTypes::Mach64:
|
|
|
|
|
std::cout << "Mac client...\n";
|
|
|
|
|
|
2016-03-27 23:57:41 +02:00
|
|
|
boost::algorithm::replace_all(renamed_binary_path, ".app", " Patched.app");
|
2015-06-30 16:05:50 +02:00
|
|
|
Helper::CopyDir(boost::filesystem::path(binary_path).parent_path()/*MacOS*/.parent_path()/*Contents*/.parent_path()
|
2014-10-28 18:33:19 +00:00
|
|
|
, boost::filesystem::path(renamed_binary_path).parent_path()/*MacOS*/.parent_path()/*Contents*/.parent_path()
|
|
|
|
|
);
|
|
|
|
|
|
2017-12-29 12:50:12 +01:00
|
|
|
do_patches<Patches::Mac, Patterns::Mac>
|
2016-03-23 18:51:50 +01:00
|
|
|
(&patcher, renamed_binary_path, wowBuild);
|
2014-10-28 18:33:19 +00:00
|
|
|
|
2014-12-13 12:29:54 +01:00
|
|
|
{
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
fs::permissions(renamed_binary_path, fs::add_perms | fs::others_exe | fs::group_exe | fs::owner_exe);
|
|
|
|
|
}
|
2017-12-29 12:50:12 +01:00
|
|
|
WriteCertificateBundle(boost::filesystem::path(appDataPath) / "Blizzard/Battle.net/Cache/web_cert_bundle");
|
2014-10-28 18:33:19 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2015-06-30 16:05:50 +02:00
|
|
|
throw std::runtime_error("Type: " + std::to_string(static_cast<uint32_t>(patcher.GetType())) + " not supported!");
|
2014-10-28 18:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << "Successfully created your patched binaries.\n";
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "EX: " << ex.what() << std::endl;
|
2014-12-19 21:35:35 +00:00
|
|
|
std::cerr << "An error occurred. Press ENTER to continue...";
|
|
|
|
|
std::cin.get();
|
2014-10-28 18:33:19 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|