diff options
author | bloerwald <bloerwald@googlemail.com> | 2014-10-28 18:33:19 +0000 |
---|---|---|
committer | DDuarte <dnpd.dd@gmail.com> | 2014-10-28 18:33:19 +0000 |
commit | ff0f84e3aa23a32106e88e84c7abe10229200e9e (patch) | |
tree | 7aafb946356dc4f82031badbcbbfbe53eac3e5d0 /src | |
parent | 6b6b6e8f09732ef0306b2cf3c6d5f8953a4fddca (diff) |
Tools/connection_patcher: remove c# code, add c++ code with minor modification, move to tools, integrate into cmake
note: no longer downloads nonexistent modules
note: now throws on not finding patterns
new dependency: boost.filesystem
Closes #13471
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/tools/connection_patcher/CMakeLists.txt | 48 | ||||
-rw-r--r-- | src/tools/connection_patcher/Constants/BinaryTypes.hpp | 37 | ||||
-rw-r--r-- | src/tools/connection_patcher/Helper.cpp | 58 | ||||
-rw-r--r-- | src/tools/connection_patcher/Helper.hpp | 36 | ||||
-rw-r--r-- | src/tools/connection_patcher/Patcher.cpp | 114 | ||||
-rw-r--r-- | src/tools/connection_patcher/Patcher.hpp | 43 | ||||
-rw-r--r-- | src/tools/connection_patcher/Patches/Mac.hpp | 42 | ||||
-rw-r--r-- | src/tools/connection_patcher/Patches/Windows.hpp | 51 | ||||
-rw-r--r-- | src/tools/connection_patcher/Patterns/Mac.hpp | 42 | ||||
-rw-r--r-- | src/tools/connection_patcher/Patterns/Windows.hpp | 51 | ||||
-rw-r--r-- | src/tools/connection_patcher/Program.cpp | 197 |
12 files changed, 720 insertions, 0 deletions
diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index c2ea8a85926..29dba016e87 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -8,6 +8,7 @@ # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +add_subdirectory(connection_patcher) add_subdirectory(map_extractor) add_subdirectory(vmap4_assembler) add_subdirectory(vmap4_extractor) diff --git a/src/tools/connection_patcher/CMakeLists.txt b/src/tools/connection_patcher/CMakeLists.txt new file mode 100644 index 00000000000..8867ec5170b --- /dev/null +++ b/src/tools/connection_patcher/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> +# +# This file is free software; as a special exception the author gives +# unlimited permission to copy and/or distribute it, with or without +# modifications, as long as this notice is preserved. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +include_directories( + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/src/server/shared + ${CMAKE_SOURCE_DIR}/src/server/shared/Cryptography + ${CMAKE_SOURCE_DIR}/src/server/shared/Debugging + ${CMAKE_SOURCE_DIR}/src/server/shared/Utilities + ${OPENSSL_INCLUDE_DIR} +) + +set(HEADER_FILES + Helper.hpp + Patcher.hpp + Constants/BinaryTypes.hpp + Patches/Mac.hpp + Patches/Windows.hpp + Patterns/Mac.hpp + Patterns/Windows.hpp +) + +set(SRC_FILES + Helper.cpp + Patcher.cpp + Program.cpp +) + +add_executable(connection_patcher ${HEADER_FILES} ${SRC_FILES}) + +target_link_libraries(connection_patcher + shared + ${OPENSSL_LIBRARIES} + ${Boost_LIBRARIES} +) + +if (UNIX) + install(TARGETS connection_patcher DESTINATION bin) +elseif (WIN32) + install(TARGETS connection_patcher DESTINATION "${CMAKE_INSTALL_PREFIX}") +endif () diff --git a/src/tools/connection_patcher/Constants/BinaryTypes.hpp b/src/tools/connection_patcher/Constants/BinaryTypes.hpp new file mode 100644 index 00000000000..fc7b34339fa --- /dev/null +++ b/src/tools/connection_patcher/Constants/BinaryTypes.hpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_CONSTANTS_BINARYTYPES_HPP +#define CONNECTION_PATCHER_CONSTANTS_BINARYTYPES_HPP + +#include <cstdint> + +namespace Connection_Patcher +{ + namespace Constants + { + enum class BinaryTypes : uint32_t + { + Pe32 = 0x0000014C, + Pe64 = 0x00008664, + Mach64 = 0xFEEDFACF + }; + } +} + +#endif diff --git a/src/tools/connection_patcher/Helper.cpp b/src/tools/connection_patcher/Helper.cpp new file mode 100644 index 00000000000..75de2452d26 --- /dev/null +++ b/src/tools/connection_patcher/Helper.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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 <SHA256.h> +#include <Util.h> + +#include <stdexcept> + +namespace Connection_Patcher +{ + namespace Helper + { + Constants::BinaryTypes GetBinaryType(std::vector<unsigned char> const& data) + { + // Check MS-DOS magic + if (*reinterpret_cast<uint16_t const*>(data.data()) == 0x5A4D) + { + uint32_t const peOffset(*reinterpret_cast<uint32_t const*>(data.data() + 0x3C)); + + // Check PE magic + if (*reinterpret_cast<uint32_t const*>(data.data() + peOffset) != 0x4550) + throw std::invalid_argument("Not a PE file!"); + + return Constants::BinaryTypes(*reinterpret_cast<uint16_t const*>(data.data() + peOffset + 4)); + } + else + { + return Constants::BinaryTypes(*reinterpret_cast<uint32_t const*>(data.data())); + } + } + + std::string GetFileChecksum(std::vector<unsigned char> const& data) + { + SHA256Hash h; + h.UpdateData(data.data(), data.size()); + h.Finalize(); + + return ByteArrayToHexStr(h.GetDigest(), h.GetLength()); + } + } +} diff --git a/src/tools/connection_patcher/Helper.hpp b/src/tools/connection_patcher/Helper.hpp new file mode 100644 index 00000000000..ed6f91363e6 --- /dev/null +++ b/src/tools/connection_patcher/Helper.hpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_HELPER_HPP +#define CONNECTION_PATCHER_HELPER_HPP + +#include "Constants/BinaryTypes.hpp" + +#include <vector> +#include <string> + +namespace Connection_Patcher +{ + namespace Helper + { + Constants::BinaryTypes GetBinaryType(std::vector<unsigned char> const& data); + std::string GetFileChecksum(std::vector<unsigned char> const& data); + } +} + +#endif diff --git a/src/tools/connection_patcher/Patcher.cpp b/src/tools/connection_patcher/Patcher.cpp new file mode 100644 index 00000000000..65cf1704472 --- /dev/null +++ b/src/tools/connection_patcher/Patcher.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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 + * asize_t with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "Patcher.hpp" +#include "Helper.hpp" + +#include <boost/filesystem.hpp> + +#include <fstream> +#include <iostream> +#include <iterator> +#include <stdexcept> + +namespace +{ + std::vector<unsigned char> read_file(boost::filesystem::path const& path) + { + std::ifstream ifs(path.string(), std::ifstream::binary); + if (!ifs) + throw std::runtime_error("could not open " + path.string()); + + ifs >> std::noskipws; + + return {std::istream_iterator<unsigned char>(ifs), std::istream_iterator<unsigned char>()}; + } + + void write_file(boost::filesystem::path const& path, std::vector<unsigned char> const& data) + { + std::ofstream ofs(path.string(), std::ofstream::binary); + if (!ofs) + throw std::runtime_error("could not open " + path.string()); + + ofs << std::noskipws; + + std::copy(data.begin(), data.end(), std::ostream_iterator<unsigned char>(ofs)); + } + + size_t SearchOffset (std::vector<unsigned char> const& binary, std::vector<unsigned char> const& pattern) + { + for (size_t i = 0; i < binary.size(); i++) + { + size_t matches = 0; + + for (size_t j = 0; j < pattern.size(); j++) + { + if (pattern.size() > (binary.size() - i)) + throw std::runtime_error("unable to find pattern"); + + if (pattern[j] == 0) + { + matches++; + continue; + } + + if (binary[i + j] != pattern[j]) + break; + + matches++; + } + + if (matches == pattern.size()) + return i; + } + + throw std::runtime_error("unable to find pattern"); + } +} + +namespace Connection_Patcher +{ + Patcher::Patcher(boost::filesystem::path file) + : binary(read_file(file)) + , Type(Helper::GetBinaryType(binary)) + {} + + void Patcher::Patch(std::vector<unsigned char> const& bytes, std::vector<unsigned char> const& pattern) + { + if (binary.size() < pattern.size()) + throw std::logic_error("pattern larger than binary"); + + if (pattern.empty()) + return; + + size_t const offset(SearchOffset(binary, pattern)); + std::cout << "Found offset " << offset << std::endl; + + if (offset != 0 && binary.size() >= bytes.size()) + for (size_t i = 0; i < bytes.size(); i++) + binary[offset + i] = bytes[i]; + } + + void Patcher::Finish(boost::filesystem::path out) + { + if (boost::filesystem::exists(out)) + boost::filesystem::remove(out); + + write_file(out, binary); + } +} diff --git a/src/tools/connection_patcher/Patcher.hpp b/src/tools/connection_patcher/Patcher.hpp new file mode 100644 index 00000000000..ecdf21815d1 --- /dev/null +++ b/src/tools/connection_patcher/Patcher.hpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_PATCHER_HPP +#define CONNECTION_PATCHER_PATCHER_HPP + +#include "Constants/BinaryTypes.hpp" + +#include <boost/filesystem.hpp> + +#include <vector> +#include <string> + +namespace Connection_Patcher +{ + struct Patcher + { + std::vector<unsigned char> binary; + Constants::BinaryTypes Type; + + Patcher (boost::filesystem::path file); + + void Patch(std::vector<unsigned char> const& bytes, std::vector<unsigned char> const& pattern); + void Finish (boost::filesystem::path out); + }; +} + +#endif diff --git a/src/tools/connection_patcher/Patches/Mac.hpp b/src/tools/connection_patcher/Patches/Mac.hpp new file mode 100644 index 00000000000..02279730668 --- /dev/null +++ b/src/tools/connection_patcher/Patches/Mac.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_PATCHES_MAC_HPP +#define CONNECTION_PATCHER_PATCHES_MAC_HPP + +#include <vector> + +namespace Connection_Patcher +{ + namespace Patches + { + namespace Mac + { + struct x64 + { + static const std::vector<unsigned char> BNet () { return { 0xB8, 0xD5, 0xF8, 0x7F, 0x82, 0x89, 0x47, 0x0C, 0x5D, 0xC3, 0x90, 0x90, 0x90 }; } + static const std::vector<unsigned char> Portal () { return { }; } + static const std::vector<unsigned char> Connect () { return { }; } + static const std::vector<unsigned char> Password () { return { 0x0F, 0x85 }; } + static const std::vector<unsigned char> Signature() { return { 0x45, 0x31, 0xED, 0x4D, 0x89, 0xFC, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xEB }; } + }; + }; + } +} + +#endif diff --git a/src/tools/connection_patcher/Patches/Windows.hpp b/src/tools/connection_patcher/Patches/Windows.hpp new file mode 100644 index 00000000000..2114f2edae1 --- /dev/null +++ b/src/tools/connection_patcher/Patches/Windows.hpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_PATCHES_WINDOWS_HPP +#define CONNECTION_PATCHER_PATCHES_WINDOWS_HPP + +#include <vector> + +namespace Connection_Patcher +{ + namespace Patches + { + namespace Windows + { + struct x86 + { + static const std::vector<unsigned char> BNet () { return { 0xC7, 0x40, 0x0C, 0xD5, 0xF8, 0x7F, 0x82 }; } + static const std::vector<unsigned char> Portal () { return { }; } + static const std::vector<unsigned char> Connect () { return { 0xEB }; } + static const std::vector<unsigned char> Password () { return { 0x75 }; } + static const std::vector<unsigned char> Signature() { return { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xEB }; } + }; + + struct x64 + { + static const std::vector<unsigned char> BNet () { return { 0xB8, 0xD5, 0xF8, 0x7F, 0x82, 0x89, 0x41, 0x0C, 0x48, 0x8B, 0xC1, 0xC3 }; } + static const std::vector<unsigned char> Portal () { return { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; } + static const std::vector<unsigned char> Connect () { return { 0xEB }; } + static const std::vector<unsigned char> Password () { return { 0x75 }; } + static const std::vector<unsigned char> Signature() { return { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xE9 }; } + }; + }; + } +} + +#endif diff --git a/src/tools/connection_patcher/Patterns/Mac.hpp b/src/tools/connection_patcher/Patterns/Mac.hpp new file mode 100644 index 00000000000..78fa0c61b1c --- /dev/null +++ b/src/tools/connection_patcher/Patterns/Mac.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_PATTERNS_MAC_HPP +#define CONNECTION_PATCHER_PATTERNS_MAC_HPP + +#include <vector> + +namespace Connection_Patcher +{ + namespace Patterns + { + namespace Mac + { + struct x64 + { + static const std::vector<unsigned char> BNet () { return { 0x8B, 0x06, 0x89, 0x47, 0x0C, 0x5D, 0xC3 }; } + static const std::vector<unsigned char> Portal () { return { }; } + static const std::vector<unsigned char> Connect () { return { }; } + static const std::vector<unsigned char> Password () { return { 0x0F, 0x84, 0x00, 0xFF, 0xFF, 0xFF, 0x49, 0x8B, 0x45, 0x00, 0xB9, 0x40 }; } + static const std::vector<unsigned char> Signature() { return { 0xE8, 0x00, 0x00, 0x00, 0x00, 0x45, 0x31, 0xED, 0x4D, 0x89, 0xFC, 0x84, 0xC0, 0x75 }; } + }; + }; + } +} + +#endif diff --git a/src/tools/connection_patcher/Patterns/Windows.hpp b/src/tools/connection_patcher/Patterns/Windows.hpp new file mode 100644 index 00000000000..0f51c471a85 --- /dev/null +++ b/src/tools/connection_patcher/Patterns/Windows.hpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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/>. + */ + +#ifndef CONNECTION_PATCHER_PATTERNS_WINDOWS_HPP +#define CONNECTION_PATCHER_PATTERNS_WINDOWS_HPP + +#include <vector> + +namespace Connection_Patcher +{ + namespace Patterns + { + namespace Windows + { + struct x86 + { + static const std::vector<unsigned char> BNet () { return { 0x8B, 0x75, 0x08, 0x8D, 0x78, 0x0C }; } + static const std::vector<unsigned char> Portal () { return { }; } + static const std::vector<unsigned char> Connect () { return { 0x74, 0x33, 0x6A, 0x04, 0xFF, 0x75, 0xF8 }; } + static const std::vector<unsigned char> Password () { return { 0x74, 0x89, 0x8B, 0x16, 0x8B, 0x42, 0x04 }; } + static const std::vector<unsigned char> Signature() { return { 0xE8, 0x00, 0x00, 0x00, 0x00, 0x84, 0xC0, 0x75, 0x5F, 0x33, 0xC0 }; } + }; + + struct x64 + { + static const std::vector<unsigned char> BNet () { return { 0x8B, 0x02, 0x89, 0x41, 0x0C, 0x48, 0x8B, 0xC1, 0xC3 }; } + static const std::vector<unsigned char> Portal () { return { 0x2E, 0x6C, 0x6F, 0x67, 0x6F, 0x6E, 0x2E, 0x62, 0x61, 0x74, 0x74, 0x6C, 0x65, 0x2E, 0x6E, 0x65, 0x74, 0x00 }; } + static const std::vector<unsigned char> Connect () { return { 0x74, 0x2C, 0x48, 0x8D, 0x4C, 0x24, 0x78 }; } + static const std::vector<unsigned char> Password () { return { 0x74, 0x84, 0x48, 0x8B, 0x03 }; } + static const std::vector<unsigned char> Signature() { return { 0xE8, 0x00, 0x00, 0x00, 0x00, 0x84, 0xC0, 0x0F, 0x85, 0x88, 0x00, 0x00, 0x00, 0x45, 0x33, 0xC0 }; } + }; + }; + } +} + +#endif diff --git a/src/tools/connection_patcher/Program.cpp b/src/tools/connection_patcher/Program.cpp new file mode 100644 index 00000000000..539c6093d53 --- /dev/null +++ b/src/tools/connection_patcher/Program.cpp @@ -0,0 +1,197 @@ +/* + * Copyright (C) 2012-2014 Arctium Emulation <http://arctium.org> + * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> + * + * 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" +#include "Patches/Mac.hpp" +#include "Patches/Windows.hpp" +#include "Patterns/Mac.hpp" +#include "Patterns/Windows.hpp" + +#include <CompilerDefs.h> + +#include <boost/algorithm/string/replace.hpp> +#include <boost/filesystem.hpp> + +#include <iostream> + +#if PLATFORM == PLATFORM_WINDOWS +#include <Shlobj.h> +#endif + +namespace Connection_Patcher +{ + namespace + { + template<typename PATCH, typename PATTERN> + void PatchModule(boost::filesystem::path file, boost::filesystem::path path) + { + std::cout << "Patching module...\n"; + + Patcher patcher(file); + std::cout << "patching Password\n"; + patcher.Patch(PATCH::Password(), PATTERN::Password()); + + std::string const moduleName(Helper::GetFileChecksum(patcher.binary) + ".auth"); + boost::filesystem::path const modulePath + (path / std::string(&moduleName[0], 2) / std::string(&moduleName[2], 2)); + + if (!boost::filesystem::exists(modulePath)) + boost::filesystem::create_directories(modulePath); + + patcher.Finish(modulePath / moduleName); + + std::cout << "Patching module finished.\n"; + } + + template<typename PATCH, typename PATTERN> + void do_module(std::string moduleName, boost::filesystem::path path) + { + boost::filesystem::path const modulePath + (path / std::string(&moduleName[0], 2) / std::string(&moduleName[2], 2)); + boost::filesystem::path const module(modulePath / moduleName); + + if (!boost::filesystem::exists (module)) + throw std::runtime_error("base module does not exist. run client once."); + + PatchModule<PATCH, PATTERN>(module, path); + } + + template<typename PATCH, typename PATTERN> + void do_patches(Patcher* patcher, boost::filesystem::path output) + { + std::cout << "patching BNet\n"; + patcher->Patch(PATCH::BNet(), PATTERN::BNet()); + std::cout << "patching Portal\n"; + patcher->Patch(PATCH::Portal(), PATTERN::Portal()); + std::cout << "patching Connect\n"; + patcher->Patch(PATCH::Connect(), PATTERN::Connect()); + std::cout << "patching Signature\n"; + patcher->Patch(PATCH::Signature(), PATTERN::Signature()); + patcher->Finish(output); + + std::cout << "Patching done.\n"; + + } + } + + // adapted from http://stackoverflow.com/questions/8593608/how-can-i-copy-a-directory-using-boost-filesystem + void copyDir(boost::filesystem::path const & source, boost::filesystem::path const & destination) + { + namespace fs = boost::filesystem; + if (!fs::exists(source) || !fs::is_directory(source)) + throw std::invalid_argument("Source directory " + source.string() + " does not exist or is not a directory."); + + if (fs::exists(destination)) + throw std::invalid_argument("Destination directory " + destination.string() + " already exists."); + + if (!fs::create_directory(destination)) + throw std::runtime_error("Unable to create destination directory" + destination.string()); + + for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) + { + fs::path current(file->path()); + if (fs::is_directory(current)) + copyDir(current, destination / current.filename()); + else + fs::copy_file(current, destination / current.filename()); + } + } +} + +int main(int argc, char** argv) +{ + using namespace Connection_Patcher; + + try + { + if (argc != 2) + throw std::invalid_argument("Wrong number of arguments: Missing client file."); + + std::string const binary_path(argv[1]); + + std::string renamed_binary_path(binary_path); + + wchar_t* commonAppData (nullptr); +#if PLATFORM == PLATFORM_WINDOWS + SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &commonAppData); +#endif + + std::cout << "Creating patched binaries for "; + + Patcher patcher(binary_path); + + switch (patcher.Type) + { + case Constants::BinaryTypes::Pe32: + std::cout << "Win32 client...\n"; + + boost::algorithm::replace_all(renamed_binary_path, ".exe", "_Patched.exe"); + do_patches<Patches::Windows::x86, Patterns::Windows::x86> + (&patcher, renamed_binary_path); + + do_module<Patches::Windows::x86, Patterns::Windows::x86> + ( "8f52906a2c85b416a595702251570f96d3522f39237603115f2f1ab24962043c.auth" + , std::wstring(commonAppData) + std::wstring(L"/Blizzard Entertainment/Battle.net/Cache/") + ); + + break; + case Constants::BinaryTypes::Pe64: + std::cout << "Win64 client...\n"; + + boost::algorithm::replace_all(renamed_binary_path, ".exe", "_Patched.exe"); + do_patches<Patches::Windows::x64, Patterns::Windows::x64> + (&patcher, renamed_binary_path); + + do_module<Patches::Windows::x64, Patterns::Windows::x64> + ( "0a3afee2cade3a0e8b458c4b4660104cac7fc50e2ca9bef0d708942e77f15c1d.auth" + , std::wstring(commonAppData) + std::wstring(L"/Blizzard Entertainment/Battle.net/Cache/") + ); + + break; + case Constants::BinaryTypes::Mach64: + std::cout << "Mac client...\n"; + + boost::algorithm::replace_all (renamed_binary_path, ".app", " Patched.app"); + copyDir ( boost::filesystem::path(binary_path).parent_path()/*MacOS*/.parent_path()/*Contents*/.parent_path() + , boost::filesystem::path(renamed_binary_path).parent_path()/*MacOS*/.parent_path()/*Contents*/.parent_path() + ); + + do_patches<Patches::Mac::x64, Patterns::Mac::x64> + (&patcher, renamed_binary_path); + + do_module<Patches::Windows::x64, Patterns::Windows::x64> + ( "97eeb2e28e9e56ed6a22d09f44e2ff43c93315e006bbad43bafc0defaa6f50ae.auth" + , "/Users/Shared/Blizzard/Battle.net/Cache/" + ); + + break; + default: + throw std::runtime_error("Type: " + std::to_string(static_cast<uint32_t>(patcher.Type)) + " not supported!"); + } + + std::cout << "Successfully created your patched binaries.\n"; + + return 0; + } + catch (std::exception const& ex) + { + std::cerr << "EX: " << ex.what() << std::endl; + return 1; + } +} |