MrPaan

UTHM FileUpload Vulnerability [PATCH]

Jan 27th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | None | 0 0
  1. <?PHP
  2.  
  3. //PATCH for https://community.uthm.edu.my/images/students/dirLIST_files/process_upload.php(POC)
  4. //FileUpload Fucker
  5.  
  6. require('config.php');
  7. require('functions.php');
  8. session_start();
  9.  
  10. if($file_uploads !=  1)
  11. {
  12.     header("Location: ../index.php");
  13.     exit;
  14. }
  15. //some servers return empty $_POST and $_FILES arrays when the file size is too large
  16. if(empty($_POST) || empty($_FILES))
  17. {
  18.     header("Location: ../index.php?err=".base64_encode("upload_error"));
  19.     exit;
  20. }
  21.  
  22. //check if file is too big
  23. if($_FILES['file']['error'] == 1)
  24. {
  25.     header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("size"));
  26.     exit;
  27. }
  28.  
  29. //check if any file was uploaded
  30. if($_FILES['file']['error'] == 4)
  31. {
  32.     header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("nofile"));
  33.     exit;
  34. }
  35.  
  36. /*
  37. PATCH START
  38. */
  39.  
  40. //check if files are php file and return error
  41. if($_FILES['file']['type'] == 'application/octet-stream')
  42. {
  43.     header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("php file are not allowed"));
  44.     exit;
  45. }
  46.  
  47. //check whether the file name is php and not jpeg
  48. $allowed =  array('gif','png' ,'jpg','jpeg'); //allowed file extension
  49. $filename = $_FILES['file']['name'];
  50. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  51. if(!in_array($ext,$allowed) ) {
  52.      header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("upload_error"));
  53.      exit;
  54. }
  55.  
  56. //check file extension php(shell file) and if yes return error
  57. $nama = $_FILES['file']['name'];
  58. $tengok = explode(".", $nama);
  59. $ext = $tengok[1];
  60. if($ext == "php"){
  61.      header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("php detected"));
  62.      exit;
  63. }
  64.  
  65. /*
  66. END Of PATCH
  67. */
  68.  
  69. $local_text = (empty($_SESSION['lang_id'])) ? set_local_text(0) : set_local_text($_SESSION['lang_id']);
  70.  
  71. if($_POST['submit'] == $local_text['upload'])
  72. {
  73.     $file_name = $_FILES['file']['name'];
  74.     if(get_magic_quotes_gpc())
  75.     {
  76.         $file_name  = stripslashes($_FILES['file']['name']);
  77.     }
  78.  
  79.     $folder = base64_decode($_POST['folder']);
  80.     (substr($folder, -1, 1) != "/" && !empty($folder) ? $folder .= "/" : $folder);
  81.     $new_path = '../'.$dir_to_browse.$folder.$file_name;
  82.    
  83.     if(in_array(strtolower(strrchr($file_name, ".")), $banned_file_types))
  84.     {
  85.         header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("upload_banned"));
  86.         exit;
  87.     }
  88.  
  89.     $same_file_counter = 1;
  90.    
  91.     while(is_file($new_path))
  92.     {
  93.         $file_ext_comp = strrchr($file_name, '.');
  94.         $file_name_comp = substr($file_name, 0, -strlen($file_ext_comp));
  95.         $new_path = '../'.$dir_to_browse.$folder.$file_name_comp.'['.$same_file_counter.']'.$file_ext_comp;                                                                                                                                                                    
  96.         $same_file_counter++;
  97.     }
  98.    
  99.     if(move_uploaded_file($_FILES['file']['tmp_name'], $new_path))
  100.     {
  101.         header("Location: ../index.php?folder=".$_POST['folder']);
  102.         exit;
  103.     }  
  104.     else
  105.     {
  106.         header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("upload_error"));
  107.         exit;
  108.     }
  109. }
  110. else
  111. {
  112.     header("Location: ../index.php?folder=".$_POST['folder']."&err=".base64_encode("upload_error"));
  113.     exit;
  114. }
  115. ?>
Add Comment
Please, Sign In to add comment