Posted
Filed under etc
 

공개 웹방화벽 Mod_Security


mod_security란?

mod_security는 웹어플리케이션 방화벽 이다

현재 공격유형을 보면 70%이상이 웹 어플리케이션레벨에서 이루어지고 있으며 이러한

공격으로부터 보다 안전하게 시스템이 유지될 수 있도록 해주는 것이 mod_security이다.

이는 mod_security가 단순 URL 뿐 아니라 GET, POST 값 그리고 HTTP1.1의 스펙을

분석하고 검사할 수 있어 실제 웹상에서 명령어를 치고 들어오는 웹해킹 및 크래킹을

방어하는 모듈이다.

설치 방법

아파치에서 설치하기 위해 가장 간단한 방법은 소스를 컴파일 하는 방법이다.

먼저 컴파일하기에 앞서 소스를 다운받디위해서는 아래 URL의사이트에 방문하여

다운받으면 된다 .

다운 받은후 아래와 같이 컴파일을 하면 된다.

$ /path/to/apache/bin/apxs -cia mod_security.c

# /path/to/apache/bin/apachectl stop

# /path/to/apache/bin/apachectl start

단 윈도우즈에서 운영중이라면 미러 사이트에서 미리 컴파일된 dll을 다운 받으면 된다.

설정에 사용되는 지시어

SecFilterEngine (On|Off) : mod_security에 의한 필터를 시작할지에 대한 지시어

SecFilterScanPOST (On|Off) : POST로 정송되는 데이터에 대한 필터의 사용여부

SecFilterDefaultAction (pass|deny|status|redirect|exec|log|nolog)

          이는 필터에 의해 검출이 된 경우 기본적으로 어떻게 처리할지에한 지시이다 .

                    pass : 검출되어도 필터링 하지 말고 통과

                    deny : 검출되면 요청 거부 (기본적으로 500에러 리턴)

                    status : 검출되거나 기타 이벤트발생시 반환할 에러

                    redirect : 검출되거나 기타 이벤트발생시 리다이렉트 시킴

                    exec : 검출되거나 기타 이벤트발생시 실행할 CGI

                    log : 로그를 남김

                    nolog : 로그를 남기지않음

SecFilterCheckURLEncoding (On|Off) 인코딩된 문자열을 기존의 문자열로 반환

SecFilterDebugLog logs <filename> : 해당 파일명으로 로그남김

SecFilterDebugLevel <log level (int)> :

                    0으로 설정하면 로그가 남지 않음

                    1. 치명적인 로그를 남기며

                    2~3 으로갈수록 자세한 정보를 남김

                    SecFilter KEYWORD : HTTP요청으로 들어오는 패킷에서 KEYWORD에 해당하는 요청이있으면 검출함

                    ex) SecFilter “../

SecFilterSelective [VARIABLES]  (REGULAR|string)

                    웹헤더의 특정 필드나 특정아파치 변수의 값을 검출할 수 있음

SecFilterSignatureAction : 다음 해당 지시어가나올때까지 하나의 체인을 구성

                    해당체인에 대한 필터 액선을 지정할수 있음

실제 사용되는 예제

아래에서 크로스 사이트스크립트를 차단하는 룰을 차단해보겟다

1. <script> 라는 태그를 무력화시킨다.

2. javacript, vbscript의 지시어를 무력화시킨다.

3. 쿠키정보를 열람하는 것을 무력화시킨다.

4. 해당 페이지를 다른 페이지로 리다이렉트 하는 것을 무력화시키다

5. 해당페이지에 스크립트로 다른 소스를 삽입하는 것을 무력화시킨다.

# 해당 지시어가 다시 나올때까지 체인을 구성하며 필터링시 XSS attack이라는

# 로그를 남기며 필터링시 차단을 하라.

SecFilterSignatureAction "log,deny,msg:'XSS attack'"

# 넘어오는 값중에 “<script“라는 문자를 필터링하라

SecFilterSelective ARGS "<script"

# 넘어오는 값중에 "javascript:"문자를 필터링하라

SecFilterSelective ARGS "javascript:"

# 넘어오는 값중에 "vbscript:"문자를 필터링하라

SecFilterSelective ARGS "vbscript:"

# 넘어오는 값중에 "document\.cookie"문자를 필터링하라

SecFilterSelective ARGS "document\.cookie"

# 넘어오는 값중에 "document\.location"문자를 필터링하라

SecFilterSelective ARGS "document\.location"

# 넘어오는 값중에 "document\.write" 문자를 필터링하라

SecFilterSelective ARGS "document\.write"

아래의 코드를 httpd.conf  적당한 곳에 복사해 넣으세요.






<IfModule mod_security.c>

    # Enable ModSecurity
    SecFilterEngine On

    # Reject requests with status 403
    SecFilterDefaultAction "deny,log,status:403"

    # Some sane defaults
    SecFilterScanPOST On
    SecFilterCheckURLEncoding On
    SecFilterCheckUnicodeEncoding Off

    # Accept almost all byte values
    SecFilterForceByteRange 1 255

    # Server masking is optional
    # SecServerSignature "Microsoft-IIS/5.0"

    # Designate a directory for temporary files
    # storage. It is a good idea to change the
    # value below to a private directory, just as
    # an additional measure against race conditions
    SecUploadDir /tmp
    SecUploadKeepFiles Off

    # Only record the interesting stuff
    SecAuditEngine RelevantOnly
    # Uncomment below to record responses with unusual statuses
    # SecAuditLogRelevantStatus ^5
    SecAuditLog logs/modsec_audit.log

    # You normally won't need debug logging
    SecFilterDebugLevel 0
    SecFilterDebugLog logs/modsec_debug.log

    # Only accept request encodings we know how to handle
    # we exclude GET requests from this because some (automated)
    # clients supply "text/html" as Content-Type
    SecFilterSelective REQUEST_METHOD "!^(GET|HEAD)$" chain
    SecFilterSelective HTTP_Content-Type "!(^application/x-www-form-urlencoded$|^multipart/form-data;)"

    # Do not accept GET or HEAD requests with bodies
    SecFilterSelective REQUEST_METHOD "^(GET|HEAD)$" chain
    SecFilterSelective HTTP_Content-Length "!^$"

    # Require Content-Length to be provided with
    # every POST request
    SecFilterSelective REQUEST_METHOD "^POST$" chain
    SecFilterSelective HTTP_Content-Length "^$"

    # Don't accept transfer encodings we know we don't handle
    SecFilterSelective HTTP_Transfer-Encoding "!^$"

</IfModule>

2011/03/05 16:14 2011/03/05 16:14