-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlocklandLiberator.c
558 lines (445 loc) · 12.3 KB
/
BlocklandLiberator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* Blockland r2033 Liberator (v1.2)
*
* Copyright (C) 2024 Elletra
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
// ----------------------------------------------------------------
#define BL_REVISION "2033"
#define PROGRAM_VERSION "1.2"
#define U8_SIZE (sizeof(uint8_t))
#define U16_SIZE (sizeof(uint16_t))
#define U32_SIZE (sizeof(uint32_t))
#define SECTION_NAME_LEN 8
// ----------------------------------------------------------------
#define OPEN_FILE(path, mode)\
FILE *file = fopen(path, mode);\
if (file == NULL)\
{\
error = Open;\
}\
else
#define CLOSE_FILE()\
if (fclose(file) != 0)\
{\
error = Close;\
}
#define READ(field)\
if (fread(&field, sizeof(field), 1, file) != 1)\
{\
return Read;\
}
#define SKIP(bytes)\
if (fseek(file, bytes * sizeof(uint8_t), SEEK_CUR))\
{\
return Seek;\
}
#define READ_SECTION(function, ...)\
error = function(__VA_ARGS__);\
if (error != None)\
{\
CLOSE_FILE()\
return error;\
}
#define WRITE_PATCH(rva, count, ...)\
{\
uint8_t byte_array[count] = { __VA_ARGS__ };\
if (fseek(file, rva_to_raw(data, rva), SEEK_SET))\
{\
printf("Error: Failed to seek to address 0x%X\n", rva);\
error = Seek;\
CLOSE_FILE()\
return error;\
}\
if (fwrite(byte_array, U8_SIZE, count, file) != count)\
{\
printf("Error: Failed to write patch at 0x%X\n", rva);\
error = Write;\
CLOSE_FILE()\
return error;\
}\
}
// ----------------------------------------------------------------
struct Args
{
bool display_help;
bool cli_mode;
const char *exe_path;
};
const uint32_t PE_OFFSET = 0x3C;
const uint16_t PE32_MAGIC = 0x10B;
const uint16_t DOS_MAGIC = 0x5A4D;
const uint32_t PE_MAGIC = 0x00004550;
const char TEXT_SECTION_NAME[] = { '.', 't', 'e', 'x', 't', '\0', '\0', '\0' };
const int RVA_ENTRY_SIZE = 8;
struct PEData
{
/* Relevant COFF header data */
uint16_t num_sections;
uint16_t optional_header_bytes;
/* Relevant Windows 32-bit optional header data */
uint32_t image_base;
uint32_t num_rvas;
/* Relevant .text section data */
uint32_t text_virtual_addr;
uint32_t text_raw_data_ptr;
};
enum Error
{
None,
Arguments,
Open,
Close,
Read,
Write,
Seek,
Unsupported,
NoDosSig,
NoPeSig,
NoPeHeader,
NoTextSection,
};
const int MIN_ERROR = None;
const int MAX_ERROR = NoTextSection;
const char *error_strings[] =
{
"No error.",
"Missing and/or invalid argument(s).",
"Failed to open file.",
"Failed to close file.",
"Failed to read byte(s).",
"Failed to write byte(s).",
"Failed to seek to address.",
"PE32+ files are not supported.",
"No DOS signature found.",
"No PE signature found.",
"No PE header found.",
"Could not locate a .text section!",
};
// ----------------------------------------------------------------
const char *get_error_string(enum Error error);
uint32_t read_u32(FILE *file);
enum Error read_dos_stub(FILE *file);
enum Error read_pe_sig(FILE *file);
enum Error read_coff_header(FILE *file, struct PEData *data);
enum Error check_optional_header(FILE *file, struct PEData *data);
enum Error read_win_header(FILE *file, struct PEData *data);
enum Error find_text_section(FILE *file, struct PEData *data);
uint32_t rva_to_raw(struct PEData *data, uint32_t rva);
enum Error read_exe(const char *path, struct PEData *data);
enum Error patch_exe(const char *path, struct PEData *data);
void print_header();
void print_help();
void pause(struct Args *args);
bool parse_args(int argc, const char *argv[], struct Args *args);
// ----------------------------------------------------------------
/**
* WARNING: This program ONLY works properly for Blockland v21 r2033! It DOES NOT check if the
* executable being patched is the correct version/revision or is even a copy of Blockland. Use at
* your own risk!
*/
int main(int argc, const char *argv[])
{
enum Error error = None;
struct Args args =
{
.display_help = false,
.cli_mode = false,
.exe_path = NULL,
};
print_header();
if (!parse_args(argc, argv, &args))
{
error = Arguments;
print_help();
}
else
{
struct PEData data;
printf("Reading file: \"%s\"\n\n", args.exe_path);
error = read_exe(args.exe_path, &data);
if (error == None)
{
error = patch_exe(args.exe_path, &data);
if (error == None)
{
printf("\nFile successfully patched!\n\n");
}
else
{
printf("Error patching executable: %s\n\n", get_error_string(error));
}
}
else
{
printf("Error reading executable: %s\n\n", get_error_string(error));
}
}
pause(&args);
return error;
}
const char *get_error_string(enum Error error)
{
return (error >= MIN_ERROR && error <= MAX_ERROR) ? error_strings[error] : "";
}
uint32_t read_u32(FILE *file)
{
uint32_t value = 0;
fread(&value, U32_SIZE, 1, file);
return value;
}
enum Error read_dos_stub(FILE *file)
{
enum Error error = None;
uint16_t magic;
READ(magic);
if (magic != DOS_MAGIC)
{
error = NoDosSig;
}
return error;
}
enum Error read_pe_sig(FILE *file)
{
enum Error error = None;
if (fseek(file, PE_OFFSET, SEEK_SET) || fseek(file, read_u32(file), SEEK_SET))
{
error = NoPeSig;
}
else
{
uint32_t magic;
READ(magic);
if (magic != PE_MAGIC)
{
error = NoPeSig;
}
}
return error;
}
/* Read the COFF header and grab what we need from it. */
enum Error read_coff_header(FILE *file, struct PEData *data)
{
SKIP(U16_SIZE)
READ(data->num_sections)
SKIP(U32_SIZE * 3)
READ(data->optional_header_bytes)
SKIP(U16_SIZE)
return None;
}
/* Check optional header to make sure this is a PE32 and not a PE32+, then skip the rest. */
enum Error check_optional_header(FILE *file, struct PEData *data)
{
enum Error error = None;
uint16_t magic;
READ(magic);
if (magic != PE32_MAGIC)
{
error = Unsupported;
}
else
{
uint32_t skip = (U8_SIZE * 2) + (U32_SIZE * 6);
SKIP(skip)
data->optional_header_bytes -= (sizeof(PE32_MAGIC) + skip);
}
return error;
}
enum Error read_win_header(FILE *file, struct PEData *data)
{
enum Error error = None;
uint32_t skip = (U32_SIZE * 11) + (U16_SIZE * 8);
READ(data->image_base)
SKIP(skip)
READ(data->num_rvas)
data->optional_header_bytes -= sizeof(data->image_base) + sizeof(data->num_rvas) + skip;
SKIP(data->optional_header_bytes)
return error;
}
enum Error find_text_section(FILE *file, struct PEData *data)
{
enum Error error = None;
char name[SECTION_NAME_LEN];
bool done = false;
bool is_text = false;
for (uint16_t i = 0; i < data->num_sections && !done; i++)
{
READ(name)
SKIP(U32_SIZE) // Virtual size
is_text = !strncmp(name, TEXT_SECTION_NAME, SECTION_NAME_LEN);
if (is_text)
{
READ(data->text_virtual_addr)
}
else
{
SKIP(U32_SIZE)
}
SKIP(U32_SIZE) // Size of raw data
if (is_text)
{
READ(data->text_raw_data_ptr)
}
else
{
SKIP(U32_SIZE)
}
SKIP((U32_SIZE * 3) + (U16_SIZE * 2))
done = error != None || is_text;
}
return !done ? NoTextSection : error;
}
uint32_t rva_to_raw(struct PEData *data, uint32_t rva)
{
return rva - data->image_base - data->text_virtual_addr + data->text_raw_data_ptr;
}
enum Error read_exe(const char *path, struct PEData *data)
{
enum Error error = None;
OPEN_FILE(path, "rb")
{
READ_SECTION(read_dos_stub, file)
READ_SECTION(read_pe_sig, file)
READ_SECTION(read_coff_header, file, data)
if (data->optional_header_bytes <= 0)
{
error = NoPeHeader;
}
else
{
READ_SECTION(check_optional_header, file, data)
READ_SECTION(read_win_header, file, data)
READ_SECTION(find_text_section, file, data)
}
CLOSE_FILE()
}
return error;
}
enum Error patch_exe(const char *path, struct PEData *data)
{
enum Error error = None;
OPEN_FILE(path, "rb+")
{
printf("Removing function definition restrictions...\n");
/* This is the fewest number of patches I could do without the game crashing. If I studied
the function more maybe I could get it lower, but this is good enough... */
WRITE_PATCH(0x43035D, 1, 0xEB)
WRITE_PATCH(0x43037E, 1, 0xEB)
WRITE_PATCH(0x4303BB, 4, 0xE9, 0x4C, 0x06, 0x00)
WRITE_PATCH(0x430A16, 5, 0xE9, 0xE9, 0x09, 0x00, 0x00)
printf("Removing function call restrictions...\n");
WRITE_PATCH(0x42F3B7, 2, 0xEB, 0x26) // `setMyBLID()`
WRITE_PATCH(0x42F417, 2, 0xEB, 0x26) // `setDedicatedToken()`
WRITE_PATCH(0x42F487, 2, 0xEB, 0x26) // `getDedicatedToken()`
WRITE_PATCH(0x42F517, 2, 0xEB, 0x26) // `setJoinToken()`
WRITE_PATCH(0x42F587, 2, 0xEB, 0x26) // `getJoinToken()`
WRITE_PATCH(0x4BAD37, 2, 0xEB, 0x2E) // `GameConnection::setPlayerName()`
WRITE_PATCH(0x4BAE17, 2, 0xEB, 0x2A) // `GameConnection::setBLID()`
WRITE_PATCH(0x4BAF57, 2, 0xEB, 0x29) // `GameConnection::getJoinToken()`
WRITE_PATCH(0x4CA097, 2, 0xEB, 0x2E) // `secureCommandToClient()`
WRITE_PATCH(0x4CA207, 2, 0xEB, 0x2E) // `secureCommandToAll()`
WRITE_PATCH(0x4CA3D7, 2, 0xEB, 0x2E) // `secureCommandToAllExcept()`
WRITE_PATCH(0x59446F, 2, 0xEB, 0x2E) // `NetConnection::connect()`
WRITE_PATCH(0x594623, 2, 0xEB, 0x2E) // `NetConnection::connectArranged()`
WRITE_PATCH(0x611F4D, 2, 0xEB, 0x2E) // `SteamGetAuthSessionTicket()`
WRITE_PATCH(0x612107, 2, 0xEB, 0x2E) // `SteamCancelAuthTicket()`
printf("Removing function password checks...\n");
WRITE_PATCH(0x4BAD78, 2, 0xEB, 0x22) // `GameConnection::setPlayerName()`
WRITE_PATCH(0x4BAE54, 2, 0xEB, 0x26) // `GameConnection::setBLID()`
WRITE_PATCH(0x4CA107, 2, 0x90, 0x90) // `secureCommandToClient()`
WRITE_PATCH(0x4CA254, 2, 0xEB, 0x16) // `secureCommandToAll()`
WRITE_PATCH(0x4CA424, 2, 0xEB, 0x16) // `secureCommandToAllExcept()`
/* `ShapeBase::setShapeName()` */
WRITE_PATCH(0x4FB0C0, 1, 0x03) // Change argument check amount from 4 to 3
WRITE_PATCH(0x4FB0C2, 1, 0x8C) // Change comparison from `==` to `>=`
WRITE_PATCH(0x4FB0C7, 2, 0xEB, 0x1E) // Remove password check
printf("Removing fxDTSBrickData field mutation restrictions...\n");
// Lets you change `fxDTSBrickData` fields after server load.
WRITE_PATCH(0x486591, 5, 0x83, 0xE0, 0xBF, 0x90, 0x90)
printf("Making transmitDataBlocks() work for fxDTSBrickData datablocks...\n");
// Makes it so that `transmitDataBlocks()` works for `fxDTSBrickData` datablocks too.
WRITE_PATCH(0x482D90, 12, 0xC6, 0x81, 0xEA, 0x01, 0x00, 0x00, 0x01, 0xE9, 0xD4, 0x79, 0xFC, 0xFF)
CLOSE_FILE()
}
return error;
}
void print_header()
{
printf(
"*************************************************\n"
"* Blockland r" BL_REVISION " Liberator (v" PROGRAM_VERSION ") by Elletra *\n"
"*************************************************\n\n"
);
}
void print_help()
{
printf(
"usage: BlocklandLiberator.exe path [-X] [-h]\n"
" options:\n"
" -h, --help Displays help.\n"
" -X, --cli Makes the program operate as a command-line interface\n"
" that takes no keyboard input and closes immediately\n"
" upon completion or failure.\n"
);
}
void pause(struct Args *args)
{
if (!args->cli_mode)
{
printf("\nPress enter key to continue...\n\n");
fflush(stdout);
getchar();
}
}
bool parse_args(int argc, const char *argv[], struct Args *args)
{
bool error = false;
for (int i = 1; i < argc && !error; i++)
{
const char *arg = argv[i];
if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
{
args->display_help = true;
}
else if (!strcmp(arg, "-X") || !strcmp(arg, "--cli"))
{
args->cli_mode = true;
}
else if (arg[0] == '-')
{
error = true;
printf("Unrecognized command-line option '%s'\n\n", arg);
}
else if (args->exe_path != NULL)
{
error = true;
printf("Error: Multiple executable files specified.\n\n");
}
else
{
args->exe_path = arg;
}
}
if (args->exe_path == NULL && !args->display_help)
{
error = true;
printf("Error: No executable file specified.\n\n");
}
return !error && !args->display_help;
}