Commit | Line | Data |
---|---|---|
a91317a6 | 1 | <?php |
1d934afe S |
2 | /** |
3 | * @author Jan Altensen (Stricted) | |
4 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
5 | * @copyright 2015 Jan Altensen (Stricted) | |
6 | */ | |
21f877e4 | 7 | class SpeedportHybrid { |
a91317a6 S |
8 | /** |
9 | * password-challenge | |
10 | * @var string | |
11 | */ | |
12 | private $challenge = ''; | |
13 | ||
809e25bd S |
14 | /** |
15 | * csrf_token | |
16 | * @var string | |
17 | */ | |
18 | private $token = ''; | |
19 | ||
a91317a6 S |
20 | /** |
21 | * hashed password | |
22 | * @var string | |
23 | */ | |
24 | private $hash = ''; | |
25 | ||
26 | /** | |
27 | * session cookie | |
28 | * @var string | |
29 | */ | |
30 | private $session = ''; | |
31 | ||
32 | /** | |
33 | * router url | |
34 | * @var string | |
35 | */ | |
b5a532a8 | 36 | private $url = ''; |
a91317a6 | 37 | |
c9e082da S |
38 | /** |
39 | * derivedk cookie | |
40 | * @var string | |
41 | */ | |
42 | private $derivedk = ''; | |
43 | ||
b5a532a8 S |
44 | public function __construct ($password, $url = 'http://speedport.ip/') { |
45 | $this->url = $url; | |
a91317a6 S |
46 | $this->getChallenge(); |
47 | ||
48 | if (empty($this->challenge)) { | |
49 | throw new Exception('unable to get the challenge from the router'); | |
50 | } | |
51 | ||
52 | $login = $this->login($password); | |
53 | ||
54 | if ($login === false) { | |
55 | throw new Exception('unable to login'); | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * Requests the password-challenge from the router. | |
61 | */ | |
62 | public function getChallenge () { | |
b5a532a8 | 63 | $path = 'data/Login.json'; |
a91317a6 | 64 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null'); |
b5a532a8 | 65 | $data = $this->sentRequest($path, $fields); |
a91317a6 | 66 | $data = json_decode($data['body'], true); |
c2678616 S |
67 | $data = $this->getValues($data); |
68 | ||
69 | if (isset($data['challengev']) && !empty($data['challengev'])) { | |
a1ef7d4f | 70 | $this->challenge = $data['challengev']; |
a91317a6 S |
71 | } |
72 | } | |
73 | ||
74 | /** | |
75 | * login into the router with the given password | |
5e44cffa | 76 | * |
a91317a6 S |
77 | * @param string $password |
78 | * @return boolean | |
79 | */ | |
80 | public function login ($password) { | |
b5a532a8 | 81 | $path = 'data/Login.json'; |
a91317a6 S |
82 | $this->hash = hash('sha256', $this->challenge.':'.$password); |
83 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash); | |
b5a532a8 | 84 | $data = $this->sentRequest($path, $fields); |
a91317a6 | 85 | $json = json_decode($data['body'], true); |
c2678616 S |
86 | $json = $this->getValues($json); |
87 | if (isset($json['login']) && $json['login'] == 'success') { | |
a91317a6 S |
88 | if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) { |
89 | preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match); | |
90 | if (isset($match[1]) && !empty($match[1])) { | |
91 | $this->session = $match[1]; | |
92 | } | |
93 | else { | |
94 | throw new Exception('unable to get the session cookie from the router'); | |
95 | } | |
96 | ||
c9e082da | 97 | // calculate derivedk |
aebf6b76 | 98 | if (!function_exists("hash_pbkdf2")) { |
c0c3776a | 99 | require_once 'CryptLib/CryptLib.php'; |
aebf6b76 S |
100 | $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1')); |
101 | $this->derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32)); | |
102 | $this->derivedk = substr($this->derivedk, 0, 32); | |
c0c3776a S |
103 | } |
104 | else { | |
105 | $this->derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32); | |
106 | } | |
c9e082da | 107 | |
809e25bd S |
108 | // get the csrf_token |
109 | $this->token = $this->getToken(); | |
110 | ||
df1e1394 S |
111 | if ($this->checkLogin() === true) { |
112 | return true; | |
113 | } | |
a91317a6 S |
114 | } |
115 | } | |
116 | ||
117 | return false; | |
118 | } | |
119 | ||
df1e1394 S |
120 | /** |
121 | * check if we are logged in | |
122 | * | |
123 | * @return boolean | |
124 | */ | |
125 | public function checkLogin () { | |
126 | $path = 'data/'.$file.'.json'; | |
127 | $fields = array(); | |
128 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
129 | $data = $this->sentRequest($path, $fields, $cookie); | |
130 | ||
131 | if (empty($data['body'])) { | |
132 | throw new Exception('unable to get '.$file.' data'); | |
133 | } | |
134 | ||
135 | $json = json_decode($data['body'], true); | |
136 | $json = $this->getValues($json); | |
137 | ||
138 | if ($json['loginstate'] != 1) { | |
139 | return false; | |
140 | } | |
141 | ||
142 | return true; | |
143 | } | |
144 | ||
a91317a6 S |
145 | /** |
146 | * logout | |
5e44cffa | 147 | * |
a91317a6 S |
148 | * @return array |
149 | */ | |
150 | public function logout () { | |
b5a532a8 | 151 | $path = 'data/Login.json'; |
df1e1394 S |
152 | $fields = array('csrf_token' => $this->token, 'logout' => 'byby'); |
153 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
154 | $data = $this->sentRequest($path, $fields, $cookie); | |
155 | if ($this->checkLogin() === false) { | |
156 | // reset challenge and session | |
157 | $this->challenge = ''; | |
158 | $this->session = ''; | |
159 | $this->token = ""; | |
160 | ||
161 | $json = json_decode($data['body'], true); | |
162 | ||
163 | return $json; | |
164 | } | |
165 | else { | |
166 | throw new Exception('logout failed'); | |
167 | } | |
a91317a6 S |
168 | } |
169 | ||
170 | /** | |
171 | * reboot the router | |
5e44cffa | 172 | * |
a91317a6 S |
173 | * @return array |
174 | */ | |
175 | public function reboot () { | |
b5a532a8 | 176 | $path = 'data/Reboot.json'; |
a91317a6 S |
177 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reboot_device' => 'true'); |
178 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 179 | $data = $this->sentRequest($path, $fields, $cookie); |
14d4f286 | 180 | |
a91317a6 S |
181 | $json = json_decode($data['body'], true); |
182 | ||
183 | return $json; | |
184 | } | |
185 | ||
87026df3 S |
186 | /** |
187 | * change dsl connection status | |
5e44cffa | 188 | * |
87026df3 S |
189 | * @param string $status |
190 | */ | |
191 | public function changeConnectionStatus ($status) { | |
b5a532a8 | 192 | $path = 'data/Connect.json'; |
87026df3 S |
193 | |
194 | if ($status == 'online' || $status == 'offline') { | |
195 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'req_connect' => $status); | |
196 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
c2678616 S |
197 | $data = $this->sentRequest($path, $fields, $cookie); |
198 | ||
199 | $json = json_decode($this->decrypt($data['body']), true); | |
200 | ||
201 | return $json; | |
87026df3 S |
202 | } |
203 | else { | |
204 | throw new Exception(); | |
205 | } | |
206 | } | |
207 | ||
a91317a6 S |
208 | /** |
209 | * return the given json as array | |
5e44cffa | 210 | * |
a91317a6 S |
211 | * @param string $file |
212 | * @return array | |
213 | */ | |
214 | public function getData ($file) { | |
b5a532a8 | 215 | $path = 'data/'.$file.'.json'; |
a91317a6 S |
216 | $fields = array(); |
217 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 218 | $data = $this->sentRequest($path, $fields, $cookie); |
a91317a6 S |
219 | |
220 | if (empty($data['body'])) { | |
221 | throw new Exception('unable to get '.$file.' data'); | |
222 | } | |
223 | ||
224 | $json = json_decode($data['body'], true); | |
225 | ||
226 | return $json; | |
227 | } | |
228 | ||
5e44cffa S |
229 | /** |
230 | * get the router syslog | |
231 | * | |
232 | * @return array | |
233 | */ | |
234 | public function getSyslog() { | |
235 | $path = 'data/Syslog.json'; | |
236 | $fields = array('exporttype' => '0'); | |
237 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
238 | $data = $this->sentRequest($path, $fields, $cookie); | |
239 | ||
240 | if (empty($data['body'])) { | |
241 | throw new Exception('unable to get syslog data'); | |
242 | } | |
243 | ||
244 | return explode("\n", $data['body']); | |
245 | } | |
246 | ||
a35e3e67 S |
247 | /** |
248 | * get the Missed Calls from router | |
249 | * | |
250 | * @return array | |
251 | */ | |
252 | public function getMissedCalls() { | |
abf641bb | 253 | $path = 'data/ExportMissedCalls.json'; |
a35e3e67 S |
254 | $fields = array('exporttype' => '1'); |
255 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
256 | $data = $this->sentRequest($path, $fields, $cookie); | |
257 | ||
258 | if (empty($data['body'])) { | |
259 | throw new Exception('unable to get syslog data'); | |
260 | } | |
261 | ||
262 | return explode("\n", $data['body']); | |
263 | } | |
264 | ||
15260eeb S |
265 | /** |
266 | * get the Taken Calls from router | |
267 | * | |
268 | * @return array | |
269 | */ | |
270 | public function getTakenCalls() { | |
abf641bb | 271 | $path = 'data/ExportTakenCalls.json'; |
15260eeb S |
272 | $fields = array('exporttype' => '2'); |
273 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
274 | $data = $this->sentRequest($path, $fields, $cookie); | |
275 | ||
276 | if (empty($data['body'])) { | |
277 | throw new Exception('unable to get syslog data'); | |
278 | } | |
279 | ||
280 | return explode("\n", $data['body']); | |
281 | } | |
282 | ||
abf641bb S |
283 | /** |
284 | * get the Dialed Calls from router | |
285 | * | |
286 | * @return array | |
287 | */ | |
288 | public function getDialedCalls() { | |
289 | $path = 'data/ExportDialedCalls.json'; | |
290 | $fields = array('exporttype' => '3'); | |
291 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
292 | $data = $this->sentRequest($path, $fields, $cookie); | |
9bd25bb4 | 293 | |
abf641bb S |
294 | if (empty($data['body'])) { |
295 | throw new Exception('unable to get syslog data'); | |
296 | } | |
297 | ||
298 | return explode("\n", $data['body']); | |
299 | } | |
300 | ||
809e25bd S |
301 | /** |
302 | * reconnect LTE | |
303 | * | |
304 | * @return array | |
305 | */ | |
c9e082da S |
306 | public function reconnectLte () { |
307 | $path = 'data/modules.json'; | |
809e25bd S |
308 | $fields = array('csrf_token' => $this->token, 'lte_reconn' => '1'); |
309 | $fields = $this->encrypt($fields); | |
c9e082da | 310 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; |
809e25bd | 311 | $data = $this->sentRequest($path, $fields, $cookie, 2); |
c9e082da S |
312 | $json = json_decode($data['body'], true); |
313 | ||
314 | return $json; | |
315 | } | |
809e25bd | 316 | |
7f4a51d2 S |
317 | /** |
318 | * reset the router to Factory Default | |
319 | * not tested | |
320 | * | |
321 | * @return array | |
322 | */ | |
9bd25bb4 S |
323 | public function resetToFactoryDefault () { |
324 | $path = 'data/resetAllSetting.json'; | |
325 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true'); | |
326 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
327 | $data = $this->sentRequest($path, $fields, $cookie); | |
328 | $json = json_decode($data['body'], true); | |
329 | ||
330 | return $json; | |
331 | } | |
7f4a51d2 | 332 | |
9bd25bb4 | 333 | |
4ae464d1 S |
334 | /** |
335 | * check if firmware is actual | |
336 | * | |
337 | * @return array | |
338 | */ | |
339 | public function checkFirmware () { | |
340 | $path = 'data/checkfirmware.json'; | |
341 | $fields = array('checkfirmware' => 'true'); | |
342 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
343 | $data = $this->sentRequest($path, $fields, $cookie); | |
344 | ||
345 | if (empty($data['body'])) { | |
346 | throw new Exception('unable to get checkfirmware data'); | |
347 | } | |
348 | ||
349 | $json = json_decode($data['body'], true); | |
350 | ||
351 | return $json; | |
352 | } | |
353 | ||
14d4f286 S |
354 | /** |
355 | * decrypt data from router | |
356 | * | |
357 | * @param string $data | |
358 | * @return array | |
359 | */ | |
c2678616 | 360 | private function decrypt ($data) { |
14d4f286 S |
361 | require_once 'CryptLib/CryptLib.php'; |
362 | $factory = new CryptLib\Cipher\Factory(); | |
363 | $aes = $factory->getBlockCipher('rijndael-128'); | |
364 | ||
365 | $iv = hex2bin(substr($this->challenge, 16, 16)); | |
366 | $adata = hex2bin(substr($this->challenge, 32, 16)); | |
367 | $dkey = hex2bin($this->derivedk); | |
368 | $enc = hex2bin($data); | |
369 | ||
370 | $aes->setKey($dkey); | |
371 | $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); | |
372 | ||
373 | $mode->decrypt($enc); | |
374 | ||
375 | return $mode->finish(); | |
376 | } | |
377 | ||
378 | /** | |
379 | * decrypt data for the router | |
380 | * | |
381 | * @param array $data | |
382 | * @return string | |
383 | */ | |
c2678616 | 384 | private function encrypt ($data) { |
14d4f286 S |
385 | require_once 'CryptLib/CryptLib.php'; |
386 | $factory = new CryptLib\Cipher\Factory(); | |
387 | $aes = $factory->getBlockCipher('rijndael-128'); | |
388 | ||
389 | $iv = hex2bin(substr($this->challenge, 16, 16)); | |
390 | $adata = hex2bin(substr($this->challenge, 32, 16)); | |
391 | $dkey = hex2bin($this->derivedk); | |
392 | ||
393 | $aes->setKey($dkey); | |
394 | $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); | |
395 | $mode->encrypt(http_build_query($data)); | |
396 | ||
809e25bd | 397 | return bin2hex($mode->finish()); |
14d4f286 S |
398 | } |
399 | ||
c2678616 S |
400 | /** |
401 | * get the values from array | |
402 | * | |
403 | * @param array $array | |
404 | * @return array | |
405 | */ | |
406 | private function getValues($array) { | |
407 | $data = array(); | |
408 | foreach ($array as $item) { | |
409 | $data[$item['varid']] = $item['varvalue']; | |
410 | } | |
411 | ||
412 | return $data; | |
413 | } | |
414 | ||
a91317a6 S |
415 | /** |
416 | * sends the request to router | |
5e44cffa | 417 | * |
b5a532a8 | 418 | * @param string $path |
809e25bd | 419 | * @param mixed $fields |
a91317a6 | 420 | * @param string $cookie |
809e25bd | 421 | * @param integer $count |
a91317a6 S |
422 | * @return array |
423 | */ | |
809e25bd | 424 | private function sentRequest ($path, $fields, $cookie = '', $count = 0) { |
b5a532a8 | 425 | $url = $this->url.$path; |
a91317a6 S |
426 | $ch = curl_init(); |
427 | curl_setopt($ch, CURLOPT_URL, $url); | |
428 | ||
429 | if (!empty($fields)) { | |
809e25bd S |
430 | if (is_array($fields)) { |
431 | curl_setopt($ch, CURLOPT_POST, count($fields)); | |
432 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
433 | } | |
434 | else { | |
435 | curl_setopt($ch, CURLOPT_POST, $count); | |
436 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); | |
437 | } | |
a91317a6 S |
438 | } |
439 | ||
440 | if (!empty($cookie)) { | |
441 | curl_setopt($ch, CURLOPT_COOKIE, $cookie); | |
442 | } | |
443 | ||
444 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
445 | curl_setopt($ch, CURLOPT_HEADER, true); | |
446 | ||
a91317a6 S |
447 | if ($cookie) { |
448 | ||
449 | } | |
450 | ||
451 | $result = curl_exec($ch); | |
452 | ||
453 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
454 | $header = substr($result, 0, $header_size); | |
455 | $body = substr($result, $header_size); | |
456 | curl_close($ch); | |
457 | ||
458 | // fix invalid json | |
14d4f286 | 459 | |
a91317a6 S |
460 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); |
461 | $body = preg_replace('/\'/i', '"', $body); | |
5e44cffa | 462 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
dae16c50 | 463 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
a91317a6 S |
464 | |
465 | return array('header' => $this->parse_headers($header), 'body' => $body); | |
466 | } | |
467 | ||
809e25bd S |
468 | /** |
469 | * get the csrf_token | |
470 | * | |
471 | * @return string | |
472 | */ | |
473 | private function getToken () { | |
474 | $path = 'html/content/overview/index.html?lang=de'; | |
475 | $fields = array(); | |
476 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
477 | $data = $this->sentRequest($path, $fields, $cookie); | |
478 | ||
479 | if (empty($data['body'])) { | |
480 | throw new Exception('unable to get csrf_token'); | |
481 | } | |
482 | ||
483 | $a = explode('csrf_token = "', $data['body']); | |
484 | $a = explode('";', $a[1]); | |
485 | ||
486 | if (isset($a[0]) && !empty($a[0])) { | |
487 | return $a[0]; | |
488 | } | |
489 | else { | |
490 | throw new Exception('unable to get csrf_token'); | |
491 | } | |
492 | } | |
493 | ||
a91317a6 S |
494 | /** |
495 | * parse the curl return header into an array | |
5e44cffa | 496 | * |
a91317a6 S |
497 | * @param string $response |
498 | * @return array | |
499 | */ | |
500 | private function parse_headers($response) { | |
501 | $headers = array(); | |
502 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
503 | ||
7f4a51d2 S |
504 | $header_text = explode("\r\n", $header_text); |
505 | foreach ($header_text as $i => $line) { | |
a91317a6 S |
506 | if ($i === 0) { |
507 | $headers['http_code'] = $line; | |
508 | } | |
509 | else { | |
510 | list ($key, $value) = explode(': ', $line); | |
511 | $headers[$key] = $value; | |
512 | } | |
513 | } | |
514 | ||
515 | return $headers; | |
516 | } | |
7f4a51d2 | 517 | } |