SECURITY

Web Security : Automating with LibWWWPerl - Checking Session Expiration

5/16/2013 7:02:48 PM

1. Problem

You want to send expired cookies to the application to see if the server really expunges its session state at about the same time the cookies expire. You can use Perl to modify the expiration date of cookies that your application sends.

2. Solution

See Example 1.

Example 1. Perl script that modifies cookies
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common;

#$myCookies = HTTP::Cookies->new(
#	file     => "cookies.txt", 
#	autosave => 1, 
#    );
$myCookies = HTTP::Cookies->new();

$URL  = "https://www.example.com/w/signup.php";
$UA   = LWP::UserAgent->new();
$UA->cookie_jar( $myCookies );

# Find a particular cookie from a particular domain. Add 1 week to
# it's expiration. Delete the original cookie, store the modified
# cookie in our cookie jar. Uses an external namespace ($find::) to
# get the key, path, and domain to search for. Sets $find::changed
# to indicate the number of cookies that matched and were modified.
sub addOneWeek {
	my ($version, $key, $val, $path, $domain, $port, $path_spec,
		$secure, $expires, $discard, $rest) = @_;

	if( ($domain eq $find::domain) and
		($path   eq $find::path  ) and
		($key    eq $find::key   ) )
		{
			$expires = $expires + (3600 * 24 * 7); # seconds per week
			$myCookies->clear( $domain, $path, $key );
			$myCookies->set_cookie( $version, $key, $val, $path,
				$domain, $port, $path_spec,	$secure, $expires, $discard,
				$rest );
			$find::changed++;
		}
}

# Find a particular cookie from a particular domain. Uses an external
# namespace ($find::) to get the key, path, and domain to search for. Prints
# all cookies that match.
sub showCookies {
	my ($version, $key, $val, $path, $domain, $port, $path_spec,
		$secure, $expires, $discard, $rest) = @_;

	if( ($domain eq $find::domain) and
		($path   eq $find::path  ) and
		($key    eq $find::key   ) )
		{
			print "$domain, $path, $key, $val, $expires\n";
		}
}

# First fetch a web page that sends a cookie.
$req  = HTTP::Request->new( GET => $URL );
$resp = $UA->request($req);

$find::domain = "example.com";
$find::path   = "/";
$find::key    = "session_id";

# Show any matching cookies, in their original form.
$myCookies->scan( \&showCookies );

# Find them, and bump their expiration time by a week.
$myCookies->scan( \&addOneWeek  );

# Show the cookie jar, now that we modified it.
$myCookies->scan( \&showCookies );

					  

3. Discussion

Note that line 7 creates an empty, temporary cookie jar that we later populate.  Lines 56, 59, and 62 pass a pointer to a function in Perl. This is because the cookie jar scan() routine uses a call-back mechanism to invoke our function on each cookie in the jar—perhaps a bit inelegant.

3.1. Bad session expirations

You might use a technique like that shown in Example 8-5 to modify the cookie you’re sent after you log in. You see, some applications rely on well-behaved web browsers to discard expired cookies. Your session will expire due to inactivity at 12:44:02, so the web application sets the cookie to expire at 12:44:02. At that time the browser will throw away the cookie, so future requests will come to the server with no session information. You would be effectively logged out, because your browser threw away your session token.

What happens if the server does not discard the expired session at 12:44:02, however, but instead keeps it around until a garbage collection process runs at 1:00 p.m.? In that case your application is not working as advertised. There is a window of opportunity after the cookie expires, but before the server cleans up its state. In that time, a legitimate user would not use their cookie (their well-behaved browser will discard the expired cookie), but the server will recognize and allow it, if it is presented.

To detect such behavior, you can write a program very similar to Example 8-5. Your script would:

  1. Receive the cookie.

  2. Store the old expiration time.

  3. Modify the cookie to have a longer expiration.

  4. Go to sleep for a while. It can sleep until a little bit after the old expiration time.

  5. After waking up, issue a request for a page that would only succeed if the session cookie were still valid (at the server). That request’s success or failure tells you whether or not the application relies on cookie expiration for its session management.

Other  
 
Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8