Jump to content
  • Advertisement
Sign in to follow this  
Wavarian

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

This topic is 720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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']);
			}
		}
	}
}
Edited by Wavarian

Share this post


Link to post
Share on other sites
Advertisement

File uploading must be enabled via php.ini (keywords are 'file_uploads = On'; 'upload_max_filesize'; 'memory_limit' and 'post_max_size')

 

Some of them affect uploads directly, some of the keywords affect them indirectly (i.e. large upload_max_filesize is useless if you run out of memory)

Share this post


Link to post
Share on other sites

This issue only occurs when you try to upload a 5-byte file though; it works for all other file sizes (up to max filesize).

Share this post


Link to post
Share on other sites

If Content-Type is text/plain, of a file, you should also add character encoding, like this:

Content-Type: text/html; charset=utf-8

In case you don't supply charset, I gess UTF-8 gets assumed and you have invalid UTF stream in case of 5 bytes and constants like that.

 

You should use the character encoding and some valid bytes in content.

Share this post


Link to post
Share on other sites

Sadly, my host won't enable error logging for my "economy" plan. If I had the time, I'd set up a local LAMP stack and test it myself.

 

At one point, I did have the "charset=utf-8" token specified, but ended up stripping everything away until I found the cause of the problem. I was actually trying to upload binary files (application/octet-stream) when I first encountered the bug.

 

In terms of response codes and the like, I receive absolutely nothing back from the server. I send my request, a few seconds go by, and then I get disconnected without any response, after which all other valid requests (from say, a web browser) are disregarded - "The connection was reset".

 

During this time, I can still FTP into the host, and if I use a VPN to change my IP address, I can once again send valid requests to the server and get a response.

 

After around 30 minutes, the server responds to requests again like nothing happened... Until I try sending another 5-byte file.

Share this post


Link to post
Share on other sites

If the host is a cheap hosting service not controlled by you, it's very possible the webhost has lame "anti-hacking" measures enabled via custom apatche/php hook and htaccess rules that reject certain input when given in specific order because it matched a malware that used similar input to compromise an unpatched server a long time ago. I've seen that kind of crap before.

Share this post


Link to post
Share on other sites

If the host is a cheap hosting service not controlled by you, it's very possible the webhost has lame "anti-hacking" measures enabled via custom apatche/php hook and htaccess rules that reject certain input when given in specific order because it matched a malware that used similar input to compromise an unpatched server a long time ago. I've seen that kind of crap before.

If an invalid data is sent, meaning they contradict what is decleread they may pass, but if they become impossible to process for server to establish the hosted active document with input at the level of- this is not three, this is -1=0, kind of way , they close connection instantly without response code, and even prohibit the IP. This is classic, and no browser do those things of coure, you may experience this behavior so many times if you hardcode requests. Totaly standard.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!