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('unknown status');
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 Missed Calls from router
269 public function getMissedCalls() {
270 $data = $this->getData('PhoneCalls');
271 $data = $this->getValues($data);
273 if (isset($data['addmissedcalls'])) {
274 return $data['addmissedcalls'];
282 * get the Taken Calls from router
286 public function getTakenCalls() {
287 $data = $this->getData('PhoneCalls');
288 $data = $this->getValues($data);
290 if (isset($data['addtakencalls'])) {
291 return $data['addtakencalls'];
299 * get the Dialed Calls from router
303 public function getDialedCalls() {
304 $data = $this->getData('PhoneCalls');
305 $data = $this->getValues($data);
307 if (isset($data['adddialedcalls'])) {
308 return $data['adddialedcalls'];
320 public function reconnectLte () {
323 $path = 'data/modules.json';
324 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
325 $data = $this->sentEncryptedRequest($path, $fields, true);
327 return $data['body'];
331 * reset the router to Factory Default
336 public function resetToFactoryDefault () {
339 $path = 'data/resetAllSetting.json';
340 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
341 $data = $this->sentRequest($path, $fields, true);
343 return $data['body'];
348 * check if firmware is actual
352 public function checkFirmware () {
355 $path = 'data/checkfirmware.json';
356 $fields = array('checkfirmware' => 'true');
357 $data = $this->sentRequest($path, $fields, true);
359 return $data['body'];
363 * decrypt data from router
365 * @param string $data
368 private function decrypt ($data) {
369 $iv = hex2bin(substr($this->challenge
, 16, 16));
370 $adata = hex2bin(substr($this->challenge
, 32, 16));
371 $key = hex2bin($this->derivedk
);
372 $enc = hex2bin($data);
374 $factory = new CryptLib\Cipher\
Factory();
375 $aes = $factory->getBlockCipher('rijndael-128');
377 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
379 $mode->decrypt($enc);
381 return $mode->finish();
385 * decrypt data for the router
387 * @param string $data
390 private function encrypt ($data) {
391 $iv = hex2bin(substr($this->challenge
, 16, 16));
392 $adata = hex2bin(substr($this->challenge
, 32, 16));
393 $key = hex2bin($this->derivedk
);
395 $factory = new CryptLib\Cipher\
Factory();
396 $aes = $factory->getBlockCipher('rijndael-128');
398 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
399 $mode->encrypt($data);
401 return bin2hex($mode->finish());
405 * get the values from array
407 * @param array $array
410 private function getValues($array) {
412 foreach ($array as $item) {
413 // thank you telekom for this piece of shit
414 if ($item['vartype'] == 'template') {
415 if (is_array($item['varvalue'])) {
416 $data[$item['varid']][] = $this->getValues($item['varvalue']);
419 // i dont know if we need this
420 $data[$item['varid']] = $item['varvalue'];
424 if (is_array($item['varvalue'])) {
425 $data[$item['varid']] = $this->getValues($item['varvalue']);
428 $data[$item['varid']] = $item['varvalue'];
437 * sends the encrypted request to router
439 * @param string $path
440 * @param mixed $fields
441 * @param string $cookie
444 private function sentEncryptedRequest ($path, $fields, $cookie = false) {
445 $count = count($fields);
446 $fields = $this->encrypt(http_build_query($fields));
447 return $this->sentRequest($path, $fields, $cookie, $count);
451 * sends the request to router
453 * @param string $path
454 * @param mixed $fields
455 * @param string $cookie
456 * @param integer $count
459 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
460 $url = $this->url
.$path.'?lang=en';
462 curl_setopt($ch, CURLOPT_URL
, $url);
464 if (!empty($fields)) {
465 if (is_array($fields)) {
466 curl_setopt($ch, CURLOPT_POST
, count($fields));
467 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
470 curl_setopt($ch, CURLOPT_POST
, $count);
471 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
475 if ($cookie === true) {
476 curl_setopt($ch, CURLOPT_COOKIE
, 'lang=en; challengev='.$this->challenge
.'; '.$this->cookie
);
479 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
480 curl_setopt($ch, CURLOPT_HEADER
, true);
482 $result = curl_exec($ch);
484 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
485 $header = substr($result, 0, $header_size);
486 $body = substr($result, $header_size);
489 // check if response is empty
491 throw new RouterException('empty response');
494 // check if body is encrypted (hex instead of json)
495 if (ctype_xdigit($body)) {
496 $body = $this->decrypt($body);
500 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
501 $body = preg_replace('/\'/i', '"', $body);
502 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
503 $body = preg_replace("/},\s+]/", "}\n]", $body);
506 if (strpos($url, '.json') !== false) {
507 $body = json_decode($body, true);
510 return array('header' => $this->parse_headers($header), 'body' => $body);
518 private function getToken () {
521 $path = 'html/content/overview/index.html';
523 $data = $this->sentRequest($path, $fields, true);
525 $a = explode('csrf_token = "', $data['body']);
526 $a = explode('";', $a[1]);
528 if (isset($a[0]) && !empty($a[0])) {
532 throw new RouterException('unable to get csrf_token');
537 * calculate the derivedk
539 * @param string $password
542 private function getDerviedk ($password) {
545 // calculate derivedk
546 if (!function_exists('hash_pbkdf2')) {
547 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
548 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
549 $derivedk = substr($derivedk, 0, 32);
552 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
555 if (empty($derivedk)) {
556 throw new RouterException('unable to calculate derivedk');
563 * get cookie from header data
568 private function getCookie ($data) {
570 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
571 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
572 if (isset($match[1]) && !empty($match[1])) {
577 if (empty($cookie)) {
578 throw new RouterException('unable to get the session cookie from the router');
585 * parse the curl return header into an array
587 * @param string $response
590 private function parse_headers($response) {
592 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
594 $header_text = explode("\r\n", $header_text);
595 foreach ($header_text as $i => $line) {
597 $headers['http_code'] = $line;
600 list ($key, $value) = explode(': ', $line);
601 $headers[$key] = $value;