Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
8 replies to this topic

#1   Members   

836
Like
0Likes
Like

Posted 24 July 2016 - 06:02 AM

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, 24 July 2016 - 06:06 AM.


#2   Members   

9061
Like
0Likes
Like

Posted 24 July 2016 - 12:43 PM

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)



#3   Members   

836
Like
0Likes
Like

Posted 24 July 2016 - 05:56 PM

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).



#4   Members   

1427
Like
0Likes
Like

Posted 26 July 2016 - 06:26 AM

What's the returned HTTP status and what does the (php) error log on the server say?


blah :)

#5   Members   

1059
Like
0Likes
Like

Posted 28 July 2016 - 05:43 AM

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.



#6   Members   

836
Like
0Likes
Like

Posted 28 July 2016 - 06:53 AM

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.



#7   Moderators   

6432
Like
0Likes
Like

Posted 28 July 2016 - 07:05 AM

Sounds like you found a bug in the host's web stack. Send them a support request.

#8   Members   

9061
Like
3Likes
Like

Posted 28 July 2016 - 08:50 AM

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.



#9   Members   

1059
Like
1Likes
Like

Posted 28 July 2016 - 03:01 PM

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.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.