Skip to content

Commit

Permalink
upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Redbeanw44602 committed Sep 16, 2022
0 parents commit d7a38de
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/MCPatcher.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.23)
project(MCPatcher)

set(CMAKE_CXX_STANDARD 20)

add_executable(MCPatcher main.cpp patcher.cpp patcher.h utils.cpp utils.h)
120 changes: 120 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "windows.h"
#include <iostream>
#include <shobjidl.h>
#include <fstream>

#include "patcher.h"
#include "utils.h"

using std::endl;
using std::cout;

#define FAIL_CANNOT_OPEN_FILE (-1)
#define FAIL_CANNOT_READ_FILE (-2)
#define FAIL_CANNOT_FIND_BYTE (-3)
#define FAIL_CURRENT_PLATFORM_NO_PATCH (-4)
#define FAIL_BACKUP (-5)

int main() {

// Add known patches;

MCPatcher::registerPatch(
Platform::Win10,
"PV1193025-1403A4F20",
{
{
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC },
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC }
},
{
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x7C, 0x24 },
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x7C, 0x24 }
}
}
);

// Ask for binary file;

HRESULT result = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
string strpath;
if (SUCCEEDED(result))
{
cout << "[i] Please open an executable for minecraft. (Minecraft.Windows.exe)" << endl;
IFileOpenDialog *openFile;
CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&openFile));
COMDLG_FILTERSPEC extNames[] =
{
{ L"Minecraft", L"*.exe" }
};
openFile->SetFileTypes(1,extNames);
openFile->Show(nullptr);
IShellItem *pItem;
result = openFile->GetResult(&pItem);
if (SUCCEEDED(result))
{
PWSTR pPath;
pItem->GetDisplayName(SIGDN_FILESYSPATH, &pPath);
std::wstringstream path;
path << pPath;
strpath = wchar2string(path.str().c_str());
cout << "[i] Selected " << strpath << "." << endl;
pItem->Release();
}
else
{
cout << "[x] Open file failed!" << endl;
}
openFile->Release();
CoUninitialize();
}
if (strpath.empty())
return FAIL_CANNOT_OPEN_FILE;

// Open file;

if (!MCPatcher::open(strpath))
{
cout << "[x] Can't read executable file!" << endl;
return FAIL_CANNOT_READ_FILE;
}
else
{
std::ofstream ofs(strpath + ".bak",ios::binary);
ofs << MCPatcher::getImage().rdbuf();
if (ofs.good())
cout << "[i] Backup created to: " << strpath + ".bak" << endl;
else
{
cout << "[x] Fail to create backup!" << endl;
return FAIL_BACKUP;
}
ofs.close();
}

// Select platform;

auto platform = Platform::Win10;
auto patches = MCPatcher::patches[platform];
if (!patches.empty())
{
cout << "[x] There are no patches available for the current platform." << endl;
return FAIL_CURRENT_PLATFORM_NO_PATCH;
}

// Do patch;

cout << "[i] Looking for bytes..." << endl;
if (MCPatcher::tryApply(platform))
cout << "[i] Patch successfully." << endl;
else
{
cout << "[x] Failed, if it is the latest version, please send issue." << endl;
return FAIL_CANNOT_FIND_BYTE;
}

MCPatcher::close();

return 0;
}
75 changes: 75 additions & 0 deletions patcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// Created by RedbeanW on 9/16/2022.
//

#include "patcher.h"

using std::cout;
using std::endl;

void MCPatcher::registerPatch(Platform platform, const string& name, const vector<pair<vector<unsigned char>,vector<unsigned char>>>& patch) {
for (auto& i : patch)
{
if (i.first.size() != i.second.size())
{
cout << "[x] The wrong patch is being registered!" << endl;
return;
}
}
patches[platform][name] = patch;
}

bool MCPatcher::open(const string &path) {
image.open(path,ios::binary|ios::in|ios::out);
return image.is_open();
}

void MCPatcher::close() {
return image.close();
}

fstream& MCPatcher::getImage() {
return image;
}

bool MCPatcher::tryApply(Platform platform) {
auto tryuse = patches[platform];
vector<std::pair<long long,vector<unsigned char>>> needModify;
auto isOk = true;
for (auto& it : tryuse)
{
cout << "[i] Trying \"" << it.first << "\" patch." << endl;
cout << "[i] Need to find " << it.second.size() << " binary position..." << endl;
needModify.clear();
auto count = 0;
for (auto& bin : it.second)
{
count++;
auto pos = findBytes(image,bin.first);
if (pos)
{
cout << "[i] Point " << count << " founded, " << pos << "." << endl;
needModify.emplace_back(pair{pos,bin.second});
}
else
{
cout << "[i] Point " << count << " not found, try the next set." << endl;
isOk = false;
break;
}
}
if (isOk)
break;
}
if (!isOk || needModify.empty())
return false;
for (auto& patch : needModify)
{
image.seekg(patch.first);
for (auto& bts : patch.second)
{
image.write((char *)&bts, sizeof(bts));
}
}
return image.good();
}
39 changes: 39 additions & 0 deletions patcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Created by RedbeanW on 9/16/2022.
//

#ifndef MCPATCHER_PATCHER_H
#define MCPATCHER_PATCHER_H

#include <vector>
#include <unordered_map>
#include <fstream>
#include <utility>
#include <iostream>

#include "utils.h"

using std::vector;
using std::unordered_map;
using std::string;
using std::pair;
using std::fstream;
using std::ios;

enum class Platform {
Win10 = 0x1
};

namespace MCPatcher {

static unordered_map<Platform,unordered_map<string,vector<pair<vector<unsigned char>,vector<unsigned char>>>>> patches;
static fstream image;

void registerPatch(Platform, const string& name, const vector<pair<vector<unsigned char>,vector<unsigned char>>>& patch);
fstream& getImage();
bool tryApply(Platform);
bool open(const string& path);
void close();
}

#endif //MCPATCHER_PATCHER_H
54 changes: 54 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Created by RedbeanW on 9/16/2022.
//

#include "utils.h"


string char2hex(unsigned char chr)
{
/*for (auto i : tmpVec){
}*/
std::ostringstream ss;
auto bin = int(chr);
ss << std::hex << bin;
return ss.str();
}

string wchar2string(const wchar_t *wchar)
{
const wchar_t *wText = wchar;
DWORD bytes = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,nullptr,0,nullptr,FALSE);
char *psText;
psText = new char[bytes];
WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,psText,bytes,nullptr,FALSE);
string str = psText;
delete []psText;
return str;
}

long long findBytes(std::fstream& file, const vector<unsigned char> &bytes)
{
unsigned char chr;
std::vector<unsigned char> tmpVec;
auto length = bytes.size();
auto loops = 0;
file.seekg(0);
while(file.read((char *)&chr, sizeof(chr)))
{
loops++;
tmpVec.emplace_back(chr);
auto it = tmpVec.rbegin();
auto pos = 0;
while (it != tmpVec.rend())
{
pos++;
if (pos > length || bytes.at(length - pos) != *it)
break;
else if (pos == length)
return loops - length;
++it;
}
}
return 0;
}
24 changes: 24 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by RedbeanW on 9/16/2022.
//

#ifndef MCPATCHER_UTILS_H
#define MCPATCHER_UTILS_H

#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>

#include "Windows.h"
#include <ShObjIdl.h>

using std::string;
using std::vector;

string char2hex(unsigned char chr);
string wchar2string(const wchar_t *wchar);
long long findBytes(std::fstream& file, const vector<unsigned char> &bytes);

#endif //MCPATCHER_UTILS_H

0 comments on commit d7a38de

Please sign in to comment.