Filtering - 입력
- 문제가 있는 정보의 입력을 막아내는 행위
- mysqli_real_escape_string 함수를 이용해 SQL문에 삽입된 따옴표(')를 escape string(/')으로 바꿔줌으로써 SQL injection 공격을 예방할 수 있다.
Escaping - 출력
- 문제가 있는 정보가 이미 입력된 상태에서 그 정보가 사용자들에게 노출되는 상황을 차단하는 행위
- htmlspecialchars함수를 이용해 html과 관련된 특수문자(<, >, ", ', %) 등을 변환시킴으로써 웹 페이지에 악성 스크립트를 삽입하는 공격기법인 XSS를 막을 수 있다.
- Non-persistent XSS vs Persistent XSS
간단한 PHP용 HTML 필터링 함수
function html_filter($content)
{
// Strip bad elements.
$content = preg_replace('/(<)(|\/)(\!|\?|html|head|title|meta|body|style|link|base|script'.
'|frameset|frame|noframes|iframe|applet|embed|object|param|noscript|noembed|map|area|basefont|xmp|plaintext|comment)/i',
'<$2$3', $content);
// Strip script handlers.
$content = preg_replace_callback("/([^a-z])(o)(n)/i",
create_function('$matches', 'if($matches[2]=="o") $matches[2] = "o";
else $matches[2] = "O"; return $matches[1].$matches[2].$matches[3];'), $content);
return $content;
}
참고
PHP 보안: https://opentutorials.org/course/3167/19612
XSS: ko.wikipedia.org/wiki/%EC%82%AC%EC%9D%B4%ED%8A%B8_%EA%B0%84_%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8C%85
'etc' 카테고리의 다른 글
[메모] 서킷브레이커 공부용 자료 모음(한글) (0) | 2022.09.22 |
---|---|
[정리] 못하지 않는 개발자 되기 (4) | 2022.08.29 |
[CLI] 터미널 기초 (0) | 2021.02.18 |
[PHP] PHP 기본 문법 (0) | 2021.01.28 |
[REST API] REST API 리드미 작성하기 (0) | 2021.01.24 |