我是靠谱客的博主 畅快小蜜蜂,这篇文章主要介绍.net core xss攻击防御的方法,现在分享给大家,希望可以做个参考。

XSS攻击全称跨站脚本攻击 ,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。

比如我们在表单提交的时候插入脚本代码

如果不进行处理,那么就是这种效果,我这里只是演示一个简单的弹窗

下面给大家分享一下我的解决方案。

需要用到这个库:HtmlSanitizer

https://github.com/mganss/HtmlSanitizer

新建一个过滤类。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class XSS { private HtmlSanitizer sanitizer; public XSS() { sanitizer = new HtmlSanitizer(); //sanitizer.AllowedTags.Add("div");//标签白名单 sanitizer.AllowedAttributes.Add("class");//标签属性白名单,默认没有class标签属性 //sanitizer.AllowedCssProperties.Add("font-family");//CSS属性白名单 } /// <summary> /// XSS过滤 /// </summary> /// <param name="html">html代码</param> /// <returns>过滤结果</returns> public string Filter(string html) { string str = sanitizer.Sanitize(html); return str; } }

新建一个过滤器

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class FieldFilterAttribute : Attribute,IActionFilter { private XSS xss; public FieldFilterAttribute() { xss = new XSS(); } //在Action方法之回之后调用 public void OnActionExecuted(ActionExecutedContext context) { } //在调用Action方法之前调用 public void OnActionExecuting(ActionExecutingContext context) { //获取Action参数集合 var ps = context.ActionDescriptor.Parameters; //遍历参数集合 foreach (var p in ps) { if (context.ActionArguments[p.Name] != null) { //当参数等于字符串 if (p.ParameterType.Equals(typeof(string))) { context.ActionArguments[p.Name] = xss.Filter(context.ActionArguments[p.Name].ToString()); } else if (p.ParameterType.IsClass)//当参数等于类 { ModelFieldFilter(p.Name, p.ParameterType, context.ActionArguments[p.Name]); } } } } /// <summary> /// 遍历修改类的字符串属性 /// </summary> /// <param name="key">类名</param> /// <param name="t">数据类型</param> /// <param name="obj">对象</param> /// <returns></returns> private object ModelFieldFilter(string key, Type t, object obj) { //获取类的属性集合 var ats = t.GetCustomAttributes(typeof(FieldFilterAttribute), false); if (obj != null) { //获取类的属性集合 var pps = t.GetProperties(); foreach (var pp in pps) { if(pp.GetValue(obj) != null) { //当属性等于字符串 if (pp.PropertyType.Equals(typeof(string))) { string value = pp.GetValue(obj).ToString(); pp.SetValue(obj, xss.Filter(value)); } else if (pp.PropertyType.IsClass)//当属性等于类进行递归 { pp.SetValue(obj, ModelFieldFilter(pp.Name, pp.PropertyType, pp.GetValue(obj))); } } } } return obj; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//属性过滤器 [FieldFilter] public class NoteBookController : ManageController { //笔记操作接口 private INoteBookAppService _noteBookApp; public NoteBookController(INoteBookAppService noteBookApp) { this._noteBookApp = noteBookApp; } public IActionResult Tab() { return View(); } }

然后在需要过滤的控制器加上过滤控制器特性就可以了。这样所有string类型的参数就都会进行过滤了。如果不需要对整个控制器进行过滤,只需要在相应的Action加上特性。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是畅快小蜜蜂最近收集整理的关于.net core xss攻击防御的方法的全部内容,更多相关.net内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(104)

评论列表共有 0 条评论

立即
投稿
返回
顶部