Giter VIP home page Giter VIP logo

mvc-honeypot's Introduction

MVC-Honeypot

Honeypot anti bot mechanism implementation in ASP.NET MVC https://dejanstojanovic.net/aspnet/2014/september/honeypot-implementation-in-mvc/

What does it do

This mechanism allows you to detect bot posts from forms on website without using CAPTCHA and bother visitors to enter weird letter and numbers.

In short, it is more elegant and user friendly approach in detecting bot form posts. It is based on masking the real field with field that has some illogical name.

When the form is posted the illogicaly named field holds actual data, and the meaningful named field is a trap field. If the meaningful named field value is set, that is proof that a bot has filled out the form (this field should not be visible on the page, so that only bots can find it inspecting document structure)

How does it work

The solution contains of three elements:

  • HtmlHelper for rendering out the input text control with honeypot trap
  • ActionFilterAttribute which validates request and marks request trap field
  • Extension method HasHoneypotTrapped for HttpRequestBase returning boolean value whether honeypot trap is triggered

How to use it

You can download the project and include in your solution as project or compiled dll. Another option is to install it with NuGet package manager.

ScreenShot

PM> Install-Package Mvc.Honeypot

There are few staps you need to to do in order to enable honeypot trap on your form page.

  • Add reference to Mvc.Honeypot
  • Add hobeypot field for the form field which will be used for the trap (usually it's an email field)
@Html.HoneyPotField("Email", Model.Email)

By default, helper will generate a text field for user and hidden field for bot.

<input name="6D9A89AAA95B1B3BFD6C7C5A6D5535FF" type="text" id="6D9A89AAA95B1B3BFD6C7C5A6D5535FF" />
<input name="Email" type="hidden" id="Email" />

As bots are getting smarter and smarter they can start checking input type of the field. The helper enables you to change input types of both value field and honey pot field.

<style type="text/css">
    .masked
    {
        display:none;
    }
</style>
@Html.HoneyPotField("Email", Model.Email, null, HtmlHelpers.InputType.Text, "masked", HtmlHelpers.InputType.Email)

This will produce more confusing html for the bot but as you see you will have to use some css to hide trap field from the normal user

<style type="text/css">
    .masked
    {
        display:none;
    }
</style>
<input name="6D9A89AAA95B1B3BFD6C7C5A6D5535FF" type="text" id="6D9A89AAA95B1B3BFD6C7C5A6D5535FF" />
<input name="Email" type="email" id="Email" class="masked" />
  • Add a filter with honeypot fields on the controller action
[HttpPost]
[HoneypotFilter("Email")]
public ActionResult PostForm(FormModel model)
{
    //Action logic
}

How to know if trap was triggered

In your post form action you should do a check similar to the following

[HttpPost]
[HoneypotFilter("Email")]
public ActionResult PostForm(FormModel model)
{
    if (ModelState.IsValid && Request.HasHoneypotTrapped())
    {
        //Honeypot trap triggered, possible bot
    }
    else if (ModelState.IsValid)
    {
        //Regular user, valid fields
    }
    else
    {
        //Regular user, invalid fields
    }
}

What to do whan you detect that honeypot is triggered

Usually when something is posted you show some thank you message and do something with posted data. In case of bot detection with honeypot you should not return any message different than normal post in your action. This will keep deceiving bot that data is successfully sent.

The only difference is that you will treat posed data differently than normal, ignore the data, log it somewhere, or mark as a bot post when storing.

mvc-honeypot's People

Contributors

dejanstojanovic avatar travispessetto avatar xactfoxy avatar

Watchers

James Cloos avatar

Forkers

viracall

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.