Skip to content

Commit d7a38de

Browse files
committed
upload.
0 parents  commit d7a38de

11 files changed

+354
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/MCPatcher.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
project(MCPatcher)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
add_executable(MCPatcher main.cpp patcher.cpp patcher.h utils.cpp utils.h)

main.cpp

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "windows.h"
2+
#include <iostream>
3+
#include <shobjidl.h>
4+
#include <fstream>
5+
6+
#include "patcher.h"
7+
#include "utils.h"
8+
9+
using std::endl;
10+
using std::cout;
11+
12+
#define FAIL_CANNOT_OPEN_FILE (-1)
13+
#define FAIL_CANNOT_READ_FILE (-2)
14+
#define FAIL_CANNOT_FIND_BYTE (-3)
15+
#define FAIL_CURRENT_PLATFORM_NO_PATCH (-4)
16+
#define FAIL_BACKUP (-5)
17+
18+
int main() {
19+
20+
// Add known patches;
21+
22+
MCPatcher::registerPatch(
23+
Platform::Win10,
24+
"PV1193025-1403A4F20",
25+
{
26+
{
27+
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC },
28+
{ 0x10, 0x84, 0xC0, 0x74, 0x15, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x4C, 0x24, 0x30, 0x48, 0x33, 0xCC }
29+
},
30+
{
31+
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*O*/0x01, 0x48, 0x8B, 0x7C, 0x24 },
32+
{ 0x48, 0x83, 0xC3, 0x10, 0x48, 0x3B, 0xDF, 0x75, 0xEA, 0xB0, /*N*/0x00, 0x48, 0x8B, 0x7C, 0x24 }
33+
}
34+
}
35+
);
36+
37+
// Ask for binary file;
38+
39+
HRESULT result = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
40+
string strpath;
41+
if (SUCCEEDED(result))
42+
{
43+
cout << "[i] Please open an executable for minecraft. (Minecraft.Windows.exe)" << endl;
44+
IFileOpenDialog *openFile;
45+
CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_ALL,
46+
IID_IFileOpenDialog, reinterpret_cast<void**>(&openFile));
47+
COMDLG_FILTERSPEC extNames[] =
48+
{
49+
{ L"Minecraft", L"*.exe" }
50+
};
51+
openFile->SetFileTypes(1,extNames);
52+
openFile->Show(nullptr);
53+
IShellItem *pItem;
54+
result = openFile->GetResult(&pItem);
55+
if (SUCCEEDED(result))
56+
{
57+
PWSTR pPath;
58+
pItem->GetDisplayName(SIGDN_FILESYSPATH, &pPath);
59+
std::wstringstream path;
60+
path << pPath;
61+
strpath = wchar2string(path.str().c_str());
62+
cout << "[i] Selected " << strpath << "." << endl;
63+
pItem->Release();
64+
}
65+
else
66+
{
67+
cout << "[x] Open file failed!" << endl;
68+
}
69+
openFile->Release();
70+
CoUninitialize();
71+
}
72+
if (strpath.empty())
73+
return FAIL_CANNOT_OPEN_FILE;
74+
75+
// Open file;
76+
77+
if (!MCPatcher::open(strpath))
78+
{
79+
cout << "[x] Can't read executable file!" << endl;
80+
return FAIL_CANNOT_READ_FILE;
81+
}
82+
else
83+
{
84+
std::ofstream ofs(strpath + ".bak",ios::binary);
85+
ofs << MCPatcher::getImage().rdbuf();
86+
if (ofs.good())
87+
cout << "[i] Backup created to: " << strpath + ".bak" << endl;
88+
else
89+
{
90+
cout << "[x] Fail to create backup!" << endl;
91+
return FAIL_BACKUP;
92+
}
93+
ofs.close();
94+
}
95+
96+
// Select platform;
97+
98+
auto platform = Platform::Win10;
99+
auto patches = MCPatcher::patches[platform];
100+
if (!patches.empty())
101+
{
102+
cout << "[x] There are no patches available for the current platform." << endl;
103+
return FAIL_CURRENT_PLATFORM_NO_PATCH;
104+
}
105+
106+
// Do patch;
107+
108+
cout << "[i] Looking for bytes..." << endl;
109+
if (MCPatcher::tryApply(platform))
110+
cout << "[i] Patch successfully." << endl;
111+
else
112+
{
113+
cout << "[x] Failed, if it is the latest version, please send issue." << endl;
114+
return FAIL_CANNOT_FIND_BYTE;
115+
}
116+
117+
MCPatcher::close();
118+
119+
return 0;
120+
}

