Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should i use Sleep() or just deny them

Im implementing a delay system so that any IP i deem abusive will automatically get an incremental delay via Sleep().

My question is, will this result in added CPU usage and thus kill my site anyways if the attacker just keeps opening new instances while being delayed? Or is the sleep() command use minimal CPU/memory and wont be much of a burden on a small script. I dont wish to flat out deny them as i'd rather they not know about the limit in an obvious way, but willing to hear why i should.

[ Please no discussion on why im deeming an IP abusive on a small site, cause heres why: I recently built a script that cURL's a page & returns information to the user and i noticed a few IP's spamming my stupid little script. cURLing too often sometimes renders my results unobtainable from the server im polling and legitimate users get screwed out of their results. ]

like image 698
lasavior Avatar asked Jul 26 '11 23:07

lasavior


People also ask

Is sleep a blocking call?

Yes, sleep is blocking.

What is the difference between Usleep and sleep?

“Sleep” can be used as a noun and as a verb. “Asleep” is an adjective, so that means these words take different positions in sentences. For example: “He's sleeping.”

What is JavaScript sleep?

JavaScript sleep/wait. The programming languages such as PHP and C has a sleep(sec) function to pause the execution for a fixed amount of time.

Is there a sleep function in JavaScript?

Unlike Java or Python, Javascript does not have a built-in sleep function. So, how might you be able to implement a method with the same functionality as the sleep function? A simple way to create a delay in Javascript is to use the setTimeout method.


4 Answers

The sleep does not use any CPU or Memory which is not already used by the process accepting the call.

The problem you will face with implementing sleep() is that you will eventually run out of file descriptors while the attacker site around waiting for your sleep to time out, and then your site will appear to be down to any other people who tries to connect.

This is a classical DDoS scenario -- the attacker do not actually try to break into your machine (they may also try to do that, but that is a different storry) instead they are trying to harm your site by using up every resource you have, being either bandwidth, file descriptors, thread for processing etc. -- and when one of your resources are used up, then you site appears to be down although your server is not actually down.

The only real defense here is to either not accept the calls, or to have a dynamic firewall configuration which filters out calls -- or a router/firewall box which does the same but off your server.

like image 160
Soren Avatar answered Nov 04 '22 11:11

Soren


I think the issue with this would be that you could potentially have a LARGE number of sleeping threads laying around the system. If you detect your abuse, immediately send back an error and be done with it.

My worry with your method is repeat abusers that get their timeout up to several hours. You'll have their threads sticking around for a long time even though they aren't using the CPU. There are other resources to keep in mind besides just CPU.

like image 35
colithium Avatar answered Nov 04 '22 09:11

colithium


Sleep() is a function that "blocks" execution for a specific amount of time. It isn't the equivalent of:

while (x<1000000);

As that would cause 100% CPU usage. It simply puts the process into a "Blocked" state in the Operating System and then puts the process back into the "Ready" state after the timer is up.

Keep in mind, though, that PHP has a default of 30-second timeout. I'm not sure if "Sleep()" conforms to that or not (I would doubt it since its a system call instead of script)

Your host may not like you having so many "Blocked" processes, so be careful of that.

EDIT: According to Does sleep time count for execution time limit?, it would appear that "Sleep()" is not affected by "max execution time" (under Linux), as I expected. Apparently it does under Windows.

like image 44
Chris Avatar answered Nov 04 '22 10:11

Chris


If you are doing what I also tried, I think you're going to be in the clear.

My authentication script built out something similar to Atwood's hellbanning idea. SessionIDs were captured in RAM and rotated on every page call. If conditions weren't met, I would flag that particular Session with a demerit. After three, I began adding sleep() calls to their executions. The limit was variable, but I settled on 3 seconds as a happy number.

With authentication, the attacker relies on performing a certain number of attempts per second to make it worth their while to attack. If this is their focal point, introducing sleep makes the system look slower than it really is, which in my opinion will make it less desirable to attack.

If you slow them down instead of flat out telling them no, you stand a slightly more reasonable chance of looking less attractive.

That being said, it is security through a "type" of obfuscation, so you can't really rely on it too terribly much. Its just another factor in my overall recipe :)

like image 40
VSack Avatar answered Nov 04 '22 09:11

VSack