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)
7 class SpeedportHybrid
{
12 private $challenge = '';
30 private $session = '';
42 private $derivedk = '';
44 public function __construct ($url = 'http://speedport.ip/') {
49 * Requests the password-challenge from the router.
51 private function getChallenge () {
52 $path = 'data/Login.json';
53 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
54 $data = $this->sentRequest($path, $fields);
55 $data = json_decode($data['body'], true);
56 $data = $this->getValues($data);
58 if (isset($data['challengev']) && !empty($data['challengev'])) {
59 $this->challenge
= $data['challengev'];
64 * login into the router with the given password
66 * @param string $password
69 public function login ($password) {
70 $this->getChallenge();
72 if (empty($this->challenge
)) {
73 throw new Exception('unable to get the challenge from the router');
76 $path = 'data/Login.json';
77 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
78 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
79 $data = $this->sentRequest($path, $fields);
80 $json = json_decode($data['body'], true);
81 $json = $this->getValues($json);
82 if (isset($json['login']) && $json['login'] == 'success') {
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];
89 throw new Exception('unable to get the session cookie from the router');
93 if (!function_exists("hash_pbkdf2")) {
94 require_once 'CryptLib/CryptLib.php';
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);
100 $this->derivedk
= hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
103 // get the csrf_token
104 $this->token
= $this->getToken();
106 if ($this->checkLogin() === true) {
116 * check if we are logged in
120 public function checkLogin () {
121 $path = 'data/SecureStatus.json';
123 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
124 $data = $this->sentRequest($path, $fields, $cookie);
126 if (empty($data['body'])) {
127 throw new Exception('unable to get '.$file.' data');
130 $json = json_decode($data['body'], true);
131 $json = $this->getValues($json);
133 if ($json['loginstate'] != 1) {
145 public function logout () {
146 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
148 $path = 'data/Login.json';
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
= '';
158 $json = json_decode($data['body'], true);
163 throw new Exception('logout failed');
172 public function reboot () {
173 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
175 $path = 'data/Reboot.json';
176 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
177 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
178 $data = $this->sentRequest($path, $fields, $cookie);
180 $json = json_decode($data['body'], true);
186 * change dsl connection status
188 * @param string $status
190 public function changeConnectionStatus ($status) {
191 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
193 $path = 'data/Connect.json';
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
;
198 $data = $this->sentRequest($path, $fields, $cookie);
200 $json = json_decode($this->decrypt($data['body']), true);
205 throw new Exception();
210 * return the given json as array
212 * @param string $file
215 public function getData ($file) {
216 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
218 $path = 'data/'.$file.'.json';
220 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
221 $data = $this->sentRequest($path, $fields, $cookie);
223 if (empty($data['body'])) {
224 throw new Exception('unable to get '.$file.' data');
227 $json = json_decode($data['body'], true);
233 * get the router syslog
237 public function getSyslog() {
238 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
240 $path = 'data/Syslog.json';
241 $fields = array('exporttype' => '0');
242 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
243 $data = $this->sentRequest($path, $fields, $cookie);
245 if (empty($data['body'])) {
246 throw new Exception('unable to get syslog data');
249 return explode("\n", $data['body']);
253 * get the Missed Calls from router
257 public function getMissedCalls() {
258 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
260 $path = 'data/ExportMissedCalls.json';
261 $fields = array('exporttype' => '1');
262 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
263 $data = $this->sentRequest($path, $fields, $cookie);
265 if (empty($data['body'])) {
266 throw new Exception('unable to get syslog data');
269 return explode("\n", $data['body']);
273 * get the Taken Calls from router
277 public function getTakenCalls() {
278 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
280 $path = 'data/ExportTakenCalls.json';
281 $fields = array('exporttype' => '2');
282 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
283 $data = $this->sentRequest($path, $fields, $cookie);
285 if (empty($data['body'])) {
286 throw new Exception('unable to get syslog data');
289 return explode("\n", $data['body']);
293 * get the Dialed Calls from router
297 public function getDialedCalls() {
298 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
300 $path = 'data/ExportDialedCalls.json';
301 $fields = array('exporttype' => '3');
302 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
303 $data = $this->sentRequest($path, $fields, $cookie);
305 if (empty($data['body'])) {
306 throw new Exception('unable to get syslog data');
309 return explode("\n", $data['body']);
317 public function reconnectLte () {
318 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
320 $path = 'data/modules.json';
321 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
322 $fields = $this->encrypt($fields);
323 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
324 $data = $this->sentRequest($path, $fields, $cookie, 2);
325 $json = json_decode($data['body'], true);
331 * reset the router to Factory Default
336 public function resetToFactoryDefault () {
337 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
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);
350 * check if firmware is actual
354 public function checkFirmware () {
355 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
357 $path = 'data/checkfirmware.json';
358 $fields = array('checkfirmware' => 'true');
359 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
360 $data = $this->sentRequest($path, $fields, $cookie);
362 if (empty($data['body'])) {
363 throw new Exception('unable to get checkfirmware data');
366 $json = json_decode($data['body'], true);
372 * decrypt data from router
374 * @param string $data
377 private function decrypt ($data) {
378 require_once 'CryptLib/CryptLib.php';
379 $factory = new CryptLib\Cipher\
Factory();
380 $aes = $factory->getBlockCipher('rijndael-128');
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);
388 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
390 $mode->decrypt($enc);
392 return $mode->finish();
396 * decrypt data for the router
401 private function encrypt ($data) {
402 require_once 'CryptLib/CryptLib.php';
403 $factory = new CryptLib\Cipher\
Factory();
404 $aes = $factory->getBlockCipher('rijndael-128');
406 $iv = hex2bin(substr($this->challenge
, 16, 16));
407 $adata = hex2bin(substr($this->challenge
, 32, 16));
408 $dkey = hex2bin($this->derivedk
);
411 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
412 $mode->encrypt(http_build_query($data));
414 return bin2hex($mode->finish());
418 * get the values from array
420 * @param array $array
423 private function getValues($array) {
425 foreach ($array as $item) {
426 $data[$item['varid']] = $item['varvalue'];
433 * sends the request to router
435 * @param string $path
436 * @param mixed $fields
437 * @param string $cookie
438 * @param integer $count
441 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
442 $url = $this->url
.$path;
444 curl_setopt($ch, CURLOPT_URL
, $url);
446 if (!empty($fields)) {
447 if (is_array($fields)) {
448 curl_setopt($ch, CURLOPT_POST
, count($fields));
449 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
452 curl_setopt($ch, CURLOPT_POST
, $count);
453 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
457 if (!empty($cookie)) {
458 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
461 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
462 curl_setopt($ch, CURLOPT_HEADER
, true);
468 $result = curl_exec($ch);
470 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
471 $header = substr($result, 0, $header_size);
472 $body = substr($result, $header_size);
477 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
478 $body = preg_replace('/\'/i', '"', $body);
479 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
480 $body = preg_replace("/},\s+]/", "}\n]", $body);
482 return array('header' => $this->parse_headers($header), 'body' => $body);
490 private function getToken () {
491 if ($this->checkLogin() !== true) throw new Exception('you musst be logged in to use this method');
493 $path = 'html/content/overview/index.html?lang=de';
495 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
496 $data = $this->sentRequest($path, $fields, $cookie);
498 if (empty($data['body'])) {
499 throw new Exception('unable to get csrf_token');
502 $a = explode('csrf_token = "', $data['body']);
503 $a = explode('";', $a[1]);
505 if (isset($a[0]) && !empty($a[0])) {
509 throw new Exception('unable to get csrf_token');
514 * parse the curl return header into an array
516 * @param string $response
519 private function parse_headers($response) {
521 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
523 $header_text = explode("\r\n", $header_text);
524 foreach ($header_text as $i => $line) {
526 $headers['http_code'] = $line;
529 list ($key, $value) = explode(': ', $line);
530 $headers[$key] = $value;