patcher.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// Created by RedbeanW on 9/16/2022.
3+
//
4+
5+
#include "patcher.h"
6+
7+
using std::cout;
8+
using std::endl;
9+
10+
void MCPatcher::registerPatch(Platform platform, const string& name, const vector<pair<vector<unsigned char>,vector<unsigned char>>>& patch) {
11+
for (auto& i : patch)
12+
{
13+
if (i.first.size() != i.second.size())
14+
{
15+
cout << "[x] The wrong patch is being registered!" << endl;
16+
return;
17+
}
18+
}
19+
patches[platform][name] = patch;
20+
}
21+
22+
bool MCPatcher::open(const string &path) {
23+
image.open(path,ios::binary|ios::in|ios::out);
24+
return image.is_open();
25+
}
26+
27+
void MCPatcher::close() {
28+
return image.close();
29+
}
30+
31+
fstream& MCPatcher::getImage() {
32+
return image;
33+
}
34+
35+
bool MCPatcher::tryApply(Platform platform) {
36+
auto tryuse = patches[platform];
37+
vector<std::pair<long long,vector<unsigned char>>> needModify;
38+
auto isOk = true;
39+
for (auto& it : tryuse)
40+
{
41+
cout << "[i] Trying \"" << it.first << "\" patch." << endl;
42+
cout << "[i] Need to find " << it.second.size() << " binary position..." << endl;
43+
needModify.clear();
44+
auto count = 0;
45+
for (auto& bin : it.second)
46+
{
47+
count++;
48+
auto pos = findBytes(image,bin.first);
49+
if (pos)
50+
{
51+
cout << "[i] Point " << count << " founded, " << pos << "." << endl;
52+
needModify.emplace_back(pair{pos,bin.second});
53+
}
54+
else
55+
{
56+
cout << "[i] Point " << count << " not found, try the next set." << endl;
57+
isOk = false;
58+
break;
59+
}
60+
}
61+
if (isOk)
62+
break;
63+
}
64+
if (!isOk || needModify.empty())
65+
return false;
66+
for (auto& patch : needModify)
67+
{
68+
image.seekg(patch.first);
69+
for (auto& bts : patch.second)
70+
{
71+
image.write((char *)&bts, sizeof(bts));
72+
}
73+
}
74+
return image.good();
75+
}

patcher.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by RedbeanW on 9/16/2022.
3+
//
4+
5+
#ifndef MCPATCHER_PATCHER_H
6+
#define MCPATCHER_PATCHER_H
7+
8+
#include <vector>
9+
#include <unordered_map>
10+
#include <fstream>
11+
#include <utility>
12+
#include <iostream>
13+
14+
#include "utils.h"
15+
16+
using std::vector;
17+
using std::unordered_map;
18+
using std::string;
19+
using std::pair;
20+
using std::fstream;
21+
using std::ios;
22+
23+
enum class Platform {
24+
Win10 = 0x1
25+
};
26+
27+
namespace MCPatcher {
28+
29+
static unordered_map<Platform,unordered_map<string,vector<pair<vector<unsigned char>,vector<unsigned char>>>>> patches;
30+
static fstream image;
31+
32+
void registerPatch(Platform, const string& name, const vector<pair<vector<unsigned char>,vector<unsigned char>>>& patch);
33+
fstream& getImage();
34+
bool tryApply(Platform);
35+
bool open(const string& path);
36+
void close();
37+
}
38+
39+
#endif //MCPATCHER_PATCHER_H

utils.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Created by RedbeanW on 9/16/2022.
3+
//
4+
5+
#include "utils.h"
6+
7+
8+
string char2hex(unsigned char chr)
9+
{
10+
/*for (auto i : tmpVec){
11+
}*/
12+
std::ostringstream ss;
13+
auto bin = int(chr);
14+
ss << std::hex << bin;
15+
return ss.str();
16+
}
17+
18+
string wchar2string(const wchar_t *wchar)
19+
{
20+
const wchar_t *wText = wchar;
21+
DWORD bytes = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,nullptr,0,nullptr,FALSE);
22+
char *psText;
23+
psText = new char[bytes];
24+
WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,psText,bytes,nullptr,FALSE);
25+
string str = psText;
26+
delete []psText;
27+
return str;
28+
}
29+
30+
long long findBytes(std::fstream& file, const vector<unsigned char> &bytes)
31+
{
32+
unsigned char chr;
33+
std::vector<unsigned char> tmpVec;
34+
auto length = bytes.size();
35+
auto loops = 0;
36+
file.seekg(0);
37+
while(file.read((char *)&chr, sizeof(chr)))
38+
{
39+
loops++;
40+
tmpVec.emplace_back(chr);
41+
auto it = tmpVec.rbegin();
42+
auto pos = 0;
43+
while (it != tmpVec.rend())
44+
{
45+
pos++;
46+
if (pos > length || bytes.at(length - pos) != *it)
47+
break;
48+
else if (pos == length)
49+
return loops - length;
50+
++it;
51+
}
52+
}
53+
return 0;
54+
}

utils.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Created by RedbeanW on 9/16/2022.
3+
//
4+
5+
#ifndef MCPATCHER_UTILS_H
6+
#define MCPATCHER_UTILS_H
7+
8+
#include <string>
9+
#include <iostream>
10+
#include <sstream>
11+
#include <vector>
12+
#include <fstream>
13+
14+
#include "Windows.h"
15+
#include <ShObjIdl.h>
16+
17+
using std::string;
18+
using std::vector;
19+
20+
string char2hex(unsigned char chr);
21+
string wchar2string(const wchar_t *wchar);
22+
long long findBytes(std::fstream& file, const vector<unsigned char> &bytes);
23+
24+
#endif //MCPATCHER_UTILS_H

0 commit comments

Comments
 (0)