Search This Blog

Home

Thursday, October 21, 2010

Captcha

Introduction


The joy of working with the MVC framework is its total flexibility! See something that works great but could work better? Or perhaps you just need to add some custom functionality or features to the framework? No problem. In this case I am interested in creating an ActionResult that is capable of generating a CAPTCHA image as its result.

Note

If you are not familiar with what CAPTCHA stands for or what it is used for take a quick look at this wiki post.

CAPTCHA generator

Before we can really look at the MVC framework and how to create a custom ActionResult that returns a CAPTCHA image, we first need create the code that generates a CAPTCA image. I have a class that does this for me (included in the download) which I will briefly cover.

How does CAPTCHA work?

The down and dirty idea of CAPTCHA is that you come up with a random number, word, or words. This generated text is then stored in the users session for matching against later. It is then distorted, tweaked, colorized, etc., and then rendered out to an image. The user takes a guess at what they see in the image and types it into a text box. This user submission is paired against what was originally stored in the session. If they match then the user is allowed to use the requested resource.

The CAPTCHA class that is provided

I have been using the same CAPTCHA class for quite some time now. Over time it has been tweaked a bit more but for the most part is the same.

  1. Generate the random text. This can be done with a word dictionary, a random string generator, or a random number generator. I have a method exposed off of my Captcha class called GenerateRandomCode which returns a random number.
  2. Create an instance of the Captcha class.
  3. Set the Text property of the Captcha instance to the random text you created in step 1.
  4. Set the height and width of the image that you wish to be generated.
  5. Set the font family that you wish to use for your generated image.
  6. Use the generated Bitmap from the Captcha.Image property in the most appropriate manner for your situation.

There are quite a few areas of the Captcha class that can also be exposed as properties such as the background color, hatching foreground and background colors, if a background image is to be used, etc. I will leave that up to you!

Note

See the download for this code.

Creating the CAPTCHA ActionResult

Now that we have a functioning CAPTCHA class that will generate a CAPTCHA image for us, lets create the custom ActionResult which will return the CAPTCHA image for us.

Creating the CaptchaResult

The first step for us is to create a new ASP.NET MVC project. Mine is simply named MvcApplication1 – nothing special. From there I am going to place my Captcha class into the Models folder of my project. Then I am going to create a new CaptchaResult class. This class will inherit from ActionResult and therefore will need to implement the ExecuteResult method.

Listing 1: CaptchaResult.cs

1.public class CaptchaResult : ActionResult
2.{
3. public override void ExecuteResult(ControllerContext context)
4. {
5. }
6.}

Next we need to set up our CaptchaResult to accept a string into the constructor for our randomly generated code. I am choosing to set this up externally to the action (and the Captcha class) so that the user can specify the code and how they wish to store it for later use (session, etc.).

Listing 2: CaptchaResult.cs

1.public string _captchaText;
2.public CaptchaResult(string captchaText)
3.{
4. _captchaText = captchaText;
5.}

From there we can create an instance of our Captcha class inside our ExecuteResult method. We will set the text via the _captchaText variable. Then we will set up the height and width of the image to be created as well as the font family that we will use.

With this data passed into our Captcha instance we can then look to saving the image into the response stream. To do that we have to clear the response. Then we will set the response type to "image/jpeg". And then we can finally save the Bitmap into the response stream.

Listing 3: CaptchaResult.cs

01.public class CaptchaResult : ActionResult
02.{
03. public string _captchaText;
04. public CaptchaResult(string captchaText)
05. {
06. _captchaText = captchaText;
07. }
08. public override void ExecuteResult(ControllerContext context)
09. {
10. Captcha c = new Captcha();
11. c.Text = _captchaText;
12. c.Width = 200;
13. c.Height = 50;
14. c.FamilyName = "Century Schoobook";
15. HttpContextBase cb = context.HttpContext;
16. cb.Response.Clear();
17. cb.Response.ContentType = "image/jpeg";
18. c.Image.Save(cb.Response.OutputStream, ImageFormat.Jpeg);
19. c.Dispose();
20. }
21.}

Now we can take a look at what it will take to use our new CaptchaResult.

Using our new CaptchaResult

In order to use our CaptchaResult we simply need to add it to one of our controllers. In this case we will add our new CaptchaResult to our HomeController class in the form of a GetCaptcha method. In this method we will need to get our generated text either from the GenerateRandomCode method in the Captcha class or from some other place (like a word dictionary). Then we will add the text to the user's session. And finally we will pass our generated text into our new CaptchaResult class which will generate the CAPTCHA image for us.

Listing 4: HomeController.cs

