473,802 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
+ 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?acti on=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 1868
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************ *************@y ahoo.com> wrote in message
news:_L******** *********@newsr ead1.news.pas.e arthlink.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?acti on=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.co m?action=edit&i d=55&hash=74F49 80E2938CDF">

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(s erialize($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(bas e64_decode($dat a));

}

else

{

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

return false;

}

}
return false;

}
function MakeGetString($ string)

{

list($data,$has h) = $this->pc_encode($str ing);
$getstring = "d=$data&h=$has h";
return $getstring;

}
function CheckGetString( )

{

$data = $_GET['d'];

$hash = $_GET['h'];
if (! $data = $this->pc_decode($dat a, $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 ($getstringarra y as $gmode) {

$newstring = $gmode;

$encstring = $insurancequote lib->MakeGetString( $newstring);

$newstringarray[$gmode] = $encstring;

}

print_r($newstr ingarray);

$smarty->assign('getstr ing',$newstring array);

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.co m?action=edit&i d=55&hash=74F49 80E2938CDF">

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************ *************@y ahoo.com> wrote in message
news:_L******** *********@newsr ead1.news.pas.e arthlink.net...

I am also doing this to make sure that, if in my code I'm performing simple functions like mysite.com?acti on=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("a ction=edit&id=5 5") .
'">Edit</a>';

function SaveGetParam($g et) {
$md5 = md5($get);
$_SESSION['SAVED_GET_PARA M'][$md5] = $get;
return "key=$md5";
}

function RestoreGetParam () {
$md5 = $_GET['key'];
$get = $_SESSION['SAVED_GET_PARA M'][$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("s cript=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="SaveGetP aram("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******** ************@co mcast.com...
"Golf Nut" <tr************ *************@y ahoo.com> wrote in message
news:_L******** *********@newsr ead1.news.pas.e arthlink.net...

I am also doing this to make sure that, if in my code I'm performing simple
functions like mysite.com?acti on=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("a ction=edit&id=5 5") .
'">Edit</a>';

function SaveGetParam($g et) {
$md5 = md5($get);
$_SESSION['SAVED_GET_PARA M'][$md5] = $get;
return "key=$md5";
}

function RestoreGetParam () {
$md5 = $_GET['key'];
$get = $_SESSION['SAVED_GET_PARA M'][$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
3544
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 last searched word in the following Variable. Ex.: Session("Word3") = Session("Word2") Session("Word2") = Session("Word1")
4
1693
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 do.. Anyone..? Best regards, Christina
182
7562
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
5
1974
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 class. Looking on the internet, one solution posted was populating Session in Session_Start in a global.asax file. Again, on postback, the value is
14
2383
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 User B has access to 5, now when both of us hits to the server at the same time then their session variables gets mixedup means either User A and USer B will have now 5 companies or both have 10 companies. Now again when User A hits to the server...
5
2057
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. As the data is received, it needs to be displayed as its received. I have made an attempt, but hit a problem. Here is a brief overview of the attempt:
2
1779
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. Functions in IMAP, POP3 AND LDAP? 13. How can I execute a php script using command line? 14. Suppose your Zend engine supports the mode Then how can u configure your php Zend engine to support mode ? 15. Shopping cart online validation...
7
4661
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 SELECT privileges on certain tables. I built all the roles and users for them and they work fine. Then she asked "Why do YOU let them see all those system tables?",
2
2785
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 uploads, to the clients accounts. I have seen similar sites everywhere, such as bank websites, USPS site, et cetera. What is required to accomplish this? I'm trying to do this with the PHP, MySQL and Adobe CS3 Master collection's 'Dreamweaver CS3' ...
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6838
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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 we have to send another system

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.