Sunday, July 24, 2016

Anyone Tried Uploading A 5-Byte File To Php Before?

Anyone Tried Uploading A 5-Byte File To Php Before?

Lately I've been tinkering around with uploading files to a PHP server via winsock, and today encountered an issue where I'd receive no response from the server, and for the next 30 minutes or so, all attempts to access the server via a browser would produce the dreaded "The connection was reset" error. Investigating further, I was actually able to connect to the server, but immediately upon sending a request, the server would close the connection.

 

The cause? You tell me. Server was running PHP 5.2. Mind you, "1", "12", "123", and "1234" all work as well.

 

This works:

REQUEST>

POST /bug/index.php HTTP/1.1
Host: sample.com
Connection: close
Content-Type: multipart/form-data; boundary=HTTPClientBoundary
Content-Length: 144

--HTTPClientBoundary
Content-Disposition: form-data; name="moo"; filename="cow.txt"
Content-Type: text/plain

123456
--HTTPClientBoundary-- 

RESPONSE>

SAVED: cow.txt

This doesn't:

REQUEST>

POST /bug/index.php HTTP/1.1
Host: sample.com
Connection: close
Content-Type: multipart/form-data; boundary=HTTPClientBoundary
Content-Length: 143

--HTTPClientBoundary
Content-Disposition: form-data; name="moo"; filename="cow.txt"
Content-Type: text/plain

12345
--HTTPClientBoundary--

RESPONSE>

<disconnect>

index.php

<?php

if(!empty($_FILES))
{
        $upload_dir = pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_DIRNAME) . '/';

        foreach($_FILES as $entry)
        {
                if(is_uploaded_file($entry['tmp_name']) && ($entry['error'] === UPLOAD_ERR_OK))
                {
                        if(move_uploaded_file($entry['tmp_name'], $upload_dir . $entry['name']))
                        {
                                echo 'SAVED: ' . $entry['name'];
                        }
                        else
                        {
                                unlink($entry['tmp_name']);
                        }
                }
        }
}

No comments:

Post a Comment