forked from prey/prey-bash-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions
360 lines (298 loc) · 9.82 KB
/
functions
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
####################################################################
# Prey Core Basic Functions - by Tomas Pollak (bootlog.org)
# URL: http://preyproject.com
# License: GPLv3
####################################################################
log(){
eval echo -e '"$1"' "$log_output"
}
####################################################################
# xml parsing functions
####################################################################
get_key(){
echo "$1" | sed -e 's/.*\/\(.*\)>/\1/' -e 'y/-/_/' # we also replace -'s to _'s
}
get_value(){
echo "$1" | sed 's/.*>\([^<].*\)<.*/\1/'
}
# expects attr name, returns value
# example: get_attribute 'name' $line
get_attribute(){
echo "$2" | sed "s/.*$1=\"\([^\"]*\)\".*/\1/"
# echo "$2" | sed -e "s/.*$1=\([a-z_\"']*\).*/\1/" -e "s/[^a-z_]//g"
}
####################################################################
# setting configs
####################################################################
# key, value
set_config(){
if [ -n "$2" ]; then
# filter $() or backticks to prevent unwanted command execution
local clean_val=$(echo "$2" | sed "s/[\`|\$(].*[\`|)]//g")
eval "${1}=\"$clean_val\""
fi
}
# module, key, value
set_module_config(){
set_config "${1}__${2}" "$3"
}
save_config_value(){
local key="$1"
local val="$2"
if [ "$val" == "true" ]; then
local val='y'
elif [ "$val" == 'false' ]; then
local val='n'
fi
sed -i.backup -e "s/^$key=.*/$key='$val'/" "$config_file" 2> /dev/null
local rs=$?
if [ -f "$config_file" ]; then
rm -f "$config_file.backup" 2> /dev/null
else
mv "$config_file.backup" "$config_file" 2> /dev/null
fi
return $rs
}
####################################################################
# local var storage
####################################################################
# fetches a var and then assigns it as $value
# expects the name of hash and then the name of var
get_var(){
HASH="$1[*]"
local ${!HASH}
eval 'echo ${'"${2}"'}'
}
# if you need to fetch a specific trace or file
get_trace(){
get_var traces ${1}__$2
}
get_file(){
get_var files ${1}__$2
}
####################################################################
# trace functions
####################################################################
add_trace(){
local urlencoded=`urlencode "$2"`
local trace="${current_module}__$1=$urlencoded"
log " ++ Adding trace for $current_module: $1"
# we need to encode whitespaces, otherwise well get into trouble
traces[${#traces[*]}]="$trace"
}
separator='########################################################'
list_traces(){
# echo " -- ${#traces[*]} traces gathered!"
for t in "${traces[@]}"
do
if [ "$post_method" == 'http' ]; then
# echo -n $t | sed 's/^\([^_].*\)__\([^=].*\)=\(.*\)/\1[\2]="\3"\&/' # query string
# echo "-F $t" | sed 's/^\([^_].*\)__\([^=].*\)=\(.*\)/\1[\2]=\3/' # form field list
# echo $t | sed 's/^\([^_].*\)__\([^=].*\)=\(.*\)$/<\2>\3<\/\2>/' # xml
local start=`echo ${t%%=*}`
local index=`echo $(( ${#start} + 1 ))`
local trace_field=`echo "-F $start" | sed 's/^\([^_].*\)__\(.*\)/\1[\2]/'`
echo "${trace_field}=${t:index}"
else
local current_node=`echo $t | sed 's/__.*//'`
if [ "$current_node" != "$previous_node" ]; then
echo -e "$separator\n# $current_node\n$separator\n"
fi
# removes module name and replaces _'s with whitespaces
echo -e "$t\n" | sed -e 's/^\([^_].*\)__/ :: /' -e 's/%20/ /g' -e 's/_/ /'
local previous_node=$current_node
fi
done
}
remove_traces(){
unset -v traces
log " -- Dropping all traces!"
}
add_file(){
if [ -f "$2" ]; then
log " ++ Adding file for $current_module: $1 -> $2"
files[${#files[*]}]=${current_module}__$1=$2
else
log " ${red}!!${color_end} Couldn't find file at $2!"
fi
}
list_files(){
# log " -- ${#files[*]} files gathered!"
for f in "${files[@]}"
do
if [ "$post_method" == 'http' ]; then
# echo -e "-F $f" | sed -e 's/=/=@/'
echo "-F $f" | sed 's/^\([^_].*\)__\([^=].*\)=\(.*\)/\1[\2]=@\3/'
else # just list the file paths
echo $f | sed 's/^.*=\(.*\)/\1/'
fi
done
}
remove_files(){
for f in "${files[@]}"
do
file=`echo $f | sed 's/^.*=\(.*\)/\1/'`
rm -f "$file"
log " -- Removed $file"
done
unset -v files
}
####################################################################
# delay functions, mac and linux
####################################################################
get_current_delay(){
# crontab -l | grep prey | sed "s/^..\([0-9]*\).*/\1/"
crontab -l | grep prey | head -1 | sed 's/ \/.*//'
}
get_delay_for(){
local delay_var=$(($1*1))
if [ "$delay_var" == "$1" ]; then # integer, minute
echo '*/'$1' * * * *'
else
case "$1" in
"hourly")
echo '1 * * * *' # first minute of every hour
;;
"daily")
echo '1 10 * * *' # at 10:01 o'clock every day
;;
"weekly")
echo '1 10 * * 1' # at 10:01 o'clock every monday
;;
# "monthly")
# echo '1 10 1 * *' # at 10:01 o'clock every 1st of month
# ;;
esac
fi
}
update_execution_delay(){
local full_path=`full_path "$base_path"`
(crontab -l | grep -v prey; echo "${new_delay}" "${full_path}/prey.sh > /var/log/prey.log") | crontab -
}
####################################################################
# http functions
####################################################################
send_request(){
local curl_arguments="$2"
local headers_file="$tmpbase/prey-curl-headers.txt"
response_body=$(getter $curl_arguments -L $1 --dump-header "$headers_file")
response_headers=$(cat "$headers_file" && rm -f "$headers_file" 2> /dev/null)
get_status_code
}
get_status_code(){
# if we get a redirect response, we should capture the last http status code
response_status=$(echo -e "$response_headers" | grep 'HTTP/' | tail -1 | cut -d" " -f2)
}
# we may eventually use a specific header for Prey
get_header_value(){
echo "$response_headers" | grep "$1" | tail -1 | sed 's/.*: \([a-z\/-]*\).*/\1/'
}
# fetches last response if online actions were enabled for the device
get_last_response(){
response_body=$(cat "$last_response")
}
####################################################################
# network core functions
####################################################################
get_internal_ip() {
if [ -z "$internal_ip" ]; then
internal_ip=`/sbin/ifconfig 2> /dev/null | grep "inet" | grep -v "inet6" | grep -v "127.0.0.1" | awk '{print $2}' | sed "s/[^0-9\.]//g" | head -1`
fi
}
####################################################################
# check mode functions
####################################################################
verify_installation(){
log " -- Checking if cron daemon is running..."
if [ -n `is_process_running 'cron'` ]; then
log " -- Cron daemon found."
else
log " !! Cron daemon not found! Try running it with 'sudo /etc/init.d/cron start'."
fi
log " -- Checking for crontab entry..."
local result=`crontab -l | grep 'prey.sh' | wc -l 2> /dev/null`
if [ "$result" -gt 0 ]; then
log " -- Found!"
else
log " !! Not found!\n -> It seems Prey's crontab entry was removed after installation. Please set again the running interval."
fi
}
verify_smtp_settings(){
log " -- Target mailbox set."
else
log " !! Target mailbox not set!"
fi
log " -- SMTP username set."
else
log " -- SMTP password not set!"
fi
if [ "$(decrypt \"$smtp_password\")" != "" ]; then
log " -- Password seems to be fine as well."
else
log " -- Password not set! (Remember it should be base64 encrypted)."
fi
}
####################################################################
# self setup functions
####################################################################
self_setup(){
log ' -- Gathering PC info...'
get_pc_info
log ' -- Sending request to Control Panel...'
local params="device[title]=$pc_name&device[device_type]=$pc_type&device[os_version]=$pc_os_version&device[os]=$(capitalize $os)"
send_request "$check_url/devices.xml" "--connect-timeout 10 -u $api_key:x -d "$params""
if [ $response_status == "201" ]; then
log ' -- Device succesfully added! Applying configuration...'
device_key=`echo "$response_body" | grep "<key>" | sed 's/.*<key>\(.*\)<\/key>.*/\1/'`
save_config_value device_key "$device_key"
if [ $? == 1 ]; then
log " -- There was a problem saving the configuration. You probably don't have permissions to modify $base_path/config."
else
log " -- All set. Assigned key is $device_key."
fi
else
log " -- Couldn't add this device to your account. Make sure you have available slots!\n"
exit 1
fi
}
####################################################################
# reverse ssh tunnel
####################################################################
open_reverse_tunnel(){
local local_tunnel_port="$1"
if [ -z "$remote_tunnel_port" ]; then
log " -- Unknown remote port to connect to. Please try again later."
return 1
fi
if [ ! -f "$tmpbase/prey-tunnel-${local_tunnel_port}.pid" ]; then
log " -- Opening reverse tunnel from ${local_tunnel_port} to ${remote_tunnel_port} as ${remote_tunnel_user} on ${remote_tunnel_host}..."
reverse_tunnel_command
if [ -f "prey-tunnel.pid" ]; then
mv "prey-tunnel.pid" "$tmpbase/prey-tunnel-${local_tunnel_port}.pid"
log " -- Tunnel open and ready to serve!"
update_device_info_with "device[active_tunnel]=${remote_tunnel_host}:${remote_tunnel_port}"
return 0
else
log " -- Couldn't open reverse tunnel!"
return 1
fi
else
echo " -- Reverse tunnel already on!"
return 0
fi
}
close_reverse_tunnel(){
local tunnel_pidfile="$tmpbase/prey-tunnel-${1}.pid"
if [ -f "${tunnel_pidfile}" ]; then
local tunnel_pid=`cat "$tunnel_pidfile"`
log " -- Closing reverse tunnel on port ${1} with PID ${tunnel_pid}..."
kill -9 $tunnel_pid &> /dev/null
if [ $? == 1 ]; then
log " -- Seems like the reverse tunnel was already closed!"
fi
rm -f "$tunnel_pidfile"
fi
}