1.public CaptchaResult GetCaptcha()
2.{
3. string captchaText = Captcha.GenerateRandomCode();
4. HttpContext.Session.Add("captcha", captchaText);
5. return new CaptchaResult(captchaText);
6.}

Once we have this new CaptchaResult in place we can test it by calling it from an image tag in the Index view for the home controller (recall that it is responsible for returning a jpeg image).

Listing 5: Index.aspx

1....
2."/home/getcaptcha" />
3....

Once you see your CAPTCHA output we can now work on implementing the capture part of the CAPTCHA. To do this all we need to do is add a form, a text box, the image, and a button.

Listing 6: Index.aspx

1.<% using (Html.BeginForm("index", "home")) { %>
2.<p><img src="/home/getcaptcha" />p>
3.<p>Please enter the number above:p>
4.<p><%=Html.TextBox("captcha")%>p>
5.<p><input type="submit" value="Submit" />p>
6.<% } %>

The output should now look like this.

Once we have the UI completed for our CAPTCHA we can now work on capturing the form submission. We will do this with a new Index action that accepts a form post.

Listing 7: HomeController.cs

01.[HttpPost]
02.public ActionResult Index(string captcha)
03.{
04. if (captcha == HttpContext.Session["captcha"].ToString())
05. ViewData["Message"] = "CAPTCHA challenge was successful!";
06. else
07. ViewData["Message"] = "CAPTCHA challenge failed - please try again!";
08. return View();
09.}

As you can see we are simply capturing the users input with our captcha method input. We then compare that users input with the value stored in the session. If the strings match we return a success message, otherwise we return a try again message. We then return the view.

Summary

In this article we took a quick look at what a CAPTCHA system would be used for. We also discussed the general process for creating a CAPTCHA. Next we discussed the Captcha class that I was going to use for this article. Then we implemented a custom ActionResult that returns the generated CAPTCHA image. With the custom CaptchaResult created we then looked at completing the implementation of our CAPTCHA systems UI by creating the view code and a new action in the home controller. Hopefully this article showed you not only how to create a CAPTCHA system but also how flexible the MVC framework is!


Tuesday, September 28, 2010

Some SQL Commands

List of all System Tables

Select *from sys.tables



List of all User Defined Tables

select * from sys.objects where type='u'

Validate All Input For Sql Injections

Always Reject input that contains the following characters.

Input character Meaning in Transact-SQL

; Query delimiter.

' Character data string delimiter.

-- Comment delimiter.

/* ... */ Comment delimiters. Text between
/* and */ is not evaluated by the server.

xp_ Used at the start of the name of catalog- extended stored procedures, such as xp_cmdshell.

SQL Injection

SQL injection is an attack in which malicious code is inserted into strings that are later passed to an instance of SQL Server for parsing and execution. Any procedure that constructs SQL statements should be reviewed for injection vulnerabilities because SQL Server will execute all syntactically valid queries that it receives. Even parameterized data can be manipulated by a skilled and determined attacker.

The primary form of SQL injection consists of direct insertion of code into user-input variables that are concatenated with SQL commands and executed. A less direct attack injects malicious code into strings that are destined for storage in a table or as metadata. When the stored strings are subsequently concatenated into a dynamic SQL command, the malicious code is executed.

The injection process works by prematurely terminating a text string and appending a new command. Because the inserted command may have additional strings appended to it before it is executed, the malefactor terminates the injected string with a comment mark "--". Subsequent text is ignored at execution time.

The following script shows a simple SQL injection. The script builds an SQL query by concatenating hard-coded strings together with a string entered by the user:
Copy

var Shipcity;
ShipCity = Request.form ("ShipCity");
var sql = "select * from OrdersTable where ShipCity = '" + ShipCity + "'";

The user is prompted to enter the name of a city. If she enters Redmond, the query assembled by the script looks similar to the following:

SELECT * FROM OrdersTable WHERE ShipCity = 'Redmond'

However, assume that the user enters the following:

Redmond'; drop table OrdersTable--

In this case, the following query is assembled by the script:

SELECT * FROM OrdersTable WHERE ShipCity = 'Redmond';drop table OrdersTable--'

The semicolon (;) denotes the end of one query and the start of another. The double hyphen (--) indicates that the rest of the current line is a comment and should be ignored. If the modified code is syntactically correct, it will be executed by the server. When SQL Server processes this statement, SQL Server will first select all records in OrdersTable where ShipCity is Redmond. Then, SQL Server will drop OrdersTable.

As long as injected SQL code is syntactically correct, tampering cannot be detected programmatically. Therefore, you must validate all user input and carefully review code that executes constructed SQL commands in the server that you are using.