Skip to content

Commit d135ece

Browse files
committed
Updates php/challenge-71.md
Auto commit by GitBook Editor
1 parent b4f0a15 commit d135ece

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

php/challenge-71.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
# Challenge
2-
```php
2+
```php
3+
function getUser($id) {
4+
global $config, $db;
5+
if (!is_resource($db)) {
6+
$db = new MySQLi(
7+
$config['dbhost'],
8+
$config['dbuser'],
9+
$config['dbpass'],
10+
$config['dbname']
11+
);
12+
}
13+
$sql = "SELECT username FROM users WHERE id = ?";
14+
$stmt = $db->prepare($sql);
15+
$stmt->bind_param('i', $id);
16+
$stmt->bind_result($name);
17+
$stmt->execute();
18+
$stmt->fetch();
19+
return $name;
20+
}
21+
22+
$var = parse_url($_SERVER['HTTP_REFERER']);
23+
parse_str($var['query']);
24+
$currentUser = getUser($id);
25+
echo '<h1>'.htmlspecialchars($currentUser).'</h1>';
26+
327
```
428

529
# Solution
30+
This challenge suffers from a connection string injection vulnerability in line 4. It occurs because of the parse_str() call in line 21 that behaves very similar to register globals. Query parameters from the referrer are extracted to variables in the current scope, thus we can control the global variable $config inside of getUser() in lines 5 to 8. To exploit this vulnerability we can connect to our own MySQL server and return arbitrary values for username, for example with the referrer http://host/?config[dbhost]=10.0.0.5&config[dbuser]=root&config[dbpass]=root&config[dbname]=malicious&id=1.
631

732
# Refference
833
+ php-security-calendar-2017

0 commit comments

Comments
 (0)