diff options
| author | Root THC | 2026-02-24 12:42:47 +0000 |
|---|---|---|
| committer | Root THC | 2026-02-24 12:42:47 +0000 |
| commit | c9cbeced5b3f2bdd7407e29c0811e65954132540 (patch) | |
| tree | aefc355416b561111819de159ccbd86c3004cf88 /other/burneye/src/debug/memdump.c | |
| parent | 073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff) | |
initial
Diffstat (limited to 'other/burneye/src/debug/memdump.c')
| -rw-r--r-- | other/burneye/src/debug/memdump.c | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/other/burneye/src/debug/memdump.c b/other/burneye/src/debug/memdump.c new file mode 100644 index 0000000..3215d20 --- /dev/null +++ b/other/burneye/src/debug/memdump.c | |||
| @@ -0,0 +1,249 @@ | |||
| 1 | /* memory dump utility | ||
| 2 | * -scut | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include <sys/types.h> | ||
| 6 | #include <sys/ptrace.h> | ||
| 7 | #include <sys/wait.h> | ||
| 8 | #include <sys/user.h> | ||
| 9 | #include <errno.h> | ||
| 10 | #include <unistd.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <stdio.h> | ||
| 13 | #include <libgen.h> /* basename */ | ||
| 14 | |||
| 15 | |||
| 16 | void | ||
| 17 | hexdump (unsigned char *data, unsigned int amount); | ||
| 18 | |||
| 19 | |||
| 20 | int | ||
| 21 | main (int argc, char *argv[]) | ||
| 22 | { | ||
| 23 | pid_t fpid; /* child pid, gets ptraced */ | ||
| 24 | char * argv0; | ||
| 25 | struct user regs; /* PTRACE pulled registers */ | ||
| 26 | unsigned long int addr, /* segment start address */ | ||
| 27 | addr_end, /* segment end address */ | ||
| 28 | len; /* length of segment */ | ||
| 29 | unsigned long int addr_walker, /* walker to dump memory */ | ||
| 30 | eip; /* current childs eip */ | ||
| 31 | |||
| 32 | /* array to temporarily store data into */ | ||
| 33 | unsigned char data_saved[sizeof (unsigned long int)]; | ||
| 34 | |||
| 35 | /* file to read mapping information */ | ||
| 36 | FILE * map_f; /* /proc/<pid>/maps stream */ | ||
| 37 | unsigned char map_line[256]; /* one line each from map */ | ||
| 38 | |||
| 39 | /* data for the dump files */ | ||
| 40 | FILE * dump_f; /* stream */ | ||
| 41 | char dump_name[64]; /* filename buffer */ | ||
| 42 | |||
| 43 | |||
| 44 | if (argc < 3 || sscanf (argv[1], "0x%lx", &eip) != 1) { | ||
| 45 | printf ("usage: %s <eip> <argv0 [argv1 [...]]>\n\n", argv[0]); | ||
| 46 | printf ("will run 'argv0' as program with given arguments, " | ||
| 47 | "until 'eip' is reached, then\n" | ||
| 48 | "dumping 'len' bytes from 'addr'.\n\n" | ||
| 49 | "example: %s 0x08049014 0x08048000 0x100 /bin/ls " | ||
| 50 | "-l\n\n", argv[0]); | ||
| 51 | |||
| 52 | exit (EXIT_FAILURE); | ||
| 53 | } | ||
| 54 | |||
| 55 | argv0 = argv[2]; | ||
| 56 | |||
| 57 | fpid = fork (); | ||
| 58 | if (fpid < 0) { | ||
| 59 | perror ("fork"); | ||
| 60 | exit (EXIT_FAILURE); | ||
| 61 | } | ||
| 62 | if (fpid == 0) { /* child */ | ||
| 63 | if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) { | ||
| 64 | perror ("ptrace PTRACE_TRACEME"); | ||
| 65 | exit (EXIT_FAILURE); | ||
| 66 | } | ||
| 67 | fprintf (stderr, " child: TRACEME set\n"); | ||
| 68 | |||
| 69 | fprintf (stderr, " child: executing: %s\n", argv[2]); | ||
| 70 | close (1); | ||
| 71 | dup2 (2, 1); | ||
| 72 | execve (argv[2], &argv[2], NULL); | ||
| 73 | |||
| 74 | /* failed ? */ | ||
| 75 | perror ("execve"); | ||
| 76 | exit (EXIT_FAILURE); | ||
| 77 | } | ||
| 78 | |||
| 79 | wait (NULL); | ||
| 80 | |||
| 81 | memset (®s, 0, sizeof (regs)); | ||
| 82 | |||
| 83 | if (ptrace (PTRACE_GETREGS, fpid, NULL, ®s) < 0) { | ||
| 84 | perror ("ptrace PTRACE_GETREGS"); | ||
| 85 | exit (EXIT_FAILURE); | ||
| 86 | } | ||
| 87 | fprintf (stderr, "[0x%08lx] first stop\n", regs.regs.eip); | ||
| 88 | |||
| 89 | /* now single step until given eip is reached */ | ||
| 90 | do { | ||
| 91 | if (ptrace (PTRACE_SINGLESTEP, fpid, NULL, NULL) < 0) { | ||
| 92 | perror ("ptrace PTRACE_SINGLESTEP"); | ||
| 93 | exit (EXIT_FAILURE); | ||
| 94 | } | ||
| 95 | wait (NULL); | ||
| 96 | |||
| 97 | memset (®s, 0, sizeof (regs)); | ||
| 98 | if (ptrace (PTRACE_GETREGS, fpid, NULL, ®s) < 0) { | ||
| 99 | perror ("ptrace PTRACE_GETREGS"); | ||
| 100 | exit (EXIT_FAILURE); | ||
| 101 | } | ||
| 102 | } while (regs.regs.eip != eip); | ||
| 103 | |||
| 104 | fprintf (stderr, "MEMDUMP: eip @ 0x%08lx, dumping...\n", eip); | ||
| 105 | |||
| 106 | snprintf (dump_name, sizeof (dump_name), "%s.regs", | ||
| 107 | basename (argv0)); | ||
| 108 | dump_name[sizeof (dump_name) - 1] = '\0'; | ||
| 109 | dump_f = fopen (dump_name, "w"); | ||
| 110 | if (dump_f == NULL) { | ||
| 111 | perror ("fopen dumpfile regs"); | ||
| 112 | exit (EXIT_FAILURE); | ||
| 113 | } | ||
| 114 | fprintf (dump_f, "eax = 0x%08lx\n", regs.regs.eax); | ||
| 115 | fprintf (dump_f, "ebx = 0x%08lx\n", regs.regs.ebx); | ||
| 116 | fprintf (dump_f, "ecx = 0x%08lx\n", regs.regs.ecx); | ||
| 117 | fprintf (dump_f, "edx = 0x%08lx\n", regs.regs.edx); | ||
| 118 | fprintf (dump_f, "esi = 0x%08lx\n", regs.regs.esi); | ||
| 119 | fprintf (dump_f, "edi = 0x%08lx\n", regs.regs.edi); | ||
| 120 | fprintf (dump_f, "ebp = 0x%08lx\n", regs.regs.ebp); | ||
| 121 | fprintf (dump_f, "esp = 0x%08lx\n", regs.regs.esp); | ||
| 122 | fprintf (dump_f, "eflags = 0x%08lx\n", regs.regs.eflags); | ||
| 123 | fprintf (dump_f, "xcs = 0x%08lx\n", regs.regs.xcs); | ||
| 124 | fprintf (dump_f, "xds = 0x%08lx\n", regs.regs.xds); | ||
| 125 | fprintf (dump_f, "xes = 0x%08lx\n", regs.regs.xes); | ||
| 126 | fprintf (dump_f, "xss = 0x%08lx\n", regs.regs.xss); | ||
| 127 | fclose (dump_f); | ||
| 128 | |||
| 129 | snprintf (map_line, sizeof (map_line), "/proc/%d/maps", fpid); | ||
| 130 | map_line[sizeof (map_line) - 1] = '\0'; | ||
| 131 | map_f = fopen (map_line, "r"); | ||
| 132 | if (map_f == NULL) { | ||
| 133 | perror ("fopen map-file"); | ||
| 134 | |||
| 135 | exit (EXIT_FAILURE); | ||
| 136 | } | ||
| 137 | |||
| 138 | while (fgets (map_line, sizeof (map_line), map_f) != NULL) { | ||
| 139 | char map_perm[8]; | ||
| 140 | |||
| 141 | if (sscanf (map_line, "%08lx-%08lx %7[rwxp-] ", | ||
| 142 | &addr, &addr_end, map_perm) != 3) | ||
| 143 | { | ||
| 144 | perror ("invalid map-line"); | ||
| 145 | |||
| 146 | exit (EXIT_FAILURE); | ||
| 147 | } | ||
| 148 | if (addr_end < addr) { | ||
| 149 | fprintf (stderr, "sanity required, not so: " | ||
| 150 | "addr = 0x%08lx, addr_end = 0x%08lx", | ||
| 151 | addr, addr_end); | ||
| 152 | |||
| 153 | exit (EXIT_FAILURE); | ||
| 154 | } | ||
| 155 | len = addr_end - addr; | ||
| 156 | map_perm[sizeof (map_perm) - 1] = '\0'; /* ;-) */ | ||
| 157 | |||
| 158 | fprintf (stderr, "MEMDUMP: -> 0x%08lx (0x%08lx bytes, " | ||
| 159 | "perm %s)\n", addr, len, map_perm); | ||
| 160 | |||
| 161 | snprintf (dump_name, sizeof (dump_name), | ||
| 162 | "%s.0x%08lx.0x%08lx.%s", | ||
| 163 | basename (argv0), addr, len, map_perm); | ||
| 164 | dump_name[sizeof (dump_name) - 1] = '\0'; | ||
| 165 | dump_f = fopen (dump_name, "wb"); | ||
| 166 | if (dump_f == NULL) { | ||
| 167 | perror ("fopen dumpfile"); | ||
| 168 | |||
| 169 | exit (EXIT_FAILURE); | ||
| 170 | } | ||
| 171 | |||
| 172 | /* save data, assuming addr is page aligned */ | ||
| 173 | for (addr_walker = 0 ; addr_walker < len ; | ||
| 174 | addr_walker += sizeof (data_saved)) | ||
| 175 | { | ||
| 176 | errno = 0; | ||
| 177 | |||
| 178 | *((unsigned long int *) &data_saved[0]) = | ||
| 179 | ptrace (PTRACE_PEEKDATA, fpid, | ||
| 180 | addr + addr_walker, NULL); | ||
| 181 | |||
| 182 | if (errno == 0 && fwrite (&data_saved[0], 1, 4, | ||
| 183 | dump_f) != 4) | ||
| 184 | { | ||
| 185 | perror ("fwrite dumpfile"); | ||
| 186 | |||
| 187 | exit (EXIT_FAILURE); | ||
| 188 | } else if (errno != 0) { | ||
| 189 | fprintf (stderr, | ||
| 190 | "[0x%08lx] invalid PTRACE_PEEKDATA\n", | ||
| 191 | addr + addr_walker); | ||
| 192 | |||
| 193 | exit (EXIT_FAILURE); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | fclose (dump_f); | ||
| 198 | } | ||
| 199 | fclose (map_f); | ||
| 200 | |||
| 201 | if (ptrace (PTRACE_DETACH, fpid, NULL, NULL) < 0) { | ||
| 202 | perror ("ptrace PTRACE_DETACH"); | ||
| 203 | exit (EXIT_FAILURE); | ||
| 204 | } | ||
| 205 | |||
| 206 | fprintf (stderr, "MEMDUMP: success. terminating.\n"); | ||
| 207 | exit (EXIT_SUCCESS); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | void | ||
| 213 | hexdump (unsigned char *data, unsigned int amount) | ||
| 214 | { | ||
| 215 | unsigned int dp, p; /* data pointer */ | ||
| 216 | const char trans[] = | ||
| 217 | "................................ !\"#$%&'()*+,-./0123456789" | ||
| 218 | ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm" | ||
| 219 | "nopqrstuvwxyz{|}~...................................." | ||
| 220 | "....................................................." | ||
| 221 | "........................................"; | ||
| 222 | |||
| 223 | for (dp = 1; dp <= amount; dp++) { | ||
| 224 | printf ("%02x ", data[dp-1]); | ||
| 225 | if ((dp % 8) == 0) | ||
| 226 | printf (" "); | ||
| 227 | if ((dp % 16) == 0) { | ||
| 228 | printf ("| "); | ||
| 229 | p = dp; | ||
| 230 | for (dp -= 16; dp < p; dp++) | ||
| 231 | printf ("%c", trans[data[dp]]); | ||
| 232 | printf ("\n"); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | if ((amount % 16) != 0) { | ||
| 236 | p = dp = 16 - (amount % 16); | ||
| 237 | for (dp = p; dp > 0; dp--) { | ||
| 238 | printf (" "); | ||
| 239 | if (((dp % 8) == 0) && (p != 8)) | ||
| 240 | printf (" "); | ||
| 241 | } | ||
| 242 | printf (" | "); | ||
| 243 | for (dp = (amount - (16 - p)); dp < amount; dp++) | ||
| 244 | printf ("%c", trans[data[dp]]); | ||
| 245 | } | ||
| 246 | printf ("\n"); | ||
| 247 | |||
| 248 | return; | ||
| 249 | } | ||
