diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-16 22:14:29 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-19 20:20:13 +0300 |
| commit | 7e8bd04389875d8569463f42923792557edc2908 (patch) | |
| tree | aad1922fcfdd0af286531b3b2b58aa6c3da67be8 | |
| parent | Memory scanning to find byte patterns (diff) | |
| download | linux-game-trainer-7e8bd04389875d8569463f42923792557edc2908.tar.gz linux-game-trainer-7e8bd04389875d8569463f42923792557edc2908.zip | |
Write other process' memory
| -rw-r--r-- | src/main.c | 27 | ||||
| -rw-r--r-- | src/vm.c | 27 | ||||
| -rw-r--r-- | src/vm.h | 2 |
3 files changed, 44 insertions, 12 deletions
| @@ -8,6 +8,20 @@ | |||
| 8 | #include "util.h" | 8 | #include "util.h" |
| 9 | #include "vm.h" | 9 | #include "vm.h" |
| 10 | 10 | ||
| 11 | void hex2bytes(char *hex) | ||
| 12 | { | ||
| 13 | size_t len = strlen(hex); | ||
| 14 | char bytes[len + 1]; | ||
| 15 | |||
| 16 | for (size_t i = 0; i < len; ++i) { | ||
| 17 | char hdig[3] = { hex[i*2], hex[i*2+1], '\0' }; | ||
| 18 | sscanf(hdig, "%hhx", &bytes[i]); | ||
| 19 | } | ||
| 20 | |||
| 21 | memcpy(hex, bytes, len); | ||
| 22 | hex[len] = '\0'; | ||
| 23 | } | ||
| 24 | |||
| 11 | int main(int argc, char *argv[]) | 25 | int main(int argc, char *argv[]) |
| 12 | { | 26 | { |
| 13 | if (argc < 2) { | 27 | if (argc < 2) { |
| @@ -23,7 +37,7 @@ int main(int argc, char *argv[]) | |||
| 23 | waitpid(pid, NULL, __WALL); | 37 | waitpid(pid, NULL, __WALL); |
| 24 | LOG("Attached to process %d\n", pid); | 38 | LOG("Attached to process %d\n", pid); |
| 25 | 39 | ||
| 26 | char *byte_seq = "secret text"; | 40 | char *byte_seq = "DEADBEEF"; |
| 27 | size_t byte_seq_len = strlen(byte_seq); | 41 | size_t byte_seq_len = strlen(byte_seq); |
| 28 | MemscanResult *head = memscan(pid, (uint8_t*)byte_seq, byte_seq_len); | 42 | MemscanResult *head = memscan(pid, (uint8_t*)byte_seq, byte_seq_len); |
| 29 | MemscanResult *cur = head; | 43 | MemscanResult *cur = head; |
| @@ -39,7 +53,18 @@ int main(int argc, char *argv[]) | |||
| 39 | cur->mapping->name); | 53 | cur->mapping->name); |
| 40 | cur = cur->next; | 54 | cur = cur->next; |
| 41 | } | 55 | } |
| 56 | |||
| 42 | printf("\n\n"); | 57 | printf("\n\n"); |
| 58 | |||
| 59 | char *buf = "CAFEBABE"; | ||
| 60 | size_t len = strlen(buf); | ||
| 61 | cur = head; | ||
| 62 | while (cur) { | ||
| 63 | void *address = cur->mapping->begin + cur->offset; | ||
| 64 | memwrite(pid, address, (uint8_t*)buf, len); | ||
| 65 | cur = cur->next; | ||
| 66 | } | ||
| 67 | |||
| 43 | ptrace(PTRACE_DETACH, pid, NULL, NULL); | 68 | ptrace(PTRACE_DETACH, pid, NULL, NULL); |
| 44 | LOG("Detached from process %d\n", pid); | 69 | LOG("Detached from process %d\n", pid); |
| 45 | 70 | ||
| @@ -1,3 +1,4 @@ | |||
| 1 | #include <sys/ptrace.h> | ||
| 1 | #include <stdlib.h> | 2 | #include <stdlib.h> |
| 2 | #include <stdio.h> | 3 | #include <stdio.h> |
| 3 | #include <string.h> | 4 | #include <string.h> |
| @@ -63,15 +64,6 @@ VMMapping* parse_vmmap (int pid) | |||
| 63 | } else { | 64 | } else { |
| 64 | head = cur = new_mapping; | 65 | head = cur = new_mapping; |
| 65 | } | 66 | } |
| 66 | |||
| 67 | LOG("%p-%p %c%c%c%c %s\n", | ||
| 68 | cur->begin, | ||
| 69 | cur->end, | ||
| 70 | cur->r ? 'r' : '-', | ||
| 71 | cur->w ? 'w' : '-', | ||
| 72 | cur->x ? 'x' : '-', | ||
| 73 | cur->s ? 's' : 'p', | ||
| 74 | cur->name); | ||
| 75 | } | 67 | } |
| 76 | 68 | ||
| 77 | return head; | 69 | return head; |
| @@ -91,7 +83,7 @@ MemscanResult* memscan(int pid, uint8_t *byte_seq, uint64_t byte_seq_len) | |||
| 91 | { | 83 | { |
| 92 | char fmem_path[1024] = {0}; | 84 | char fmem_path[1024] = {0}; |
| 93 | sprintf(fmem_path, "/proc/%d/mem", pid); | 85 | sprintf(fmem_path, "/proc/%d/mem", pid); |
| 94 | FILE *fmem = fopen(fmem_path, "rb+"); | 86 | FILE *fmem = fopen(fmem_path, "rb"); |
| 95 | VMMapping *vmmaps_head = parse_vmmap(pid); | 87 | VMMapping *vmmaps_head = parse_vmmap(pid); |
| 96 | VMMapping *cur_vmmap = vmmaps_head; | 88 | VMMapping *cur_vmmap = vmmaps_head; |
| 97 | MemscanResult *cur = NULL, *head = NULL; | 89 | MemscanResult *cur = NULL, *head = NULL; |
| @@ -101,7 +93,6 @@ MemscanResult* memscan(int pid, uint8_t *byte_seq, uint64_t byte_seq_len) | |||
| 101 | cur_vmmap = cur_vmmap->next; | 93 | cur_vmmap = cur_vmmap->next; |
| 102 | continue; | 94 | continue; |
| 103 | } | 95 | } |
| 104 | LOG("Scanning [%p]\n", cur_vmmap->begin); | ||
| 105 | 96 | ||
| 106 | size_t region_size = cur_vmmap->end - cur_vmmap->begin; | 97 | size_t region_size = cur_vmmap->end - cur_vmmap->begin; |
| 107 | uint8_t region_data[region_size]; | 98 | uint8_t region_data[region_size]; |
| @@ -135,6 +126,20 @@ MemscanResult* memscan(int pid, uint8_t *byte_seq, uint64_t byte_seq_len) | |||
| 135 | 126 | ||
| 136 | cur_vmmap = cur_vmmap->next; | 127 | cur_vmmap = cur_vmmap->next; |
| 137 | } | 128 | } |
| 129 | fclose(fmem); | ||
| 138 | 130 | ||
| 139 | return head; | 131 | return head; |
| 140 | } | 132 | } |
| 133 | |||
| 134 | void memwrite(int pid, void *address, uint8_t *data, size_t data_len) | ||
| 135 | { | ||
| 136 | char fmem_path[1024] = {0}; | ||
| 137 | sprintf(fmem_path, "/proc/%d/mem", pid); | ||
| 138 | FILE *fmem = fopen(fmem_path, "rb+"); | ||
| 139 | |||
| 140 | fseek(fmem, (off_t)address, SEEK_SET); | ||
| 141 | fwrite(data, 1, data_len, fmem); | ||
| 142 | fclose(fmem); | ||
| 143 | |||
| 144 | LOG("Data written successfully at address %p\n", address); | ||
| 145 | } | ||
| @@ -1,6 +1,7 @@ | |||
| 1 | #ifndef _VM_H_ | 1 | #ifndef _VM_H_ |
| 2 | #define _VM_H_ | 2 | #define _VM_H_ |
| 3 | #include <stdint.h> | 3 | #include <stdint.h> |
| 4 | #include <sys/types.h> | ||
| 4 | 5 | ||
| 5 | typedef struct VMMapping { | 6 | typedef struct VMMapping { |
| 6 | void *begin; | 7 | void *begin; |
| @@ -24,4 +25,5 @@ VMMapping* parse_vmmap (int pid); | |||
| 24 | 25 | ||
| 25 | MemscanResult* memscan(int pid, uint8_t *byte_seq, uint64_t byte_seq_len); | 26 | MemscanResult* memscan(int pid, uint8_t *byte_seq, uint64_t byte_seq_len); |
| 26 | 27 | ||
| 28 | void memwrite(int pid, void *address, uint8_t *data, size_t data_len); | ||
| 27 | #endif // _VM_H_ | 29 | #endif // _VM_H_ |
