SECURITY

Web Security : Automating with LibWWWPerl - Testing Session Fixation

5/16/2013 7:03:17 PM

1. Problem

Session fixation is a problem where the server receives a session token from the web browser that does not correspond to a valid session. Rather than issue a new session token of its own making, the server accepts the browser-provided session token. Such situations can be leveraged by attackers to steal session information and credentials. This Perl script in Example 8-6 checks for an application server that behaves badly in this way.

2. Solution

See Example 1.

Example 1. Testing for session fixation with Perl
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common;

$URL       = "https://www.example.com/w/signup.php";
$UA        = LWP::UserAgent->new();
$myCookies = HTTP::Cookies->new(
    file           => "cookies.txt",
    autosave       => 1,
    ignore_discard => 1,
);
$UA->cookie_jar($myCookies);

# Find a particular cookie from a particular domain. Uses an external
# namespace ($find::) to get the key, path, and domain to search for.
# Puts found cookie into array @find::cookie.
sub findCookie {
    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 "$version, $key, $val, $path, $domain, $expires\n";
        @find::cookie = @_;
    }
}

# Our Malicious Cookie: Contains a known session ID.
my $version = 0;
my $key     = "session_id";
my $val     = "1234567890abcdef";
my $path    = "/";
my $domain  = "example.com";
my $expires = "123412345";

# Add the malicious cookie to our jar. Fields we don't care
# about are undefined.
$myCookies->set_cookie(
    $version, $key,  $val,     $path, $domain, undef,
    undef,    undef, $expires, undef, undef
);

$req = HTTP::Request->new( GET => $URL );
$UA->prepare_request($req);
$resp = $UA->request($req);

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

# See if we have any cookies for that site, path, and key.
$myCookies->scan( \&findCookie );
if (    ( $domain eq $find::cookie[4] )
    and ( $path   eq $find::cookie[3] )
    and ( $key    eq $find::cookie[1] ) )
{
	# We have one. See if it contains our value.
    if ( $val eq $find::cookie[2] ) {
        print "Test failed: cookie returned was ours.\n";
    } else {
        print "Test passed: cookie returned was new.\n";
    }
} else {
    print "Test script failure: no matching cookie found.\n";
}

					  

3. Discussion

In this example we know something about the target application, so our call to set_cookie() (line 42) sets only the fields of the cookie that matter. You might have a slightly different script for testing your application if different cookie fields matter to your application.

The goal of a session fixation attack is to send a cookie to a victim (e.g., in a URL) and have the victim use it. When the victim uses that cookie, they are vulnerable to various session-stealing attacks because the attacker knows their cookie—he created it in the first place. To find out more about session fixation attacks, search for “session fixation attack pattern” on Google.

In this test we test for it by creating a bogus cookie that is easy to recognize. We send the contrived cookie to the server and then check what cookie the server sends back to us. If the server sends us our malicious cookie back, then the application fails the test.
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