-
Notifications
You must be signed in to change notification settings - Fork 0
/
memgrep
executable file
·86 lines (64 loc) · 1.99 KB
/
memgrep
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
#!/bin/sh
# $1 - PID of process to grep memory of
target_pid="$1"
# the rest of arguments is passed to grep; but we need to remove the first one
shift
# extract search regex; this is passed to grep.
search="$1"
shift
# First let's change to a temporary dir...
# Set $TMPDIR to "/tmp" only if it didn't have a value previously
: ${TMPDIR:=/tmp}
# We can find out about $TMPDIR at:
# http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
# Create a private temporary directory inside $TMPDIR
# Remove the temporary directory when the script finishes
unset temporary_dir
trap '[ -n "$temporary_dir" ] && rm -rf "$temporary_dir"' EXIT
save_mask="$(umask)"
umask 077
temporary_dir="$(mktemp -d "$TMPDIR/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")" \
|| { echo 'Error creating a temporary directory' >&2; exit 1; }
umask "$save_mask"
cd "$temporary_dir" \
|| { echo 'Error changing to temporary directory' >&2; exit 1; }
filename='memdump'
# Now let's iterate over mapped memory regions and grep them.
first_line=1
while read map; do
# the EOF block below may not be indented...
IFS=' -' read -r start stop _ <<EOF
$map
EOF
rm -f "$filename"
gdb \
</dev/null >/dev/null -nx -batch \
-ex 'set pagination off' -ex 'set height 0' -ex 'set width 0' \
-ex "attach $target_pid" -ex "dump memory $filename 0x$start 0x$stop" \
-ex 'detach' -ex 'quit'
if ! [ -f "$filename" ]; then
echo "Error: gdb cannot create memory dump."
exit 1
fi
if result="$(grep "$search" \
--binary \
--byte-offset \
--no-filename \
--only-matching \
--binary-files=text \
"$@" \
"$filename"
)"; then
if [ "$first_line" -gt 0 ]; then
first_line=0
else
echo
fi
echo "$map:"
echo "$result" | cat -v | while read line; do
if ! { echo "$line" | grep "$search" --color=auto; }; then
echo "$line"
fi
done
fi
done < /proc/"$target_pid"/maps