Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Pages

         

nolim8ts

12:44 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



Hi All,

I'm in the process of creating dynamic pages and was wondering if the below code which achieves the result I am after would be vulnerable to attacks / exploits. If so, what would be best practice (examples appreciated).


<?php
include('./includes/config.php');
include('./includes/banner.php');
include('./functions/site_links.php');
?>
<html>
<head>
<title><?php echo $sitetitle; ?></title>
<link rel='stylesheet' href='default.css' type='text/css' />
</head>
<body>
<?php
echo $banner;
?>
<div id="sitelinkswrap">
<div id="sitelinks">
<div class='menuBar'>

<ul id='nav'>

<?php echo menu_links(); ?>

</ul>
</div>
</div>
</div>

</body>
</html>

rocknbil

7:53 pm on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not that I can see, there's no input(?)

nolim8ts

11:38 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



Hi,

There will be a contact form which will be posting to itself but will be using:

action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"



The form will have validation before it is submitted. As a side-note note, does any anyone know of decent examples for server side validation. I'm currently using JavaScript but that can be disabled by the end user.

rocknbil

2:26 am on Feb 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot post links to them, but there are tons of resources out there discussing the vulnerabilities of PHP_SELF. They're easy to find. Use a hard coded value, or create a constant, or a variable whatever.

By validation, I am presuming you mean fields not empty, correct formats for stuff - these are fairly simple but will go on a case by case basis. The validation for an email field is very different than a plain text field. It's going to vary geographically too.

as for validation in respect to security, there's security discussions all over this site. There is no one off "do this and you'll be safe." The truth is, you can do everything everyone suggests and still get hacked, if they are determined enough.

But you can decrease the odds, a lot. It's more of a philosophy than anything else.

- Every user input is a potential hack. This quote by Selena Sol really describes the problem, and leads to best choices. Treat any request - to a form or NOT - as the poison it is.

- Throw anything you don't expect away, examine everything you want to keep like it were dating your daughter. Or son. The most simple example - and least cleansed - is this.

script.php?record_id=1234

If you're expecting a number, this simple code makes sure it is.

if (isset($_GET['record_id']) and ($_GET['record_id'] > 0)) {
// we're okay.
}
else { die("Invalid input supplied"; }

Why not use is_numeric? In most cases, you will never query 0 for your database, and zero passes is_numeric. Any attempt to abuse this input with **anything** but a number will fail. Text will always evaluate to zero.

The second best thing I can suggest, make logging input a requirement, not a vague thought in your wish list. If you start getting attacked, the information to be gleaned from a simple log that logs raw input to your site is going to be gold in figuring out what they are up to, and how to stop them. This is going to be vastly different than what you get from access logs, and a lot more specific and intelligible. Choose a private location off the domain root, log all input to a file there, and check it as often as you have time for.

There are many many more, as mentioned, it's going to depend on what you're doing. Sometimes you will want to allow html; you'll need to define what IS valid HTML for use on your site and in your database, and what is not, and compare input against your definitions. Sometimes you won't, so you can kill anything that come close to an HTML tag. Sometimes you'll want to allow special characters, sometimes you won't.

Use all the tools PHP provides you, but it's not always enough. Every time you look at input, stop and think: can I make sure what comes in here is only what I need, and some unknown input can't get past it?