思路: 对程序代码进行过滤非法的关键字

新建控制台程序,编写代码测试过滤效果

    class Program
{ static void Main(string[] args)
{
//GetStrRegex();
Console.WriteLine("请输入字符串:");
string str = Console.ReadLine();
for (int i = 0; i < 100; i++)
{
Test(str);
} }
static void Test(string str)
{ Console.WriteLine("请输入正则表达式:");
string StrRegex = Console.ReadLine(); str = Regex.Replace(str, StrRegex, "", RegexOptions.IgnoreCase); Console.WriteLine($"处理后的字符串为:{str}"); }
}

输入字符串测试及正则表达式,观察测试效果

字符串:<script>(script)</script><style>alert("中国伟大复兴")</style><h1>111</h1><h2>222</h2>drop delete <div style=""> select update exec trunc database table  index @@@hao好的// 中国。湖北。武汉&&  湖北-- 中国加油!

正则表达式:

a:    <[^>]*|&nbsp;
b:    <[^>]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt)\b|^\+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|<\s*img\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)

 

经过多次测试,选择你所认为合适的正则表达式

下面是我目前选择的正则表达式,你可以根据需要进行修改

   static string GetStrRegex()
{
List<string> strList = new List<string>();
List<string> htmlList = new List<string>() { "<h1>","<h2>","<h3>","<h4>","<h5>","<h6>","<style>","<script>","javascript","onload","onerror","eval","alert","prompt"};
List<string> sqlList = new List<string>() { "select","update","delete","drop","trunc","exec","table","database","or","and"};
List<string> chList = new List<string>() { "//","--", "@", "&" ,"||"};
strList.AddRange(htmlList);
strList.AddRange(sqlList);
strList.AddRange(chList);
string strRegex = string.Join("|", strList.ToArray());
Console.WriteLine($"你的正则表达式是{strRegex}");
return strRegex;
}

测试效果

其实最简单的做法就是url编码、html编码一下就可以了

C# 防XSS攻击 示例的更多相关文章

  1. 【前端安全】JavaScript防XSS攻击

    什么是XSS XSS(Cross Site Scripting),跨站脚本攻击,是一种允许攻击者在另外一个用户的浏览器中执行恶意代码脚本的脚本注入式攻击.本来缩小应该是CSS,但为了和层叠样式(Cas ...

  2. HTML标签防XSS攻击过滤模块--待优化

    HTML标签防XSS攻击过滤模块 http://cnodejs.org/topic/5058962f8ea56b5e7806b2a3

  3. java请求URL带参之防XSS攻击

    1.web.xml新增filter配置 <!-- URL请求参数字符过滤或合法性校验 --> <filter> <filter-name>XssFilter< ...

  4. 防xss攻击

    官方:https://jsxss.com/zh/index.html xss csrf https://www.cnblogs.com/443855539-wind/p/6055816.html 一. ...

  5. webform非表单提交时防xss攻击

     1.webform默认配置下,主动防御了针对表单提交的xss攻击,但这次发生时因为url导致的,所以webform的默认防御机制不起作用  webform下输出非表单提交获得的数据的时候,要加htm ...

  6. PHP 防xss攻击

    PHP直接输出html的,可以采用以下的方法进行过滤: 1.htmlspecialchars函数 2.htmlentities函数 3.HTMLPurifier.auto.php插件 4.Remove ...

  7. SpringBoot防XSS攻击

    1 . pom中增加依赖 <!-- xss过滤组件 --> <dependency> <groupId>org.jsoup</groupId> < ...

  8. [BUGCASE]CI框架的post方法对url做了防xss攻击的处理引发的文件编码错误

    一.问题描述 出现问题的链接: http://adm.apply.wechat.com/admin/index.php/order/detail?country=others&st=1& ...

  9. node防xss攻击插件

    var xss = require('node-xss').clean; router.post("/orders/insert-orders", function (req, r ...

  10. 防XSS攻击解决方法

    1.web.xml文件中新增filter配置 <!-- URL请求参数字符过滤或合法性校验 --> <filter> <filter-name>XssFilter& ...

随机推荐

  1. [ABC265D] Iroha and Haiku (New ABC Edition)

    Problem Statement There is a sequence $A=(A_0,\ldots,A_{N-1})$ of length $N$. Determine if there exi ...

  2. offline RL | Pessimistic Bootstrapping (PBRL):在 Q 更新中惩罚 uncertainty,拉低 OOD Q value

    论文题目:Pessimistic Bootstrapping for Uncertainty-Driven Offline Reinforcement Learning,ICLR 2022,6 6 8 ...

  3. NetSuite 开发日记 —— 科目类型码

    科目类型码可用于搜索判断科目类型 代码实现 var sch = search.create({ type: 'account', filters: ['type', 'anyof', 'Bank'], ...

  4. Vue学习笔记-生命周期

    整体页面代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  5. el-table 设置合并行或列时,显示错乱问题

    1. 需求效果图: 2. 接口数据格式: 点击查看代码 const list = [ { contractNo: "CAI-20220801001", contractItem: ...

  6. 2.elasticsearch中的mapping

    mapping 顾名思义,代表了映射关系.是文档中字段和数据类型的映射关系 为什么要了解mapping 虽然elasticsearch中已尽有的动态mapping(Dynamic Mapping),而 ...

  7. netty自定义channel id

    netty自定义channel id.netty custom channel id 搞搞netty时发现默认的id很长,无法直接自定义. 于是我网上搜索了search一下,发现没有相关文章,那就自己 ...

  8. Java并发(二十二)----wait notify的正确姿势

    开始之前先看看,sleep(long n) 和 wait(long n) 的区别: 1) sleep 是 Thread 的静态方法,而 wait 是 Object 的方法 2) sleep 不需要强制 ...

  9. Spring源码学习笔记4——BeanFactoryPostProcessor执行

    一丶BeanFactoryPostProcessor是什么 Spring留给我们的一个扩展接口,在BeanDefinition加载注册完之后,并执行一些前置操作(笔记3)之后会反射生产所有的BeanF ...

  10. Go语言实现GoF设计模式:备忘录模式的实践探索

    本文分享自华为云社区<[Go实现]实践GoF的23种设计模式:备忘录模式>,作者:元闰子. 简介 相对于代理模式.工厂模式等设计模式,备忘录模式(Memento)在我们日常开发中出镜率并不 ...