Directory Traversal probleem
Ik ben bezig met een klein nieuw projectje waar ik een week geleden aan begonnen ben.
Het is cloudsoftware voor op het web geprogrammeerd in PHP. En ik ben dus bezig met de File Manager.
Iedere user krijgt een specifiek mapje op de cloud. Dat is de bedoeling, maar het is niet de bedoeling
dat ze verder uit hun eigen mapje om zo helemaal door het systeem heen te snuffelen. Ik heb al geprobeerd
om realpath() functie te gebruiken, maar hij gaat nogsteeds het user mapje uit. Heel irritant dus. Hier
de code van hoe het er nu uitziet:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$root_path = $_SERVER['DOCUMENT_ROOT']."/cloudsoftware/users/tim/";
$realbase = realpath($root_path);
function test()
{
GLOBAL $realbase;
if(isset($_GET['p']))
{
$_SESSION['dir'] = $_GET['p'];
echo "<script>window.location = '?';</script>";
}
if(empty($_SESSION['dir']))
{
$realbase = str_replace('\\', '/', $realbase);
} else {
$realbase = $_SESSION['dir'];
}
$exp = explode("/", $realbase);
foreach($exp as $x=>$dirx)
{
if(empty($dirx))
{
continue;
}
$do = "<a href='?p='>";
for($i=0;$i<=$x;$i++)
{
$do .= $exp[$i]."/";
}
$do .= "$dirx</a></li>\n";
}
chdir($realbase);
}
test();
?>
$root_path = $_SERVER['DOCUMENT_ROOT']."/cloudsoftware/users/tim/";
$realbase = realpath($root_path);
function test()
{
GLOBAL $realbase;
if(isset($_GET['p']))
{
$_SESSION['dir'] = $_GET['p'];
echo "<script>window.location = '?';</script>";
}
if(empty($_SESSION['dir']))
{
$realbase = str_replace('\\', '/', $realbase);
} else {
$realbase = $_SESSION['dir'];
}
$exp = explode("/", $realbase);
foreach($exp as $x=>$dirx)
{
if(empty($dirx))
{
continue;
}
$do = "<a href='?p='>";
for($i=0;$i<=$x;$i++)
{
$do .= $exp[$i]."/";
}
$do .= "$dirx</a></li>\n";
}
chdir($realbase);
}
test();
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
function get_root_dir()
{
// include dir
GLOBAL $realbase;
$folder_list = "";
$file_list = "";
$files = scandir($realbase);
foreach($files as $file)
{
if(($file != '.'))
{
if(is_dir($realbase.'/'.$file))
{
$folder_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-folder' style='color: #2dacff; font-size: 25px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>ddd</td>
</tr>";
if(empty(is_dir($realbase.'/'.$file)))
{
echo 'go back';
}
} elseif(is_file($realbase.'/'.$file)) {
$file_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-file' style='color: #2dacff; font-size: 27px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>Delete</td>
</tr>";
}
}
}
print $folder_list;
print $file_list;
}
// declare function
get_root_dir();
?>
function get_root_dir()
{
// include dir
GLOBAL $realbase;
$folder_list = "";
$file_list = "";
$files = scandir($realbase);
foreach($files as $file)
{
if(($file != '.'))
{
if(is_dir($realbase.'/'.$file))
{
$folder_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-folder' style='color: #2dacff; font-size: 25px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>ddd</td>
</tr>";
if(empty(is_dir($realbase.'/'.$file)))
{
echo 'go back';
}
} elseif(is_file($realbase.'/'.$file)) {
$file_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-file' style='color: #2dacff; font-size: 27px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>Delete</td>
</tr>";
}
}
}
print $folder_list;
print $file_list;
}
// declare function
get_root_dir();
?>
Heeft iemand een idee wat ik hier precies fout doe? Kan het even niet zo 1..2...3 zien :P
Gr Cocotj
Gewijzigd op 05/05/2022 18:23:40 door Cocotj bos
Gewijzigd op 05/05/2022 17:53:35 door - Ariën -
- Ariën - op 05/05/2022 17:53:02:
Kan je jouw code tussen code-tags plaatsen? Zie ook de Veelgestelde Vragen.
Hier tussen code tags:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$root_path = $_SERVER['DOCUMENT_ROOT']."/cloudsoftware/users/tim/";
$realbase = realpath($root_path);
function test()
{
GLOBAL $realbase;
if(isset($_GET['p']))
{
$_SESSION['dir'] = $_GET['p'];
echo "<script>window.location = '?';</script>";
}
if(empty($_SESSION['dir']))
{
$realbase = str_replace('\\', '/', $realbase);
} else {
$realbase = $_SESSION['dir'];
}
$exp = explode("/", $realbase);
foreach($exp as $x=>$dirx)
{
if(empty($dirx))
{
continue;
}
$do = "<a href='?p='>";
for($i=0;$i<=$x;$i++)
{
$do .= $exp[$i]."/";
}
$do .= "$dirx</a></li>\n";
}
chdir($realbase);
}
test();
?>
$root_path = $_SERVER['DOCUMENT_ROOT']."/cloudsoftware/users/tim/";
$realbase = realpath($root_path);
function test()
{
GLOBAL $realbase;
if(isset($_GET['p']))
{
$_SESSION['dir'] = $_GET['p'];
echo "<script>window.location = '?';</script>";
}
if(empty($_SESSION['dir']))
{
$realbase = str_replace('\\', '/', $realbase);
} else {
$realbase = $_SESSION['dir'];
}
$exp = explode("/", $realbase);
foreach($exp as $x=>$dirx)
{
if(empty($dirx))
{
continue;
}
$do = "<a href='?p='>";
for($i=0;$i<=$x;$i++)
{
$do .= $exp[$i]."/";
}
$do .= "$dirx</a></li>\n";
}
chdir($realbase);
}
test();
?>
dit zorgt ervoor dat er een sessie word aangemaakt als je in je File Manager zit,
En dat je beweegbaar door de bestanden heen kan. Maar je kan dus ook uit je usermap en dat is niet de bedoeling.
Hier een stuk code van hoe de bestanden worden gelist:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
function get_root_dir()
{
// include dir
GLOBAL $realbase;
$folder_list = "";
$file_list = "";
$files = scandir($realbase);
foreach($files as $file)
{
if(($file != '.'))
{
if(is_dir($realbase.'/'.$file))
{
$folder_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-folder' style='color: #2dacff; font-size: 25px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>ddd</td>
</tr>";
if(empty(is_dir($realbase.'/'.$file)))
{
echo 'go back';
}
} elseif(is_file($realbase.'/'.$file)) {
$file_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-file' style='color: #2dacff; font-size: 27px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>Delete</td>
</tr>";
}
}
}
print $folder_list;
print $file_list;
}
// declare function
get_root_dir();
?>
function get_root_dir()
{
// include dir
GLOBAL $realbase;
$folder_list = "";
$file_list = "";
$files = scandir($realbase);
foreach($files as $file)
{
if(($file != '.'))
{
if(is_dir($realbase.'/'.$file))
{
$folder_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-folder' style='color: #2dacff; font-size: 25px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>ddd</td>
</tr>";
if(empty(is_dir($realbase.'/'.$file)))
{
echo 'go back';
}
} elseif(is_file($realbase.'/'.$file)) {
$file_list .= "<tr>
<td><hr><a href='?p=".$realbase.'/'.$file."' style='text-decoration: none; color: #333; font-size: 17px;'><i class='fa-solid fa-file' style='color: #2dacff; font-size: 27px;'></i> $file</a></td>
<td><hr>".sizeFormat(filesize($realbase.'/'.$file))."</td>
<td><hr>".date ("F d Y H:i:s.", filemtime($realbase.'/'.$file))."</td>
<td><hr>Delete</td>
</tr>";
}
}
}
print $folder_list;
print $file_list;
}
// declare function
get_root_dir();
?>
Gewijzigd op 05/05/2022 18:09:19 door cocotj bos
path traversal mogelijk, bijvoorbeeld ook met '../' in de vorm '/foo/../bar/' ergens tussenin.
Je gebruikt wel '.' voor de huidige directory maar controleert niet op '..' voor een bovenliggende directory. Dat laatste maakt