1
1
#include < iostream>
2
- #include < Windows.h>
3
2
#include < vector>
4
3
#include < cstdint>
5
4
#include < filesystem>
5
+ #include < fstream>
6
+
7
+ #ifdef _WIN32
8
+ #include < Windows.h>
9
+ #include < conio.h>
6
10
7
- bool readFile ( const std::string& filename, std::vector< uint8_t >& buffer )
11
+ static std::string SelectPEFile ( )
8
12
{
9
- HANDLE fileHandle = CreateFileA (
10
- filename.c_str (),
11
- GENERIC_READ,
12
- FILE_SHARE_READ,
13
- NULL ,
14
- OPEN_EXISTING,
15
- FILE_ATTRIBUTE_NORMAL,
16
- NULL
17
- );
18
-
19
- if (fileHandle == INVALID_HANDLE_VALUE)
13
+ TCHAR szBuffer[MAX_PATH] = { 0 };
14
+ OPENFILENAME file = { 0 };
15
+
16
+ ZeroMemory (&file, sizeof (file));
17
+
18
+ file.hwndOwner = NULL ;
19
+ file.lStructSize = sizeof (file);
20
+ file.lpstrFilter = L" vscript.dll(vscript.dll)\0 vscript.dll\0 所有文件(*.*)\0 *.*\0\0 " ;
21
+ file.lpstrInitialDir = L" " ;
22
+ file.lpstrFile = szBuffer;
23
+ file.nMaxFile = sizeof (szBuffer) / sizeof (*szBuffer);
24
+ file.nFilterIndex = 0 ;
25
+ file.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
26
+
27
+ if (GetOpenFileName (&file))
20
28
{
21
- std::cerr << " [ERROR] " << " Failed to create handle to: " << filename << std::endl;
22
- // std::cout << "[REMINDER] " << "Make sure the executable is in same folder with: " << filename << std::endl;
23
- return false ;
29
+ int size = WideCharToMultiByte (CP_ACP, 0 , file.lpstrFile , -1 , NULL , 0 , NULL , NULL );
30
+ if (size == 0 )
31
+ {
32
+ return std::string ();
33
+ }
34
+ std::string result (size - 1 , 0 );
35
+ WideCharToMultiByte (CP_ACP, 0 , file.lpstrFile , -1 , &result[0 ], size, NULL , NULL );
36
+ return result;
24
37
}
38
+ else
39
+ {
40
+ return std::string ();
41
+ }
42
+ }
43
+
44
+ static void Pause ()
45
+ {
46
+ std::cout << " Press any key to continue..." << std::flush;
47
+ (void )_getch ();
48
+ }
25
49
26
- DWORD fileSize = GetFileSize (fileHandle, NULL );
27
- buffer.resize (fileSize);
50
+ #else
28
51
29
- DWORD bytesRead = 0 ;
30
- ReadFile (fileHandle, buffer. data (), fileSize, &bytesRead, NULL );
52
+ static void Pause ()
53
+ {
31
54
32
- CloseHandle (fileHandle);
55
+ }
33
56
57
+ #endif // _WIN32
58
+
59
+ static bool readFile (const std::string & fileName, std::vector<uint8_t > & buffer)
60
+ {
61
+ std::ifstream file (fileName, std::ios::binary);
62
+
63
+ if (!file.is_open ())
64
+ {
65
+ std::cerr << " [ERROR] " << " Failed to open file: " << fileName << std::endl;
66
+ return false ;
67
+ }
68
+
69
+ file.seekg (0 , std::ios::end);
70
+ std::streamsize fileSize = file.tellg ();
71
+ file.seekg (0 , std::ios::beg);
72
+
73
+ buffer.resize (static_cast <size_t >(fileSize));
74
+ file.read (reinterpret_cast <char *>(buffer.data ()), fileSize);
75
+
76
+ file.close ();
34
77
return true ;
35
78
}
36
79
37
- std::vector<size_t > findHexArray (std::vector<uint8_t >& buffer, const std::vector<uint8_t >& hexArray)
80
+ static std::vector<size_t > findHexArray (const std::vector<uint8_t > & buffer, const std::vector<uint8_t > & hexArray)
38
81
{
39
82
std::vector<size_t > positions;
40
83
@@ -43,16 +86,11 @@ std::vector<size_t> findHexArray(std::vector<uint8_t>& buffer, const std::vector
43
86
44
87
for (size_t i = 0 ; i <= bufferSize - arraySize; ++i)
45
88
{
46
- bool found = true ;
47
-
48
- for (size_t j = 0 ; j < arraySize; ++j) {
49
- if (buffer[i + j] != hexArray[j] && hexArray[j] != 0x2A ) {
50
- found = false ;
51
- break ;
52
- }
53
- }
54
-
55
- if (found)
89
+ if (std::ranges::equal (buffer.begin () + i, buffer.begin () + i + arraySize, hexArray.begin (), hexArray.end (),
90
+ [](uint8_t a, uint8_t b)
91
+ {
92
+ return a == b || b == 0x2A ;
93
+ }))
56
94
{
57
95
std::cout << " [INFO] " << " Found position at buffer index: " << i << std::endl;
58
96
positions.push_back (i);
@@ -62,37 +100,27 @@ std::vector<size_t> findHexArray(std::vector<uint8_t>& buffer, const std::vector
62
100
return positions;
63
101
}
64
102
65
- bool writeFile (const std::string& filename , const std::vector<uint8_t >& buffer)
103
+ static bool writeFile (const std::string & fileName , const std::vector<uint8_t > & buffer)
66
104
{
67
- HANDLE fileHandle = CreateFileA (
68
- filename.c_str (),
69
- GENERIC_WRITE,
70
- 0 ,
71
- NULL ,
72
- CREATE_ALWAYS,
73
- FILE_ATTRIBUTE_NORMAL,
74
- NULL
75
- );
76
-
77
- if (fileHandle == INVALID_HANDLE_VALUE)
78
- {
79
- std::cerr << " [ERROR] " << " Failed to create handle to: " << filename << std::endl;
80
- // std::cout << "[REMINDER] " << "Make sure the executable is in same folder with: " << filename << std::endl;
105
+ std::ofstream file (fileName, std::ios::binary);
81
106
107
+ if (!file.is_open ())
108
+ {
109
+ std::cerr << " [ERROR] " << " Failed to open file: " << fileName << std::endl;
82
110
return false ;
83
111
}
84
112
85
- DWORD bytesWritten = 0 ;
86
- WriteFile (fileHandle, buffer.data (), buffer.size (), &bytesWritten, NULL );
113
+ file.write (reinterpret_cast <const char *>(buffer.data ()), buffer.size ());
87
114
88
- CloseHandle (fileHandle );
115
+ file. close ( );
89
116
90
- return (bytesWritten == buffer. size ()) ;
117
+ return true ;
91
118
}
92
119
93
- void PatchFile ( std::string fileName, std::vector<uint8_t > originalArray, std::vector<uint8_t > replacedArray)
120
+ static void patchFile ( const std::string & fileName, const std::vector<uint8_t > & originalArray, const std::vector<uint8_t > & replacedArray)
94
121
{
95
122
std::vector<uint8_t > buffer;
123
+
96
124
if (!readFile (fileName, buffer))
97
125
{
98
126
return ;
@@ -102,19 +130,19 @@ void PatchFile(std::string fileName, std::vector<uint8_t> originalArray, std::ve
102
130
103
131
if (positions.empty ())
104
132
{
105
- std::cout << " [ERROR] " << fileName << " is already patched" << std::endl;
133
+ std::cerr << " [ERROR] " << fileName << " is already patched" << std::endl;
106
134
return ;
107
135
}
108
136
137
+ if (std::filesystem::exists (fileName + " .bak" ))
138
+ {
139
+ std::filesystem::remove (fileName + " .bak" );
140
+ }
141
+ std::filesystem::copy (fileName, fileName + " .bak" );
142
+
109
143
for (size_t pos : positions)
110
144
{
111
- for (size_t i = 0 ; i < replacedArray.size (); ++i)
112
- {
113
- if (replacedArray[i] != 0x2A )
114
- {
115
- buffer[pos + i] = replacedArray[i];
116
- }
117
- }
145
+ std::ranges::copy (replacedArray.begin (), replacedArray.end (), buffer.begin () + pos);
118
146
}
119
147
120
148
if (!writeFile (fileName, buffer))
@@ -125,48 +153,6 @@ void PatchFile(std::string fileName, std::vector<uint8_t> originalArray, std::ve
125
153
std::cout << " [SUCCESS] " << fileName << " has been patched." << std::endl;
126
154
}
127
155
128
- static std::string SelectPEFile ()
129
- {
130
- TCHAR szBuffer[MAX_PATH] = { 0 };
131
- OPENFILENAME file = { 0 };
132
-
133
- ZeroMemory (&file, sizeof (file));
134
-
135
- file.hwndOwner = NULL ;
136
- file.lStructSize = sizeof (file);
137
- file.lpstrFilter = L" vscript.dll(vscript.dll)\0 vscript.dll\0 所有文件(*.*)\0 *.*\0\0 " ;
138
- file.lpstrInitialDir = L" " ;
139
- file.lpstrFile = szBuffer;
140
- file.nMaxFile = sizeof (szBuffer) / sizeof (*szBuffer);
141
- file.nFilterIndex = 0 ;
142
- file.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
143
-
144
- if (GetOpenFileName (&file))
145
- {
146
- int size = WideCharToMultiByte (CP_ACP, 0 , file.lpstrFile , -1 , NULL , 0 , NULL , NULL );
147
- if (size == 0 )
148
- {
149
- return std::string ();
150
- }
151
- std::string result (size - 1 , 0 );
152
- WideCharToMultiByte (CP_ACP, 0 , file.lpstrFile , -1 , &result[0 ], size, NULL , NULL );
153
- return result;
154
- }
155
- else
156
- {
157
- return std::string ();
158
- }
159
- }
160
-
161
- static void Pause ()
162
- {
163
- std::cout << " Press any key to continue..." << std::flush;
164
- HANDLE h = GetStdHandle (STD_INPUT_HANDLE);
165
- FlushConsoleInputBuffer (h);
166
- WaitForSingleObject (h, INFINITE);
167
- FlushConsoleInputBuffer (h);
168
- }
169
-
170
156
int main (int argc, const char * const * argv)
171
157
{
172
158
std::string filePath;
@@ -198,23 +184,21 @@ int main(int argc, const char * const * argv)
198
184
{
199
185
filePath = " ./bin/win64/vscript.dll" ;
200
186
}
201
-
187
+ # ifdef _WIN32
202
188
if (!std::filesystem::exists (filePath))
203
189
{
204
190
filePath = SelectPEFile ();
205
191
}
192
+ #endif // _WIN32
206
193
if (std::filesystem::exists (filePath))
207
194
{
208
- std::filesystem::copy (filePath, filePath + " .bak" );
209
-
210
- PatchFile (filePath, { 0xBE , 0x01 , 0x2A , 0x2A , 0x2A , 0x2B , 0xD6 , 0x74 , 0x2A , 0x3B , 0xD6 }, { 0xBE , 0x02 });
195
+ patchFile (filePath, { 0xBE , 0x01 , 0x2A , 0x2A , 0x2A , 0x2B , 0xD6 , 0x74 , 0x2A , 0x3B , 0xD6 }, { 0xBE , 0x02 });
211
196
}
212
197
else
213
198
{
214
199
std::cerr << " File doesn't exist" << std::endl;
215
200
return 2 ;
216
201
}
217
-
218
202
}
219
203
catch (const std::exception & e)
220
204
{
0 commit comments