🌐 This is an automatic translation of a topic from Underc0de, the largest Spanish-speaking hacking and IT community. Read the original (Spanish) and reply →
rollth ·
Hello Underc0ders, in this case I come to explain how you can exploit a vulnerability in an upload.php. There is more information about this on the forum, but what I've seen is either in video or not explained the way I would have liked it to be explained to me.<br /><br />The first thing we have to know is what a php uploader is.<br /><br />
CitarA php uploader is a piece of code that will allow us to upload a file to a web page.
<br /><br />In this case our vulnerable test code will be the following (The code is not mine, it is Los enlaces son solo para miembros. Registrate o iniciá sesión modified).<br /><br />Código: php
<html> <br /><head> <br /><style type="text/css"> <br />.upload{ <br />background:#e7e7e7; <br />box-shadow:0px 0px 10px black; <br />width:500px; <br />height:200px; <br />margin-right:auto; <br />margin-left:auto; <br />border-radius:20px; <br /><br />} <br />form{ <br /> margin: 126px auto 0; <br /> width: 225px; <br /> } <br /> label{ <br /> display: block; <br /> } <br /> input[type="file"]{ <br /> display: block; <br /> margin: 8px 0; <br /> } <br /> div.resultado{ <br /> margin: 25px auto 0; <br /> width: 225px; <br /> } <br /> div.resultado img{ <br /> border: 2px solid #EEEEEE; <br /> height: auto; <br /> width: 225px; <br /> } <br /></style> <br /></head> <br /><body> <br /><div class="upload"> <br /><form action="" method="post" enctype="multipart/form-data"> <br /> <br><br>Upload a file: <br /> <input type="file" name="archivo" id="archivo" /> <br> <br /> <input type="submit" name="boton" value="Subir" /> <br /></form> <br /><div> <br /><div class="resultado"> <br /><?php <br />if(isset($_POST['boton'])){ <br /> // We make a condition in which we only allow images to be uploaded and that they are smaller than 20 KB<br /> if ((true==true) && ($_FILES["archivo"]["size"] < 200000000)) { <br /> <br /> //If there was an error in the upload, show it, from the $_FILES variable we can extract the value of [error], which stores a boolean value (1 or 0).<br /> if ($_FILES["archivo"]["error"] > 0) { <br /> echo $_FILES["archivo"]["error"] . "<br />"; <br /> } else { <br /> // If there was no error, we make another condition to make sure the file is not duplicated<br /> if (file_exists("archivos/" . $_FILES["archivo"]["name"])) { <br /> echo $_FILES["archivo"]["name"] . " ya existe. "; <br /> } else { <br /> // If it is not a duplicate file and there was no error, we proceed to upload to the /archivos folder, after that we show the uploaded image<br /> move_uploaded_file($_FILES["archivo"]["tmp_name"], <br /> "archivos/" . $_FILES["archivo"]["name"]); <br /> echo "Archivo Subido <br />"; <br /> echo "<img src='archivos/".$_FILES["archivo"]["name"]."' />"; <br /> } <br /> } <br /> } else { <br /> echo "Archivo no permitido"; <br /> } <br />} <br />?> <br /></div> <br /></body> <br /></html>
<br /><br />This code lets us upload files of any extension and this is where we are going to take advantage. In this case I am going to upload a shell Los enlaces son solo para miembros. Registrate o iniciá sesión, which is very intuitive to handle.<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />Now we just have to go to the directory where the malicious file is, in this case: "/archivos/c99.php". <br /><br />Here I leave a picture of the shell. The operation, as I said, is very intuitive.<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />(I know I have to activate Windows xD)<br /><br />Now I am going to show a filter that YOU MUST NOT USE and how to do a bypass, that is, to skip the filter. (After that I will show the final filter).<br /><br />Código: php
<html> <br /><head> <br /><style type="text/css"> <br />.upload{ <br />background:#e7e7e7; <br />box-shadow:0px 0px 10px black; <br />width:500px; <br />height:200px; <br />margin-right:auto; <br />margin-left:auto; <br />border-radius:20px; <br /><br />} <br />form{ <br /> margin: 126px auto 0; <br /> width: 225px; <br /> } <br /> label{ <br /> display: block; <br /> } <br /> input[type="file"]{ <br /> display: block; <br /> margin: 8px 0; <br /> } <br /> div.resultado{ <br /> margin: 25px auto 0; <br /> width: 225px; <br /> } <br /> div.resultado img{ <br /> border: 2px solid #EEEEEE; <br /> height: auto; <br /> width: 225px; <br /> } <br /></style> <br /></head> <br /><body> <br /><div class="upload"> <br /><form action="" method="post" enctype="multipart/form-data"> <br /> <br><br>Upload a file: <br /> <input type="file" name="archivo" id="archivo" /> <br> <br /> <input type="submit" name="boton" value="Subir" /> <br /></form> <br /><div> <br /><div class="resultado"> <br /><?php <br />if(isset($_POST['boton'])){ <br /> // We make a condition in which we only allow images to be uploaded and that they are smaller than 20 KB<br /> if ((($_FILES["archivo"]["type"] == "image/gif") || <br /> ($_FILES["archivo"]["type"] == "image/jpeg") || <br /> ($_FILES["archivo"]["type"] == "image/pjpeg")) && <br /> ($_FILES["archivo"]["size"] < 20000)) { <br /> <br /> //If there was an error in the upload, show it, from the $_FILES variable we can extract the value of [error], which stores a boolean value (1 or 0).<br /> if ($_FILES["archivo"]["error"] > 0) { <br /> echo $_FILES["archivo"]["error"] . "<br />"; <br /> } else { <br /> // If there was no error, we make another condition to make sure the file is not duplicated<br /> if (file_exists("archivos/" . $_FILES["archivo"]["name"])) { <br /> echo $_FILES["archivo"]["name"] . " ya existe. "; <br /> } else { <br /> // If it is not a duplicate file and there was no error, we proceed to upload to the /archivos folder, after that we show the uploaded image<br /> move_uploaded_file($_FILES["archivo"]["tmp_name"], <br /> "archivos/" . $_FILES["archivo"]["name"]); <br /> echo "Archivo Subido <br />"; <br /> echo "<img src='archivos/".$_FILES["archivo"]["name"]."' />"; <br /> } <br /> } <br /> } else { <br /> echo "Archivo no permitido"; <br /> } <br />} <br />?> <br /></div> <br /></body> <br /></html>
<br /><br />If you look at these lines:<br /> Código: php
<br /><br />What the php does is check the content type of the file and does not check the file's extension (whether it is php, html, etc). To bypass this filter what you have to do is the following:<br /><br />1º You have to download firefox, since we are going to use a firefox Los enlaces son solo para miembros. Registrate o iniciá sesión for this test.<br /><br />2º You have to rename the file c99.php to c99.jpg, since it is one of the file types that this uploader allows us to upload.<br /><br />3º On the website we click browse and set our file c99.jpg (DO NOT CLICK UPLOAD YET). It should look like this.<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />4º We start Tamper Data. (It is in the menu at the top in the toolbar).<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />5º We click start modification.<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />6º Now we can click to send the file, a little screen will appear and we have to click modify. <br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />7º Then another screen will appear and we have to modify the code on the right.<br /><br />Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />In that code you have to look for something of this style.<br /><br />Código: text
<br /><br />And change "c99.jpg" to c99.php. You click send and that's it. This will bypass the filter since even though you are uploading the file with the .php extension the content type is that of a jpg, and the upload code checks the content type and not the extension.<br /><br />Finally, one way to fix this would be to add these lines.<br /><br />The code has been edited because as @seth said it was still vulnerable.<br /><br />Código: php
<br /><br />With which the final code would look like this.<br /><br />Código: php
<html> <br /><head> <br /><style type="text/css"> <br />.upload{ <br />background:#e7e7e7; <br />box-shadow:0px 0px 10px black; <br />width:500px; <br />height:200px; <br />margin-right:auto; <br />margin-left:auto; <br />border-radius:20px; <br /><br />} <br />form{ <br /> margin: 126px auto 0; <br /> width: 225px; <br /> } <br /> label{ <br /> display: block; <br /> } <br /> input[type="file"]{ <br /> display: block; <br /> margin: 8px 0; <br /> } <br /> div.resultado{ <br /> margin: 25px auto 0; <br /> width: 225px; <br /> } <br /> div.resultado img{ <br /> border: 2px solid #EEEEEE; <br /> height: auto; <br /> width: 225px; <br /> } <br /></style> <br /></head> <br /><body> <br /><div class="upload"> <br /><form action="" method="post" enctype="multipart/form-data"> <br /> <br><br>Upload a file: <br /> <input type="file" name="archivo" id="archivo" /> <br> <br /> <input type="submit" name="boton" value="Subir" /> <br /></form> <br /><div> <br /><div class="resultado"> <br /><?php <br /><br />$blacklist = array(".php", ".phtml", ".php3", ".php4" , ".html", "htaccess");<br /> <br /> foreach ($blacklist as $item) {<br /> if(preg_match("/$item\$/i", $_FILES['userfile']['name'])) {<br /> echo "No se permiten archivos PHP";<br /> exit;<br /> }<br />}<br />if(isset($_POST['boton'])){ <br /> // We make a condition in which we only allow images to be uploaded and that they are smaller than 20 KB<br /> if ((($_FILES["archivo"]["type"] == "image/gif") || <br /> ($_FILES["archivo"]["type"] == "image/jpeg") || <br /> ($_FILES["archivo"]["type"] == "image/pjpeg")) && <br /> ($_FILES["archivo"]["size"] < 200000000)) { <br /> <br /> //If there was an error in the upload, show it, from the $_FILES variable we can extract the value of [error], which stores a boolean value (1 or 0).<br /> if ($_FILES["archivo"]["error"] > 0) { <br /> echo $_FILES["archivo"]["error"] . "<br />"; <br /> } else { <br /> // If there was no error, we make another condition to make sure the file is not duplicated<br /> if (file_exists("archivos/" . $_FILES["archivo"]["name"])) { <br /> echo $_FILES["archivo"]["name"] . " ya existe. "; <br /> } else { <br /> // If it is not a duplicate file and there was no error, we proceed to upload to the /archivos folder, after that we show the uploaded image<br /> move_uploaded_file($_FILES["archivo"]["tmp_name"], <br /> "archivos/" . $_FILES["archivo"]["name"]); <br /> echo "Archivo Subido <br />"; <br /> echo "<img src='archivos/".$_FILES["archivo"]["name"]."' />"; <br /> } <br /> } <br /> } else { <br /> echo "Archivo no permitido"; <br /> } <br />} <br />?> <br /></div> <br /></body> <br /></html>
<br /><br /><br />I hope this is useful.<br /><br />You can also follow me on Twitter if you'd like: Los enlaces son solo para miembros. Registrate o iniciá sesión<br /><br />Regards.<br />
rush ·
Nice tutorial and this is called "RFI" Remote File Inclusion :D
<br /><br />Sorry if I'm wrong, but from what I understand RFI is when it loads content through the GET method like.<br /><br />servervictima.com/file.php?file="archivo" and this file can be replaced by a shell.<br /><br />I leave Los enlaces son solo para miembros. Registrate o iniciá sesión info.<br /><br />If I'm wrong please correct me :D
<br />Rush, quit the marijuana, I've told you that stuff is harmful XD :v, just kidding :P, Greetings bro!, Nice tuto! <br />PS: Burp suite is cool too C:
rush ·
Hahaha sorry for the confusion, so I have a misunderstanding and I don't remember, but according to me it was also called that for being able to upload a shell in an upload or I'm high on marijuana as @arthusu says haha term confusions
bernatixer ·
Good contribution, thanks for sharing it :)
blackdrake ·
That shell reminds me of a pretty tempting website ^^ (you know what I mean), good contribution @rollth, it will come in handy for more than one.<br /><br />Regards.
<br /><br />That shell doesn't ring a bell to me ::). Thanks for your comment @blackdrake :D
meteor0 ·
Nice one @rollth, it's a pretty common vulnerability, easy and fast, <br />Regards :)
EPSILON ·
Definitely this technique is known as "bypass upload", @rush nothing to do bro xD, @arthusu you are right bro Burpsuite is awesome, @rollth the post turned out really good and you used a classic shell the c99 xD.<br /><br />Greetings to everyone!
rush ·
You're right @EPSILON, a bypass upload, this technique has been around a long time haha maybe I confused it because in RFI the c99 shell was used a lot