독까의 이야기

웹사이트 분석툴인 goaccess 를 IIS 에 적용하고자 한다. 

1. 패키지 다운로드

https://goaccess.io/download

Download 버튼을 누르면 goaccess-1.9.1.tar.gz 파일이 다운 됨. 

goaccess-1.9.1.tar.gz
0.71MB

우선 해당 파일을 C 드라이브에 압축 해제하고 다음 단계 진행. 

 

2. cygwin 설치 및 패키지 다운로드 

goaccess 가 리눅스 기반으로 생성되었기 때문에, 윈도우에서 동작 시키려면 추가 프로그램이 필요 함. 

cygwin 설치

https://www.cygwin.com/install.html

setup-x86_64.exe
1.34MB

설치 파일 실행하고 아래 단계를 진행 

Local Package Directory에는 다운로드한 패키지들을 저장 본인이 알아서 경로 지정.
http://cygwin.mirror.constant.com 선택

이 후, 패키지 검색 화면이 나오는데 기본으로 필요한 패키지와 goaccess 를 위한 패키지를 찾아서 설치.

- gcc

gcc-core 를 선택하고, "Skip" 을 두 번 클릭해서 버전이 나오게 해야 함. 

"다음" 은 누르면 안 됨. 

필요한 패키지 전체 활성화 완료 후에 "다음" 버튼을 눌러서 설치 진행해야 함. 

- gcc-g++

- make

- gdb

- wget

 

그리고 goaccess 개발사에서 안내한 패키지를 다운로드 한다. 위에 설치 된 것과 중복이 있으니 확인 필요.

https://goaccess.io/faq#installation

- libGeoIP

- libncurses / libncursesw10

- automake

- gettext

 

참고로, goaccess 설치 메뉴얼에는 없는 패키지가 있는데 "libmaxminddb" 이것도 설치해야 함. 

./configure 명령어 실행 할 때 해당 패키지 없어서 에러 남. 메뉴얼 없데이트가 안 된 듯. 

- libmaxminddb

 

이제 "다음" 버튼 눌러서 선택한 패키지 확인하고 설치 진행 하면 됨. 

 

# 패키지가 유닉스 계열 압축 파일이므로, 서버에 해당 압축을 해제 할 수 있는 프로그램이 설치되어 있어야 함. 

없으면 다운로드 완료했음에도 압축 해제 할 수 없다고 에러 엄청 나옴. 

 

3. goaccess 설치

바탕화면에 등록 된 아이콘 실행해서 cygwin CLI 실행 

아까 압축 풀어놨던 경로로 이동 : cd c:/goaccess-1.9.1

- 구성 : configure

명령어 실행 : 둘 중에 아무거나 해도 상관없는 것 같음. 

./configure --enable-utf8 --enable-geoip=legacy

or 

./configure --enable-utf8 --enable-geoip=mmdb

오류 없이 완료 되면 정상. 

 

- 빌드 : make

make

 

- 설치 : make install

make install

 

4. goaccess 실행

IIS 웹로그가 저장 된 경로를 지정하여 로그 파일 분석

goaccess -f "C:\inetpub\logs\LogFiles\W3SVC1\u_ex240405.log"

윈도우 IIS 의 로그 형식이니까 W3C 를 "스페이스바"로 선택하고, "엔터"

CLI 로 로그 확인시에는 이런 화면이 호출 됨. 

 

5. html 로 로그 파일을 변환하여 웹브라우저에서 실행하는 방법

IIS 웹로그를 파싱하여 html 로 변환하는 작업을 해야 함. 

goaccess 가 기본적으로는 apache time 및 date 형식을 따르기 때문에 아래와 같이 포맷을 수정해야 정상적으로 변환 됨. 

goaccess --no-global-config "C:\inetpub\logs\LogFiles\W3SVC1\u_ex240405.log" --log-format '%d %t %^ %m %U %^ %^ %^ %h %u %R %s %^ %^ %^ %L' --date-format=%Y-%m-%d --time-format=%T -o "D:\WWW_ROOT\test\log.html"

파싱 작업이 완료되서 웹브라우저에서 "사이트주소/log.html" 실행하면 이런 화면을 볼 수 있음. 

 

참고로, 당일 로그 외에도 기간 설정하여 로그 분석도 가능함. 

예를 들어, 2024.04.01 ~ 2024.04.05 에 생성 된 IIS 웹로그를 분석하고자 하면 

