473,472 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

want to create a session class

hello all!

I would like to create a session class which would transparently handle
sessions as well as serialize, encode and compute an md5 hash of all
$_REQUEST information. This would essentially intercept all $_GET strings
and $_POST data.

I would envision upon session creation (in the session class constructor)
that a random string secret would be created that would be saved to
$_SESSION['secret'] for example. I would take all the $_REQUEST data,
base64_encode() it, then serialize it and perform an md5() on it
contatenated with the secret. The base64_encoded serialized data would be
saved along with the hash in $_SESSION.

I guess I'm having a hard time conceptualizing this, much less explain it. I
hope someone can understand what I'm trying to do. Basically, I want to
ensure that any POST and GET data isn't hijacked or tampered with, which
would be verified upon using the passed data by verifying against the hash.
Perhaps the secret shouldn't be put in $_SESSION, since a user could
potentially see this? Is there somewhere else I could store this?

I am also doing this to make sure that, if in my code I'm performing simple
functions like mysite.com?action=edit&id=55, that someone doesn't
arbitrarily mess with the id or action, since this URL would be rewritten
and/or passed solely using the GET string in the $_SESSION as described
above.

Anyone have any ideas, comments or suggestions as to what I should do?

TIA!

-GN

Jul 17 '05 #1
5 1850
Hi,

I'm not sure what you are trying to do will work, as surely if a user does
edit the GET string by selecting "Copy Shortcut" and pasting it into the
address bar then these incorrect values will be sent to the page and be
hashed incorrectly. It would only work if a user clicked on a link then
edited it, but this might also incorrectly catch users that click a link,
then press back and click a different link.

A better way to protect a web application from user hacking is to, at every
possible opportunity, test user data to make sure that they are allowed to
edit certain records eg. as soon as you get the id=55 check whether that
user has permission to access record no.55, and if not abort the page.

Also another very important thing to guard against is SQL injection bugs in
your code.

As for checking whether post/get data isn't hijacked there's no way to know
for sure as a hacker will probably have the ability to do that
transparently, the best you can do is use an SSL server but that wont
protect the system from users with virii/trojans installed on their
machine.
Oh by the way the user shouldn't be able to access any variables in
$_SESSION unless your code prints them to the screen

Regards,
Andrew Crowe

"Golf Nut" <tr*************************@yahoo.com> wrote in message
news:_L*****************@newsread1.news.pas.earthl ink.net...
hello all!

I would like to create a session class which would transparently handle
sessions as well as serialize, encode and compute an md5 hash of all
$_REQUEST information. This would essentially intercept all $_GET strings
and $_POST data.

I would envision upon session creation (in the session class constructor)
that a random string secret would be created that would be saved to
$_SESSION['secret'] for example. I would take all the $_REQUEST data,
base64_encode() it, then serialize it and perform an md5() on it
contatenated with the secret. The base64_encoded serialized data would be
saved along with the hash in $_SESSION.

I guess I'm having a hard time conceptualizing this, much less explain it. I hope someone can understand what I'm trying to do. Basically, I want to
ensure that any POST and GET data isn't hijacked or tampered with, which
would be verified upon using the passed data by verifying against the hash. Perhaps the secret shouldn't be put in $_SESSION, since a user could
potentially see this? Is there somewhere else I could store this?

I am also doing this to make sure that, if in my code I'm performing simple functions like mysite.com?action=edit&id=55, that someone doesn't
arbitrarily mess with the id or action, since this URL would be rewritten
and/or passed solely using the GET string in the $_SESSION as described
above.

Anyone have any ideas, comments or suggestions as to what I should do?

TIA!

-GN

Jul 17 '05 #2
What you could do is add a hash to any links, eg.

<a href="mysite.com?action=edit&id=55&hash=74F4980E29 38CDF">

This would be a quick way of stopping users editing the id parameter, but
you couldn't use it to validate any user form data

--
Regards,
Andrew Crowe
Jul 17 '05 #3
Andrew,

Thanks for your thoughts and comments!

What you're talking about below is actually what I'm working on. Below is
some sample code from a class I've created:
function pc_encode($data)

{

$secret = $_SESSION["secret"];
$data = base64_encode(serialize($data));

$hash = md5($this->$secret . $data);
return array($data, $hash);

}
function pc_decode($data, $hash)

{

$secret = $_SESSION["secret"];
if (!empty($data) && !empty($hash))

{

if (md5($this->$secret . $data) == $hash)

{

return unserialize(base64_decode($data));

}

else

{

error_log("Valdation Error: data has been modified!!");

return false;

}

}
return false;

}
function MakeGetString($string)

{

list($data,$hash) = $this->pc_encode($string);
$getstring = "d=$data&h=$hash";
return $getstring;

}
function CheckGetString()

{

$data = $_GET['d'];

$hash = $_GET['h'];
if (! $data = $this->pc_decode($data, $hash))

return false;

else

return $data;

}

And now some code that's actually on the php script page (mind you I'm using
Smarty templating). Remember this is a rough rendition before profiling and
cleaning up, so excuse the messy code! :)

$getstringarray = array('m','a','d','s');

$newstringarray = array();

