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 if ($data[1]['varid'] == 'challengev') {
68 $this->challenge
= $data[1]['varvalue'];
73 * login into the router with the given password
75 * @param string $password
78 public function login ($password) {
79 $path = 'data/Login.json';
80 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
81 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
82 $data = $this->sentRequest($path, $fields);
83 $json = json_decode($data['body'], true);
84 if ($json[15]['varid'] == 'login' && $json[15]['varvalue'] == 'success') {
85 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
86 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
87 if (isset($match[1]) && !empty($match[1])) {
88 $this->session
= $match[1];
91 throw new Exception('unable to get the session cookie from the router');
95 $this->derivedk
= hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
98 $this->token
= $this->getToken();
112 public function logout () {
113 $path = 'data/Login.json';
114 $fields = array('logout' => 'byby');
115 $data = $this->sentRequest($path, $fields);
116 // reset challenge and session
117 $this->challenge
= '';
120 $json = json_decode($data['body'], true);
130 public function reboot () {
131 $path = 'data/Reboot.json';
132 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
133 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
134 $data = $this->sentRequest($path, $fields, $cookie);
136 $json = json_decode($data['body'], true);
142 * change dsl connection status
144 * @param string $status
146 public function changeConnectionStatus ($status) {
147 $path = 'data/Connect.json';
149 if ($status == 'online' ||
$status == 'offline') {
150 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
151 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
152 $this->sentRequest($path, $fields, $cookie);
155 throw new Exception();
160 * return the given json as array
162 * the following paths are known to be valid:
164 * /data/interfaces.json
167 * /data/dhcp_client.json
168 * /data/dhcp_server.json
172 * /data/igmp_proxy.json
173 * /data/igmp_snooping.json
179 * /data/bonding_client.json
180 * /data/bonding_tunnel.json
181 * /data/filterlist.json
182 * /data/bonding_tr181.json
185 * /data/Status.json (No login needed)
187 * @param string $file
190 public function getData ($file) {
191 $path = 'data/'.$file.'.json';
193 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
194 $data = $this->sentRequest($path, $fields, $cookie);
196 if (empty($data['body'])) {
197 throw new Exception('unable to get '.$file.' data');
200 $json = json_decode($data['body'], true);
206 * get the router syslog
210 public function getSyslog() {
211 $path = 'data/Syslog.json';
212 $fields = array('exporttype' => '0');
213 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
214 $data = $this->sentRequest($path, $fields, $cookie);
216 if (empty($data['body'])) {
217 throw new Exception('unable to get syslog data');
220 return explode("\n", $data['body']);
224 * get the Missed Calls from router
228 public function getMissedCalls() {
229 $path = 'data/ExportMissedCalls.json';
230 $fields = array('exporttype' => '1');
231 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
232 $data = $this->sentRequest($path, $fields, $cookie);
234 if (empty($data['body'])) {
235 throw new Exception('unable to get syslog data');
238 return explode("\n", $data['body']);
242 * get the Taken Calls from router
246 public function getTakenCalls() {
247 $path = 'data/ExportTakenCalls.json';
248 $fields = array('exporttype' => '2');
249 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
250 $data = $this->sentRequest($path, $fields, $cookie);
252 if (empty($data['body'])) {
253 throw new Exception('unable to get syslog data');
256 return explode("\n", $data['body']);
260 * get the Dialed Calls from router
264 public function getDialedCalls() {
265 $path = 'data/ExportDialedCalls.json';
266 $fields = array('exporttype' => '3');
267 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
268 $data = $this->sentRequest($path, $fields, $cookie);
270 if (empty($data['body'])) {
271 throw new Exception('unable to get syslog data');
274 return explode("\n", $data['body']);
282 public function reconnectLte () {
283 $path = 'data/modules.json';
284 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
285 $fields = $this->encrypt($fields);
286 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
287 $data = $this->sentRequest($path, $fields, $cookie, 2);
288 $json = json_decode($data['body'], true);
294 // i dont want test this :D, feel free to test it and report if it works or not
295 public function resetToFactoryDefault () {
296 $path = 'data/resetAllSetting.json';
297 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
298 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
299 $data = $this->sentRequest($path, $fields, $cookie);
300 $json = json_decode($data['body'], true);
307 * check if firmware is actual
311 public function checkFirmware () {
312 $path = 'data/checkfirmware.json';
313 $fields = array('checkfirmware' => 'true');
314 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
315 $data = $this->sentRequest($path, $fields, $cookie);
317 if (empty($data['body'])) {
318 throw new Exception('unable to get checkfirmware data');
321 $json = json_decode($data['body'], true);
327 * decrypt data from router
329 * @param string $data
332 public function decrypt ($data) {
333 require_once 'CryptLib/CryptLib.php';
334 $factory = new CryptLib\Cipher\
Factory();
335 $aes = $factory->getBlockCipher('rijndael-128');
337 $iv = hex2bin(substr($this->challenge
, 16, 16));
338 $adata = hex2bin(substr($this->challenge
, 32, 16));
339 $dkey = hex2bin($this->derivedk
);
340 $enc = hex2bin($data);
343 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
345 $mode->decrypt($enc);
347 return $mode->finish();
351 * decrypt data for the router
356 public function encrypt ($data) {
357 require_once 'CryptLib/CryptLib.php';
358 $factory = new CryptLib\Cipher\
Factory();
359 $aes = $factory->getBlockCipher('rijndael-128');
361 $iv = hex2bin(substr($this->challenge
, 16, 16));
362 $adata = hex2bin(substr($this->challenge
, 32, 16));
363 $dkey = hex2bin($this->derivedk
);
366 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
367 $mode->encrypt(http_build_query($data));
369 return bin2hex($mode->finish());
373 * sends the request to router
375 * @param string $path
376 * @param mixed $fields
377 * @param string $cookie
378 * @param integer $count
381 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
382 $url = $this->url
.$path;
384 curl_setopt($ch, CURLOPT_URL
, $url);
386 if (!empty($fields)) {
387 if (is_array($fields)) {
388 curl_setopt($ch, CURLOPT_POST
, count($fields));
389 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
392 curl_setopt($ch, CURLOPT_POST
, $count);
393 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
397 if (!empty($cookie)) {
398 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
401 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
402 curl_setopt($ch, CURLOPT_HEADER
, true);
409 $result = curl_exec($ch);
411 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
412 $header = substr($result, 0, $header_size);
413 $body = substr($result, $header_size);
418 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
419 $body = preg_replace('/\'/i', '"', $body);
420 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
421 $body = preg_replace("/},\s+]/", "}\n]", $body);
423 return array('header' => $this->parse_headers($header), 'body' => $body);
431 private function getToken () {
432 $path = 'html/content/overview/index.html?lang=de';
434 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
435 $data = $this->sentRequest($path, $fields, $cookie);
437 if (empty($data['body'])) {
438 throw new Exception('unable to get csrf_token');
441 $a = explode('csrf_token = "', $data['body']);
442 $a = explode('";', $a[1]);
444 if (isset($a[0]) && !empty($a[0])) {
448 throw new Exception('unable to get csrf_token');
453 * parse the curl return header into an array
455 * @param string $response
458 private function parse_headers($response) {
460 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
462 foreach (explode("\r\n", $header_text) as $i => $line) {
464 $headers['http_code'] = $line;
467 list ($key, $value) = explode(': ', $line);
468 $headers[$key] = $value;