4aa517d0d500bc55e985ec3509f0bc1dc441486a
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)
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[1]['varvalue'];
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 $this->derivedk
= hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
100 // get the csrf_token
101 $this->token
= $this->getToken();
115 public function logout () {
116 $path = 'data/Login.json';
117 $fields = array('logout' => 'byby');
118 $data = $this->sentRequest($path, $fields);
119 // reset challenge and session
120 $this->challenge
= '';
124 $json = json_decode($data['body'], true);
134 public function reboot () {
135 $path = 'data/Reboot.json';
136 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
137 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
138 $data = $this->sentRequest($path, $fields, $cookie);
140 $json = json_decode($data['body'], true);
146 * change dsl connection status
148 * @param string $status
150 public function changeConnectionStatus ($status) {
151 $path = 'data/Connect.json';
153 if ($status == 'online' ||
$status == 'offline') {
154 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
155 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
156 $data = $this->sentRequest($path, $fields, $cookie);
158 $json = json_decode($this->decrypt($data['body']), true);
163 throw new Exception();
168 * return the given json as array
170 * the following paths are known to be valid:
172 * /data/interfaces.json
175 * /data/dhcp_client.json
176 * /data/dhcp_server.json
180 * /data/igmp_proxy.json
181 * /data/igmp_snooping.json
187 * /data/bonding_client.json
188 * /data/bonding_tunnel.json
189 * /data/filterlist.json
190 * /data/bonding_tr181.json
193 * /data/Status.json (No login needed)
195 * @param string $file
198 public function getData ($file) {
199 $path = 'data/'.$file.'.json';
201 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
202 $data = $this->sentRequest($path, $fields, $cookie);
204 if (empty($data['body'])) {
205 throw new Exception('unable to get '.$file.' data');
208 $json = json_decode($data['body'], true);
214 * get the router syslog
218 public function getSyslog() {
219 $path = 'data/Syslog.json';
220 $fields = array('exporttype' => '0');
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 Missed Calls from router
236 public function getMissedCalls() {
237 $path = 'data/ExportMissedCalls.json';
238 $fields = array('exporttype' => '1');
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 Taken Calls from router
254 public function getTakenCalls() {
255 $path = 'data/ExportTakenCalls.json';
256 $fields = array('exporttype' => '2');
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']);
268 * get the Dialed Calls from router
272 public function getDialedCalls() {
273 $path = 'data/ExportDialedCalls.json';
274 $fields = array('exporttype' => '3');
275 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
276 $data = $this->sentRequest($path, $fields, $cookie);
278 if (empty($data['body'])) {
279 throw new Exception('unable to get syslog data');
282 return explode("\n", $data['body']);
290 public function reconnectLte () {
291 $path = 'data/modules.json';
292 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
293 $fields = $this->encrypt($fields);
294 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
295 $data = $this->sentRequest($path, $fields, $cookie, 2);
296 $json = json_decode($data['body'], true);
302 // i dont want test this :D, feel free to test it and report if it works or not
303 public function resetToFactoryDefault () {
304 $path = 'data/resetAllSetting.json';
305 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
306 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
307 $data = $this->sentRequest($path, $fields, $cookie);
308 $json = json_decode($data['body'], true);
315 * check if firmware is actual
319 public function checkFirmware () {
320 $path = 'data/checkfirmware.json';
321 $fields = array('checkfirmware' => 'true');
322 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
323 $data = $this->sentRequest($path, $fields, $cookie);
325 if (empty($data['body'])) {
326 throw new Exception('unable to get checkfirmware data');
329 $json = json_decode($data['body'], true);
335 * decrypt data from router
337 * @param string $data
340 private function decrypt ($data) {
341 require_once 'CryptLib/CryptLib.php';
342 $factory = new CryptLib\Cipher\
Factory();
343 $aes = $factory->getBlockCipher('rijndael-128');
345 $iv = hex2bin(substr($this->challenge
, 16, 16));
346 $adata = hex2bin(substr($this->challenge
, 32, 16));
347 $dkey = hex2bin($this->derivedk
);
348 $enc = hex2bin($data);
351 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
353 $mode->decrypt($enc);
355 return $mode->finish();
359 * decrypt data for the router
364 private function encrypt ($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
);
374 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
375 $mode->encrypt(http_build_query($data));
377 return bin2hex($mode->finish());
381 * get the values from array
383 * @param array $array
386 private function getValues($array) {
388 foreach ($array as $item) {
389 $data[$item['varid']] = $item['varvalue'];
396 * sends the request to router
398 * @param string $path
399 * @param mixed $fields
400 * @param string $cookie
401 * @param integer $count
404 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
405 $url = $this->url
.$path;
407 curl_setopt($ch, CURLOPT_URL
, $url);
409 if (!empty($fields)) {
410 if (is_array($fields)) {
411 curl_setopt($ch, CURLOPT_POST
, count($fields));
412 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
415 curl_setopt($ch, CURLOPT_POST
, $count);
416 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
420 if (!empty($cookie)) {
421 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
424 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
425 curl_setopt($ch, CURLOPT_HEADER
, true);
432 $result = curl_exec($ch);
434 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
435 $header = substr($result, 0, $header_size);
436 $body = substr($result, $header_size);
441 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
442 $body = preg_replace('/\'/i', '"', $body);
443 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
444 $body = preg_replace("/},\s+]/", "}\n]", $body);
446 return array('header' => $this->parse_headers($header), 'body' => $body);
454 private function getToken () {
455 $path = 'html/content/overview/index.html?lang=de';
457 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
458 $data = $this->sentRequest($path, $fields, $cookie);
460 if (empty($data['body'])) {
461 throw new Exception('unable to get csrf_token');
464 $a = explode('csrf_token = "', $data['body']);
465 $a = explode('";', $a[1]);
467 if (isset($a[0]) && !empty($a[0])) {
471 throw new Exception('unable to get csrf_token');
476 * parse the curl return header into an array
478 * @param string $response
481 private function parse_headers($response) {
483 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
485 foreach (explode("\r\n", $header_text) as $i => $line) {
487 $headers['http_code'] = $line;
490 list ($key, $value) = explode(': ', $line);
491 $headers[$key] = $value;