forked from prey/prey-bash-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpull
110 lines (84 loc) · 3.86 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
#!/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./")
}
# we could eventually use a different method for status checking
check_device_status(){
if [ "$post_method" == 'http' ]; then
local request_headers="-H X-Encrypt-Response:aes-128-cbc"
if [ -n "$device_key" ]; then
local check_url="$check_url/devices/$device_key.xml"
else
error_exit "You need to enter your Device Key for querying the Control Panel!"
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 request_headers="$request_headers -H X-Local-IP:$internal_ip -H X-Gateway-IP:$gateway_ip"
fi
send_request "$check_url" "$request_headers"
check_encrypted_response
}
log_response_error(){
log " !! Got empty status code! This probably means there is a connection issue."
log " -- Please check your firewall settings, allowing Curl to connect to ${check_url} through port 80."
log " -- If you use a proxy server look for the curl_options setting in the config file."
log " -- The response we got was: \n\n${response_headers}\n${response_body}\n"
}
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)
[ $? == 1 ] && error_exit "Unable to decrypt response correctly! Exiting..."
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
}