序文
みなさん、こんにちは、いい人は私です、私はいい人です、私は-0niseです。主要な脆弱性報告プラットフォームでは、XSSの脆弱性がよく見られます。問題は、なぜこの種の脆弱性が発生するのですか?この脆弱性はどのように修正すべきですか?
文章
1.xss? XSS? XSSとは何ですか?
XSSは、クロスサイトスクリプトとも呼ばれます。私は彼にもともとCSSと呼ばれていたとは言いませんが、私たちが使用するカスケードスタイルシートCSSと混同しないようにします。 CSS(クロスサイトスクリプト攻撃)、CSS(カスケードスタイルシート)はばかげており、違いを伝えることはできません。 XSSと呼ばれます。
2. XSSの危険は何ですか?
実験1:
0x00コンストラクトコード
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "Utf-8"%> <%string path = request.getContextpath(); string basepath = request.getscheme()+"://"+request.getServername()+":"+request.getServerport( " - // w3c // dtd HTML 4.01 Transitional // en"> <HTML> <head> <head> <base href = "<%= basepath%>"> <title>私のjsp 'index.jsp'開始ページ</title> <meta http-equiv = "pragma" content = " content = "no-cache"> <meta http-equiv = "compies" content = "0"> <meta http-equiv = "keywords" content = "keyword11、keyword2、keyword3"> <meta http-equiv = "descripting content =" request.setchAracterencoding( "utf-8"); //ユーザーの受信値文字列tmp = request.getParameter( "opr"); //デフォルトの受信値は空です(tmp == null){out.print( "111"); } else {// transcoding string opr = new String(tmp.getBytes( "ISO-8859-1")、 "UTF-8"); out.print(opr); }%>私はコンテンツです</div> </body> </html>0x01環境レイアウト
0x02脆弱性ドリル
http:// localhost:8080/xss/index.jsp?opr = i%e6%98%a7%a7%8b
次に、http:// localhost:8080/xss/index.jsp?opr = 0niseにアクセスしてください
最後に、「偉大な法律」を発見しました。
OPRパラメーターが等しいページを印刷します。 (ナンセンスのようです)
写真をロードして見てみましょう
訪問:http:// localhost:8080/xss/index.jsp?opr =%3cimg%20src =%221.png%22%3e%3c/img%3e
すべての画像をロードできるため、JSファイルもロードされていますか?
訪問: http:// localhost:8080/xss/index.jsp?opr =%3cscript%3ealert(/i%e6%98%a5%e7% A7%8B%E7%A4%BE%E5%8C%BA%E6%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6/)%3C/スクリプト%3E
JS? JS?ジャンプ後にアドレスを変更することは可能ですか?
http:// localhost:8080/xss/index.jsp?opr =%3cscript%3elocation.href =%27http://bbs.icunqiu.com%27%3c/script%3e
XSSはJSをロードできるため、JSを介して地元のものを開きますか?
MD5.exeファイルを事前に配置します
訪問:http:// localhost:8080/xss/index.jsp?opr = <scrip> var objshell = new ActiveXObject( "wscript.shell"); objshell.run( "g:/work/xss/webroot/md5.exe"); </script>
ローカルファイルでさえ開くことができるので、リモートファイルトロイの木馬?コンピュータのスプーフィングがありますか?これはゆっくりと象限です。私はそれを言わなかった。 。 。 。 。
すべてのファイルを開くことができるので、いくつかのファイルを書き込むことはどうですか?
訪問:http:// localhost:8080/xss/index.jsp?opr =%3cscript%3evar%20fso、tf; fso%20 =%20 new%20activexobject(%22scripting.filesystemObject%22); tf%20 =%20fso.createTextfile(%22d://////////////////////////tett。 Tf.WriteLine(%22i%E6%98%A5%E7%A7%8B%E7%A4%BE%E5%8C%Ba%E6%A2%E8%BF%8E%E6%82%A8%22); Tf.Close ();アラート(%22%E6%96%87%E4%BB%B6%E5%86%99%E5%85%A5%E6%88%90%E5%8a%9F%EF%BC%81%22);%3C/スクリプト%3E
上記の実験を通じて、OPRパラメーター割り当て操作を確認できます。 OPRパラメーターに値がない場合、実行できません。攻撃された人は、攻撃する前に事前に設計された攻撃者にアクセスする必要があります。このXSS攻撃方法は、ストレージXSSと呼ばれます
より強力な実験を見たい場合は、読み続けてください。
実験2:
序文:
ほとんどのWebサイトはデータを扱います。では、XSSの脆弱性が現れると、これらのWebサイトはどのように見えますか?
0x00コンストラクトコード
データベースパーツ
Basedao.java
java.sql.connection; Import java.sql.drivermanager; Import java.sql.preparedStatement; Import java.sql.Resultset; Import Java.sql.sqlecception; Import java.sql.Statement; public connectementao { /connection getconn( try {class.forname( "com.microsoft.sqlserver.jdbc.sqlserverdriver"); conn = drivermanager.getConnection( "jdbc:sqlserver:// localhost:1433; databasename = sqltmp"、 "sa"、 "sa"); } catch(classNotFoundException e){e.printstacktrace(); } catch(sqlexception e){e.printstacktrace(); } return conn; } // public void closeall(connection conn、statement stat、resultet rs){try {if(rs!= null)rs.close(); if(stat!= null)stat.close(); if(conn!= null)conn.close(); } catch(sqlexception e){e.printstacktrace(); }} // closeメソッドパブリックvoid closeall(connection conn、preated -statement pstat、resultet rs){try {if(rs!= null)rs.close(); if(pstat!= null)pstat.close(); if(conn!= null)conn.close(); } catch(sqlexception e){e.printstacktrace(); }} // public void closeall(connection conn、preatedstatement pstat){try {if(pstat!= null)pstat.close(); if(conn!= null)conn.close(); } catch(sqlexception e){e.printstacktrace(); }} // public int updateを追加、削除、変更するためのパブリックメソッド(String SQL、Object [] Pram){preatedStatement PSTAT = null;接続conn = null; int a = 0; try {conn = getConn(); pstat = conn.preparestatement(sql); //パラメーターセットを介して転送し、セットのパラメーター対応を(int i = 1; i <= pram.length; i ++){pstat.setobject(i、pram [i-1]); } //呼び出し方法a = pstat.executeupdate(); } catch(sqlexception e){e.printstacktrace(); }最後に{closeall(conn、pstat); } return a; }}commentdao.java
Java.sql。*;インポートJava.util。*;インポートエンティティ。*;パブリッククラスのcommentDao extendas basedao { /***すべてのメッセージを取得*** /publicリスト<comm> getComment(){// sqlステートメントsql = "select cid、cname、ccontext from comments";リスト<comm> list = new arrayList <comm>(); //データベース接続オブジェクト接続conn = null; // sql実行オブジェクトreportStatement pstmt = null; //データベース実行return値結果rs = null; try {//データベースリンクConn = this.getConn(); // SQL実行オブジェクトPSTMT = CONN.PREPARESTATEMENT(SQL)を作成します。 // sqlステートメントのreturn値rs = pstmt.executequery(); // while(rs.next()){comment = new comm(); comment.setcid(rs.getint( "cid")); comment.setcname(rs.getString( "cname")); comment.setcontext(rs.getString( "ccontext")); list.add(コメント); }} catch(例外e){e.printstacktrace(); }最後に{// this.closeall(conn、pstmt、rs); }返品リスト。 } public int addComment(commコメント){string sql = "コメント値に挿入値(?、?)"; //影響を受ける行の数int result = 0; //データベース接続オブジェクト接続conn = null; // sql実行オブジェクトreportStatement pstmt = null; try {//データベースリンクConn = this.getConn(); // SQL実行オブジェクトPSTMT = CONN.PREPARESTATEMENT(SQL)を作成します。 //パラメーターを設定しますpstmt.setString(1、comment.getcname()); pstmt.setString(2、comment.getccontext()); // sqlステートメント結果= pstmt.executeupdate(); } catch(Exception e){e.printstacktrace(); }最後に{this.closeall(conn、pstmt); } return result; }}commentservlvet
java.io。*;インポートjavax.servlet。*;インポートjavax.servlet.http。*;インポートエンティティ。 request.setcharacterencoding( "utf-8"); Response.setContentType( "text/html; charset = utf-8"); printwriter out = response.getWriter(); string opr = request.getParameter( "opr"); commentdao commentdao = new CommentDao(); //パラメーターが空であるかどうかを取得する場合(opr == null || opr.equals( "all")){request.setattribute( "all"、commentdao.getcomment()); // forward request.getRequestDispatcher( "comment.jsp")。 } else if(opr.equals( "add")){comment comment = new comm(); comment.setcname(request.getParameter( "uname")); comment.setCcontext(request.getParameter( "context")); if(commentdao.addcomment(comment)> 0){out.print( "<script> alert( 'メッセージを正常に去る'を残す); location.href = 'commentservlvet?opr = all'; </script>"); } else {out.print( "<script> alert( 'message failed'); location.href = 'commentservlvet?opr = all'; </script>"); }} else {request.setattribute( "all"、commentdao.getComment()); // forward request.getRequestDispatcher( "comment.jsp")。 } out.flush(); out.close(); } / ** * dopost() * / public void dopost(httpservletrequest request、httpservletResponse応答)servletexception、ioexception {doget(request、response); }}comment.jsp
<%@ページ言語= "java" import = "java.util。*、entity。*" pageencoding = "utf-8"%> <%string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getServername()+" " - // W3C // DTD HTML 4.01 Transitional // En"> <HTML> <head> <head> <base href = "<%= basepath%>"> <title> my jsp 'comment.jsp'開始ページ</title> <meta http-equiv = "pragma" content = " content = "no-cache"> <meta http-equiv = "compers" content = "0"> <meta http-equiv = "keywords" content = "keyword1、keyword2、keyword3"> <meta http-equiv = "description" content = " if(request.getAttribute( "all")== null){request.getRequestDispatcher( "Contumentervlvet?opr = all")。 }%> <table> <%list <entity.comm> list =(list <entity.comm>)request.getAttribute( "all"); for(int i = 0; i <list.size(); i ++){%> <tr> <td> <%= list.get(i).getCname() <TextArea rows = "5" cols = "30" name = "context"> </textarea> nickname:<input type = "text" name = "uname"/> <入力タイプ= "submit" value = "submit"/> </form> </body> </html> </html> 0x01脆弱性実験
root@1~#
メッセージボードにメッセージを残します。
<script> var objShell = new ActiveXObject("wscript.shell");objShell.Run("G:/work/XSS/WebRoot/Md5.exe");</script>
次に、http:// localhost:8080/xss/comment.jspにアクセスしてください
このように、このページにアクセスする限り、ソフトウェアは自動的に開き、リモートファイルが作成されますか?ゆっくりと理解してください。
root@2~#
メッセージボードにメッセージを残します。
次のようにコードをコピーします:<script> var fso、tf; fso = new ActiveXObject( "Scripting.filesystemObject"); tf = fso.createTextfile( "d://test.txt"、true); tf.writeLine( "i chunqiu Community You"); tf.close(); tf.close();
次に、http:// localhost:8080/xss/comment.jspにアクセスしてください
ファイルの書き込みは成功しました。
root@3~#
メッセージコンテンツ:
[code] <scrip> location.href = 'http://bbs.icunqiu.com' </script> [code]
ページをご覧ください:http:// localhost:8080/xss/comment.jsp
メッセージページにアクセスすると、攻撃者固有のWebサイトに自動的にジャンプします。これは伝説的なハイジャックですか?
3.XSS防御ソリューション
sayingにもあるように、攻撃があっても、防御があります。 XSSと同様に、攻撃方法と防御ソリューションがあります。
El Expression + JSTLタグライブラリ
el(式言語):[size = 12.0000pt] JSPを簡単に作成します。表現言語は、ECMAScriptとXPath Expression Languageに触発されています。 JSPコードをより簡単にするために、JSPの式を簡素化する方法を提供します。
JSTL(JSP Standard Tag Library):オープンソースJSPタグライブラリ。
実験1防衛コード:
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "UTF-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core"プレフィックス= "c"%> <%string path = request. request.getscheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/";%> <! 'index.jsp'開始ページ</title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "expires" content = "0"> <meta http-equiv = "fike kike kike kike =" contents " http-equiv = "description" content = "これは私のページです"> </head> <body> <div style = "margin:0 auto"> <%request.setcharacterencoding( "utf-8");文字列tmp = request.getParameter( "opr"); //着信値が空であるかどうか(tmp == null){out.print( "111"); } else {// transcoding string opr = new String(tmp.getBytes( "ISO-8859-1")、 "UTF-8"); request.setattribute( "name"、opr); %> <c:out value = "$ {requestscope.name}"> </c:out> <%}%>私はコンテンツ</div> </body> </html>実験2防衛コード:
<%@ページ言語= "java" import = "java.util。*、entity。*" pageencoding = "utf-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "" c%> <%string = <request.getContextpath path = request.getScheme()+"://"+request.getServername()+":"+request.getServerport()+path+"/";%>
< http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" keyword1、keyword3 "keyword3"> <meta http-equiv = "content =" request.setcharacterencoding( "utf-8"); if(request.getAttribute( "all")== null){request.getRequestDispatcher( "Contumentervlvet?opr = all")。 }%> <table> <! - 防衛xssソリューション - > <c:foreach var = "x" items = "$ {requestscope.all}"> <tr> <td> <c:out value = "$ {x.getCname()}"> </c:out> </td> <td> < </td> </td> </tr> </c:foreach> </table> <form action = "commentservlvet?opr = add" method = "post"> <textarea rows = "5" cols = "30" name = "context"> </textarea> nickname:<input type = "text" name = "uname"/> <input = " </body> </html>結論
技術は白黒ではなく、専門化は非常に優れています。