Thursday, July 22, 2010

IsCallBack VS IsPostback

I just looking in couple framework. and just curious what they use to bind the UI is using
! IsCallback instead of ! IsPostback

Why ?

Just making summary out of this

IsCallBack will be set to true if you doing a partial postback.

if you checking using IsCallBack and there is no ajax call , it will not affect anything. Just similar like you don't use the checking which is doesn't improve your performance.

But there should be a reason behind it, or probably they have a mistype because of Autocomplete provided by VS =p

Wednesday, July 14, 2010

Protect your apps from ClickJacking

Here an interesting video which I recently lookat.

To protect your apps

put this code

if (top != self)
self.location.href = "";

Monday, July 12, 2010

CSRF Attack Prevention on .NET

In Addition to Rob's AntiXSS, we also need to secure CSRF Attack in Defence Jobs.
Here is what I found :
* Check this video to understand how the CSRF works & How you check your site if it is secure.
* Prevention
- ViewStateUserKey in (ASP.NET)
If you use viewstate in ASP.NET. it is recommended that you include ViewStateUserKey and Encript them
*(Include this on your base page)
protected override OnInit(EventArgs e)
if (User.Identity.IsAuthenticated)
ViewStateUserKey = Session.SessionID;
* Encript your viewstate in web.config (ViewStateEncriptionMode="Always")
However ViewStateUserKey this is not fully protect you from CSRF. This just to add an addition security layer to your application.
* Recommended Prevention
Because ViewStateUserKey is not completely protect you from the CSRF Attack, You need to protect your application using per-request nonce to hidden form / URL
There are framework which can automatically done this.
- This .NET version unfortunately only supply protection using URL method. (Nonce token is added on URL). This version doesn't support the hidden field method.
This framework (.NET HTTPModule) will added the per-request nonce to hidden field & cookies and validate it when post method or postback triggered.
Installation of ANTICSRF
- Add AntiCSRF.dll to Bin Folder
- Register AntiCSRF HttpModule on web config


- Configure Settings




    - If you don't want to proptect your page,
you can add class attribute
or page interface <%@ Implements Interface="Idunno.AntiCsrf.ISuppressCsrfCheck" %>


How it works (ANTICSRF)

* HTTP MODULE on PreSendRequestHeaders and PreRequestHandlerExecute

context.PreSendRequestHeaders += PreSendRequestHeaders;

context.PreRequestHandlerExecute += PreRequestHandlerExecute;

* Adding pre-request nonce token
- Add nonce on hidden field __CSRFTOKEN and cokkie __CSRFCOOKIE (configurable on settings)
* Validate nonce token on hidden field with cokkie
- It will validate the token when (POST Request or Postback)

- Get Request will NOT be validated unless it is Postback
- It will NOT validate any SuppressCsrfCheck class attribute or any class which inherits Idunno.AntiCsrf.ISuppressCsrfCheck
* If Attack detected
- hidden field or cookkie token are null/empty
- hidden field and cookkie token is not match
-ACTION (based on configuration setting detectionResult)
Throw an exception
- Or redirect to other page based on configuration setting (errorPage)

Limitations of ANTICSRF

  • Non-ASP.NET forms are not protected with this module.
  • ---------------------
    * This Framework will not protect the GET Request (Except if it is postback).
    For example
    - when you use AJAX call using GET Request, It will not validate the token.
    - But if you want to use AJAX call using the POST Request,
    You must Suppress the AntiCSRF validation by what I mention above on the Intallation. by adding the attribute or page attribute.
    Because If you don't suppress the AntiCSRF validation, it will detect as AntiCSRF Attack, because they can't find the token located on your hidden field.
    Please have a read on How It Works explanation above.

    XSS Prevention attack

    Most web developer must know about RequestValidation configuration in .NET
    which we can disable the XSS attack.

    But If we want to disable 'RequestValidation' it so we can have flexibility to handle it, We can use Server.HtmlEncode(). to display it.

    However this still not enough. This will expose to XSS attack.
    My college (Rob) find a utility which nice to replace Server.HTMLEncode().

    Download AntiXSSLibrary.dll
    and replace Server.HTMLEncode() with AntiXss.UrlEncode();