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