aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c125
1 files changed, 88 insertions, 37 deletions
diff --git a/src/main.c b/src/main.c
index 667ca4c..623d767 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,4 @@
1#include <string.h> 1#include <string.h>
2#include <stdint.h>
3#include <stdlib.h> 2#include <stdlib.h>
4#include <stdio.h> 3#include <stdio.h>
5#include <sys/ptrace.h> 4#include <sys/ptrace.h>
@@ -8,24 +7,94 @@
8#include "util.h" 7#include "util.h"
9#include "vm.h" 8#include "vm.h"
10 9
11void hex2bytes(char *hex) 10Bytes hex2bytes(const char *hex)
12{ 11{
13 size_t len = strlen(hex); 12 size_t hlen = strlen(hex);
14 char bytes[len + 1]; 13 Bytes bytes = {
14 .data = xmalloc(hlen / 2),
15 .len = hlen / 2,
16 };
15 17
16 for (size_t i = 0; i < len; ++i) { 18 for (size_t i = 0; i < hlen; ++i) {
17 char hdig[3] = { hex[i*2], hex[i*2+1], '\0' }; 19 char hdig[3] = { hex[i*2], hex[i*2+1], '\0' };
18 sscanf(hdig, "%hhx", &bytes[i]); 20 sscanf(hdig, "%hhx", &bytes.data[i]);
19 } 21 }
20 22
21 memcpy(hex, bytes, len); 23 return bytes;
22 hex[len] = '\0';
23} 24}
24 25
25int main(int argc, char *argv[]) 26void action_scan(int pid, int argc, const char *argv[])
26{ 27{
27 if (argc < 2) { 28 if (argc == 0) {
28 ERROR("Usage: %s <tracee_pid>\n", argv[0]); 29 ERROR("Scan: Missing argument <byte_sequence>\n");
30 }
31
32 Bytes aob = hex2bytes(argv[0]);
33 MemscanResult *results_head = memscan(pid, aob);
34 MemscanResult *cur = results_head;
35 while (cur) {
36 void *address = cur->mapping->begin + cur->offset;
37 printf("%p\n", address);
38 cur = cur->next;
39 }
40}
41
42void action_read(int pid, int argc, const char *argv[])
43{
44 if (argc == 0) {
45 ERROR("Write: Missing argument <nbytes>\n");
46 }
47
48 if (argc == 1) {
49 ERROR("Read: Missing argument(s) [<address> ...]");
50 }
51
52 size_t nbytes = atol(argv[0]);
53 if (nbytes == 0) {
54 perror("atol");
55 exit(1);
56 }
57
58 void *address[argc-1];
59 for (size_t i = 0; i < argc-1; ++i) {
60 sscanf(argv[i+1], "%p", &address[i]);
61 }
62
63 for (size_t i = 0; i < argc-1; ++i) {
64 Bytes bytes = memread(pid, address[i], nbytes);
65 for (size_t j = 0; j < bytes.len; ++j) {
66 printf("%02hhx", bytes.data[j]);
67 }
68 printf("\n");
69 }
70}
71
72void action_write(int pid, int argc, const char *argv[])
73{
74 if (argc == 0) {
75 ERROR("Write: Missing argument <bytes>\n");
76 }
77
78 if (argc == 1) {
79 ERROR("Write: Missing argument [<address> ...]\n");
80 }
81
82 Bytes aob = hex2bytes(argv[0]);
83 printf("%s\n", aob.data);
84 void *address[argc-1];
85 for (size_t i = 0; i < argc-1; ++i) {
86 sscanf(argv[i+1], "%p", &address[i]);
87 }
88
89 for (size_t i = 0; i < argc-1; ++i) {
90 memwrite(pid, address[i], aob);
91 }
92}
93
94int main(int argc, const char *argv[])
95{
96 if (argc < 3) {
97 ERROR("Usage: %s <tracee_pid> (scan|read|write) [args ...]\n", argv[0]);
29 } 98 }
30 99
31 int pid; 100 int pid;
@@ -37,32 +106,14 @@ int main(int argc, char *argv[])
37 waitpid(pid, NULL, __WALL); 106 waitpid(pid, NULL, __WALL);
38 LOG("Attached to process %d\n", pid); 107 LOG("Attached to process %d\n", pid);
39 108
40 char *byte_seq = "DEADBEEF"; 109 if (strcmp(argv[2], "scan") == 0) {
41 size_t byte_seq_len = strlen(byte_seq); 110 action_scan(pid, argc-3, &argv[3]);
42 MemscanResult *head = memscan(pid, (uint8_t*)byte_seq, byte_seq_len); 111 } else if (strcmp(argv[2], "read") == 0) {
43 MemscanResult *cur = head; 112 action_read(pid, argc-3, &argv[3]);
44 113 } else if (strcmp(argv[2], "write") == 0) {
45 printf("\n\n\nMemory scan results:\n"); 114 action_write(pid, argc-3, &argv[3]);
46 printf("%-16s|%-16s|%-10s|%-s\n", "Address", "Base", "Offset", "Name"); 115 } else {
47 puts("--------------------------------------------------"); 116 ERROR("Unknown option '%s'\n", argv[1]);
48 while (cur) {
49 printf("%-16p|%-16p|%#-10lx|%-s\n",
50 cur->mapping->begin + cur->offset,
51 cur->mapping->begin,
52 cur->offset,
53 cur->mapping->name);
54 cur = cur->next;
55 }
56
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 } 117 }
67 118
68 ptrace(PTRACE_DETACH, pid, NULL, NULL); 119 ptrace(PTRACE_DETACH, pid, NULL, NULL);