This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
pull
130 lines (101 loc) · 4.22 KB
/
pull
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
#!/bin/bash
####################################################################
# Prey Core Pull Functions - by Tomas Pollak (bootlog.org)
# URL: http://preyproject.com
# License: GPLv3
####################################################################
# returns if we have have network access, otherwise 0
# you can check against the gateway or the net, change it if you like
check_net_status(){
# get_gateway_ip
# net_check_target=$gateway_ip
log ' -- Looking for connection...'
local net_check_target="www.google.com"
if [ "$os" == "windows" ]; then
local ping_params='-n'
else
local ping_params='-q -c'
fi
connected=`ping $ping_params 1 $net_check_target &> /dev/null && echo 1 || echo 0`
# if there's no connection with ping, lets try to see if curl gets through
# since some routers/networks block ICMP requests
if [ $connected == 0 ]; then
log ' -- Trying alternate method...'
connected=`getter --connect-timeout 3 $net_check_target &> /dev/null && echo 1 || echo 0`
fi
}
randomize_subdomain(){
check_url=`echo $check_url | sed "s/\/\//\/\/$RANDOM./"`
}
# fetches last response if online actions were enabled for the device
get_last_response(){
response_body=$(cat "$last_response")
}
# we could eventually use a different method for status checking
check_device_status(){
if [ "$post_method" == 'http' ]; then
local curl_headers="-H X-Encrypt-Response:aes-128-cbc"
if [ -n "$device_key" ]; then
local check_url="$check_url/devices/$device_key.xml"
else
log " !! You need to enter your Device Key for querying the Control Panel!\n"
exit 1
fi
[ "$randomize_check_host" == 'y' ] && randomize_subdomain
fi
if [ "$extended_headers" == 'y' ]; then
log ' -- Requesting with extra sauce!'
get_internal_ip
get_gateway_ip
local curl_headers="$curl_headers -H X-Local-IP:$internal_ip -H X-Gateway-IP:$gateway_ip"
fi
send_request "$check_url" "$curl_headers"
check_encrypted_response
}
send_request(){
local curl_options="$2"
local headers_file="$tmpbase/prey-curl-headers.txt"
response_body=$(getter $curl_options -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/'
}
check_encrypted_response(){
[[ "$response_status" != "200" && "$response_status" != "404" ]] && return 1
local content_type=$(get_header_value 'Content-Type')
if [[ -n "`find_in "$content_type" 'xml'`" && -z "`find_in "$response_body" '<config'`" ]]; then
log " ++ Got encrypted response! Processing... "
local cipher_key=$(md5_hash_for ${api_key})
response_body=$(echo "$response_body" | openssl aes-128-cbc -d -a -k "$cipher_key" 2> /dev/null)
if [ $? == 1 ]; then
log " -- Unable to decrypt response correctly! Exiting..."
exit 1
fi
fi
}
verify_keys(){
send_request "$control_panel_url/devices.xml" "-u $api_key:x"
if [[ "$response_status" == "200" || "$response_status" == "404" ]]; then
log ' ** API key is valid. Your user account is correctly set up.'
local device_state=$(echo "$response_body" | grep $device_key -A1 | tail -1 | sed 's/<[^>]*>//g;s/ //g')
if [ -n "$device_state" ]; then
log " ** Device key is valid. Good. Current status is ${bold}${device_state}${bold_end}.\n"
return 0
else
log " !! Device key not valid!\n -> We couldn't find this device on your Control Panel account. Please reconfigure your settings, or reinstall if necessary.\n"
fi
elif [ "$response_status" == "401" ]; then
log " !! API key not valid! Got $response_status status code.\n -> Remember to check your inbox to verify your account. If you already have you should reconfigure your settings, or reinstall if necessary.\n"
else
log " !! Got unexpected response status: ${response_status}.\n"
[ -z "$response_headers" ] && log "It seems your PC is unable to reach the Control Panel. Please check your firewall settings so that Curl can connect to $control_panel_url over port 80 (HTTP).\n"
fi
return 1
}