-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathutils.sh
209 lines (181 loc) · 4.87 KB
/
utils.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
# Copyright (c) 2017 Rory McNamara
declare -A hex
for ((j=1; j < 256; j++)); do
# we can't set \0 or \x0a in an array, so skip those, and we can check for them later. all other bytes are ok
[[ $j -eq 10 ]] && continue
hex[$(echo -ne "\\x$(printf '%x' ${j})")]=${j}
done
tohex() {
# basically 'od', convert \x01\x02\x03 to 01 02 03
local i
for ((i=${1}; i > 0; i--)); do
# above maybe 7b? we accidentally read 2 bytes, so disable unicode
LC_ALL=C IFS= read -n 1 -d '' -r c
if [[ -z "$c" ]]; then
# null byte
echo -n "00 "
else
hexc=${hex[${c}]}
if [[ ! -z "${hexc}" ]]; then
printf '%02x ' "${hexc}"
else
printf '%02x ' 10
fi
fi
done
}
hexlify() {
x=$(printf "%016x" ${1})
echo -n "\\x${x:14:2}\\x${x:12:2}\\x${x:10:2}\\x${x:8:2}\\x${x:6:2}\\x${x:4:2}\\x${x:2:2}\\x${x:0:2}"
}
write() {
printf -- $1 >> $2
}
filesize() {
IFS=+ FILESIZE=($(dd if=${1} of=/dev/null bs=1 2>&1))
FILESIZE=${FILESIZE[0]}
IFS=
}
getbase() {
IFS=- read -a addrs <<< $1
echo ${addrs[0]}
}
findregion() {
IFS=$'\n ' MAPFILE=($(<${MAPS}))
# start at 4 because 0 wont be equal to the region name
# so we can just skip it
for ((i=4; i<${#MAPFILE[@]}; i++)); do
[[ ${MAPFILE[${i}]} = *${1}* && ${MAPFILE[$((${i}-4))]} ]] && break
done
BASE=$(getbase ${MAPFILE[$((${i}-5))]})
}
getlibs() {
IFS=$'\n' MAPFILE=($(<${MAPS}))
LIBS=()
for MAP in ${MAPFILE[@]}; do
IFS=' ' MAPLINE=(${MAP})
if [[ "${MAPLINE[1]}" = "r-xp" && "${MAPLINE[5]}" = /* ]]; then
LIBS+=("${MAPLINE[5]}")
fi
done
echo ${LIBS[@]}
}
getlibc() {
getlibs | IFS=' ' read -a LIBS
for ((i=0; i < ${#LIBS[@]}; i++)); do
if [[ "${LIBS[${i}]}" = */libc-* ]]; then
echo "${LIBS[${i}]}"
return
fi
done
}
declare -A gadgetcache
findgadget() {
if [[ ${gadgetcache[${1}]+FOUND} ]]; then
# cache hit
gadgetaddr=${gadgetcache[${1}]}
else
# cache miss
getlibs | read -a LIBS
# eval splits up the LIBS as args
# if we don't find one, try to find something in /usr/lib/* and LD_PRELOAD
matches=()
match=()
eval grep -Fao --byte-offset "$1" ${LIBS[@]} | grep -o "^[^:]*:[^:]*" | while IFS=$'\n' read match; do
IFS=: match=(${match})
getsect "${match[0]}" ".text" ${PROGBITS} | IFS=' ' read textaddr textsize
if [[ ${match[1]} -gt ${textaddr} && ${match[1]} -lt $((${textaddr}+${textsize})) ]]; then
break
fi
done
# 0 is file
# 1 is offset
if [[ -z "${match[0]}" || -z "${match[1]}" ]]; then
matches=()
match=()
eval grep -Fao --byte-offset "$1" /usr/lib/* 2>/dev/null | grep -o "^[^:]*:[^:]*" | while IFS=$'\n' read match; do
IFS=: match=(${match})
getsect "${match[0]}" ".text" ${PROGBITS} | IFS=' ' read textaddr textsize
if [[ ${match[1]} -gt ${textaddr} && ${match[1]} -lt $((${textaddr}+${textsize})) ]]; then
break
fi
done
if [[ -z "${match[0]}" ]]; then
exit 1
else
[[ "${PRELOAD[@]}" = *${match[0]}* ]] || PRELOAD+=("${match[0]}")
fi
return
fi
findregion ${match[0]}
gadgetaddr=$(hexlify $(($((16#${BASE}))+${match[1]})))
gadgetcache[${1}]=${gadgetaddr}
fi #cache miss
}
relocatelibc() {
findregion "libc-"
hexlify $(($((16#${BASE}))+$((16#${1}))))
}
fnargs() {
SYSCALLSIZE=0
SYSCALL=
findgadget "$(printf "\x58\xc3")" # pop rax ; ret
SYSCALL=${SYSCALL}${gadgetaddr}
# when we're called from pltcall, this is already a valid address string
# otherwise we need to encode it, because it's a syscall id
if [[ ${1} = \\x* ]]; then
SYSCALL=${SYSCALL}${1}
else
SYSCALL=${SYSCALL}$(hexlify ${1})
fi
shift
SYSCALLSIZE=$((${SYSCALLSIZE}+2))
[[ -z ${1} ]] && return
# this is from the x64 Linux ABI
GADGETS=(
"\x5f\xc3" # pop rdi; ret
"\x5e\xc3" # pop rsi; ret
"\x5a\xc3" # pop rdx; ret
"\x59\xc3" # pop rcx; ret
"\xff\xd0\xc3" # pop r8 ; ret
"\xff\xd1\xc3" # pop r9 ; ret
)
for gadget in ${GADGETS[@]}; do
findgadget "$(printf ${gadget})"
SYSCALL=${SYSCALL}${gadgetaddr}
SYSCALL=${SYSCALL}$(hexlify ${1})
shift
SYSCALLSIZE=$((${SYSCALLSIZE}+2))
[[ -z ${1} ]] && break
done
}
# execute a syscall in the ROPChain, with all arguments setup appropriately
syscall() {
fnargs $@
findgadget "$(printf "\x0f\x05\xc3")" # syscall ; ret
SYSCALL=${SYSCALL}${gadgetaddr}
SYSCALLSIZE=$((${SYSCALLSIZE}+1))
# global management
SLEDLEN=$((${SLEDLEN}-${SYSCALLSIZE}))
SYSCALLS+=(${SYSCALL})
}
# execute a libc call in the ROPChain, with all arguments setup appropriately
pltcall() {
[[ -z ${LIBC} ]] && getlibc | read LIBC
# if we're preparing, we don't want to search for a function which is SLOW
# we know libc functions will be present
if [[ "${PREPARE}" = "PREPARE" ]]; then
SYM=0
else
getsym ${LIBC} ${1}
SYM=$(relocatelibc ${symaddr})
fi
shift
fnargs ${SYM} ${@}
findgadget "$(printf "\xff\xe0")" # jmp rax
SYSCALL=${SYSCALL}${gadgetaddr}
SYSCALLSIZE=$((${SYSCALLSIZE}+1))
# global management
SLEDLEN=$((${SLEDLEN}-${SYSCALLSIZE}))
SYSCALLS+=(${SYSCALL})
}