summaryrefslogtreecommitdiff
path: root/other/burneye/src/debug/memdump.c.old
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/burneye/src/debug/memdump.c.old
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/burneye/src/debug/memdump.c.old')
-rw-r--r--other/burneye/src/debug/memdump.c.old164
1 files changed, 164 insertions, 0 deletions
diff --git a/other/burneye/src/debug/memdump.c.old b/other/burneye/src/debug/memdump.c.old
new file mode 100644
index 0000000..eda7963
--- /dev/null
+++ b/other/burneye/src/debug/memdump.c.old
@@ -0,0 +1,164 @@
1
2#include <sys/types.h>
3#include <sys/ptrace.h>
4#include <sys/wait.h>
5#include <sys/user.h>
6#include <errno.h>
7#include <unistd.h>
8#include <stdlib.h>
9#include <stdio.h>
10
11void
12hexdump (unsigned char *data, unsigned int amount);
13
14int
15main (int argc, char *argv[])
16{
17 pid_t fpid;
18 struct user regs;
19 unsigned long int addr,
20 len;
21 unsigned long int addr_walker,
22 eip;
23 unsigned char data_saved[256];
24
25 if (argc < 5 || sscanf (argv[1], "0x%lx", &eip) != 1 ||
26 sscanf (argv[2], "0x%lx", &addr) != 1 ||
27 sscanf (argv[3], "0x%lx", &len) != 1)
28 {
29 printf ("usage: %s <eip> <addr> <len> <argv0 [argv1 [...]]>\n\n", argv[0]);
30 printf ("will run 'argv0' as program with given arguments, until 'eip' is reached, then\n"
31 "dumping 'len' bytes from 'addr'.\n\n"
32 "example: %s 0x08049014 0x08048000 0x100 /bin/ls -l\n\n", argv[0]);
33 exit (EXIT_FAILURE);
34 }
35
36 fpid = fork ();
37 if (fpid < 0) {
38 perror ("fork");
39 exit (EXIT_FAILURE);
40 }
41 if (fpid == 0) { /* child */
42 if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) != 0) {
43 perror ("ptrace PTRACE_TRACEME");
44 exit (EXIT_FAILURE);
45 }
46 fprintf (stderr, " child: TRACEME set\n");
47
48 fprintf (stderr, " child: executing: %s\n", argv[4]);
49 close (1);
50 dup2 (2, 1);
51 execve (argv[4], &argv[4], NULL);
52
53 /* failed ? */
54 perror ("execve");
55 exit (EXIT_FAILURE);
56 }
57
58#if 0
59 if (ptrace (PTRACE_ATTACH, fpid, NULL, NULL) < 0) {
60 perror ("ptrace");
61 exit (EXIT_FAILURE);
62 }
63 fprintf (stderr, "attached to pid %d (from us, the parent, pid %d)\n", fpid, getpid ());
64#endif
65// sleep (1);
66
67#if 0
68 /* trace until trap'ed */
69 if (ptrace (PTRACE_CONT, fpid, NULL, NULL) < 0) {
70 perror ("ptrace PTRACE_CONT");
71 exit (EXIT_FAILURE);
72 }
73#endif
74 fprintf (stderr, "it should execve now\n");
75 sleep (1);
76
77 memset (&regs, 0, sizeof (regs));
78
79 if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
80 perror ("ptrace PTRACE_GETREGS");
81 exit (EXIT_FAILURE);
82 }
83 fprintf (stderr, "[0x%08lx] first stop\n", regs.regs.eip);
84
85 /* now single step until given eip is reached */
86 do {
87 if (ptrace (PTRACE_SINGLESTEP, fpid, NULL, NULL) < 0) {
88 perror ("ptrace PTRACE_SINGLESTEP");
89 exit (EXIT_FAILURE);
90 }
91 wait (NULL);
92
93 memset (&regs, 0, sizeof (regs));
94 if (ptrace (PTRACE_GETREGS, fpid, NULL, &regs) < 0) {
95 perror ("ptrace PTRACE_GETREGS");
96 exit (EXIT_FAILURE);
97 }
98 } while (regs.regs.eip != eip);
99
100 fprintf (stderr, "hook traped @ 0x%08lx\n", eip);
101 fprintf (stderr, "dumping 0x%lx bytes @ 0x%08lx\n", len, addr);
102
103 /* save data */
104 for (addr_walker = 0 ; addr_walker < len ; ++addr_walker) {
105 errno = 0;
106 *((unsigned long int *) &data_saved[0]) = ptrace (PTRACE_PEEKDATA, fpid,
107 addr + addr_walker, NULL);
108 if (errno == 0)
109 write (1, &data_saved[0], 1);
110 else
111 fprintf (stderr, "- [0x%08lx] invalid PTRACE_PEEKDATA\n",
112 addr + addr_walker), _exit(1);
113 }
114// hexdump (data_saved, sizeof (data_saved));
115
116 if (ptrace (PTRACE_DETACH, fpid, NULL, NULL) < 0) {
117 perror ("ptrace PTRACE_DETACH");
118 exit (EXIT_FAILURE);
119 }
120
121 fprintf (stderr, "success. terminating.\n");
122 exit (EXIT_SUCCESS);
123}
124
125
126
127void
128hexdump (unsigned char *data, unsigned int amount)
129{
130 unsigned int dp, p; /* data pointer */
131 const char trans[] =
132 "................................ !\"#$%&'()*+,-./0123456789"
133 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
134 "nopqrstuvwxyz{|}~...................................."
135 "....................................................."
136 "........................................";
137
138 for (dp = 1; dp <= amount; dp++) {
139 printf ("%02x ", data[dp-1]);
140 if ((dp % 8) == 0)
141 printf (" ");
142 if ((dp % 16) == 0) {
143 printf ("| ");
144 p = dp;
145 for (dp -= 16; dp < p; dp++)
146 printf ("%c", trans[data[dp]]);
147 printf ("\n");
148 }
149 }
150 if ((amount % 16) != 0) {
151 p = dp = 16 - (amount % 16);
152 for (dp = p; dp > 0; dp--) {
153 printf (" ");
154 if (((dp % 8) == 0) && (p != 8))
155 printf (" ");
156 }
157 printf (" | ");
158 for (dp = (amount - (16 - p)); dp < amount; dp++)
159 printf ("%c", trans[data[dp]]);
160 }
161 printf ("\n");
162
163 return;
164}