해당 로그들을 통합하여 변환하면 됨

powershell 스크립트로 저장 후 실행 : merge-logs.ps1

$firstFile = 240401
$lastFile = 240415
$targetFolder = "C:\inetpub\logs\LogFiles\W3SVC1"
$outputFile = "C:\inetpub\logs\LogFiles\W3SVC1\merged.log"

# 기존의 합친 파일이 있다면 삭제
If (Test-Path $outputFile) {
    Remove-Item $outputFile
}

# 지정된 범위 내의 파일 병합
$firstFile..$lastFile | ForEach-Object {
    $fileName = "u_ex" + $_ + ".log"
    $filePath = Join-Path -Path $targetFolder -ChildPath $fileName
    If (Test-Path $filePath) {
        Get-Content $filePath | Add-Content $outputFile
    }
}

 

아니면, 폴더를 새로 만들어서 필요한 로그들 복사 후 통째로 통합해도 됨. 이 경우 CMD 로 간단하게 작성 가능

copy C:\inetpub\logs\LogFiles\Merged\u_ex*.log merged.log

 

생성 된 통합 로그 파일을 html 로 변환해서 호출하면 더 많은 정보를 확인 할 수 있음. 

goaccess.exe --no-global-config "C:\inetpub\logs\LogFiles\W3SVC1\merged.log" --log-format '%d %t %^ %m %U %^ %^ %^ %h %u %R %s %^ %^ %^ %L' --date-format=%Y-%m-%d --time-format=%T -o "D:\WWW_ROOT\test\log.html"

 

통합 로그의 기간을 늘리면 이렇게도 됨. 

 

6. 테스트 종료

리눅스 계열과 다르게, 윈도우 기반으로 실행하려면 반드시 cygwin 을 통해야 함. 

CMD 를 통한 실행은 불가. 

일정 주기로 log.html 을 생성하려고 하면

powershell 을 이용해서 cygwin 실행하고 로그 변환하는 방식을 사용할 수 있을 것 같음. 

이전 글에서는 Custom 설정으로 진행했고, 이번에는 Default 로 진행하려고 한다. 

우선 Modsecurity 설치 후 프로그램 삭제해도, applicationHost.config 에는 Modesecurity 모듈이 남아 있다.

따라서, 해당 항목을 찾아서 직접 삭제해줘야 깨끗하게 테스트 가능하다.

            <add name="ModSecurity IIS" image="C:\Windows\System32\inetsrv\ModSecurityIIS.dll"/>

                <add name="ModSecurity IIS"/>

 

1. ModSecurity 설치

처음부터 끝까지 Default 로 진행하면 된다. 이전 글에서 체크 해제 했던 부분도 기본으로 한다.

 

2. IIS 설정 파일 확인 및 수정

C:\Windows\System32\inetsrv\config\applicationHost.config

설치형으로 진행하면, 모듈 및 전역 설정이 포함되어 있다. 

일부 값을 수정 및 전역 설정 삭제 한다. 

        <section name="ModSecurity" overrideModeDefault="Deny" allowDefinition="Everywhere" /></sectionGroup>

            <add name="ModSecurity IIS (32bits)" image="%SystemRoot%\SysWOW64\inetsrv\ModSecurityIIS.dll" preCondition="bitness32" />
            <add name="ModSecurity IIS (64bits)" image="%SystemRoot%\System32\inetsrv\ModSecurityIIS.dll" preCondition="bitness64" />

        <ModSecurity enabled="true" configFile="C:\Program Files\ModSecurity IIS\modsecurity_iis.conf" />


                <add name="ModSecurity IIS (32bits)" preCondition="bitness32" />
                <add name="ModSecurity IIS (64bits)" preCondition="bitness64" />


수정 : Deny 일 경우, 차단 정책이 적용되지 않음
<section name="modsecurity" overridemodedefault="Allow" allowdefinition="everywhere" >

삭제 : Web 전역 설정이 적용되어 정상 호출값도 차단 됨 
        <modsecurity enabled="true" configfile="c:\program files\modsecurity iis\modsecurity_iis.conf" >

 

3. modsecurity.conf 수정

C:\Program Files\ModSecurity IIS\modsecurity.conf

SecRuleEngine DetectionOnly 주석 처리 

SecRuleEngine On : 구문 추가