foreach ($getstringarray as $gmode) {

$newstring = $gmode;

$encstring = $insurancequotelib->MakeGetString($newstring);

$newstringarray[$gmode] = $encstring;

}

print_r($newstringarray);

$smarty->assign('getstring',$newstringarray);

Then I use the Smarty template to, in this case, have the following in my
link: <a href=page.php?{$getstring.m}> for example. I'm using a random
string using time(), etc., to create the $secret used in the code above and
save it in $_SESSION["secret"]. I would like to extend this to encapsulate
post data as well, i suppose saving it to hidden form fields using d and h
as above for GET data, the d field containing the serialized data and h the
hash.

Any thoughts about this? Again, TIA!!

GN
"Andrew Crowe" <an************@yahoo.co.uk> wrote in message
news:40**********************@news.easynet.co.uk.. .
What you could do is add a hash to any links, eg.

<a href="mysite.com?action=edit&id=55&hash=74F4980E29 38CDF">

This would be a quick way of stopping users editing the id parameter, but
you couldn't use it to validate any user form data

--
Regards,
Andrew Crowe

Jul 17 '05 #4
"Golf Nut" <tr*************************@yahoo.com> wrote in message
news:_L*****************@newsread1.news.pas.earthl ink.net...

I am also doing this to make sure that, if in my code I'm performing simple functions like mysite.com?action=edit&id=55, that someone doesn't
arbitrarily mess with the id or action, since this URL would be rewritten
and/or passed solely using the GET string in the $_SESSION as described
above.


You don't need to do any of that any secret or validation stuff at all.
Since you're relying on session, just store the variables in the session and
not pass the data through GET. Example:

echo '<a href="/somescript.php?' . SaveGetParam("action=edit&id=55") .
'">Edit</a>';

function SaveGetParam($get) {
$md5 = md5($get);
$_SESSION['SAVED_GET_PARAM'][$md5] = $get;
return "key=$md5";
}

function RestoreGetParam() {
$md5 = $_GET['key'];
$get = $_SESSION['SAVED_GET_PARAM'][$md5];
parse_str($get, $_GET);
}

Jul 17 '05 #5
Chung,

Thanks for the fantastic advice! It works like a charm - I don't know why
this didn't occur to me before!

Now I suppose what I can do is create a marshalling script of sorts that
handles all redirects and I can actually incorporate the actual script to
run within the encoded string, e.g.,
SaveGetParam("script=users.php&action=edit&id=100" ) and then let the
marshall script reference (e..g, redirect.php? . SaveGet.. . .) invoke the
script to execute.

How would you suggest to incoporate this methodology into the subsequent
edit form which would contain post data and subsequently the id of the
current record? Something like <input type=hidden name=id
value="SaveGetParam("id=100")">? The invoked form method would then parse
this value and perform the necessary function, corret?

Thanks again!

Regards,

GN

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:oc********************@comcast.com...
"Golf Nut" <tr*************************@yahoo.com> wrote in message
news:_L*****************@newsread1.news.pas.earthl ink.net...

I am also doing this to make sure that, if in my code I'm performing simple
functions like mysite.com?action=edit&id=55, that someone doesn't
arbitrarily mess with the id or action, since this URL would be rewritten and/or passed solely using the GET string in the $_SESSION as described
above.


You don't need to do any of that any secret or validation stuff at all.
Since you're relying on session, just store the variables in the session

and not pass the data through GET. Example:

echo '<a href="/somescript.php?' . SaveGetParam("action=edit&id=55") .
'">Edit</a>';

function SaveGetParam($get) {
$md5 = md5($get);
$_SESSION['SAVED_GET_PARAM'][$md5] = $get;
return "key=$md5";
}

function RestoreGetParam() {
$md5 = $_GET['key'];
$get = $_SESSION['SAVED_GET_PARAM'][$md5];
parse_str($get, $_GET);
}

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PM | last post by:
I'm trying to make a kind of search history containing the 3 last searched words. So I'm using 3 Session Variables: Word1 / Word2 / Word3. In order to get this history working, I need to put the...
4
by: Christina N | last post by:
Can anyone give me a code example of how to use a global array to keep track of all active sessions (their sessionid, logontime, etc)? I need a code example, not a prosa description of what to...
182
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
5
by: Steven Blair | last post by:
I have the following code: Session = new CurrentUser("TEST"); When I postback to the server, the Session is null. My guess is a only the refence to my actual class is stored, rather than the...
14
by: aroraamit81 | last post by:
Hi, I am facing a trouble. I have some Session variables in my code and somehow my session variables are getting mixed up with other users. For example User A has access to 10 companies and...
5
by: Steven Blair | last post by:
I need to write an ASP.NET application which can do the following: 1. Create a socket which will stay alive and continously read data. 2. The data read needs to be displayed on the webpage. 3....
2
by: gvijayasurya | last post by:
11. What are the different tables present in mysql, which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10)) ? 12....
7
by: Gary | last post by:
Hello guys! Bear with me, I am a newbie. She is the Data Warehouse manager. She has about 50 users to use the Oracle database from M$ Access via ODBC connection. All those users have only...
2
by: nja2222 | last post by:
I would like to create a page for my clients to login and check for updates on their accounts. Then I would like to create a page where my employees can login and make updates, specifically new file...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.