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 ($password, $url = 'http://speedport.ip/') {
46 $this->getChallenge();
48 if (empty($this->challenge
)) {
49 throw new Exception('unable to get the challenge from the router');
52 $login = $this->login($password);
54 if ($login === false) {
55 throw new Exception('unable to login');
60 * Requests the password-challenge from the router.
62 public function getChallenge () {
63 $path = 'data/Login.json';
64 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
65 $data = $this->sentRequest($path, $fields);
66 $data = json_decode($data['body'], true);
67 $data = $this->getValues($data);
69 if (isset($data['challengev']) && !empty($data['challengev'])) {
70 $this->challenge
= $data['challengev'];
75 * login into the router with the given password
77 * @param string $password
80 public function login ($password) {
81 $path = 'data/Login.json';
82 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
83 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
84 $data = $this->sentRequest($path, $fields);
85 $json = json_decode($data['body'], true);
86 $json = $this->getValues($json);
87 if (isset($json['login']) && $json['login'] == 'success') {
88 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
89 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
90 if (isset($match[1]) && !empty($match[1])) {
91 $this->session
= $match[1];
94 throw new Exception('unable to get the session cookie from the router');
98 if (!function_exists("hash_pbkdf2")) {
99 require_once 'CryptLib/CryptLib.php';
100 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
101 $this->derivedk
= bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
102 $this->derivedk
= substr($this->derivedk
, 0, 32);
105 $this->derivedk
= hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
108 // get the csrf_token
109 $this->token
= $this->getToken();
111 if ($this->checkLogin() === true) {
121 * check if we are logged in
125 public function checkLogin () {
126 $path = 'data/'.$file.'.json';
128 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
129 $data = $this->sentRequest($path, $fields, $cookie);
131 if (empty($data['body'])) {
132 throw new Exception('unable to get '.$file.' data');
135 $json = json_decode($data['body'], true);
136 $json = $this->getValues($json);
138 if ($json['loginstate'] != 1) {
150 public function logout () {
151 $path = 'data/Login.json';
152 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
153 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
154 $data = $this->sentRequest($path, $fields, $cookie);
155 if ($this->checkLogin() === false) {
156 // reset challenge and session
157 $this->challenge
= '';
161 $json = json_decode($data['body'], true);
166 throw new Exception('logout failed');
175 public function reboot () {
176 $path = 'data/Reboot.json';
177 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
178 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
179 $data = $this->sentRequest($path, $fields, $cookie);
181 $json = json_decode($data['body'], true);
187 * change dsl connection status
189 * @param string $status
191 public function changeConnectionStatus ($status) {
192 $path = 'data/Connect.json';
194 if ($status == 'online' ||
$status == 'offline') {
195 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
196 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
197 $data = $this->sentRequest($path, $fields, $cookie);
199 $json = json_decode($this->decrypt($data['body']), true);
204 throw new Exception();
209 * return the given json as array
211 * @param string $file
214 public function getData ($file) {
215 $path = 'data/'.$file.'.json';
217 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
218 $data = $this->sentRequest($path, $fields, $cookie);
220 if (empty($data['body'])) {
221 throw new Exception('unable to get '.$file.' data');
224 $json = json_decode($data['body'], true);
230 * get the router syslog
234 public function getSyslog() {
235 $path = 'data/Syslog.json';
236 $fields = array('exporttype' => '0');
237 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
238 $data = $this->sentRequest($path, $fields, $cookie);
240 if (empty($data['body'])) {
241 throw new Exception('unable to get syslog data');
244 return explode("\n", $data['body']);
248 * get the Missed Calls from router
252 public function getMissedCalls() {
253 $path = 'data/ExportMissedCalls.json';
254 $fields = array('exporttype' => '1');
255 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
256 $data = $this->sentRequest($path, $fields, $cookie);
258 if (empty($data['body'])) {
259 throw new Exception('unable to get syslog data');
262 return explode("\n", $data['body']);
266 * get the Taken Calls from router
270 public function getTakenCalls() {
271 $path = 'data/ExportTakenCalls.json';
272 $fields = array('exporttype' => '2');
273 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
274 $data = $this->sentRequest($path, $fields, $cookie);
276 if (empty($data['body'])) {
277 throw new Exception('unable to get syslog data');
280 return explode("\n", $data['body']);
284 * get the Dialed Calls from router
288 public function getDialedCalls() {
289 $path = 'data/ExportDialedCalls.json';
290 $fields = array('exporttype' => '3');
291 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
292 $data = $this->sentRequest($path, $fields, $cookie);
294 if (empty($data['body'])) {
295 throw new Exception('unable to get syslog data');
298 return explode("\n", $data['body']);
306 public function reconnectLte () {
307 $path = 'data/modules.json';
308 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
309 $fields = $this->encrypt($fields);
310 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
311 $data = $this->sentRequest($path, $fields, $cookie, 2);
312 $json = json_decode($data['body'], true);
318 * reset the router to Factory Default
323 public function resetToFactoryDefault () {
324 $path = 'data/resetAllSetting.json';
325 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
326 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
327 $data = $this->sentRequest($path, $fields, $cookie);
328 $json = json_decode($data['body'], true);
335 * check if firmware is actual
339 public function checkFirmware () {
340 $path = 'data/checkfirmware.json';
341 $fields = array('checkfirmware' => 'true');
342 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
343 $data = $this->sentRequest($path, $fields, $cookie);
345 if (empty($data['body'])) {
346 throw new Exception('unable to get checkfirmware data');
349 $json = json_decode($data['body'], true);
355 * decrypt data from router
357 * @param string $data
360 private function decrypt ($data) {
361 require_once 'CryptLib/CryptLib.php';
362 $factory = new CryptLib\Cipher\
Factory();
363 $aes = $factory->getBlockCipher('rijndael-128');
365 $iv = hex2bin(substr($this->challenge
, 16, 16));
366 $adata = hex2bin(substr($this->challenge
, 32, 16));
367 $dkey = hex2bin($this->derivedk
);
368 $enc = hex2bin($data);
371 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
373 $mode->decrypt($enc);
375 return $mode->finish();
379 * decrypt data for the router
384 private function encrypt ($data) {
385 require_once 'CryptLib/CryptLib.php';
386 $factory = new CryptLib\Cipher\
Factory();
387 $aes = $factory->getBlockCipher('rijndael-128');
389 $iv = hex2bin(substr($this->challenge
, 16, 16));
390 $adata = hex2bin(substr($this->challenge
, 32, 16));
391 $dkey = hex2bin($this->derivedk
);
394 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
395 $mode->encrypt(http_build_query($data));
397 return bin2hex($mode->finish());
401 * get the values from array
403 * @param array $array
406 private function getValues($array) {
408 foreach ($array as $item) {
409 $data[$item['varid']] = $item['varvalue'];
416 * sends the request to router
418 * @param string $path
419 * @param mixed $fields
420 * @param string $cookie
421 * @param integer $count
424 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
425 $url = $this->url
.$path;
427 curl_setopt($ch, CURLOPT_URL
, $url);
429 if (!empty($fields)) {
430 if (is_array($fields)) {
431 curl_setopt($ch, CURLOPT_POST
, count($fields));
432 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
435 curl_setopt($ch, CURLOPT_POST
, $count);
436 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
440 if (!empty($cookie)) {
441 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
444 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
445 curl_setopt($ch, CURLOPT_HEADER
, true);
451 $result = curl_exec($ch);
453 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
454 $header = substr($result, 0, $header_size);
455 $body = substr($result, $header_size);
460 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
461 $body = preg_replace('/\'/i', '"', $body);
462 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
463 $body = preg_replace("/},\s+]/", "}\n]", $body);
465 return array('header' => $this->parse_headers($header), 'body' => $body);
473 private function getToken () {
474 $path = 'html/content/overview/index.html?lang=de';
476 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
477 $data = $this->sentRequest($path, $fields, $cookie);
479 if (empty($data['body'])) {
480 throw new Exception('unable to get csrf_token');
483 $a = explode('csrf_token = "', $data['body']);
484 $a = explode('";', $a[1]);
486 if (isset($a[0]) && !empty($a[0])) {
490 throw new Exception('unable to get csrf_token');
495 * parse the curl return header into an array
497 * @param string $response
500 private function parse_headers($response) {
502 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
504 $header_text = explode("\r\n", $header_text);
505 foreach ($header_text as $i => $line) {
507 $headers['http_code'] = $line;
510 list ($key, $value) = explode(': ', $line);
511 $headers[$key] = $value;