-
Notifications
You must be signed in to change notification settings - Fork 0
/
pad.c
268 lines (217 loc) · 5.21 KB
/
pad.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
** pad.c -- Process Address space Dump
**
** Peek in the address space of a running process.
**
** Usage: pad pid [hexaddress [numbytes]]
** ==================================================================
** Author: Gerlof Langeveld (2018)
** Copyright (C) 2018 AT Computing BV
** ==================================================================
** This file is free software. You can redistribute it and/or modify
** it under the terms of the GNU General Public License (GPL); either
** version 3, or (at your option) any later version.
*/
#define __FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#define BYTESPERLINE 16
#define MAXAR 1024
struct arange {
void *start;
long long length;
char name[128];
char perm[16];
} ar[MAXAR];
char *usage = "Usage: pad pid [hexaddress [numbytes]]\n";
char dumpall = 1;
static void dumparea(int, long long, long long, int);
static void dumpline(long long, unsigned char *, int);
static void detachproc(int);
static int getaddranges(long, struct arange [], int);
int
main(int argc, char *argv[])
{
char fname[1000], line[128], *p;
unsigned char *buf;
int memfd;
long long pid, address, length;
int i, n;
// argument verification
//
if (argc < 2 || argc > 4) {
fprintf(stderr, usage);
exit(1);
}
// argument conversion
//
pid = strtoll(argv[1], &p, 10);
if (*p) {
fprintf(stderr, usage);
fprintf(stderr, "invalid pid value\n");
exit(1);
}
if (argc > 2) {
dumpall = 0;
address = strtoll(argv[2], &p, 16);
if (*p) {
fprintf(stderr, usage);
fprintf(stderr, "invalid address value\n");
exit(1);
}
if (argc == 4) {
length = strtoll(argv[3], &p, 10);
if (*p) {
fprintf(stderr, usage);
fprintf(stderr, "invalid length value\n");
exit(1);
}
} else {
length = 16;
}
}
// attach target process: required to be able to read memory
//
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
perror("Attach to specified pid");
exit(1);
}
(void) wait(NULL);
// open memory of target process
//
snprintf(fname, sizeof fname, "/proc/%d/mem", pid);
if ( (memfd = open(fname, O_RDONLY)) == -1) {
perror("Open memory of process");
detachproc(pid);
exit(1);
}
if (dumpall) {
int nar;
// determine all address ranges of this process
//
if ( (nar = getaddranges(pid, ar, MAXAR)) == -1) {
exit(1);
}
// dump address ranges one-by-one
//
for (i=0; i < nar; i++) {
printf("------------ perms=%s vsize=%lldKiB %s\n",
ar[i].perm, ar[i].length/1024, ar[i].name);
dumparea(memfd, pid,
(long long)ar[i].start, ar[i].length);
printf("\n");
}
} else {
dumparea(memfd, pid, address, length);
}
(void) close(memfd);
// detach process
//
detachproc(pid);
}
/*
** read memory area and dump as a number of lines
*/
static void
dumparea(int memfd, long long pid, long long addr, int len)
{
long i;
unsigned char *buf;
// seek to requested address and read bunch of bytes
//
if ( (buf = malloc(len)) == 0) {
perror("Can't allocate read buffer");
detachproc(pid);
exit(1);
}
if ( lseek(memfd, addr, SEEK_SET) == -1) {
perror("Seek to memory address");
detachproc(pid);
exit(1);
}
if ( (len = read(memfd, buf, len)) == -1) {
perror("Read memory address");
detachproc(pid);
exit(1);
}
// print hex output line-by-line
//
for (i=0; i < len; i+=BYTESPERLINE, addr+=BYTESPERLINE) {
dumpline(addr, &buf[i],
len-i>BYTESPERLINE ? BYTESPERLINE:len-i);
}
free(buf);
}
/*
** print one line of hexadecimal and character output
*/
static void
dumpline(long long addr, unsigned char *buf, int len)
{
char hexpart[BYTESPERLINE*3+1], chars[BYTESPERLINE+1];
int i;
// print address at beginning of line
//
printf("%012llx ", addr);
// format every byte in hexadecimal and character representation
//
for (i=0; i < len; i++) {
sprintf(hexpart+i*3, "%02x ", buf[i]);
if (isprint(buf[i]))
chars[i] = buf[i]; // printable
else
chars[i] = '.'; // non-printable
}
chars[i] = '\0';
// print hexadecimal and character representation
//
printf("%-*s %s\n", BYTESPERLINE*3, hexpart, chars);
}
/*
** detach target process from current process
*/
static void
detachproc(int pid)
{
(void) ptrace(PTRACE_CONT, pid, NULL, NULL);
(void) ptrace(PTRACE_DETACH, pid, NULL, NULL);
}
/*
** get start address and length of every virtual memory area
** in process' address space
*/
static int
getaddranges(long pid, struct arange ar[], int maxar)
{
FILE *fp;
char path[128], line[256];
int i = 0;
void *end;
snprintf(path, sizeof path, "/proc/%d/smaps", pid);
if ( (fp = fopen(path, "r")) == NULL) {
perror("Open smaps");
return -1;
}
while ( fgets(line, sizeof line, fp) )
{
if (i >= maxar)
break;
if ( sscanf(line, "%p-%p", &(ar[i].start), &end) < 2)
continue;
ar[i].name[0] = 0;
sscanf(line, "%*s %15s %*s %*s %*s %127s",
ar[i].perm, ar[i].name);
ar[i].length = end - ar[i].start;
i++;
}
fclose(fp);
return i;
}