forked from webpwnized/mutillidae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-file.php
executable file
·208 lines (182 loc) · 7.81 KB
/
upload-file.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php include_once (__SITE_ROOT__.'/classes/FileUploadExceptionHandler.php');?>
<?php include_once (__SITE_ROOT__.'/includes/back-button.inc');?>
<?php include_once (__SITE_ROOT__.'/includes/hints/hints-menu-wrapper.inc'); ?>
<?php
try{
switch ($_SESSION["security-level"]){
case "0": // This code is insecure. No input validation is performed.
$lEnableJavaScriptValidation = FALSE;
$lEnableHTMLControls = FALSE;
$lValidateFileUpload = FALSE;
$lAllowedFileSize = 2000000;
$lUploadDirectoryFlag = "CLIENT_DECIDES";
break;
case "1": // This code is insecure. No input validation is performed.
$lEnableJavaScriptValidation = TRUE;
$lEnableHTMLControls = TRUE;
$lValidateFileUpload = FALSE;
$lAllowedFileSize = 2000000;
$lUploadDirectoryFlag = "CLIENT_DECIDES";
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
$lEnableJavaScriptValidation = TRUE;
$lEnableHTMLControls = TRUE;
$lValidateFileUpload = TRUE;
$lAllowedFileSize = 20000;
$lUploadDirectoryFlag = "TEMP_DIRECTORY";
break;
}// end switch
//$lWebServerUploadDirectory = __SITE_ROOT__.DIRECTORY_SEPARATOR.'uploads';
$lWebServerUploadDirectory = sys_get_temp_dir();
$lFormSubmitted = $lFileMovedSuccessfully = FALSE;
if (isset($_POST["upload-file-php-submit-button"]) || isset($_REQUEST["upload-file-php-submit-button"])) {
$lFormSubmitted = TRUE;
}// end if
if ($lFormSubmitted){
switch ($lUploadDirectoryFlag){
case "CLIENT_DECIDES": $lTempDirectory = $_REQUEST["UPLOAD_DIRECTORY"];break;
case "WEB_SERVER": $lTempDirectory = $lWebServerUploadDirectory;break;
case "TEMP_DIRECTORY": $lTempDirectory = sys_get_temp_dir();break;
}// end switch
/* Common file properties */
$lFilename = $_FILES["filename"]["name"];
$lFileTempName = $_FILES["filename"]["tmp_name"];
$lFileType = $_FILES["filename"]["type"];
$lFileSize = $_FILES["filename"]["size"];
$lFileUploadErrorCode = $_FILES["filename"]["error"];
$lFilePermanentName = $lTempDirectory . DIRECTORY_SEPARATOR . $lFilename;
/* File properties needed for validation */
$lAllowedFileExtensions = array("gif", "jpeg", "jpg", "png");
$lAllowedFileTypes = array("image/gif", "image/jpeg", "image/jpg", "image/pjpeg", "image/x-png", "image/png");
$lFilenameParts = explode(".", $lFilename);
$lFileExtension = end($lFilenameParts);
$lValidationMessage = "Validation not performed";
$lFileMovedMessage = "Moving file was not attempted";
/* File property strings suitible for printing */
if ($lFileSize > 1000){
$lFileSizeString = number_format($lFileSize/1000). " KB";
}else{
$lFileSizeString = number_format($lFileSize). " Bytes";
}//end if
if ($lAllowedFileSize > 1000){
$lAllowedFileSizeString = number_format($lAllowedFileSize/1000). " KB";
}else{
$lAllowedFileSizeString = number_format($lAllowedFileSize). " Bytes";
}//end if
$lFileUploadMessage = "File uploaded to {$lFileTempName}";
if ($lFileUploadErrorCode != UPLOAD_ERR_OK) {
$lFileUploadMessage = "Error detected during file upload (Code {$lFileUploadErrorCode}). See error output for detail.";
throw new FileUploadExceptionHandler($lFileUploadErrorCode);
}//end if UPLOAD_ERR_OK
$lFileValid = TRUE;
if ($lValidateFileUpload){
$lValidationMessage = "Validation performed.";
if (!in_array($lFileExtension, $lAllowedFileExtensions)) {
$lValidationMessage .= " File extension {$lFileExtension} not allowed.";
$lFileValid = FALSE;
}// end if
if (!in_array($lFileType, $lAllowedFileTypes)) {
$lValidationMessage .= " File type {$lFileType} not allowed.";
$lFileValid = FALSE;
}// end if
if ($lFileSize > $lAllowedFileSize){
$lValidationMessage .= "File size {$lFileSizeString} exceeds allowed file size {$lAllowedFileSizeString}.";
$lFileValid = FALSE;
}// end if
}// end if $lValidateFileUpload
if ($lFileValid){
if (move_uploaded_file($lFileTempName, $lFilePermanentName)) {
$lFileMovedSuccessfully = TRUE;
$lFileMovedMessage = "File moved to {$lFilePermanentName}";
}else{
$lFileMovedSuccessfully = FALSE;
$lFileMovedMessage = "Error Detected. Unable to move PHP temp file {$lTempDirectory} to permanent location {$lFilePermanentName}";
throw new Exception($lFileMovedMessage);
}//end if move_uploaded_file
}// end if $lFileValid
}//end if $lFormSubmitted
}catch(Exception $e){
echo $CustomErrorHandler->FormatError($e, "Error uploading file");
}// end try
?>
<script type="text/javascript">
var onSubmitOfForm = function(/* HTMLForm */ theForm){
try{
<?php
if($lEnableJavaScriptValidation){
echo "var lValidateInput = \"TRUE\"" . PHP_EOL;
}else{
echo "var lValidateInput = \"FALSE\"" . PHP_EOL;
}// end if
?>
var lMAX_FILE_SIZE = <?php echo $lAllowedFileSize;?>;
if(lValidateInput == "TRUE"){
if (theForm.id_max_file_size.value > lMAX_FILE_SIZE){
alert('Maximum file size is not allowed to be larger than '+lMAX_FILE_SIZE);
return false;
};// end if
};// end if(lValidateInput)
return true;
}catch(e){
alert("Error: " + e.message);
};// end catch
};// end JavaScript function onSubmitOfForm()
</script>
<div class="page-title">Upload a File</div>
<div> </div>
<?php
if ($lFormSubmitted) {
echo "<div>
<table style='width: 600px;'>
<tr><td class='label' colspan='2'>{$lFileUploadMessage}</td></tr>
<tr><td class='label' colspan='2'>{$lFileMovedMessage}</td></tr>
<tr><td class='label' colspan='2'>{$lValidationMessage}</td></tr>
<tr><td> </td></tr>
<tr><td class='label'>Original File Name</td><td>{$lFilename}</td></tr>
<tr><td class='label'>Temporary File Name</td><td>{$lFileTempName}</td></tr>
<tr><td class='label'>Permanent File Name</td><td>{$lFilePermanentName}</td></tr>
<tr><td class='label'>File Type</td><td>{$lFileType}</td></tr>
<tr><td class='label'>File Size</td><td>{$lFileSizeString}</td></tr>
</table>
</div>
<div> </div>";
}//end if
?>
<div>
<form enctype="multipart/form-data" action="./index.php?page=upload-file.php" method="POST" onsubmit="return onSubmitOfForm(this);">
<table>
<tr id="id-bad-cred-tr" style="display: none;">
<td colspan="2" class="error-message">
Authentication Error: File upload error
</td>
</tr>
<tr><td></td></tr>
<tr>
<td colspan="2" class="form-header">Please choose file to upload</td>
</tr>
<tr><td></td></tr>
<tr>
<td colspan="2">
<!-- UPLOAD_DIRECTORY hidden input is only considered in security level 0 -->
<input type="hidden" name="UPLOAD_DIRECTORY" value="<?php echo $lWebServerUploadDirectory; ?>" />
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" id="id_max_file_size" value="<?php echo $lAllowedFileSize; ?>" />
<label for="filename-text" class="label">Filename</label>
<input type="text" style="background-color:#ffffff;color:#000000;font-family:courier" disabled="disabled" name="filename-text" id="idFilenameText" size="50" />
<img src="./images/upload-32-32.png" align="middle" onclick="idFilename.click();" />
<input type="file" id="idFilename" name="filename" style="display: none;" onchange="idFilenameText.value=this.value" />
</td>
</tr>
<tr><td></td></tr>
<tr>
<td colspan="2" style="text-align:center;">
<input name="upload-file-php-submit-button" class="button" type="submit" value="Upload File" />
</td>
</tr>
<tr><td></td></tr>
</table>
</form>
</div>