|
1 | 1 | # Challenge |
2 | 2 | ```php |
| 3 | +class FTP { |
| 4 | + public $sock; |
3 | 5 |
|
| 6 | + public function __construct($host, $port, $user, $pass) { |
| 7 | + $this->sock = fsockopen($host, $port); |
| 8 | + |
| 9 | + $this->login($user, $pass); |
| 10 | + $this->cleanInput(); |
| 11 | + $this->mode($_REQUEST['mode']); |
| 12 | + $this->send($_FILES['file']); |
| 13 | + } |
| 14 | + |
| 15 | + private function cleanInput() { |
| 16 | + $_GET = array_map('intval', $_GET); |
| 17 | + $_POST = array_map('intval', $_POST); |
| 18 | + $_COOKIE = array_map('intval', $_COOKIE); |
| 19 | + } |
| 20 | + |
| 21 | + public function login($username, $password) { |
| 22 | + fwrite($this->sock, "USER " . $username . "\n"); |
| 23 | + fwrite($this->sock, "PASS " . $password . "\n"); |
| 24 | + } |
| 25 | + |
| 26 | + public function mode($mode) { |
| 27 | + if ($mode == 1 || $mode == 2 || $mode == 3) { |
| 28 | + fputs($this->sock, "MODE $mode\n"); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public function send($data) { |
| 33 | + fputs($this->sock, $data); |
| 34 | + } |
| 35 | +} |
4 | 36 | ``` |
5 | 37 |
|
6 | 38 | # Solution |
| 39 | +This challenge contains two bugs that can be used together to inject data into the open FTP connection. The first bug is the usage of $_REQUEST in line 9 while only sanitizing $_GET and $_POST in lines 14 to 16. $_REQUEST is the combination of $_GET, $_POST, and $_COOKIE but it is only a copy of the values, not a reference. Therefore the sanitization of $_GET, $_POST, and $_COOKIE alone is not sufficient. A real world example of a vulnerability that is caused by a similar confusion can be found in our blog. |
| 40 | + |
| 41 | +The second bug is the usage of the type-unsafe comparison == instead of === in line 25. This enables an attacker to inject and execute new commands in the existing connection, for example a delete command with the query string ?mode=1%0a%0dDELETE%20test.file. |
7 | 42 |
|
8 | 43 | # Refference |
9 | | -+ php-security-calendar-2017 |
| 44 | ++ php-security-calendar-2017 Day 16 - Poem |
0 commit comments