...
...
if(!empty($uploaded_files)){
foreach ($uploaded_files as $element_name){
if(empty($form_review)){
//move file and check for invalid file
$destination_file = $input['machform_data_path'].DATA_DIR."/form_{$form_id}/files/{$element_name}-{$record_insert_id}-{$_FILES[$element_name]['name']}";
if (move_uploaded_file($_FILES[$element_name]['tmp_name'], $destination_file)) {
$filename = mysql_real_escape_string($_FILES[$element_name]['name']);
$query = "update ap_form_{$form_id} set $element_name='{$element_name}-{$record_insert_id}-{$filename}' where id='$record_insert_id'";
do_query($query);
}
}else{
//for form with review enabled, append .tmp suffix to all uploaded files
//move file and check for invalid file
$destination_file = $input['machform_data_path'].DATA_DIR."/form_{$form_id}/files/{$element_name}-{$record_insert_id}-{$_FILES[$element_name]['name']}.tmp";
if (move_uploaded_file($_FILES[$element_name]['tmp_name'], $destination_file)) {
$filename = mysql_real_escape_string($_FILES[$element_name]['name']);
$query = "update ap_form_{$form_id}_review set $element_name='{$element_name}-{$record_insert_id}-{$filename}' where id='$record_insert_id'";
do_query($query);
}
In beginning, the hacker must aim view.php located at the root of site, observing the lines inside of mentioned file would be a big lead to disclosure of vulnerability:
These two lines have functions leading to have both MySQL injection and Arbitrary file upload vulnerability. I’m not going to audit codes, I may just illustrate the attack started by applying brute-force procedure on ID parameter so as to find a form consisting file upload form, it can be achieved by any program, I just issued a Linux command helped me find it properly:
In URL above, [ID] is gathered in brute-force phase, [element name] is gathered by viewing HTML source, and [mysql_insert_id()] should be brute-forced again. Being relatively difficult, I’ve recorded a clip demonstrating what I’ve said:
http://y-shahinzadeh.ir/tutorial/machform.rar
2. MySQL Injection (Error based) and XSS:
=========================================
...
...
$input_array = ap_sanitize_input($_POST);
...
...
Exploit (POST to view.php after finding HTML elements):