🌐 크로스 사이트 스크립트 (XSS) - 실습 시뮬레이터

CWE-79: Improper Neutralization of Input During Web Page Generation ('XSS')

✅ 학습 진행도
  • 진단 시작
  • XSS 취약점 식별
  • HTML 엔티티 치환(replaceAll)
✏️ Java 코드 에디터 취약한 코드
BoardController.java 💡 특수문자(<, >, &, ", ')를 HTML 엔티티로 변환하세요.
public String showBoard(String content) {
    // [취약한 코드] 외부 입력값 content를 별도의 검증이나 변환 없이
    // 그대로 브라우저로 출력하면 스크립트가 실행될 수 있다.
    // 예: <script>alert(1)</script>
    return content; 
}
💡 수정 가이드 (KISA 가이드 기준)
1. replaceAll()을 이용한 치환

스크립트 태그 및 속성값에 사용되는 5대 특수문자를 안전한 HTML 엔티티로 변환합니다.

content = content.replaceAll("&", "&"); content = content.replaceAll("<", "<"); content = content.replaceAll(">", ">"); content = content.replaceAll("\"", """); content = content.replaceAll("'", "'"); return content;
📊 분석 결과 및 설명
🎯 목표

게시판 본문 등 사용자 입력값을 출력할 때, 악성 스크립트가 포함되어 있어도 실행되지 않고 텍스트로만 표시되도록 해야 합니다.

⚠️ 현재 취약점

Stored XSS

입력값을 그대로 반환(Reflected)하거나 저장 후 출력(Stored)할 때 스크립트 실행이 가능합니다.

✅ 학습 진행도
  • 진단 시작
  • 취약점 식별
  • escape() 함수 적용
🐍 Python 코드 에디터 취약한 코드
app.py (Flask) 💡 escape() 함수나 템플릿 엔진의 자동 이스케이프를 사용하세요
from flask import Flask, request

app = Flask(__name__)

@app.route('/hello')
def hello():
    name = request.args.get('name', '')
    # [취약한 코드] 사용자 입력을 그대로 HTML에 포함하여 반환
    # name에  입력 시 실행됨
    return '

Hello, %s

' % name
💡 수정 가이드 (Python 가이드 기준)
1. escape() 함수 사용

Markupsafe 라이브러리의 escape 함수를 사용하여 특수문자를 변환합니다. (Flask에서는 `render_template` 사용 시 자동 적용됨)

from markupsafe import escape return '

Hello, %s

' % escape(name)
📊 분석 결과 및 설명
🎯 목표

HTML 컨텍스트에서 신뢰할 수 없는 데이터가 렌더링될 때, 이를 안전한 텍스트로 처리해야 합니다.

⚠️ 현재 취약점

Reflected XSS

%s 포맷팅을 통해 HTML 문자열을 직접 생성하고 있어 취약합니다.

입력 시 실행됨 return '

Hello, %s

' % name`; document.getElementById('codeEditorPython').value = initial; document.getElementById('codeDisplayPython').textContent = initial; if(window.Prism) Prism.highlightElement(document.getElementById('codeDisplayPython')); document.getElementById('feedbackPython').innerHTML = ''; document.getElementById('validateBtnPython').disabled = true; document.getElementById('codeStatusPython').className = 'status-badge status-vulnerable'; document.getElementById('codeStatusPython').textContent = '취약한 코드'; for(let i=0; i<3; i++) updateChecklist('progressChecklistPython', i, false); }