SIGN UP MEMBER LOGIN:    
ARTICLE

Building ASP.NET bot protection (CAPTCHA-like)

Posted by Dmitiriy Salko Articles | ASP.NET Controls in C# February 29, 2008
This article shows how to build captcha-like protection from spam and other bots in ASP.NET.
Reader Level:
Download Files:
 

Introduction

 

One day I have decided to build own CAPTCHA for my project. Why? First of all I need quite strong bot protection. Second reason I want to make my contribution for the community. That's why I decided to make this project completely free for use. Why I am thinking that result of my work is a real strong protection? First of all it is new so there is no ready OCR for this one. Also, you can change it by yourself and receive completely different CAPTCHA. I am calling my CAPTCHA "ADSS AntiBot". I have a friend, he is breaking such security using OCR. He said not very bad. You can make your own protection based on this (link to us greatly appreciated, ADSS).

 

Background

 

Everybody knows that world wide web filled up with spam bots, and webmaster (usually using developers) fight against this problem. Because every webmaster dreams about real visitors, not about thousands of bots that using some service and even resell it on another web site. So, the things known as CAPTCHA ("Completely Automated Public turing test to tell Computers and Humans Apart") were made to fight against bots. I am using term AntiBot for my application.

 

Using the code

 

How to use downloaded source? Very easily. Just put some image on your asp.net page with Image URL set to captcha.ashx. This handler generates new image each time it accessed, and stores right value into the session.

string number_server_side=(string)Session[ADSSAntiBot.SESSION_CAPTCHA];

if (number_server_side == TextBox_number.Text)

{ // The code entered is valid }

else

{ // The code entered invalid }

This way you can use created HTTP handler. Another thing, I want to note, the image size. You can easily set it using ADSS AntiBot public constructor inside ProcessRequest function of handler.

public void ProcessRequest {context.Response.ContentType = "image/jpeg"; ADSSAntiBot captcha = new ADSSAntiBot(300,80);

// Set size using constructor string str = captcha.DrawNumbers(5);

if (context.Session[ ADSSAntiBot.SESSION_CAPTCHA] == null) context.Session.Add(ADSSAntiBot.SESSION_CAPTCHA, str);

else {context.Session[ ADSSAntiBot.SESSION_CAPTCHA] = str; }

Bitmap bmp = captcha.Result; bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);}


Next picture shows result of this code. Sometimes you want large image, sometimes smaller. Points of Interest so, how we are creating this "hard to OCR" image? And how to improve it? We are using graphic path to warp image. Basically transformations is sin wave, regular random noise and some rotation. What is the goal? To create text that can be read by the human, not by the bot. How we are making this? We are "shaking" sub paths of our text. So, what is the goal of transform? First of all, we need to change it to make a letter separation hard. For this we are adding line through all the number displayed. Second thing, we need to remember, our numbers should be hard to OCR using a neural network. By moving path points randomly and by sin wave we are making line very think in some places. On the sample image, you can see this effect on "9" and "6". The following function is the heart of our transformation:

public GraphicsPath RandomWarp(GraphicsPath path)
public GraphicsPath RandomWarp(GraphicsPath path)

// Add line //

int PsCount = 10;

PointF[] curvePs = new PointF[PsCount * 2];

for (int u = 0; u < PsCount; u++) { curvePs[u].X = u * (Width / PsCount); curvePs[u].Y = Height / 2; }

for (int u = PsCount; u < (PsCount * 2); u++) { curvePs[u].X = (u - PsCount) * (Width / PsCount); curvePs[u].Y = Height / 2 + 2; }

path.AddLines(curvePs);

double eps = Height * 0.05;

double amp = rnd.NextDouble() * (double)(Height / 3);

double size = rnd.NextDouble() * (double)(Width / 4) + Width / 8; double offset = (double)(Height / 3);

PointF[] pn = new PointF[path.PointCount];

byte[] pt = new byte[path.PointCount];

GraphicsPath np2 = new GraphicsPath();

GraphicsPathIterator iter = new GraphicsPathIterator(path);

for (int i = 0; i < iter.SubpathCount; i++) { GraphicsPath sp = new GraphicsPath();

Matrix m = new Matrix();

m.RotateAt(Convert.ToSingle(rnd.NextDouble() * 30 - 15),sp.PathPoints[0]);

m.Translate(-1 * i, 0); sp.Transform(m);

p2.AddPath(sp, true);

for (int i = 0; i < np2.PointCount; i++) { pn[i] = Wave(np2.PathPoints[i], amp, size); pt[i] = np2.PathTypes[i];

GraphicsPath newpath = new GraphicsPath(pn, pt);

return newpath;


We are not using lot of fonts like others do, not using colors. Color model only helps OCRing many captchas, font is not an obstacle too. But here we are making "random" font using this warp. I just wanted to show that .net framework makes it possible to protect site from Bots much better then other web languages. Also, I'm interesting in your suggestions about non-captcha bot protections. I'm going to continue my research in this direction. History The code described in this article is complete freeware. You can try it online. Also, you can download the last version on ADSS Web Site. Feel free to create your CAPTCHAs based on this one to fight against spam.

Login to add your contents and source code to this article
share this article :
post comment
 

A formal release under the terms of LGPL (or the MIT licence), formally establishes you as the author and freedom for the code to be used. May I also suggest that you post the complete project to www.CodePlex.Com

Posted by Clive Chinery Mar 16, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor