Commit | Line | Data |
---|---|---|
a91317a6 | 1 | <?php |
1d934afe S |
2 | /** |
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) | |
6 | */ | |
a91317a6 S |
7 | class speedport { |
8 | /** | |
9 | * password-challenge | |
10 | * @var string | |
11 | */ | |
12 | private $challenge = ''; | |
13 | ||
809e25bd S |
14 | /** |
15 | * csrf_token | |
16 | * @var string | |
17 | */ | |
18 | private $token = ''; | |
19 | ||
a91317a6 S |
20 | /** |
21 | * hashed password | |
22 | * @var string | |
23 | */ | |
24 | private $hash = ''; | |
25 | ||
26 | /** | |
27 | * session cookie | |
28 | * @var string | |
29 | */ | |
30 | private $session = ''; | |
31 | ||
32 | /** | |
33 | * router url | |
34 | * @var string | |
35 | */ | |
b5a532a8 | 36 | private $url = ''; |
a91317a6 | 37 | |
c9e082da S |
38 | /** |
39 | * derivedk cookie | |
40 | * @var string | |
41 | */ | |
42 | private $derivedk = ''; | |
43 | ||
b5a532a8 S |
44 | public function __construct ($password, $url = 'http://speedport.ip/') { |
45 | $this->url = $url; | |
a91317a6 S |
46 | $this->getChallenge(); |
47 | ||
48 | if (empty($this->challenge)) { | |
49 | throw new Exception('unable to get the challenge from the router'); | |
50 | } | |
51 | ||
52 | $login = $this->login($password); | |
53 | ||
54 | if ($login === false) { | |
55 | throw new Exception('unable to login'); | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * Requests the password-challenge from the router. | |
61 | */ | |
62 | public function getChallenge () { | |
b5a532a8 | 63 | $path = 'data/Login.json'; |
a91317a6 | 64 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null'); |
b5a532a8 | 65 | $data = $this->sentRequest($path, $fields); |
a91317a6 S |
66 | $data = json_decode($data['body'], true); |
67 | if ($data[1]['varid'] == 'challengev') { | |
68 | $this->challenge = $data[1]['varvalue']; | |
69 | } | |
70 | } | |
71 | ||
72 | /** | |
73 | * login into the router with the given password | |
5e44cffa | 74 | * |
a91317a6 S |
75 | * @param string $password |
76 | * @return boolean | |
77 | */ | |
78 | public function login ($password) { | |
b5a532a8 | 79 | $path = 'data/Login.json'; |
a91317a6 S |
80 | $this->hash = hash('sha256', $this->challenge.':'.$password); |
81 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash); | |
b5a532a8 | 82 | $data = $this->sentRequest($path, $fields); |
a91317a6 S |
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]; | |
89 | } | |
90 | else { | |
91 | throw new Exception('unable to get the session cookie from the router'); | |
92 | } | |
93 | ||
c9e082da S |
94 | // calculate derivedk |
95 | $this->derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32); | |
96 | ||
809e25bd S |
97 | // get the csrf_token |
98 | $this->token = $this->getToken(); | |
99 | ||
a91317a6 S |
100 | return true; |
101 | } | |
102 | } | |
103 | ||
104 | return false; | |
105 | } | |
106 | ||
107 | /** | |
108 | * logout | |
5e44cffa | 109 | * |
a91317a6 S |
110 | * @return array |
111 | */ | |
112 | public function logout () { | |
b5a532a8 | 113 | $path = 'data/Login.json'; |
a91317a6 | 114 | $fields = array('logout' => 'byby'); |
b5a532a8 | 115 | $data = $this->sentRequest($path, $fields); |
a91317a6 S |
116 | // reset challenge and session |
117 | $this->challenge = ''; | |
118 | $this->session = ''; | |
119 | ||
120 | $json = json_decode($data['body'], true); | |
121 | ||
122 | return $json; | |
123 | } | |
124 | ||
125 | /** | |
126 | * reboot the router | |
5e44cffa | 127 | * |
a91317a6 S |
128 | * @return array |
129 | */ | |
130 | public function reboot () { | |
b5a532a8 | 131 | $path = 'data/Reboot.json'; |
a91317a6 S |
132 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reboot_device' => 'true'); |
133 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 134 | $data = $this->sentRequest($path, $fields, $cookie); |
14d4f286 | 135 | |
a91317a6 S |
136 | $json = json_decode($data['body'], true); |
137 | ||
138 | return $json; | |
139 | } | |
140 | ||
87026df3 S |
141 | /** |
142 | * change dsl connection status | |
5e44cffa | 143 | * |
87026df3 S |
144 | * @param string $status |
145 | */ | |
146 | public function changeConnectionStatus ($status) { | |
b5a532a8 | 147 | $path = 'data/Connect.json'; |
87026df3 S |
148 | |
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; | |
b5a532a8 | 152 | $this->sentRequest($path, $fields, $cookie); |
87026df3 S |
153 | } |
154 | else { | |
155 | throw new Exception(); | |
156 | } | |
157 | } | |
158 | ||
a91317a6 S |
159 | /** |
160 | * return the given json as array | |
5e44cffa | 161 | * |
a91317a6 S |
162 | * the following paths are known to be valid: |
163 | * /data/dsl.json | |
164 | * /data/interfaces.json | |
165 | * /data/arp.json | |
166 | * /data/session.json | |
167 | * /data/dhcp_client.json | |
168 | * /data/dhcp_server.json | |
169 | * /data/ipv6.json | |
170 | * /data/dns.json | |
171 | * /data/routing.json | |
172 | * /data/igmp_proxy.json | |
173 | * /data/igmp_snooping.json | |
174 | * /data/wlan.json | |
175 | * /data/module.json | |
176 | * /data/memory.json | |
177 | * /data/speed.json | |
178 | * /data/webdav.json | |
179 | * /data/bonding_client.json | |
180 | * /data/bonding_tunnel.json | |
181 | * /data/filterlist.json | |
182 | * /data/bonding_tr181.json | |
183 | * /data/letinfo.json | |
5e44cffa | 184 | * |
a91317a6 | 185 | * /data/Status.json (No login needed) |
5e44cffa | 186 | * |
a91317a6 S |
187 | * @param string $file |
188 | * @return array | |
189 | */ | |
190 | public function getData ($file) { | |
b5a532a8 | 191 | $path = 'data/'.$file.'.json'; |
a91317a6 S |
192 | $fields = array(); |
193 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 194 | $data = $this->sentRequest($path, $fields, $cookie); |
a91317a6 S |
195 | |
196 | if (empty($data['body'])) { | |
197 | throw new Exception('unable to get '.$file.' data'); | |
198 | } | |
199 | ||
200 | $json = json_decode($data['body'], true); | |
201 | ||
202 | return $json; | |
203 | } | |
204 | ||
5e44cffa S |
205 | /** |
206 | * get the router syslog | |
207 | * | |
208 | * @return array | |
209 | */ | |
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); | |
215 | ||
216 | if (empty($data['body'])) { | |
217 | throw new Exception('unable to get syslog data'); | |
218 | } | |
219 | ||
220 | return explode("\n", $data['body']); | |
221 | } | |
222 | ||
a35e3e67 S |
223 | /** |
224 | * get the Missed Calls from router | |
225 | * | |
226 | * @return array | |
227 | */ | |
228 | public function getMissedCalls() { | |
abf641bb | 229 | $path = 'data/ExportMissedCalls.json'; |
a35e3e67 S |
230 | $fields = array('exporttype' => '1'); |
231 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
232 | $data = $this->sentRequest($path, $fields, $cookie); | |
233 | ||
234 | if (empty($data['body'])) { | |
235 | throw new Exception('unable to get syslog data'); | |
236 | } | |
237 | ||
238 | return explode("\n", $data['body']); | |
239 | } | |
240 | ||
15260eeb S |
241 | /** |
242 | * get the Taken Calls from router | |
243 | * | |
244 | * @return array | |
245 | */ | |
246 | public function getTakenCalls() { | |
abf641bb | 247 | $path = 'data/ExportTakenCalls.json'; |
15260eeb S |
248 | $fields = array('exporttype' => '2'); |
249 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
250 | $data = $this->sentRequest($path, $fields, $cookie); | |
251 | ||
252 | if (empty($data['body'])) { | |
253 | throw new Exception('unable to get syslog data'); | |
254 | } | |
255 | ||
256 | return explode("\n", $data['body']); | |
257 | } | |
258 | ||
abf641bb S |
259 | /** |
260 | * get the Dialed Calls from router | |
261 | * | |
262 | * @return array | |
263 | */ | |
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); | |
9bd25bb4 | 269 | |
abf641bb S |
270 | if (empty($data['body'])) { |
271 | throw new Exception('unable to get syslog data'); | |
272 | } | |
273 | ||
274 | return explode("\n", $data['body']); | |
275 | } | |
276 | ||
809e25bd S |
277 | /** |
278 | * reconnect LTE | |
279 | * | |
280 | * @return array | |
281 | */ | |
c9e082da S |
282 | public function reconnectLte () { |
283 | $path = 'data/modules.json'; | |
809e25bd S |
284 | $fields = array('csrf_token' => $this->token, 'lte_reconn' => '1'); |
285 | $fields = $this->encrypt($fields); | |
c9e082da | 286 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; |
809e25bd | 287 | $data = $this->sentRequest($path, $fields, $cookie, 2); |
c9e082da S |
288 | $json = json_decode($data['body'], true); |
289 | ||
290 | return $json; | |
291 | } | |
809e25bd | 292 | |
9bd25bb4 | 293 | /* |
809e25bd | 294 | // i dont want test this :D, feel free to test it and report if it works or not |
9bd25bb4 S |
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); | |
301 | ||
302 | return $json; | |
303 | } | |
304 | */ | |
305 | ||
4ae464d1 S |
306 | /** |
307 | * check if firmware is actual | |
308 | * | |
309 | * @return array | |
310 | */ | |
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); | |
316 | ||
317 | if (empty($data['body'])) { | |
318 | throw new Exception('unable to get checkfirmware data'); | |
319 | } | |
320 | ||
321 | $json = json_decode($data['body'], true); | |
322 | ||
323 | return $json; | |
324 | } | |
325 | ||
14d4f286 S |
326 | /** |
327 | * decrypt data from router | |
328 | * | |
329 | * @param string $data | |
330 | * @return array | |
331 | */ | |
332 | public function decrypt ($data) { | |
333 | require_once 'CryptLib/CryptLib.php'; | |
334 | $factory = new CryptLib\Cipher\Factory(); | |
335 | $aes = $factory->getBlockCipher('rijndael-128'); | |
336 | ||
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); | |
341 | ||
342 | $aes->setKey($dkey); | |
343 | $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); | |
344 | ||
345 | $mode->decrypt($enc); | |
346 | ||
347 | return $mode->finish(); | |
348 | } | |
349 | ||
350 | /** | |
351 | * decrypt data for the router | |
352 | * | |
353 | * @param array $data | |
354 | * @return string | |
355 | */ | |
356 | public function encrypt ($data) { | |
357 | require_once 'CryptLib/CryptLib.php'; | |
358 | $factory = new CryptLib\Cipher\Factory(); | |
359 | $aes = $factory->getBlockCipher('rijndael-128'); | |
360 | ||
361 | $iv = hex2bin(substr($this->challenge, 16, 16)); | |
362 | $adata = hex2bin(substr($this->challenge, 32, 16)); | |
363 | $dkey = hex2bin($this->derivedk); | |
364 | ||
365 | $aes->setKey($dkey); | |
366 | $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]); | |
367 | $mode->encrypt(http_build_query($data)); | |
368 | ||
809e25bd | 369 | return bin2hex($mode->finish()); |
14d4f286 S |
370 | } |
371 | ||
a91317a6 S |
372 | /** |
373 | * sends the request to router | |
5e44cffa | 374 | * |
b5a532a8 | 375 | * @param string $path |
809e25bd | 376 | * @param mixed $fields |
a91317a6 | 377 | * @param string $cookie |
809e25bd | 378 | * @param integer $count |
a91317a6 S |
379 | * @return array |
380 | */ | |
809e25bd | 381 | private function sentRequest ($path, $fields, $cookie = '', $count = 0) { |
b5a532a8 | 382 | $url = $this->url.$path; |
a91317a6 S |
383 | $ch = curl_init(); |
384 | curl_setopt($ch, CURLOPT_URL, $url); | |
385 | ||
386 | if (!empty($fields)) { | |
809e25bd S |
387 | if (is_array($fields)) { |
388 | curl_setopt($ch, CURLOPT_POST, count($fields)); | |
389 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
390 | } | |
391 | else { | |
392 | curl_setopt($ch, CURLOPT_POST, $count); | |
393 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); | |
394 | } | |
a91317a6 S |
395 | } |
396 | ||
397 | if (!empty($cookie)) { | |
398 | curl_setopt($ch, CURLOPT_COOKIE, $cookie); | |
399 | } | |
400 | ||
401 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
402 | curl_setopt($ch, CURLOPT_HEADER, true); | |
403 | ||
404 | ||
405 | if ($cookie) { | |
406 | ||
407 | } | |
408 | ||
409 | $result = curl_exec($ch); | |
410 | ||
411 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
412 | $header = substr($result, 0, $header_size); | |
413 | $body = substr($result, $header_size); | |
414 | curl_close($ch); | |
415 | ||
416 | // fix invalid json | |
14d4f286 | 417 | |
a91317a6 S |
418 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); |
419 | $body = preg_replace('/\'/i', '"', $body); | |
5e44cffa | 420 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
dae16c50 | 421 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
a91317a6 S |
422 | |
423 | return array('header' => $this->parse_headers($header), 'body' => $body); | |
424 | } | |
425 | ||
809e25bd S |
426 | /** |
427 | * get the csrf_token | |
428 | * | |
429 | * @return string | |
430 | */ | |
431 | private function getToken () { | |
432 | $path = 'html/content/overview/index.html?lang=de'; | |
433 | $fields = array(); | |
434 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
435 | $data = $this->sentRequest($path, $fields, $cookie); | |
436 | ||
437 | if (empty($data['body'])) { | |
438 | throw new Exception('unable to get csrf_token'); | |
439 | } | |
440 | ||
441 | $a = explode('csrf_token = "', $data['body']); | |
442 | $a = explode('";', $a[1]); | |
443 | ||
444 | if (isset($a[0]) && !empty($a[0])) { | |
445 | return $a[0]; | |
446 | } | |
447 | else { | |
448 | throw new Exception('unable to get csrf_token'); | |
449 | } | |
450 | } | |
451 | ||
a91317a6 S |
452 | /** |
453 | * parse the curl return header into an array | |
5e44cffa | 454 | * |
a91317a6 S |
455 | * @param string $response |
456 | * @return array | |
457 | */ | |
458 | private function parse_headers($response) { | |
459 | $headers = array(); | |
460 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
461 | ||
462 | foreach (explode("\r\n", $header_text) as $i => $line) { | |
463 | if ($i === 0) { | |
464 | $headers['http_code'] = $line; | |
465 | } | |
466 | else { | |
467 | list ($key, $value) = explode(': ', $line); | |
468 | $headers[$key] = $value; | |
469 | } | |
470 | } | |
471 | ||
472 | return $headers; | |
473 | } | |
474 | } |