-
Notifications
You must be signed in to change notification settings - Fork 0
/
InjectFilter
145 lines (63 loc) · 2.2 KB
/
InjectFilter
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//接口请求过滤特殊字符,防注入
public class InjectFilter extends IsmpServletFilter {
private String failPage = "/loginout.jsp";//发生注入时,跳转页面
public void doFilter(ServletRequest request,ServletResponse response,
FilterChain filterchain)throws IOException, ServletException {
//判断是否有注入攻击字符
HttpServletRequest req = (HttpServletRequest) request;
String inj = injectInput(req);
if (!inj.equals("")) {
request.getRequestDispatcher(failPage).forward(request, response);
return;
} else {
// 传递控制到下一个过滤器
filterchain.doFilter(request, response);
}
}
/**
* 判断request中是否含有注入攻击字符
* @param request
* @return
*/
public String injectInput(ServletRequest request) {
Enumeration e = request.getParameterNames();
String attributeName;
String attributeValues[];
String inj = "";
while (e.hasMoreElements()) {
attributeName = (String)e.nextElement();
//不对密码信息进行过滤,一般密码中可以包含特殊字符
if(attributeName.equals("userPassword")||attributeName.equals("confirmPassword")||attributeName.equals("PASSWORD")
||attributeName.equals("password")||attributeName.equals("PASSWORD2")||attributeName.equals("valiPassword")){
continue;
}
attributeValues = request.getParameterValues(attributeName);
for (int i = 0; i < attributeValues.length; i++) {
if(attributeValues[i]==null||attributeValues[i].equals(""))
continue;
inj = injectChar(attributeValues[i]);
if (!inj.equals("")) {
return inj;
}
}
}
return inj;
}
/**
* 判断字符串中是否含有注入攻击字符
* @param str
* @return
*/
public String injectChar(String str) {
String inj_str = "\" ) \' * %";
String inj_stra[] = inj_str.split(" ");
for (int i = 0 ; i < inj_stra.length ; i++ )
{
if (str.indexOf(inj_stra[i])>=0)
{
return inj_stra[i];
}
}
return "";
}
}