forked from hellman/fixenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r.sh
executable file
·275 lines (251 loc) · 6.98 KB
/
r.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
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
269
270
271
272
273
274
275
#!/bin/bash
#
# fixenv - A script to make stack addresses sane
# https://github.com/hellman/fixenv
# Copyright (C) 2014 hellman
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
if [ "$SC" == "" ]; then
export SC=$'\x6a\x31\x58\x99\xcd\x80\x89\xc3\x89\xc1\x6a\x46\x58\xcd\x80\xb0\x0b\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x89\xd1\xcd\x80'
fi
export PWD=`pwd`
IFS=" "
unsetenv=(`echo -n 'env -u SSH_CLIENT -u SSH_TTY -u USER -u MAIL -u SSH_CONNECTION -u LOGNAME -u HOME -u LANG -u _ -u TERM COLUMNS=157 LINES=32'`)
envlist=()
fname=""
# "CMD=sh" "ADDR=`perl -e 'print pack("V", 0x1672a0)'`"
# -----------------------------------------------------
# put envorder.c source
# -----------------------------------------------------
function put_envorder_source {
cat > .envorder.c <<EOF
#include <stdio.h>
#include <string.h>
extern char ** environ;
int main(int argc, char *argv[]) {
int i,j;
FILE * fd = fopen(".gdblist", "w");
for (i = 0; environ[i] != NULL; i++) {
unsigned char *q, *p;
p = strchr(environ[i],(int)'=');
q = environ[i];
while (q != p)
fprintf(fd, "\\\\x%02x", *q++);
q++;
fprintf(fd, "=");
while (*q)
fprintf(fd, "\\\\x%02x", *q++);
fprintf(fd, "\\\\x0a\n");
}
fclose(fd);
return 0;
}
EOF
}
# -----------------------------------------------------
# put getvar.c source
# -----------------------------------------------------
function put_getvar_source {
cat > .getvar.c <<EOF
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void printvar(char * p, char * varname) {
unsigned char *q;
q = (unsigned char *)&p;
printf("%p \\\\x%02x\\\\x%02x\\\\x%02x\\\\x%02x (%s)\\n", p, *q, *(q+1), *(q+2), *(q+3), varname);
}
int main(int argc, char *argv[], char *env[]) {
if (argc == 2) {
printvar((char*)getenv(argv[1]), argv[1]);
} else {
int i;
char *p;
for (i = 0; env[i] != NULL; i++) {
p = strchr(env[i], (int)'=');
if (p) {
*p++ = 0;
printvar(p, env[i]);
}
}
}
return 0;
}
EOF
}
# -----------------------------------------------------
# put dump.c source
# -----------------------------------------------------
function put_dump_source {
cat > .dump.c <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#define DEFAULT_SIZE 0x200
int main (int argc, char* argv[], char *env[]) {
unsigned char *p, *c, *start, *end;
start = (void *) ((unsigned int)argv & 0xfffff000);
end = start + 0x1000;
start = end - DEFAULT_SIZE;
if (argc >= 2)
sscanf(argv[1], "%p", &start);
if (argc >= 3)
end = start + abs(atoi(argv[2]));
fprintf(stderr, "From %p to %p\n", start, end);
for (p = start; p < end; p += 16) {
printf("%08x ", p);
for (c = p; c < p+16; c++)
printf("%s%02x", (((unsigned int)c & 0x1) ? "" : " "), *c);
printf(" ");
for (c = p; c < p+16; c++)
printf("%c", (isprint(*c) ? *c : '.'));
printf("\n");
}
return 0;
}
EOF
}
# -----------------------------------------------------
# prints env-variable(s) address in memory
# -----------------------------------------------------
function getvar {
if [ ! -f ".getvar.c" ]; then
put_getvar_source
rm -f .getvar
fi
if [ ! -f ".getvar" ]; then
gcc .getvar.c -o .getvar
fi
runprog "./.getvar" "${@:1}"
}
# -----------------------------------------------------
# prints env-variable(s) address in memory
# -----------------------------------------------------
function dump {
if [ ! -f ".dump.c" ]; then
put_dump_source
rm -f .dump
fi
if [ ! -f ".dump" ]; then
gcc .dump.c -o .dump
fi
runprog "./.dump" "${@:1}"
}
# -----------------------------------------------------
# gets env-vars, ordered as env-vars in gdb
# resulting array is in envlist (global)
# -----------------------------------------------------
function getenvs {
if [ ! -f ".envorder.c" ]; then
put_envorder_source
rm -f .envorder
fi
if [ ! -f ".envorder" ]; then
gcc .envorder.c -o .envorder
fi
IFS=""
"${unsetenv[@]}" gdb "`pwd`/.envorder" >/dev/null 2>&1 <<EOF
r
q
EOF
IFS=$'\x0a'
envs=$(cat .gdblist)
envlist=()
for env in $envs; do
IFS=""
envlist=("${envlist[@]}" "`printf "$env"`")
done
}
# -----------------------------------------------------
# runs arguments in gdb
# -----------------------------------------------------
function rungdb {
rm -f "`pwd`/.launcher"
checkprog "$1"
ln -s "$fname" ".launcher"
IFS=""
"${unsetenv[@]}" gdb --args "`pwd`/.launcher" "${@:2}"
}
# -----------------------------------------------------
# runs program directly
# env-vars are ordered like in gdb
# -----------------------------------------------------
function runprog {
rm -f "`pwd`/.launcher"
getenvs
checkprog "$1"
ln -s "$fname" ".launcher"
IFS=""
if [ "$preprog" ]; then
env -i "${envlist[@]}" "$preprog" "`pwd`/.launcher" "${@:2}"
else
env -i "${envlist[@]}" "`pwd`/.launcher" "${@:2}"
fi
}
# -----------------------------------------------------
# checks program for existance
# and returns fullpath
# -----------------------------------------------------
function checkprog {
fname="`which $1`" #full path
if [ "${1:0:2}" = "./" ]; then
fname="`readlink -f "${1:2}"`"
fi
if [ ! -e "$fname" ]; then
echo "Error: cant find program '$fname'"
exit
fi
}
# -----------------------------------------------------
# Main switch
# -----------------------------------------------------
IFS=""
case $1 in
gdb)
if [ $2 = "--args" ]; then
prog="$3"
args=("${@:3}")
else
prog="$2"
args=("${@:2}")
fi
rungdb "${args[@]}"
;;
strace|ltrace)
preprog="$1"
runprog "${@:2}"
;;
getvar|getadr)
getvar "${@:2}"
;;
dump)
dump "${@:2}"
;;
clean)
rm -f .getvar .getvar.c
rm -f .envorder .envorder.c
rm -f .dump.c .dump
rm -f .gdblist .launcher
;;
""|"-h"|"--help")
echo "Usage:"
echo " $0 getvar [var_name] - get address of envvar's value"
echo " $0 dump [start_addr [size]] - dump the end of the stack"
echo
echo "Running programs:"
echo " $0 ./program - run program"
echo " $0 strace ./program - run program in strace"
echo " $0 ltrace ./program - run program in ltrace"
echo " $0 gdb ./program [arg1 [arg2 [ ... ]]] - run program in gdb"
echo
;;
*)
runprog "${@:1}"
;;
esac
rm -f .launcher