All-station anti-injection commonProgram
Create a global. asax application file under the root directory of the site, which can be completely protected against injection. The program content is as follows:
<% @ Application language = "C #" %>
<SCRIPT runat = "server">
Void application_start (Object sender, eventargs E)
{
//Code
}
Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed
}
Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs
}
Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts
}
Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session Mode
// If it is set to StateServer or sqlserver, this event is not triggered.
}
/// <Summary>
/// Replace all injected strings and record injection Information
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Void application_beginrequest (Object sender, eventargs E)
{
//************************
// Global anti-Injection
// Author: Yi Qiu Han
//************************
// Triggered when an application request is received.
String [] KEYWORDS = new string [] {";", "'", "--", "XP _", "XP _"};
String [] safekeys = "& #59; | & #39; | & #45; & #45; | & #120; & #112; & #95; | & #88; & #80; & #95; | & #120; & #80; & #95; | & #88; & #112; & #95; ". split ('| ');
String querystring = server. urldecode (request. querystring. tostring ());
String url = request. url. absolutepath;
// Excluded Extension
String [] dotfilename = URL. Split ('.');
String dotname = dotfilename [dotfilename. Length-1];
Dotfilename = new string [] {"axd "};
// Exit directly when the excluded extension is displayed.
Foreach (string STR in dotfilename)
{
If (STR = dotname)
Return;
}
For (INT I = 0; I <keywords. length; I ++)
{
String key = keywords [I];
If (querystring. Contains (key ))
{
//******************************
// Record injection Information
//******************************
String intorecord = system. datetime. Now. tostring ();
Intorecord + = "" + request. url. Host;
Intorecord + = "" + request. requesttype;
Intorecord + = "" + request. url. absolutepath;
Intorecord + = "" + server. urldecode (request. querystring. tostring ());
Intorecord + = "" + request. userhostaddress;
Intorecord + = "" + request. useragent;
Intorecord + = "\ r ";
Try
{
String Path = server. mappath (@ "/_ intorecordlog /");
If (! System. Io. Directory. exists (PATH ))
System. Io. Directory. createdirectory (PATH );
System. Io. file. appendalltext (path + datetime. Now. tostring ("yyyymmdd") + ". log", intorecord, system. Text. encoding. Default );
}
Catch {}
// Replace the injected URL and jump to the URL
Querystring = querystring. Replace (Key, safekeys [I]);
Response. Redirect (URL + "? "+ Querystring );
Response. End ();
}
}
}
</SCRIPT>