forked from wins1ey/LibreSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.c
234 lines (200 loc) · 6.2 KB
/
signature.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
#include "signature.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
#include "memory.h"
#include "process.h"
#include <luajit.h>
extern game_process process;
// Error handling macro
#define HANDLE_ERROR(msg) \
do { \
perror(msg); \
return NULL; \
} while (0)
// Error logging function
void log_error(const char* format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "Error in sig_scan: ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
}
MemoryRegion* get_memory_regions(pid_t pid, int* count)
{
char maps_path[256];
if (snprintf(maps_path, sizeof(maps_path), "/proc/%d/maps", pid) < 0) {
HANDLE_ERROR("Failed to create maps path");
}
FILE* maps_file = fopen(maps_path, "r");
if (!maps_file) {
HANDLE_ERROR("Failed to open maps file");
}
MemoryRegion* regions = NULL;
int capacity = 0;
*count = 0;
char line[256];
while (fgets(line, sizeof(line), maps_file)) {
if (*count >= capacity) {
capacity = capacity == 0 ? 10 : capacity * 2;
MemoryRegion* temp = realloc(regions, capacity * sizeof(MemoryRegion));
if (!temp) {
free(regions);
fclose(maps_file);
HANDLE_ERROR("Failed to allocate memory for regions");
}
regions = temp;
}
uintptr_t start, end;
if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &start, &end) != 2) {
continue; // Skip lines that don't match the expected format
}
regions[*count].start = start;
regions[*count].end = end;
(*count)++;
}
fclose(maps_file);
return regions;
}
bool match_pattern(const uint8_t* data, const uint16_t* pattern, size_t pattern_size)
{
for (size_t i = 0; i < pattern_size; ++i) {
uint8_t byte = pattern[i] & 0xFF;
bool ignore = (pattern[i] >> 8) & 0x1;
if (!ignore && data[i] != byte) {
return false;
}
}
return true;
}
uint16_t* convert_signature(const char* signature, size_t* pattern_size)
{
char* signature_copy = strdup(signature);
if (!signature_copy) {
return NULL;
}
char* token = strtok(signature_copy, " ");
size_t size = 0;
size_t capacity = 10;
uint16_t* pattern = (uint16_t*)malloc(capacity * sizeof(uint16_t));
if (!pattern) {
free(signature_copy);
return NULL;
}
while (token != NULL) {
if (size >= capacity) {
capacity *= 2;
uint16_t* temp = (uint16_t*)realloc(pattern, capacity * sizeof(uint16_t));
if (!temp) {
free(pattern);
free(signature_copy);
return NULL;
}
pattern = temp;
}
if (strcmp(token, "??") == 0) {
pattern[size] = 0xFF00; // Set the upper byte to 1 to indicate ignoring this byte
} else {
pattern[size] = strtol(token, NULL, 16);
}
size++;
token = strtok(NULL, " ");
}
free(signature_copy);
*pattern_size = size;
return pattern;
}
bool validate_process_memory(pid_t pid, uintptr_t address, void* buffer, size_t size)
{
struct iovec local_iov = { buffer, size };
struct iovec remote_iov = { (void*)address, size };
ssize_t nread = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0);
return nread == (ssize_t)size;
}
int perform_sig_scan(lua_State* L)
{
if (lua_gettop(L) != 2) {
log_error("Invalid number of arguments: expected 2 (signature, offset)");
lua_pushnil(L);
return 1;
}
if (!lua_isstring(L, 1) || !lua_isnumber(L, 2)) {
log_error("Invalid argument types: expected (string, number)");
lua_pushnil(L);
return 1;
}
pid_t p_pid = process.pid;
const char* signature = lua_tostring(L, 1);
int offset = lua_tointeger(L, 2);
// Validate signature string
if (strlen(signature) == 0) {
log_error("Signature string cannot be empty");
lua_pushnil(L);
return 1;
}
size_t pattern_length;
uint16_t* pattern = convert_signature(signature, &pattern_length);
if (!pattern) {
log_error("Failed to convert signature");
lua_pushnil(L);
return 1;
}
int regions_count = 0;
MemoryRegion* regions = get_memory_regions(p_pid, ®ions_count);
if (!regions) {
free(pattern);
log_error("Failed to get memory regions");
lua_pushnil(L);
return 1;
}
for (int i = 0; i < regions_count; i++) {
MemoryRegion region = regions[i];
ssize_t region_size = region.end - region.start;
uint8_t* buffer = malloc(region_size);
if (!buffer) {
free(pattern);
free(regions);
log_error("Failed to allocate memory for region buffer");
lua_pushnil(L);
return 1;
}
if (!validate_process_memory(p_pid, region.start, buffer, region_size)) {
free(buffer);
continue; // Continue to next region
}
for (size_t j = 0; j <= region_size - pattern_length; ++j) {
if (match_pattern(buffer + j, pattern, pattern_length)) {
uintptr_t result = region.start + j + offset;
char full_hex_str[32]; // Increased buffer size for safety
if (snprintf(full_hex_str, sizeof(full_hex_str), "0x%" PRIxPTR, result) < 0) {
free(buffer);
free(pattern);
free(regions);
log_error("Failed to convert result to string");
lua_pushnil(L);
return 1;
}
free(buffer);
free(pattern);
free(regions);
lua_pushstring(L, full_hex_str);
return 1;
}
}
free(buffer);
}
free(pattern);
free(regions);
// No match found
log_error("No match found for the given signature");
lua_pushnil(L);
return 1;
}