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/wrez/debug/pcdump.c | |
| parent | 073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff) | |
initial
Diffstat (limited to 'other/wrez/debug/pcdump.c')
| -rw-r--r-- | other/wrez/debug/pcdump.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/other/wrez/debug/pcdump.c b/other/wrez/debug/pcdump.c new file mode 100644 index 0000000..95b96b7 --- /dev/null +++ b/other/wrez/debug/pcdump.c | |||
| @@ -0,0 +1,148 @@ | |||
| 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[], char *envp[]) | ||
| 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 < 2) { | ||
| 45 | printf ("usage: %s <argv0 [argv1 [...]]>\n\n", argv[0]); | ||
| 46 | printf ("will run 'argv0' as program with given arguments, " | ||
| 47 | "dumping 'eip'\n\n", argv[0]); | ||
| 48 | |||
| 49 | exit (EXIT_FAILURE); | ||
| 50 | } | ||
| 51 | |||
| 52 | fpid = fork (); | ||
| 53 | if (fpid < 0) { | ||
| 54 | perror ("fork"); | ||
| 55 | exit (EXIT_FAILURE); | ||
| 56 | } | ||
| 57 | if (fpid == 0) { /* child */ | ||
| 58 | if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) { | ||
| 59 | perror ("ptrace PTRACE_TRACEME"); | ||
| 60 | exit (EXIT_FAILURE); | ||
| 61 | } | ||
| 62 | fprintf (stderr, " child: TRACEME set\n"); | ||
| 63 | |||
| 64 | fprintf (stderr, " child: executing: %s\n", argv[2]); | ||
| 65 | close (1); | ||
| 66 | dup2 (2, 1); | ||
| 67 | execve (argv[1], &argv[1], envp); | ||
| 68 | |||
| 69 | /* failed ? */ | ||
| 70 | perror ("execve"); | ||
| 71 | exit (EXIT_FAILURE); | ||
| 72 | } | ||
| 73 | |||
| 74 | wait (NULL); | ||
| 75 | |||
| 76 | memset (®s, 0, sizeof (regs)); | ||
| 77 | |||
| 78 | if (ptrace (PTRACE_GETREGS, fpid, NULL, ®s) < 0) { | ||
| 79 | perror ("ptrace PTRACE_GETREGS"); | ||
| 80 | exit (EXIT_FAILURE); | ||
| 81 | } | ||
| 82 | fprintf (stderr, "[0x%08lx] first stop\n", regs.regs.eip); | ||
| 83 | |||
| 84 | /* now single step until given eip is reached */ | ||
| 85 | do { | ||
| 86 | if (ptrace (PTRACE_SINGLESTEP, fpid, NULL, NULL) < 0) { | ||
| 87 | perror ("ptrace PTRACE_SINGLESTEP"); | ||
| 88 | exit (EXIT_FAILURE); | ||
| 89 | } | ||
| 90 | wait (NULL); | ||
| 91 | |||
| 92 | memset (®s, 0, sizeof (regs)); | ||
| 93 | if (ptrace (PTRACE_GETREGS, fpid, NULL, ®s) < 0) { | ||
| 94 | perror ("ptrace PTRACE_GETREGS"); | ||
| 95 | exit (EXIT_FAILURE); | ||
| 96 | } | ||
| 97 | fprintf (stderr, "0x%08lx\n", regs.regs.eip); | ||
| 98 | } while (1); | ||
| 99 | |||
| 100 | if (ptrace (PTRACE_DETACH, fpid, NULL, NULL) < 0) { | ||
| 101 | perror ("ptrace PTRACE_DETACH"); | ||
| 102 | exit (EXIT_FAILURE); | ||
| 103 | } | ||
| 104 | |||
| 105 | fprintf (stderr, "MEMDUMP: success. terminating.\n"); | ||
| 106 | exit (EXIT_SUCCESS); | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | void | ||
| 112 | hexdump (unsigned char *data, unsigned int amount) | ||
| 113 | { | ||
| 114 | unsigned int dp, p; /* data pointer */ | ||
| 115 | const char trans[] = | ||
| 116 | "................................ !\"#$%&'()*+,-./0123456789" | ||
| 117 | ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm" | ||
| 118 | "nopqrstuvwxyz{|}~...................................." | ||
| 119 | "....................................................." | ||
| 120 | "........................................"; | ||
| 121 | |||
| 122 | for (dp = 1; dp <= amount; dp++) { | ||
| 123 | printf ("%02x ", data[dp-1]); | ||
| 124 | if ((dp % 8) == 0) | ||
| 125 | printf (" "); | ||
| 126 | if ((dp % 16) == 0) { | ||
| 127 | printf ("| "); | ||
| 128 | p = dp; | ||
| 129 | for (dp -= 16; dp < p; dp++) | ||
| 130 | printf ("%c", trans[data[dp]]); | ||
| 131 | printf ("\n"); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | if ((amount % 16) != 0) { | ||
| 135 | p = dp = 16 - (amount % 16); | ||
| 136 | for (dp = p; dp > 0; dp--) { | ||
| 137 | printf (" "); | ||
| 138 | if (((dp % 8) == 0) && (p != 8)) | ||
| 139 | printf (" "); | ||
| 140 | } | ||
| 141 | printf (" | "); | ||
| 142 | for (dp = (amount - (16 - p)); dp < amount; dp++) | ||
| 143 | printf ("%c", trans[data[dp]]); | ||
| 144 | } | ||
| 145 | printf ("\n"); | ||
| 146 | |||
| 147 | return; | ||
| 148 | } | ||
