Lorsque j'ai eu un tas de tentatives d'attaques par injection sur mon serveur, j'étais inquiet qu'ils prennent des ressources inutiles. J'ai écrit (hacké!) Un HttpModule dans C# qui filtrerait la plupart des attaques par injection xss et sql. Le code est collé ci-dessous, avec la section de configuration requise pour qu'un site Web l'utilise. Il devrait être mis dans un projet et compilé à WebSecurityFilter.dll, qui devrait alors être référencé par le projet web (ou sinon déposé dans le répertoire bin).
Cela fonctionnera uniquement avec asp.net, donc j'espère que votre site est basé sur asp.net (j'ai demandé dans un commentaire, mais je n'ai pas eu de réponse).
section de configuration Web (ajoutez dans la <httpModules> section de <system.web>:
<add name="SecurityHttpModule" type="WebSecurityFilter.SecurityHttpModule, WebSecurityFilter" />
code pour le module (SecurityHttpModule.cs):
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace WebSecurityFilter
{
class SecurityHttpModule : IHttpModule
{
class RegexWithDesc : Regex
{
string _errorText;
public string ErrorText
{
get { return _errorText; }
}
public RegexWithDesc(string regex, RegexOptions options, string errorText)
:base(regex, options)
{
_errorText = errorText;
}
}
/// <summary>
/// error text displayed when security violation is detected
/// </summary>
private string _errorhtml =
@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">" +
@"<html xmlns=""http://www.w3.org/1999/xhtml"" >" +
@"<body style=""background:black;"">" +
@"<table style=""width:100%"" >" +
@"<tr><td align=""center"">" +
@"<div style=""border:3px solid red;text-align:center;width:95%;color:red;padding:10px;text-decoration:blink;"">" +
@"SECURITY VIOLATION" +
@"<br/>" +
//@"<br/>" +
//@"go away" +
//@"<br/>" +
@"<br/>" +
@"{0}" +
@"<br/>" +
@"</div>" +
@"</td></tr>" +
@"</table>" +
@"</body>" +
@"</html>";
// regex for default checks
// http://www.securityfocus.com/infocus/1768
static RegexOptions _defaultRegexOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;
RegexWithDesc[] _regexCollection = new RegexWithDesc[]
{
new RegexWithDesc(@"((¼|<)[^\n]+(>|¾)*)|javascript|unescape", _defaultRegexOptions, "XSS 1"), //3.3
// new RegexWithDesc(@"(\')|(\-\-)", _defaultRegexOptions, "SQL 1"), //2.1
new RegexWithDesc(@"(=)[^\n]*(\'|(\-\-)|(;))", _defaultRegexOptions, "SQL 2"), //2.2
//new RegexWithDesc(@"\w*(\')(or)", _defaultRegexOptions, "SQL 3"), //2.3
new RegexWithDesc(@"(\')\s*(or|union|insert|delete|drop|update|create|(declare\[email protected]\w+))", _defaultRegexOptions, "SQL 4"), //2.4
new RegexWithDesc(@"exec(((\s|\+)+(s|x)p\w+)|(\[email protected]))", _defaultRegexOptions, "SQL 5") //2.5
};
#region IHttpModule Members
public void Dispose()
{
// nothing to do
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
try
{
List<string> toCheck = new List<string>();
foreach (string key in HttpContext.Current.ApplicationInstance.Request.QueryString.AllKeys)
{
toCheck.Add(HttpContext.Current.ApplicationInstance.Request[key]);
}
foreach (string key in HttpContext.Current.ApplicationInstance.Request.Form.AllKeys)
{
toCheck.Add(HttpContext.Current.ApplicationInstance.Request.Form[key]);
}
foreach (RegexWithDesc regex in _regexCollection)
{
foreach (string param in toCheck)
{
string dp = HttpUtility.UrlDecode(param);
if (regex.IsMatch(dp))
{
HttpContext.Current.ApplicationInstance.Response.Write(string.Format(_errorhtml, regex.ErrorText));
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
}
}
}
catch (System.Threading.ThreadAbortException x)
{
throw;
}
catch (Exception ex)
{
HttpContext.Current.ApplicationInstance.Response.Write(string.Format(_errorhtml, "Attack Vector Detected"));
HttpContext.Current.ApplicationInstance.Response.Write(string.Format(_errorhtml, ex.GetType().ToString()));
HttpContext.Current.ApplicationInstance.CompleteRequest();
return;
}
}
#endregion
}
}
Espérons que tout au format correct ...
Je vais essayer de poster un lien vers le projet complet dans un zip ce soir.
Quelle est la langue dans laquelle l'application incriminée est écrite? –