2 require_once('RebootException.class.php');
3 require_once('RouterException.class.php');
4 require_once('CryptLib/CryptLib.php');
7 * @author Jan Altensen (Stricted)
8 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 * @copyright 2015 Jan Altensen (Stricted)
11 class SpeedportHybrid
{
16 const VERSION
= '1.0.3';
22 private $challenge = '';
52 private $derivedk = '';
54 public function __construct ($url = 'http://speedport.ip/') {
59 * Requests the password-challenge from the router.
61 private function getChallenge () {
62 $path = 'data/Login.json';
63 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
64 $data = $this->sentRequest($path, $fields);
65 $data = $this->getValues($data['body']);
67 if (isset($data['challengev']) && !empty($data['challengev'])) {
68 return $data['challengev'];
71 throw new RouterException('unable to get the challenge from the router');
76 * login into the router with the given password
78 * @param string $password
81 public function login ($password) {
82 $this->challenge
= $this->getChallenge();
84 $path = 'data/Login.json';
85 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
86 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
87 $data = $this->sentRequest($path, $fields);
88 $json = $this->getValues($data['body']);
90 if (isset($json['login']) && $json['login'] == 'success') {
91 $this->cookie
= $this->getCookie($data);
93 $this->derivedk
= $this->getDerviedk($password);
96 $this->token
= $this->getToken();
98 if ($this->checkLogin(false) === true) {
107 * check if we are logged in
109 * @param boolean $exception
112 public function checkLogin ($exception = true) {
113 // check if challenge or session is empty
114 if (empty($this->challenge
) ||
empty($this->cookie
)) {
115 if ($exception === true) {
116 throw new RouterException('you musst be logged in to use this method');
122 $path = 'data/SecureStatus.json';
124 $data = $this->sentRequest($path, $fields, true);
125 $data = $this->getValues($data['body']);
127 if ($data['loginstate'] != 1) {
128 if ($exception === true) {
129 throw new RouterException('you musst be logged in to use this method');
143 public function logout () {
146 $path = 'data/Login.json';
147 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
148 $data = $this->sentRequest($path, $fields, true);
149 $data = $this->getValues($data['body']);
150 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
151 // reset challenge and session
152 $this->challenge
= '';
155 $this->derivedk
= '';
168 public function reboot () {
171 $path = 'data/Reboot.json';
172 $fields = array('csrf_token' => $this->token
, 'reboot_device' => 'true');
173 $data = $this->sentEncryptedRequest($path, $fields, true);
174 $data = $this->getValues($data['body']);
176 if ($data['status'] == 'ok') {
177 // reset challenge and session
178 $this->challenge
= '';
181 $this->derivedk
= '';
183 // throw an exception because router is unavailable for other tasks
184 // like $this->logout() or $this->checkLogin
185 throw new RebootException('Router Reboot');
192 * change dsl connection status
194 * @param string $status
197 public function changeDSLStatus ($status) {
200 $path = 'data/Connect.json';
202 if ($status == 'online' ||
$status == 'offline') {
203 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
204 $data = $this->sentRequest($path, $fields, true);
205 $data = $this->getValues($data['body']);
207 if ($data['status'] == 'ok') {
215 throw new RouterException('unknown status');
220 * change lte connection status
222 * @param string $status
225 public function changeLTEStatus ($status) {
226 throw new Exception('unstable funtion');
227 $path = 'data/Modules.json';
229 if ($status == '0' ||
$status == '1' ||
$status == 'yes' ||
$status == 'no') {
230 if ($status == 'yes') $status = '1';
231 else if ($status == 'no') $status = '0';
233 $fields = array('csrf_token' => $this->token
, 'use_lte' => $status);
234 $data = $this->sentEncryptedRequest($path, $fields, true);
240 throw new RouterException('unknown status');
245 * get phone book entrys
249 public function getPhoneBookEntrys () {
250 $data = $this->getData('PhoneBook');
251 $data = $this->getValues($data);
253 if (isset($data['addbookentry'])) {
254 return $data['addbookentry'];
262 * add Phone Book Entry
264 * @param string $name
265 * @param string $firstname
266 * @param string $private
267 * @param string $work
268 * @param string $mobile
273 public function addPhoneBookEntry ($name, $firstname, $private, $work, $mobile, $id = -1) {
276 $path = 'data/PhoneBook.json';
278 'csrf_token' => $this->getToken(),
281 'phonebook_name' => $name,
282 'phonebook_vorname' => $firstname,
283 'phonebook_number_p' => $private,
284 'phonebook_number_a' => $work,
285 'phonebook_number_m' => $mobile
288 $data = $this->sentRequest($path, $fields, true);
289 $data = $this->getValues($data['body']);
291 if ($data['status'] == 'ok') {
295 throw new RouterException('can not add/edit Phone Book Entry');
300 * edit Phone Book Entry
303 * @param string $name
304 * @param string $firstname
305 * @param string $private
306 * @param string $work
307 * @param string $mobile
311 public function changePhoneBookEntry ($id, $name, $firstname, $private, $work, $mobile) {
312 return $this->addPhoneBookEntry($name, $firstname, $private, $work, $private, $id);
316 * delete Phone Book Entry
322 public function deletePhoneBookEntry ($id) {
325 $path = 'data/PhoneBook.json';
327 'csrf_token' => $this->getToken(),
329 'deleteEntry' => 'delete'
332 $data = $this->sentRequest($path, $fields, true);
333 $data = $this->getValues($data['body']);
335 if ($data['status'] == 'ok') {
339 throw new RouterException('can not delete Phone Book Entry');
345 * get uptime based on online (connection) time
349 public function getUptime () {
350 $data = $this->getData('LAN');
351 $data = $this->getValues($data);
353 return $data['days_online'];
357 * return the given json as array
359 * @param string $file
362 public function getData ($file) {
363 if ($file != 'Status') $this->checkLogin();
365 $path = 'data/'.$file.'.json';
367 $data = $this->sentRequest($path, $fields, true);
369 return $data['body'];
373 * get the router syslog
377 public function getSyslog() {
378 $data = $this->getData('SystemMessages');
379 $data = $this->getValues($data);
381 if (isset($data['addmessage'])) {
382 return $data['addmessage'];
390 * get the Missed Calls from router
394 public function getMissedCalls() {
395 $data = $this->getData('PhoneCalls');
396 $data = $this->getValues($data);
398 if (isset($data['addmissedcalls'])) {
399 return $data['addmissedcalls'];
407 * get the Taken Calls from router
411 public function getTakenCalls() {
412 $data = $this->getData('PhoneCalls');
413 $data = $this->getValues($data);
415 if (isset($data['addtakencalls'])) {
416 return $data['addtakencalls'];
424 * get the Dialed Calls from router
428 public function getDialedCalls() {
429 $data = $this->getData('PhoneCalls');
430 $data = $this->getValues($data);
432 if (isset($data['adddialedcalls'])) {
433 return $data['adddialedcalls'];
445 public function reconnectLte () {
448 $path = 'data/modules.json';
449 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
450 $data = $this->sentEncryptedRequest($path, $fields, true);
452 return $data['body'];
456 * reset the router to Factory Default
461 public function resetToFactoryDefault () {
464 $path = 'data/resetAllSetting.json';
465 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
466 $data = $this->sentRequest($path, $fields, true);
468 return $data['body'];
473 * check if firmware is actual
477 public function checkFirmware () {
480 $path = 'data/checkfirmware.json';
481 $fields = array('checkfirmware' => 'true');
482 $data = $this->sentRequest($path, $fields, true);
484 return $data['body'];
488 * decrypt data from router
490 * @param string $data
493 private function decrypt ($data) {
494 $iv = hex2bin(substr($this->challenge
, 16, 16));
495 $adata = hex2bin(substr($this->challenge
, 32, 16));
496 $key = hex2bin($this->derivedk
);
497 $enc = hex2bin($data);
499 $factory = new CryptLib\Cipher\
Factory();
500 $aes = $factory->getBlockCipher('rijndael-128');
502 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
504 $mode->decrypt($enc);
506 return $mode->finish();
510 * decrypt data for the router
512 * @param string $data
515 private function encrypt ($data) {
516 $iv = hex2bin(substr($this->challenge
, 16, 16));
517 $adata = hex2bin(substr($this->challenge
, 32, 16));
518 $key = hex2bin($this->derivedk
);
520 $factory = new CryptLib\Cipher\
Factory();
521 $aes = $factory->getBlockCipher('rijndael-128');
523 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
524 $mode->encrypt($data);
526 return bin2hex($mode->finish());
530 * get the values from array
532 * @param array $array
535 private function getValues($array) {
537 foreach ($array as $item) {
538 // thank you telekom for this piece of shit
539 if ($item['vartype'] == 'template') {
540 if (is_array($item['varvalue'])) {
541 $data[$item['varid']][] = $this->getValues($item['varvalue']);
544 // i dont know if we need this
545 $data[$item['varid']] = $item['varvalue'];
549 if (is_array($item['varvalue'])) {
550 $data[$item['varid']] = $this->getValues($item['varvalue']);
553 $data[$item['varid']] = $item['varvalue'];
562 * sends the encrypted request to router
564 * @param string $path
565 * @param mixed $fields
566 * @param string $cookie
569 private function sentEncryptedRequest ($path, $fields, $cookie = false) {
570 $count = count($fields);
571 $fields = $this->encrypt(http_build_query($fields));
572 return $this->sentRequest($path, $fields, $cookie, $count);
576 * sends the request to router
578 * @param string $path
579 * @param mixed $fields
580 * @param string $cookie
581 * @param integer $count
584 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
585 $url = $this->url
.$path.'?lang=en';
587 curl_setopt($ch, CURLOPT_URL
, $url);
589 if (!empty($fields)) {
590 if (is_array($fields)) {
591 curl_setopt($ch, CURLOPT_POST
, count($fields));
592 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
595 curl_setopt($ch, CURLOPT_POST
, $count);
596 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
600 if ($cookie === true) {
601 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->cookie
);
604 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
605 curl_setopt($ch, CURLOPT_HEADER
, true);
607 $result = curl_exec($ch);
609 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
610 $header = substr($result, 0, $header_size);
611 $body = substr($result, $header_size);
614 // check if response is empty
616 throw new RouterException('empty response');
619 // check if body is encrypted (hex instead of json)
620 if (ctype_xdigit($body)) {
621 $body = $this->decrypt($body);
625 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
626 $body = preg_replace('/\'/i', '"', $body);
627 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
628 $body = preg_replace("/},\s+]/", "}\n]", $body);
631 if (strpos($url, '.json') !== false) {
632 $body = json_decode($body, true);
635 return array('header' => $this->parse_headers($header), 'body' => $body);
643 private function getToken () {
646 $path = 'html/content/overview/index.html';
648 $data = $this->sentRequest($path, $fields, true);
650 $a = explode('csrf_token = "', $data['body']);
651 $a = explode('";', $a[1]);
653 if (isset($a[0]) && !empty($a[0])) {
657 throw new RouterException('unable to get csrf_token');
662 * calculate the derivedk
664 * @param string $password
667 private function getDerviedk ($password) {
670 // calculate derivedk
671 if (!function_exists('hash_pbkdf2')) {
672 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
673 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
674 $derivedk = substr($derivedk, 0, 32);
677 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
680 if (empty($derivedk)) {
681 throw new RouterException('unable to calculate derivedk');
688 * get cookie from header data
693 private function getCookie ($data) {
695 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
696 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
697 if (isset($match[1]) && !empty($match[1])) {
702 if (empty($cookie)) {
703 throw new RouterException('unable to get the session cookie from the router');
710 * parse the curl return header into an array
712 * @param string $response
715 private function parse_headers($response) {
717 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
719 $header_text = explode("\r\n", $header_text);
720 foreach ($header_text as $i => $line) {
722 $headers['http_code'] = $line;
725 list ($key, $value) = explode(': ', $line);
726 $headers[$key] = $value;