-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
88 lines (79 loc) · 2.67 KB
/
Copy pathmain.c
File metadata and controls
88 lines (79 loc) · 2.67 KB
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
#include "memtool.h"
/**
* @file main.c
* @brief Contains the main function which serves as the program's entry point.
*
* The main function accepts command-line arguments and returns an integer
* status code to indicate the success or failure of program execution.
*
* @param argc The number of command-line arguments.
* @param argv Array of command-line arguments, represented as strings.
*
* @return int Returns 0 if the program executes successfully, non-zero otherwise.
*/
void handle_map_command(process_id_t pid) {
ProcessMap* map = read_process_maps(pid);
if (map) {
print_memory_map(map);
free(map);
}
}
void handle_search_command(ProcessHandle* handle, process_id_t pid, const char* pattern) {
ProcessMap* map = read_process_maps(pid);
if (map) {
for (int i = 0; i < map->count; i++) {
// Only search readable memory
#ifdef PLATFORM_WINDOWS
if (map->regions[i].Protection & PAGE_READONLY)
#else
if (map->regions[i].perms[0] == 'r')
#endif
{
search_pattern(handle, &map->regions[i], pattern, strlen(pattern));
}
}
free(map);
}
}
void handle_write_command(ProcessHandle* handle, const char* address_str, const char* data) {
void* address = (void*)strtoul(address_str, NULL, 0);
size_t data_len = strlen(data);
size_t bytes_written = write_process_memory(handle, address, data, data_len);
if (bytes_written == data_len) {
printf("Successfully wrote %zu bytes to address %p\n", bytes_written, address);
} else {
printf("Failed to write to address %p\n", address);
}
}
int main(int argc, char* argv[]) {
if (argc < 3) {
printf("Usage: %s <command> <pid> [options]\n", argv[0]);
printf("Commands: map, search, write\n");
return 1;
}
process_id_t pid = atoi(argv[2]);
ProcessHandle* handle = open_process(pid);
if (!handle) {
printf("Failed to open process %d\n", pid);
return 1;
}
if (strcmp(argv[1], "map") == 0) {
handle_map_command(pid);
} else if (strcmp(argv[1], "search") == 0) {
if (argc < 4) {
printf("Usage: %s search <pid> <pattern>\n", argv[0]);
close_process(handle);
return 1;
}
handle_search_command(handle, pid, argv[3]);
} else if (strcmp(argv[1], "write") == 0) {
if (argc < 5) {
printf("Usage: %s write <pid> <address> <data>\n", argv[0]);
close_process(handle);
return 1;
}
handle_write_command(handle, argv[3], argv[4]);
}
close_process(handle);
return 0;
}