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 changeConnectionStatus ($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();
220 * get uptime based on online (connection) time
224 public function getUptime () {
225 $data = $this->getData('LAN');
226 $data = $this->getValues($data);
228 return $data['days_online'];
232 * return the given json as array
234 * @param string $file
237 public function getData ($file) {
238 if ($file != 'Status') $this->checkLogin();
240 $path = 'data/'.$file.'.json';
242 $data = $this->sentRequest($path, $fields, true);
244 return $data['body'];
248 * get the router syslog
252 public function getSyslog() {
253 $data = $this->getData('SystemMessages');
254 $data = $this->getValues($data);
256 if (isset($data['addmessage'])) {
257 return $data['addmessage'];
265 * get the router syslog
269 public function test() {
270 $data = $this->getData('NASLight');
271 $data = $this->getValues($data);
274 $data = $this->getValues($data);
276 if (isset($data['addmessage'])) {
277 return $data['addmessage'];
286 * get the Missed Calls from router
290 public function getMissedCalls() {
291 $data = $this->getData('PhoneCalls');
292 $data = $this->getValues($data);
294 if (isset($data['addmissedcalls'])) {
295 return $data['addmissedcalls'];
303 * get the Taken Calls from router
307 public function getTakenCalls() {
308 $data = $this->getData('PhoneCalls');
309 $data = $this->getValues($data);
311 if (isset($data['addtakencalls'])) {
312 return $data['addtakencalls'];
320 * get the Dialed Calls from router
324 public function getDialedCalls() {
325 $data = $this->getData('PhoneCalls');
326 $data = $this->getValues($data);
328 if (isset($data['adddialedcalls'])) {
329 return $data['adddialedcalls'];
341 public function reconnectLte () {
344 $path = 'data/modules.json';
345 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
346 $data = $this->sentEncryptedRequest($path, $fields, true);
348 return $data['body'];
352 * reset the router to Factory Default
357 public function resetToFactoryDefault () {
360 $path = 'data/resetAllSetting.json';
361 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
362 $data = $this->sentRequest($path, $fields, true);
364 return $data['body'];
369 * check if firmware is actual
373 public function checkFirmware () {
376 $path = 'data/checkfirmware.json';
377 $fields = array('checkfirmware' => 'true');
378 $data = $this->sentRequest($path, $fields, true);
380 return $data['body'];
384 * decrypt data from router
386 * @param string $data
389 private function decrypt ($data) {
390 $factory = new CryptLib\Cipher\
Factory();
391 $aes = $factory->getBlockCipher('rijndael-128');
393 $iv = hex2bin(substr($this->challenge
, 16, 16));
394 $adata = hex2bin(substr($this->challenge
, 32, 16));
395 $dkey = hex2bin($this->derivedk
);
396 $enc = hex2bin($data);
399 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
401 $mode->decrypt($enc);
403 return $mode->finish();
407 * decrypt data for the router
412 private function encrypt ($data) {
413 $factory = new CryptLib\Cipher\
Factory();
414 $aes = $factory->getBlockCipher('rijndael-128');
416 $iv = hex2bin(substr($this->challenge
, 16, 16));
417 $adata = hex2bin(substr($this->challenge
, 32, 16));
418 $dkey = hex2bin($this->derivedk
);
421 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
422 $mode->encrypt(http_build_query($data));
424 return bin2hex($mode->finish());
428 * get the values from array
430 * @param array $array
433 private function getValues($array) {
435 foreach ($array as $item) {
436 // thank you telekom for this piece of shit
437 if ($item['vartype'] == 'template') {
438 if (is_array($item['varvalue'])) {
439 $data[$item['varid']][] = $this->getValues($item['varvalue']);
442 $data[$item['varid']] = $item['varvalue'];
446 if (is_array($item['varvalue'])) {
447 $data[$item['varid']] = $this->getValues($item['varvalue']);
450 $data[$item['varid']] = $item['varvalue'];
459 * sends the encrypted request to router
461 * @param string $path
462 * @param mixed $fields
463 * @param string $cookie
466 private function sentEncryptedRequest ($path, $fields, $cookie = false) {
467 $count = count($fields);
468 $fields = $this->encrypt($fields);
469 return $this->sentRequest($path, $fields, $cookie, $count);
473 * sends the request to router
475 * @param string $path
476 * @param mixed $fields
477 * @param string $cookie
478 * @param integer $count
481 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
482 $url = $this->url
.$path.'?lang=en';
484 curl_setopt($ch, CURLOPT_URL
, $url);
486 if (!empty($fields)) {
487 if (is_array($fields)) {
488 curl_setopt($ch, CURLOPT_POST
, count($fields));
489 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
492 curl_setopt($ch, CURLOPT_POST
, $count);
493 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
497 if ($cookie === true) {
498 curl_setopt($ch, CURLOPT_COOKIE
, 'lang=en; challengev='.$this->challenge
.'; '.$this->cookie
);
501 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
502 curl_setopt($ch, CURLOPT_HEADER
, true);
504 $result = curl_exec($ch);
506 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
507 $header = substr($result, 0, $header_size);
508 $body = substr($result, $header_size);
511 // check if response is empty
513 throw new RouterException('empty response');
516 // check if body is encrypted (hex instead of json)
517 if (ctype_xdigit($body)) {
518 $body = $this->decrypt($body);
522 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
523 $body = preg_replace('/\'/i', '"', $body);
524 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
525 $body = preg_replace("/},\s+]/", "}\n]", $body);
528 if (strpos($url, '.json') !== false) {
529 $body = json_decode($body, true);
532 return array('header' => $this->parse_headers($header), 'body' => $body);
540 private function getToken () {
543 $path = 'html/content/overview/index.html';
545 $data = $this->sentRequest($path, $fields, true);
547 $a = explode('csrf_token = "', $data['body']);
548 $a = explode('";', $a[1]);
550 if (isset($a[0]) && !empty($a[0])) {
554 throw new RouterException('unable to get csrf_token');
559 * calculate the derivedk
561 * @param string $password
564 private function getDerviedk ($password) {
567 // calculate derivedk
568 if (!function_exists('hash_pbkdf2')) {
569 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
570 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
571 $derivedk = substr($derivedk, 0, 32);
574 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
577 if (empty($derivedk)) {
578 throw new RouterException('unable to calculate derivedk');
585 * get cookie from header data
590 private function getCookie ($data) {
592 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
593 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
594 if (isset($match[1]) && !empty($match[1])) {
599 if (empty($cookie)) {
600 throw new RouterException('unable to get the session cookie from the router');
607 * parse the curl return header into an array
609 * @param string $response
612 private function parse_headers($response) {
614 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
616 $header_text = explode("\r\n", $header_text);
617 foreach ($header_text as $i => $line) {
619 $headers['http_code'] = $line;
622 list ($key, $value) = explode(': ', $line);
623 $headers[$key] = $value;