Limiting Output from Unmaintainable PHP Code

  • PHP
  • Thread starter Borek
  • Start date
  • Tags
    Output Php
In summary: I just glanced at this post again and noticed a mistake. I copied some code without changing it to match this dialog. My apologies if this caused you delay. Here it is with the correction - since it's too late to edit the original:define ('BOREK_APPROVED_ENTRY', true);$VolumeHackLimit = 1000;function volume_hack_check(){ global $VolumeHackLimit; if($VolumeHackLimit<=0) die("Bandwidth exceeded!"); $VolumeHackLimit--;}function volume_hack_check(){if
  • #1
Borek
Mentor
28,951
4,245
I have a site made in PHP eons ago by someone I no longer have contact with. Lately site was attacked with a SQL injection attempt. Input is sanitized, so there is no immediate danger for the database, but this attack exposed vulnerability - if value of one the parameters is not from the predefined set, program ends in an endless loop, throwing warnings. That's in a way equivalent to a case statement missing default. In effect code generates multimegabyte output, eating bandwidth (20 GB on March 10th - which is what caught my attention, as typically daily traffic it is in tens of MB range). It doesn't happen often, still, judging from logs every few months someone tries hacking.

I did some digging in the source and I don't see how to correct the problem without writing everything from scratch - perfect example of unmaintainable code. Site is not worth the effort, still, it brings enough money from adsense to pay a third of the server costs, so I don't feel like just closing it.

Any ideas about how to limit the output without changing the code? Or with just a small changes? So far I thought about limiting execution time through max_execution_time in php.ini or by calling set_time_limit(1), but perhaps you can think of some other, better ways?
 
Technology news on Phys.org
  • #3
You can count how many time a particular output is executed - and end it if it runs past 1000 or whatever.
You can look at the IP addresses of the hacker and the next time offer them some alternative content.
 
  • #4
AlephZero said:
Can you add an error handler to treat the warnings as errors and terminate the program? Something like
http://stackoverflow.com/questions/2071024/treating-warnings-as-errors

Yes, error handler crossed my mind as well.

.Scott said:
You can count how many time a particular output is executed - and end it if it runs past 1000 or whatever.

Can you elaborate? How to count?
 
  • #5
Obviously, I don't know what your starting point is.

I'll assume that you have several "main" php files that are intended to be entered directly and many others that are only intended to be "include"d from other php files. That being the case, you should put this kind of statement as the first statement in each main php file:
define ('BOREK_APPROVED_ENTRY', true);

And this statement at the start of all the *.inc files and other php files:
if ( !defined('IN_CERTMESSAGEBOARD') )
{
die("Hacking attempt - common");
}

This is a very common anti-hacking method - and there's a good chance you already have this in your code.

I don't know what your economy of effort is. I describe this without using an include file, but this new code can obviously be included in one.

This new code sets "$VolumeHackLimit" to 1000 and then provides a function to allow it to be counted down. It goes only at the top of the php files that are intended for webpage entry. Elsewhere in the main php file or in any included code, you make a call to this new function - as shown here:
Code:
<?php
define ('BOREK_APPROVED_ENTRY', true);

$VolumeHackLimit = 1000;
function volume_hack_check()
{
  global $VolumeHackLimit;
  if($VolumeHackLimit<=0) die("Bandwidth exceeded!");
  $VolumeHackLimit--;
}

... later in your code ...

  volume_hack_check();

...

?>
 
  • #6
I just glanced at this post again and noticed a mistake. I copied some code without changing it to match this dialog. My apologies if this caused you delay. Here it is with the correction - since it's too late to edit the original:

I'll assume that you have several "main" php files that are intended to be entered directly and many others that are only intended to be "include"d from other php files. That being the case, you should put this kind of statement as the first statement in each main php file:
define ('BOREK_APPROVED_ENTRY', true);

And this statement at the start of all the *.inc files and other php files:
if ( !defined('BOREK_APPROVED_ENTRY') )
{
die("Hacking attempt - common");
}

This is a very common anti-hacking method - and there's a good chance you already have this in your code.

I don't know what your economy of effort is. I describe this without using an include file, but this new code can obviously be included in one.

This new code sets "$VolumeHackLimit" to 1000 and then provides a function to allow it to be counted down. It goes only at the top of the php files that are intended for webpage entry. Elsewhere in the main php file or in any included code, you make a call to this new function - as shown here:
Code:
<?php
define ('BOREK_APPROVED_ENTRY', true);

$VolumeHackLimit = 1000;
function volume_hack_check()
{
  global $VolumeHackLimit;
  if($VolumeHackLimit<=0) die("Bandwidth exceeded!");
  $VolumeHackLimit--;
}

... later in your code ...

  volume_hack_check();

...

?>
 
  • #7
If I understand your idea correctly, I would have to browse the code to identify all the places where I should put volume_hack_check() call. That's exactly what I want to avoid.
 
  • #8
Borek said:
If I understand your idea correctly, I would have to browse the code to identify all the places where I should put volume_hack_check() call. That's exactly what I want to avoid.
Ahhh. So you can't tell what output is being generated? And besides, the problem might be with a single template.

You've already mentioned "max_execution_time" in the PHP runtime configuration. Perhaps the function "set_time_limit(1);" can be used more surgically. If you decide to use these, you also need to consider the "implicit_flush" parameter. Setting that parameter to TRUE could make your site too sluggish, but if it doesn't, it will tie the execution time (or rather the period of execution) to the amount of output generated.

Actually, if you've found the request in the log, you do know where the output is coming from. All you have to do is issue the request from your own browser and see what you get back. Since its megabytes, there should be terms that repeat thousands of times. Those are the terms that you would "volume_hack_check()". Of course, it would be best to do this after setting those PHP.ini parameters first - even if only for debugging.

You said you found the bad requests in your sites logs? So you could check $_SERVER['REMOTE_ADDR'] on entry to abandon or side-track requests from those IPs. Apparently the malformed requests are not easy to recognize, otherwise you would just check them on entry and bounce the request.

In any case, good luck.
 

1. What is the purpose of limiting output from unmaintainable PHP code?

The purpose of limiting output from unmaintainable PHP code is to improve the overall quality and maintainability of the code. By limiting the output, it becomes easier to identify and fix any errors or bugs that may arise. This also helps to ensure that the code is more organized and easier to understand for future developers.

2. How can I limit output from unmaintainable PHP code?

To limit output from unmaintainable PHP code, you can use various techniques such as using proper indentation and formatting, using comments to explain complex code, breaking up large blocks of code into smaller, more manageable chunks, and using meaningful variable and function names.

3. Why is it important to limit output from unmaintainable PHP code?

Limiting output from unmaintainable PHP code is important because it helps to prevent the code from becoming too complex and difficult to maintain. This can lead to issues such as bugs, slow performance, and difficulty understanding and updating the code in the future. By limiting the output, the code becomes more maintainable and easier to work with.

4. Can limiting output affect the functionality of my PHP code?

In most cases, limiting output from unmaintainable PHP code will not affect the functionality of the code. However, it is important to carefully review and test the code after making any changes to ensure that it is still functioning correctly. If any issues arise, they can be addressed and fixed accordingly.

5. Is it necessary to limit output from all PHP code?

No, it is not necessary to limit output from all PHP code. This technique is most beneficial for large, complex, and unmaintainable code. If your code is already well-organized and easy to understand, it may not require any limitations on output. However, it is always recommended to follow best practices for coding to ensure maintainability and readability.

Similar threads

  • Programming and Computer Science
Replies
5
Views
3K
Back
Top