차단 테스트 항목 추가
SecRule ARGS:param "@contains test" "id:1,deny,status:401"
SecRule ARGS:param "@contains cmd" "id:2,deny,status:402"
SecRule ARGS:param "@contains gunnm" "id:3,deny,status:403"

 

4. 웹사이트 web.config 수정

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <ModSecurity enabled="true" configFile="C:\Program Files\ModSecurity IIS\modsecurity.conf" />
    </system.webServer>
</configuration>

 

5. 테스트

이전 글과 동일하게 입력 및 확인

개별 conf 파일 적용 확인

IIS 웹서버에 ModSecurity 모듈을 추가하려고 한다.

IIS 전용 웹방화벽 Webkinght 가 있지만, 추가 모듈 적용 여부도 테스트해야 한다. 

 

1. ModSecurity 설치 (ModSecurityIIS_2.9.1-64b.msi)

 

ModSecurityIIS_2.9.1-64b.msi
6.45MB

 

체크 박스 선택이 "Default" 인데, custom 설정 예정이라 체크 해제.

 

2. IIS 설정 파일 확인 및 수정

C:\Windows\System32\inetsrv\config\applicationHost.config

    <configSections>

        <section name="ModSecurity" overrideModeDefault="Deny" allowDefinition="Everywhere"/></sectionGroup>
    </configSections>


수정
        
<section name="modsecurity" overridemodedefault="Allow" allowdefinition="everywhere">

 

 

3. IIS 에 ModSecurity 모듈 추가하기

appcmd.exe install module /name:"ModSecurity IIS" /image:"C:\Windows\System32\inetsrv\ModSecurityIIS.dll"

 

 

4. IIS 설정 파일 재확인

C:\Windows\System32\inetsrv\config\applicationHost.config 추가 등록 내역 확인

        <globalModules>
            <add name="ModSecurity IIS" image="C:\Windows\System32\inetsrv\ModSecurityIIS.dll" />
        </globalModules>
....
        <modules>
                <add name="ModSecurity IIS" />
        </modules>

 

5. IIS 재시작

iisreset

 

6. modsecurity.conf 수정

C:\Program Files\ModSecurity IIS\modsecurity.conf

SecRuleEngine DetectionOnly 주석 처리 

SecRuleEngine On : 구문 추가

차단 테스트 항목 추가
SecRule ARGS:param "@contains test" "id:1,deny,status:401"
SecRule ARGS:param "@contains cmd" "id:2,deny,status:402"
SecRule ARGS:param "@contains gunnm" "id:3,deny,status:403"

 

7. 웹사이트 web.config 수정

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <ModSecurity enabled="true" configFile="C:\Program Files\ModSecurity IIS\modsecurity.conf" />
    </system.webServer>
</configuration>

 

8. 서버 로컬에서 테스트

차단룰 : SecRule ARGS:param "@contains test" "id:1,deny,status:401"
차단룰 : SecRule ARGS:param "@contains cmd" "id:2,deny,status:402"
차단룰 : SecRule ARGS:param "@contains gunnm" "id:3,deny,status:403"

 

모듈 적용이 잘 됨. 

 

이제는 테스트의 주 목적인 개별 ModSecurity.conf 를 적용하는 테스트를 한다.

일반적으로 웹서버에서 한 개의 사이트만 구동하는 것이 아니므로, 사이트별 정책 적용을 목적으로 한다.

 

9. C:\Program Files\ModSecurity IIS\TESTmodsecurity.conf 생성 및 수정

기존 modsecurity.conf 파일 복사 후 수정

SecRule ARGS:param "@contains Mod" "id:1,deny,status:404"
SecRule ARGS:param "@contains Sec" "id:2,deny,status:405"
SecRule ARGS:param "@contains Test" "id:3,deny,status:406"

 

10. C:\Program Files\ModSecurity IIS\modsecurity_iis.conf 수정

Include modsecurity.conf
Include modsecurity_crs_10_setup.conf
Include owasp_crs\activated_rules\*.conf
Include owasp_crs\base_rules\*.conf

추가
Include TESTmodsecurity.conf

 

11. web.config 수정

 

        <ModSecurity enabled="true" configFile="C:\Program Files\ModSecurity IIS\modsecurity.conf" />        

수정
        <ModSecurity enabled="true" configFile="C:\Program Files\ModSecurity IIS\TESTmodsecurity.conf" />        

 

12. 서버 로컬에서 테스트

 

로깅 설정이나 추가 차단 정책 설정은 차후에 테스트 한다.