2 require_once('RebootException.class.php');
3 require_once('RouterException.class.php');
6 * @author Jan Altensen (Stricted)
7 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
8 * @copyright 2015 Jan Altensen (Stricted)
10 class SpeedportHybrid
{
15 const VERSION
= '1.0.2';
21 private $challenge = '';
39 private $session = '';
51 private $derivedk = '';
53 public function __construct ($url = 'http://speedport.ip/') {
58 * Requests the password-challenge from the router.
60 private function getChallenge () {
61 $path = 'data/Login.json';
62 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
63 $data = $this->sentRequest($path, $fields);
64 $data = json_decode($data['body'], true);
65 $data = $this->getValues($data);
67 if (isset($data['challengev']) && !empty($data['challengev'])) {
68 $this->challenge
= $data['challengev'];
73 * login into the router with the given password
75 * @param string $password
78 public function login ($password) {
79 $this->getChallenge();
81 if (empty($this->challenge
)) {
82 throw new RouterExeption('unable to get the challenge from the router');
85 $path = 'data/Login.json';
86 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
87 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
88 $data = $this->sentRequest($path, $fields);
89 $json = json_decode($data['body'], true);
90 $json = $this->getValues($json);
91 if (isset($json['login']) && $json['login'] == 'success') {
92 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
93 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
94 if (isset($match[1]) && !empty($match[1])) {
95 $this->session
= $match[1];
98 throw new RouterExeption('unable to get the session cookie from the router');
101 // calculate derivedk
102 if (!function_exists("hash_pbkdf2")) {
103 require_once 'CryptLib/CryptLib.php';
104 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
105 $this->derivedk
= bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
106 $this->derivedk
= substr($this->derivedk
, 0, 32);
109 $this->derivedk
= hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
112 // get the csrf_token
113 $this->token
= $this->getToken();
115 if ($this->checkLogin() === true) {
125 * check if we are logged in
129 public function checkLogin () {
130 if (empty($this->challenge
) && empty($this->session
)) {
134 $path = 'data/SecureStatus.json';
136 $data = $this->sentRequest($path, $fields, true);
138 if (empty($data['body'])) {
139 throw new RouterExeption('unable to get SecureStatus data');
142 $json = json_decode($data['body'], true);
143 $json = $this->getValues($json);
145 if ($json['loginstate'] != 1) {
157 public function logout () {
158 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
160 $path = 'data/Login.json';
161 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
162 $data = $this->sentRequest($path, $fields, true);
163 if ($this->checkLogin() === false) {
164 // reset challenge and session
165 $this->challenge
= '';
169 $json = json_decode($data['body'], true);
174 throw new RouterExeption('logout failed');
183 public function reboot () {
184 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
186 $path = 'data/Reboot.json';
187 $fields = array('csrf_token' => $this->token
, 'reboot_device' => 'true');
188 $data = $this->sentEncryptedRequest($path, $fields, true);
190 $json = json_decode($data['body'], true);
191 $json = $this->getValues($json);
193 if ($json['status'] == 'ok') {
194 // throw an exception because router is unavailable for other tasks
195 // like $this->logout() or $this->checkLogin
196 throw new RebootException('Router Reboot');
199 throw new RouterException('unable to reboot');
204 * change dsl connection status
206 * @param string $status
208 public function changeConnectionStatus ($status) {
209 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
211 $path = 'data/Connect.json';
213 if ($status == 'online' ||
$status == 'offline') {
214 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
215 $data = $this->sentRequest($path, $fields, true);
217 $json = json_decode($data['body'], true);
222 throw new RouterExeption();
227 * return the given json as array
229 * @param string $file
232 public function getData ($file) {
233 if ($this->checkLogin() !== true && $file != "Status") throw new RouterExeption('you musst be logged in to use this method');
235 $path = 'data/'.$file.'.json';
237 $data = $this->sentRequest($path, $fields, true);
239 if (empty($data['body'])) {
240 throw new RouterExeption('unable to get '.$file.' data');
243 $json = json_decode($data['body'], true);
249 * get the router syslog
253 public function getSyslog() {
254 return $this->exportData('0');
258 * get the Missed Calls from router
262 public function getMissedCalls() {
263 return $this->exportData('1');
267 * get the Taken Calls from router
271 public function getTakenCalls() {
272 return $this->exportData('2');
276 * get the Dialed Calls from router
280 public function getDialedCalls() {
281 return $this->exportData('3');
285 * export data from router
289 private function exportData ($type) {
290 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
292 $path = 'data/Syslog.json';
293 $fields = array('exporttype' => $type);
294 $data = $this->sentRequest($path, $fields, true);
296 if (empty($data['body'])) {
297 throw new RouterExeption('unable to get export data');
300 return explode("\n", $data['body']);
308 public function reconnectLte () {
309 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
311 $path = 'data/modules.json';
312 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
313 $data = $this->sentEncryptedRequest($path, $fields, true);
314 $json = json_decode($data['body'], true);
320 * reset the router to Factory Default
325 public function resetToFactoryDefault () {
326 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
328 $path = 'data/resetAllSetting.json';
329 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
330 $data = $this->sentRequest($path, $fields, true);
331 $json = json_decode($data['body'], true);
338 * check if firmware is actual
342 public function checkFirmware () {
343 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
345 $path = 'data/checkfirmware.json';
346 $fields = array('checkfirmware' => 'true');
347 $data = $this->sentRequest($path, $fields, true);
349 if (empty($data['body'])) {
350 throw new RouterExeption('unable to get checkfirmware data');
353 $json = json_decode($data['body'], true);
359 * decrypt data from router
361 * @param string $data
364 private function decrypt ($data) {
365 require_once 'CryptLib/CryptLib.php';
366 $factory = new CryptLib\Cipher\
Factory();
367 $aes = $factory->getBlockCipher('rijndael-128');
369 $iv = hex2bin(substr($this->challenge
, 16, 16));
370 $adata = hex2bin(substr($this->challenge
, 32, 16));
371 $dkey = hex2bin($this->derivedk
);
372 $enc = hex2bin($data);
375 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
377 $mode->decrypt($enc);
379 return $mode->finish();
383 * decrypt data for the router
388 private function encrypt ($data) {
389 require_once 'CryptLib/CryptLib.php';
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
);
398 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
399 $mode->encrypt(http_build_query($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 $data[$item['varid']] = $item['varvalue'];
420 * sends the encrypted request to router
422 * @param string $path
423 * @param mixed $fields
424 * @param string $cookie
427 private function sentEncryptedRequest ($path, $fields, $cookie = false) {
428 $count = count($fields);
429 $fields = $this->encrypt($fields);
430 return $this->sentRequest($path, $fields, $cookie, $count);
434 * sends the request to router
436 * @param string $path
437 * @param mixed $fields
438 * @param string $cookie
439 * @param integer $count
442 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
443 $url = $this->url
.$path.'?lang=en';
445 curl_setopt($ch, CURLOPT_URL
, $url);
447 if (!empty($fields)) {
448 if (is_array($fields)) {
449 curl_setopt($ch, CURLOPT_POST
, count($fields));
450 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
453 curl_setopt($ch, CURLOPT_POST
, $count);
454 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
458 if ($cookie === true) {
459 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->session
);
462 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
463 curl_setopt($ch, CURLOPT_HEADER
, true);
469 $result = curl_exec($ch);
471 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
472 $header = substr($result, 0, $header_size);
473 $body = substr($result, $header_size);
476 // check if body is encrypted (hex instead of json)
477 if (ctype_xdigit($body)) {
478 $body = $this->decrypt($body);
482 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
483 $body = preg_replace('/\'/i', '"', $body);
484 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
485 $body = preg_replace("/},\s+]/", "}\n]", $body);
487 return array('header' => $this->parse_headers($header), 'body' => $body);
495 private function getToken () {
496 if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
498 $path = 'html/content/overview/index.html';
500 $data = $this->sentRequest($path, $fields, true);
502 if (empty($data['body'])) {
503 throw new RouterExeption('unable to get csrf_token');
506 $a = explode('csrf_token = "', $data['body']);
507 $a = explode('";', $a[1]);
509 if (isset($a[0]) && !empty($a[0])) {
513 throw new RouterExeption('unable to get csrf_token');
518 * parse the curl return header into an array
520 * @param string $response
523 private function parse_headers($response) {
525 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
527 $header_text = explode("\r\n", $header_text);
528 foreach ($header_text as $i => $line) {
530 $headers['http_code'] = $line;
533 list ($key, $value) = explode(': ', $line);
534 $headers[$key] = $value;