Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- juice-shop
- JDBC
- 백준알고리즘
- sqli
- CodeQL
- 알고리즘
- LGTM
- github action
- C언어
- DVWA
- OWASP
- github
- Python
- gosec
- goKart
- SUA
- Network
- ubuntu
- 자료구조
- Juice Shop
- OpenSource
- firewall
- MySQL
- Codeup
- Database
- 운영체제
- 데이터통신
- virtualbox
- JSP
- gotify
Archives
- Today
- Total
비트(bit)주세요
[Python] 취약/안전 코드를 작성해보기 본문
728x90
1. SQL Injection
SQL Injection 취약점에 취약한 코드
cmd = "update people set name='%s' where id='%s'" % (name, id)
curs.execute(cmd)
SQL Injection 취약점에 안전한 코드
cmd = "update people set name=%s where id=%s"
curs.execute(cmd, (name, id))
2. file download (wargame 소스코드 발췌)
file dowload 취약점에 취약한 코드
@APP.route('/read')
def read_memo():
error = False
data = b''
filename = request.args.get('name', '')
try:
with open(f'{UPLOAD_DIR}/{filename}', 'rb') as f:
data = f.read()
except (IsADirectoryError, FileNotFoundError):
error = True
return render_template('read.html',
filename=filename,
content=data.decode('utf-8'),
error=error)
file download 취약점에 안전한 코드
@APP.route('/read')
def read_memo():
error = False
data = b''
filename = request.args.get('name', '')
filename = filename.replace('../', '')
try:
with open(f'{UPLOAD_DIR}/{filename}', 'rb') as f:
data = f.read()
except (IsADirectoryError, FileNotFoundError):
error = True
return render_template('read.html',
filename=filename,
content=data.decode('utf-8'),
error=error)
3. XSS (Django 소스코드 발췌)
취약한 코드
var li = $('<li>').append(res.result + ' - ' + res.time.htmlEscape());
안전한 코드
var li = $('<li>').append(res.result.htmlEscape() + ' - ' + res.time.htmlEscape());
취약한 코드
var query = decodeURIComponent(windows.location.search.match(/inputSearch=(.*?)(&|$)/)[1]).replace('+',' ');
안전한 코드
import DOMPurify from 'dompurify';
var query = DOMPurify.sanitize(decodeURIComponent(window.location.search.match(/inputSearch=(.*?)(&|$)/)[1]).replace('+', ' '));
취약한 코드
+ "ErrorId: " + errorId + "<br>URL: " + window.location.href + "<br>UserAgent: " + userAgent + "<br>" + msg + " in " + url + " at line " + linenumber + "'</div></div>");
안전한 코드
+ "ErrorId: " + errorId + "<br>URL: " + padutils.escapeHtml(window.location.href) + "<br>UserAgent: " + userAgent + "<br>" + msg + " in " + url + " at line " + linenumber + "'</div></div>");
'SUA > 오픈소스 보안' 카테고리의 다른 글
정보보안 SUA - [오픈소스 보안] 11주차 (0) | 2021.12.14 |
---|---|
[codeQL] U Boot Challenge - VS Code (0) | 2021.12.11 |
[Juice-Shop] CAPTCHA Bypass (0) | 2021.12.10 |
[Juice-Shop] XXE Data Access (0) | 2021.12.10 |
정보보안 SUA - [오픈소스 보안] 10주차 (0) | 2021.12.08 |