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 (version_compare(phpversion(), '5.5.0', '<')) {
99 require_once 'CryptLib/CryptLib.php';
100 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2();
101 $this->derivedk
= $pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
104 $this->derivedk
= hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
107 // get the csrf_token
108 $this->token
= $this->getToken();
122 public function logout () {
123 $path = 'data/Login.json';
124 $fields = array('logout' => 'byby');
125 $data = $this->sentRequest($path, $fields);
126 // reset challenge and session
127 $this->challenge
= '';
131 $json = json_decode($data['body'], true);
141 public function reboot () {
142 $path = 'data/Reboot.json';
143 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
144 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
145 $data = $this->sentRequest($path, $fields, $cookie);
147 $json = json_decode($data['body'], true);
153 * change dsl connection status
155 * @param string $status
157 public function changeConnectionStatus ($status) {
158 $path = 'data/Connect.json';
160 if ($status == 'online' ||
$status == 'offline') {
161 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
162 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
163 $data = $this->sentRequest($path, $fields, $cookie);
165 $json = json_decode($this->decrypt($data['body']), true);
170 throw new Exception();
175 * return the given json as array
177 * @param string $file
180 public function getData ($file) {
181 $path = 'data/'.$file.'.json';
183 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
184 $data = $this->sentRequest($path, $fields, $cookie);
186 if (empty($data['body'])) {
187 throw new Exception('unable to get '.$file.' data');
190 $json = json_decode($data['body'], true);
196 * get the router syslog
200 public function getSyslog() {
201 $path = 'data/Syslog.json';
202 $fields = array('exporttype' => '0');
203 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
204 $data = $this->sentRequest($path, $fields, $cookie);
206 if (empty($data['body'])) {
207 throw new Exception('unable to get syslog data');
210 return explode("\n", $data['body']);
214 * get the Missed Calls from router
218 public function getMissedCalls() {
219 $path = 'data/ExportMissedCalls.json';
220 $fields = array('exporttype' => '1');
221 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
222 $data = $this->sentRequest($path, $fields, $cookie);
224 if (empty($data['body'])) {
225 throw new Exception('unable to get syslog data');
228 return explode("\n", $data['body']);
232 * get the Taken Calls from router
236 public function getTakenCalls() {
237 $path = 'data/ExportTakenCalls.json';
238 $fields = array('exporttype' => '2');
239 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
240 $data = $this->sentRequest($path, $fields, $cookie);
242 if (empty($data['body'])) {
243 throw new Exception('unable to get syslog data');
246 return explode("\n", $data['body']);
250 * get the Dialed Calls from router
254 public function getDialedCalls() {
255 $path = 'data/ExportDialedCalls.json';
256 $fields = array('exporttype' => '3');
257 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
258 $data = $this->sentRequest($path, $fields, $cookie);
260 if (empty($data['body'])) {
261 throw new Exception('unable to get syslog data');
264 return explode("\n", $data['body']);
272 public function reconnectLte () {
273 $path = 'data/modules.json';
274 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
275 $fields = $this->encrypt($fields);
276 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
277 $data = $this->sentRequest($path, $fields, $cookie, 2);
278 $json = json_decode($data['body'], true);
284 * reset the router to Factory Default
289 public function resetToFactoryDefault () {
290 $path = 'data/resetAllSetting.json';
291 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
292 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
293 $data = $this->sentRequest($path, $fields, $cookie);
294 $json = json_decode($data['body'], true);
301 * check if firmware is actual
305 public function checkFirmware () {
306 $path = 'data/checkfirmware.json';
307 $fields = array('checkfirmware' => 'true');
308 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
309 $data = $this->sentRequest($path, $fields, $cookie);
311 if (empty($data['body'])) {
312 throw new Exception('unable to get checkfirmware data');
315 $json = json_decode($data['body'], true);
321 * decrypt data from router
323 * @param string $data
326 private function decrypt ($data) {
327 require_once 'CryptLib/CryptLib.php';
328 $factory = new CryptLib\Cipher\
Factory();
329 $aes = $factory->getBlockCipher('rijndael-128');
331 $iv = hex2bin(substr($this->challenge
, 16, 16));
332 $adata = hex2bin(substr($this->challenge
, 32, 16));
333 $dkey = hex2bin($this->derivedk
);
334 $enc = hex2bin($data);
337 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
339 $mode->decrypt($enc);
341 return $mode->finish();
345 * decrypt data for the router
350 private function encrypt ($data) {
351 require_once 'CryptLib/CryptLib.php';
352 $factory = new CryptLib\Cipher\
Factory();
353 $aes = $factory->getBlockCipher('rijndael-128');
355 $iv = hex2bin(substr($this->challenge
, 16, 16));
356 $adata = hex2bin(substr($this->challenge
, 32, 16));
357 $dkey = hex2bin($this->derivedk
);
360 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
361 $mode->encrypt(http_build_query($data));
363 return bin2hex($mode->finish());
367 * get the values from array
369 * @param array $array
372 private function getValues($array) {
374 foreach ($array as $item) {
375 $data[$item['varid']] = $item['varvalue'];
382 * sends the request to router
384 * @param string $path
385 * @param mixed $fields
386 * @param string $cookie
387 * @param integer $count
390 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
391 $url = $this->url
.$path;
393 curl_setopt($ch, CURLOPT_URL
, $url);
395 if (!empty($fields)) {
396 if (is_array($fields)) {
397 curl_setopt($ch, CURLOPT_POST
, count($fields));
398 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
401 curl_setopt($ch, CURLOPT_POST
, $count);
402 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
406 if (!empty($cookie)) {
407 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
410 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
411 curl_setopt($ch, CURLOPT_HEADER
, true);
417 $result = curl_exec($ch);
419 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
420 $header = substr($result, 0, $header_size);
421 $body = substr($result, $header_size);
426 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
427 $body = preg_replace('/\'/i', '"', $body);
428 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
429 $body = preg_replace("/},\s+]/", "}\n]", $body);
431 return array('header' => $this->parse_headers($header), 'body' => $body);
439 private function getToken () {
440 $path = 'html/content/overview/index.html?lang=de';
442 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
443 $data = $this->sentRequest($path, $fields, $cookie);
445 if (empty($data['body'])) {
446 throw new Exception('unable to get csrf_token');
449 $a = explode('csrf_token = "', $data['body']);
450 $a = explode('";', $a[1]);
452 if (isset($a[0]) && !empty($a[0])) {
456 throw new Exception('unable to get csrf_token');
461 * parse the curl return header into an array
463 * @param string $response
466 private function parse_headers($response) {
468 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
470 $header_text = explode("\r\n", $header_text);
471 foreach ($header_text as $i => $line) {
473 $headers['http_code'] = $line;
476 list ($key, $value) = explode(': ', $line);
477 $headers[$key] = $value;