Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 103
  • Last Modified:

PHP Submission

Hi Experts,

Is it possible when a form is submitted to a PHP page, that PHP page to check the absolute path of the form page?

In other words, I have just noticed some invalid data I have been validating with JavaScript, and found out that someone has view source, saved it on the local machine, tampered with the JavaScript and adjusted the form action to the PHP's absolute URL.

So, if I can verify the absolute URL of the page being submitted (without using hidden fields, of course), then I can avoid this.

Thanks,
0
APD Toronto
Asked:
APD Toronto
  • 8
  • 8
  • 5
  • +2
3 Solutions
 
InsoftserviceCommented:
Please show your code if possible.
I would suggest you to validate it via both using JavaScript and using php.

As Php Validation will simply resolve your issue.
0
 
GaryCommented:
You mean check where the page has come from?
You can check the referrer but totally unreliable.
On the form page set session (e.g. $_SESSSION['form-visited'] )and on the page that the form submits to check this session exists e.g.
if(isset($_SESSSION['form-visited'])){

Nothing is completely 100% foolproof but this is about as good as it gets.
0
 
APD TorontoAuthor Commented:
So, you're saying using a PHP session variable, that way no one can change this...?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
GaryCommented:
Yep, it's only stored at the server and as long as you are not echoing it out to the browser it cannot be known by the user.
0
 
APD TorontoAuthor Commented:
What if ...

1- The HTML and JavaScript is downloaded on a local machine

2- User goes to the real form on the server, the server creates the session variable

3- User changes URL of same window to point to the local version of their machine, with the adjusted code.

4- User clicks Submit from their local machine that goes to the PHP page on the server. Wouldn't the PHP page pickup the session variable from step 2?
0
 
Dave BaldwinFixer of ProblemsCommented:
That method won't work.  Sessions normally set a cookie to identify that browser/computer.  The way browsers handle cookies is that the session_id will only be sent if the page is from the original source.  A page on 'localhost' will not send a session_id for a page from the server.
0
 
GaryCommented:
http://en.wikipedia.org/wiki/Cross-site_request_forgery#Prevention

No one system is going to be 100% foolproof but session is pretty reliable as it only exists while on your site.
0
 
APD TorontoAuthor Commented:
To test, I just wrote the following 2 pages.

How come on test2.php $currSess always returns '' (empty string)? I realize that in test.php I'm running session_start(), but shouldn't that be carried over to all pages thereafter until session_end() is executes?

//test.php:

<?php
    session_start();
    $_SESSION['sess'] = session_id();
?>

<form method="post" action="test2.php">
    
    <input type="text" name="txtSess" value="<?php echo $_SESSION['sess']; ?>">
    
    <input type="submit">
    
</form>

//test2.php:

<?php
    
    $currSess = session_id();
    $reqSess = $_REQUEST['txtSess'];
    
    echo 'curr = ' . $currSess . ' <br> '
        . 'req = ' . $reqSess . ' <br><br>';
    
    if ($currSess == $reqSess) 
    {
        echo 'session is VALID';
    }
 else 
    {
        echo 'session is not VALID';
    }

?>

Open in new window

0
 
GaryCommented:
session_start() is needed on all pages where you are using sessions and before the code
0
 
APD TorontoAuthor Commented:
but wouldn't that keep generating new sessions?
0
 
GaryCommented:
No, session are per user per visit (unless the session times out which isn't a worry for you anyway)

http://php.net/manual/en/function.session-start.php
0
 
Dave BaldwinFixer of ProblemsCommented:
No, session_start() checks to see if a valid session exists already.  If it does, then it continues the current session and you can access any $_SESSION variables you have set.  It only starts a new session if it does not find an existing one.  This is probably one of the most common misunderstandings about PHP sessions.
0
 
APD TorontoAuthor Commented:
Using the code above, with session_start() in test2.php, I was able to bypass the code using the scenario within ID: 40337055 above.

So, I'll look at the ASP Code in my other post, with the assumption that there's something equivalent in PHP. Otherwise, I'm open to other suggestions.
0
 
Dave BaldwinFixer of ProblemsCommented:
" bypass the code"?  What does that mean?  Is that what you want or not?
0
 
APD TorontoAuthor Commented:
If you refer to my original post, I noticed my JavaScript is being bypassed.  I need to prevent this.
0
 
GaryCommented:
That can't happen because the session cookie is only stored at the original url, if you post from another site (or even locally thru localhost) then there is no session cookie.
0
 
APD TorontoAuthor Commented:
When I went through my 4-step procedure above, using the code, I got "session is VALID"
0
 
GaryCommented:
No you're right (put my logic head on - what was that tv program where he used to change his head...)
Like we are all saying there is no 100% foolproof way - you can implement all the different things to check like referer and sessions but it won't deter the hardened hacker and that is why you need to cleanse any data sent through a form.
0
 
APD TorontoAuthor Commented:
Is there a php equivalent to HTTP_REFERRER and Remote IP
0
 
Dave BaldwinFixer of ProblemsCommented:
You can not prevent your javascript from being bypassed.  You can Not.  Spammers and hackers scan websites every day looking for vulnerable forms and post directly to the action pages which bypasses everything on the form page.

Javascript checking and validation on the form page should be there to help the user fill out the form correctly.  PHP checking is to prevent the spammers and hackers from posting nonsense or breaking into your site and databases.
0
 
Dave BaldwinFixer of ProblemsCommented:
Here are a pair of simple PHP pages that use the things we've talked about.  Adjust the URLs and file names to suit your situation.
PHPsess01.php
<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');

// Create a unique session ID to use with this page
session_start(); 

$domain = $_SERVER['REMOTE_ADDR'];

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Sessions check</title>
</head>
<body>
<h1>PHP Sessions check</h1>
<form action="PHPsess02.php" method="post">
<input type="text" name="innie1" value="innie1" />&nbsp;
<input type="text" name="outie2" value="outie2" />&nbsp;
<input type="submit" value="Submit" />
</form>

</body>
</html>

Open in new window

PHPsess02.php
<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');

// check referrer, if no referrer, exit because it is a direct post
// Note that the refferrer URL is a complete absolute URL like http://www.yoursite.com/yourpage.php
// That's why I'm splitting it up to check it.  It could also be 'https://'.
if(isset($_SERVER['HTTP_REFERER'])) {
	$refchk = $_SERVER['HTTP_REFERER'];
	$refchka = explode("//",$refchk);
	if($refchka[1] != "10.202.46.40/ee2/PHPsessions/PHPsess01.php") exit;
	}
//else exit without creating a session;

// Create a unique session ID to use with this page
session_start(); 
$sessid = session_id();

$domain = $_SERVER['REMOTE_ADDR'];

if (!isset($_POST['innie1']))  $innie1 = ''; else $innie1 = $_POST['innie1'];
if (!isset($_POST['outie2']))  $outie2 = ''; else $outie2 = $_POST['outie2'];

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Sessions check</title>
</head>
<body>
<h1>PHP Sessions check</h1>
<p>Remote server IP address is: <?php echo $domain ?></p>
<form action="PHPsess02.php" method="post">
<input type="text" name="innie1" value="<?php echo strtoupper ($innie1) ?>" />&nbsp;
<input type="text" name="outie2" value="<?php echo strtoupper ($outie2) ?>" />&nbsp;
<input type="submit" value="Submit" />
</form>

</body>
</html>

Open in new window

0
 
Ray PaseurCommented:
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now