-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathipcrypt.php
More file actions
204 lines (152 loc) · 4.31 KB
/
ipcrypt.php
File metadata and controls
204 lines (152 loc) · 4.31 KB
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
<?php
define('ZERO_IV', "\x0\x0\x0\x0\x0\x0\x0\x0");
function get_ipcrypt_key($ipcrypt)
{
if (!function_exists('openssl_encrypt'))
exit("Could not find the the openssl PHP extension.");
if (!isset($ipcrypt_key))
{
$ipcrypt_key = '';
if (is_file($ipcrypt))
$ipcrypt_key = file_get_contents($ipcrypt);
if (strlen($ipcrypt_key) == 0)
{
$ipcrypt_key = openssl_random_pseudo_bytes(56);
if (!file_put_contents($ipcrypt, $ipcrypt_key))
{
exit("Can't write generated ipcrypt key");
}
}
}
}
function webcp_encrypt_ip($ip)
{
global $ipcrypt;
global $ipcrypt_key;
if (empty($ipcrypt))
return $ip;
$ipcrypt_key = get_ipcrypt_key($ipcrypt);
$ip_dec = @inet_pton($ip);
if (is_numeric($ip))
$ipbytes = pack('N', $ip);
else if ($ip_dec !== false)
$ipbytes = $ip_dec;
else
return "BADIP";
if (strlen($ipbytes) >= 8)
$ipbytes = substr($ipbytes, 0, 8);
$cyphertext = openssl_encrypt($ipbytes, 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
return 'ip_' . rtrim(base64_encode($cyphertext), '=');
}
function webcp_encrypt_hdid($hdid)
{
global $ipcrypt;
global $ipcrypt_key;
if (empty($ipcrypt))
return $hdid;
$ipcrypt_key = get_ipcrypt_key($ipcrypt);
$hdid_parts = explode('-', $hdid);
if (isset($hdid_parts[1]))
{
$hdid_dec = intval(hexdec($hdid_parts[0]) * 0x10000 + hexdec($hdid_parts[1]));
if ($hdid_dec > 0x7FFFFFFF)
$hdid_dec = -0x100000000 + $hdid_dec;
}
else
{
$hdid_dec = false;
}
if (is_numeric($hdid))
$ipbytes = pack('N', $hdid);
else if ($hdid_dec !== false)
$ipbytes = pack('N', $hdid_dec);
else
return "BADHDID";
if (strlen($ipbytes) >= 4)
$ipbytes = substr($ipbytes, 0, 4);
$cyphertext = openssl_encrypt($ipbytes, 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
return 'hd_' . rtrim(base64_encode($cyphertext), '=');
}
function webcp_encrypt_computer($computer)
{
global $ipcrypt;
global $ipcrypt_key;
if (empty($ipcrypt))
return $computer;
$ipcrypt_key = get_ipcrypt_key($ipcrypt);
$ipbytes = $computer;
if (strlen($ipbytes) >= 15)
$ipbytes = substr($ipbytes, 0, 15);
else
$ipbytes = $ipbytes . str_repeat(' ', 15 - strlen($ipbytes));
$cyphertext = openssl_encrypt($ipbytes, 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
return 'pc_' . rtrim(base64_encode($cyphertext), '=');
}
function webcp_decrypt_ip($ip)
{
global $ipcrypt;
global $ipcrypt_key;
if (empty($ipcrypt))
return $ip;
$ipcrypt_key = get_ipcrypt_key($ipcrypt);
if (substr($ip, 0, 3) == 'ip_')
{
if (strlen($ip) == 14 && strlen(base64_decode(substr($ip, 3))) == 8)
{
$plaintext = openssl_decrypt(base64_decode(substr($ip, 3)), 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
if ($plaintext === false || strlen($plaintext) == 0)
return 'IPBAD';
$iparray = long2ip(unpack('N', $plaintext));
return $iparray[1];
}
else if (strlen($ip) == 25 && strlen(base64_decode(substr($ip, 3))) == 16)
{
$plaintext = openssl_decrypt(base64_decode(substr($ip, 3)), 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
if ($plaintext === false || strlen($plaintext) == 0)
return 'IPBAD';
return inet_ntop($plaintext . "\x0\x0\x0\x0\x0\x0\x0\x0");
}
}
return 'IPBAD';
}
function webcp_decrypt_hdid($ip)
{
global $ipcrypt;
global $ipcrypt_key;
if (empty($ipcrypt))
return $ip;
$ipcrypt_key = get_ipcrypt_key($ipcrypt);
if (substr($ip, 0, 3) == 'hd_')
{
if (strlen($ip) == 14 && strlen(base64_decode(substr($ip, 3))) == 8)
{
$plaintext = openssl_decrypt(base64_decode(substr($ip, 3)), 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
if ($plaintext === false || strlen($plaintext) == 0)
return 'HDIDBAD';
$resarray = unpack('N', $plaintext);
$result = sprintf("%08x", $resarray[1]);
$result = strtoupper(substr($result,0,4).'-'.substr($result,4,4));
return $result;
}
}
return 'HDIDBAD';
}
function webcp_decrypt_computer($ip)
{
global $ipcrypt;
global $ipcrypt_key;
if (empty($ipcrypt))
return $ip;
$ipcrypt_key = get_ipcrypt_key($ipcrypt);
if (substr($ip, 0, 3) == 'pc_')
{
if (strlen($ip) == 25 && strlen(base64_decode(substr($ip, 3))) == 16)
{
$plaintext = openssl_decrypt(base64_decode(substr($ip, 3)), 'blowfish', $ipcrypt, OPENSSL_RAW_DATA, ZERO_IV);
if ($plaintext === false || strlen($plaintext) == 0)
return 'COMPUTERBAD';
return rtrim($plaintext);
}
}
return 'COMPUTERBAD';
}