3e40be4b1c7acc952a83751a0e5d56677c2a6344
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 $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 * @param string $file
173 public function getData ($file) {
174 $path = 'data/'.$file.'.json';
176 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
177 $data = $this->sentRequest($path, $fields, $cookie);
179 if (empty($data['body'])) {
180 throw new Exception('unable to get '.$file.' data');
183 $json = json_decode($data['body'], true);
189 * get the router syslog
193 public function getSyslog() {
194 $path = 'data/Syslog.json';
195 $fields = array('exporttype' => '0');
196 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
197 $data = $this->sentRequest($path, $fields, $cookie);
199 if (empty($data['body'])) {
200 throw new Exception('unable to get syslog data');
203 return explode("\n", $data['body']);
207 * get the Missed Calls from router
211 public function getMissedCalls() {
212 $path = 'data/ExportMissedCalls.json';
213 $fields = array('exporttype' => '1');
214 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
215 $data = $this->sentRequest($path, $fields, $cookie);
217 if (empty($data['body'])) {
218 throw new Exception('unable to get syslog data');
221 return explode("\n", $data['body']);
225 * get the Taken Calls from router
229 public function getTakenCalls() {
230 $path = 'data/ExportTakenCalls.json';
231 $fields = array('exporttype' => '2');
232 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
233 $data = $this->sentRequest($path, $fields, $cookie);
235 if (empty($data['body'])) {
236 throw new Exception('unable to get syslog data');
239 return explode("\n", $data['body']);
243 * get the Dialed Calls from router
247 public function getDialedCalls() {
248 $path = 'data/ExportDialedCalls.json';
249 $fields = array('exporttype' => '3');
250 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
251 $data = $this->sentRequest($path, $fields, $cookie);
253 if (empty($data['body'])) {
254 throw new Exception('unable to get syslog data');
257 return explode("\n", $data['body']);
265 public function reconnectLte () {
266 $path = 'data/modules.json';
267 $fields = array('csrf_token' => $this->token
, 'lte_reconn' => '1');
268 $fields = $this->encrypt($fields);
269 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
270 $data = $this->sentRequest($path, $fields, $cookie, 2);
271 $json = json_decode($data['body'], true);
277 * reset the router to Factory Default
282 public function resetToFactoryDefault () {
283 $path = 'data/resetAllSetting.json';
284 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reset_all' => 'true');
285 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
286 $data = $this->sentRequest($path, $fields, $cookie);
287 $json = json_decode($data['body'], true);
294 * check if firmware is actual
298 public function checkFirmware () {
299 $path = 'data/checkfirmware.json';
300 $fields = array('checkfirmware' => 'true');
301 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
302 $data = $this->sentRequest($path, $fields, $cookie);
304 if (empty($data['body'])) {
305 throw new Exception('unable to get checkfirmware data');
308 $json = json_decode($data['body'], true);
314 * decrypt data from router
316 * @param string $data
319 private function decrypt ($data) {
320 require_once 'CryptLib/CryptLib.php';
321 $factory = new CryptLib\Cipher\
Factory();
322 $aes = $factory->getBlockCipher('rijndael-128');
324 $iv = hex2bin(substr($this->challenge
, 16, 16));
325 $adata = hex2bin(substr($this->challenge
, 32, 16));
326 $dkey = hex2bin($this->derivedk
);
327 $enc = hex2bin($data);
330 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
332 $mode->decrypt($enc);
334 return $mode->finish();
338 * decrypt data for the router
343 private function encrypt ($data) {
344 require_once 'CryptLib/CryptLib.php';
345 $factory = new CryptLib\Cipher\
Factory();
346 $aes = $factory->getBlockCipher('rijndael-128');
348 $iv = hex2bin(substr($this->challenge
, 16, 16));
349 $adata = hex2bin(substr($this->challenge
, 32, 16));
350 $dkey = hex2bin($this->derivedk
);
353 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
354 $mode->encrypt(http_build_query($data));
356 return bin2hex($mode->finish());
360 * get the values from array
362 * @param array $array
365 private function getValues($array) {
367 foreach ($array as $item) {
368 $data[$item['varid']] = $item['varvalue'];
375 * sends the request to router
377 * @param string $path
378 * @param mixed $fields
379 * @param string $cookie
380 * @param integer $count
383 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
384 $url = $this->url
.$path;
386 curl_setopt($ch, CURLOPT_URL
, $url);
388 if (!empty($fields)) {
389 if (is_array($fields)) {
390 curl_setopt($ch, CURLOPT_POST
, count($fields));
391 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
394 curl_setopt($ch, CURLOPT_POST
, $count);
395 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
399 if (!empty($cookie)) {
400 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
403 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
404 curl_setopt($ch, CURLOPT_HEADER
, true);
410 $result = curl_exec($ch);
412 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
413 $header = substr($result, 0, $header_size);
414 $body = substr($result, $header_size);
419 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
420 $body = preg_replace('/\'/i', '"', $body);
421 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
422 $body = preg_replace("/},\s+]/", "}\n]", $body);
424 return array('header' => $this->parse_headers($header), 'body' => $body);
432 private function getToken () {
433 $path = 'html/content/overview/index.html?lang=de';
435 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
436 $data = $this->sentRequest($path, $fields, $cookie);
438 if (empty($data['body'])) {
439 throw new Exception('unable to get csrf_token');
442 $a = explode('csrf_token = "', $data['body']);
443 $a = explode('";', $a[1]);
445 if (isset($a[0]) && !empty($a[0])) {
449 throw new Exception('unable to get csrf_token');
454 * parse the curl return header into an array
456 * @param string $response
459 private function parse_headers($response) {
461 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
463 $header_text = explode("\r\n", $header_text);
464 foreach ($header_text as $i => $line) {
466 $headers['http_code'] = $line;
469 list ($key, $value) = explode(': ', $line);
470 $headers[$key] = $value;