logstash 로그 중

 

Received an event that has a different character encoding than you configured.

expected_charset => "UTF-8"

 

로그가 있다면

 

input 설정 내용에 아래의 charset을 추가하여 설정해주세요.

 

codec => plain { charset => "CP949" }

 

 

'기술 노트 > logstash' 카테고리의 다른 글

logstash.conf_230405  (0) 2023.04.05
logstash.conf_230306  (0) 2023.03.06
logstash.conf_23.02.20  (0) 2023.02.20
logstash 파일 파싱하기  (0) 2023.02.20
logstash 설정  (0) 2023.02.20

OS가 centos에서 ubuntu로 변경되어 ubuntu에서도 logstash 설정하는 내용을 남깁니다.

 

설명을 위해서 fortigate log를 파싱하기 위한 logstash 설정 입니다.

내용 확인 후 활용하시면 좋을 것 같아요.

 

logstash 설정 파일 생성하기

logmgmt@syslogserver:~$ sudo cat /etc/logstash/conf.d/logstash.conf
input {
        file {
                path => "/var/log/rsyslog/192.168.000.000/*.log"
                start_position => "beginning"
                tags => ["fortigate"]
        }
}

filter {
        if "fortigate" in [tags] {
                grok {
                        patterns_dir => ["/etc/logstash/pattern.d"]
                                match => { "message" => [ "%{FORTILOG} %{GREEDYDATA:sub_message}" ] }
                }
                kv {
                        source => "sub_message"
                        value_split => "="
                }
                mutate { remove_field => [ "message" ] }
                mutate { remove_field => [ "sub_message" ] }
                if "wan" in [srcintfrole] {
                        geoip {
                                source => "srcip"
                                target => "geoip_src"
                        }
                }
                if [sentbyte] != "" and [rcvdbyte] != "" {
                        bytes {
                                source => "rcvdbyte"
                                target => "receivedbyte"
                        }
                        bytes {
                                source => "sentbyte"
                                target => "sentedbyte"
                        }
                }
                mutate {
                        convert => {
                                "rcvdpkt" => "integer"
                                "sentpkt" => "integer"
                                "proto" => "integer"
                                "srcserver" => "integer"
                                "sessionid" => "integer"
                                "duration" => "integer"
                                "policyid" => "integer"
                                "HOUR" => "integer"
                                "MINUTE" => "integer"
                                "SECOND" => "integer"
                        }
                }
        }
}

output {
        if "fortigate" in [tags] {
                if "traffic" in [type] {
                        elasticsearch {
                                hosts => "http://192.168.000.000:9200"
                                index => "logstash-fortigate-traffic-index-%{+YYYY.MM.dd}"
                        }
                }
                else if "event" in [type] {
                        elasticsearch {
                                hosts => "http://192.168.000.000:9200"
                                index => "logstash-fortigate-event-index-%{+YYYY.MM.dd}"
                        }
                }
                else if "utm" in [type] {
                        elasticsearch {
                                hosts => "http://192.168.000.000:9200"
                                index => "logstash-fortigate-utm-index-%{+YYYY.MM.dd}"
                        }
                }
        }
}
logmgmt@syslogserver:~$

 

 

fortigate 패턴 파일 생성하기

logmgmt@syslogserver:~$ sudo cat /etc/logstash/pattern.d/fortigate.pattern
####################################
###Fortinet Syslog Pattern Types:###
####################################

FORTILOG (?<timestamp>^\w+\s+\d+\s+\d+\:\d+\:\d+)\s(?<log_send_dev>(?:%{IP}|\_gateway))
logmgmt@syslogserver:~$

 

logstash-filter-bytes 필터 설치하기

logmgmt@syslogserver:~$ sudo /usr/share/logstash/bin/logstash-plugin install logstash-filter-bytes
Using bundled JDK: /usr/share/logstash/jdk
Validating logstash-filter-bytes
Resolving mixin dependencies
Installing logstash-filter-bytes
Installation successful
logmgmt@syslogserver:~$

'기술 노트 > ubuntu' 카테고리의 다른 글

Prometheus 설치  (1) 2023.12.21
kibana 설정하기  (1) 2023.12.05
elasticsearch 설정하기  (0) 2023.12.04
rsyslog 설정하기  (1) 2023.12.04
우분투에서 IP 주소 확인하기  (1) 2023.12.04

fortigate log 파싱 부분이 추가되었습니다.

fortigate 로그 파싱할때에는 logstash-filter-bytes를 사용하였습니다.

이유는 logstash에서 로그를 파싱하면 모든 자료는 기본적으로 type이 text입니다.

마찬가지로 숫자도 text로 인식합니다.

그래서 데이터 전송량도 type에 맞게 bytes라는 필터를 추가로 설치하였습니다.

logstash-filter-bytes의 설치는 아래의 작성 글을 참고하시기 바랍니다.

https://dirt-spoon.tistory.com/80

 

input {
        file {
                path => "/var/log/rsyslog/192.168.10.2/*.log"
                start_position => "beginning"
                tags => ["ap1"]
        }
        file {
                path => "/var/log/rsyslog/192.168.10.3/*.log"
                start_position => "beginning"
                tags => ["ap2"]
        }
        file {
                path => "/var/log/rsyslog/192.168.10.4/*.log"
                start_position => "beginning"
                tags => ["ap3"]
        }
        file {
                path => "/var/log/rsyslog/192.168.10.5/*.log"
                start_position => "beginning"
                tags => ["ap4"]
        }
        file {
                path => "/var/log/rsyslog/192.168.0.14/*.log"
                start_position => "beginning"
                tags => ["fortigate"]
        }
}

filter {
        if "ap1" in [tags] or "ap2" in [tags] or "ap3" in [tags] or "ap4" in [tags]  {
                grok {
                        patterns_dir => ["/etc/logstash/pattern.d"]
                        match => {
                                "message" => [
                                        "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{IPORHOST:process}\[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}",
                                        "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} \[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}",
                                        "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{WORD:process}\: User\: %{GREEDYDATA:user} last logged %{GREEDYDATA:access_result} in %{GREEDYDATA:access_day}\#%{NUMBER:deauthentication_reason_code}, to %{IP:destination_ip}, from %{IP:source_ip} using %{WORD:access_method}",
                                        "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{WORD:process}\: User\: %{GREEDYDATA:reason} in %{GREEDYDATA:access_day}\#%{NUMBER:deauthentication_reason_code}, to %{IP:destination_ip}, from %{IP:source_ip} using %{WORD:access_method}"
                                ]
                        }
                }
                mutate { remove_field => [ "message" ] }
        }
        else if "fortigate" in [tags] {
                grok {
                        patterns_dir => ["/etc/logstash/pattern.d"]
                        match => { "message" => [ "%{FORTILOG} %{GREEDYDATA:sub_message}" ] }
                }
                kv {
                        source => "sub_message"
                        value_split => "="
                }
                mutate { remove_field => [ "message" ] }
                mutate { remove_field => [ "sub_message" ] }
                if "wan" in [srcintfrole] {
                        geoip {
                                source => "srcip"
                                target => "geoip_src"
                        }
                }
                if [sentbyte] != "" and [rcvdbyte] != "" {
                        bytes {
                                source => "rcvdbyte"
                                target => "receivedbyte"
                        }
                        bytes {
                                source => "sentbyte"
                                target => "sentedbyte"
                        }
                }
                mutate {
                        convert => {
                                "rcvdpkt" => "integer"
                                "sentpkt" => "integer"
                                "proto" => "integer"
                                "srcserver" => "integer"
                                "sessionid" => "integer"
                                "duration" => "integer"
                                "policyid" => "integer"
                                "HOUR" => "integer"
                                "MINUTE" => "integer"
                                "SECOND" => "integer"
                        }
                }
        }
}

output {
        if "ap1" in [tags] {
                elasticsearch {
                        hosts => "http://192.168.0.17:9200"
                        index => "logstash-ap1-index-%{+YYYY.MM.dd}"
                }
        }
        else if "ap2" in [tags] {
                elasticsearch {
                        hosts => "http://192.168.0.17:9200"
                        index => "logstash-ap2-index-%{+YYYY.MM.dd}"
                }
        }
        else if "ap3" in [tags] {
                elasticsearch {
                        hosts => "http://192.168.0.17:9200"
                        index => "logstash-ap3-index-%{+YYYY.MM.dd}"
                }
        }
        else if "ap4" in [tags] {
                elasticsearch {
                        hosts => "http://192.168.0.17:9200"
                        index => "logstash-ap4-index-%{+YYYY.MM.dd}"
                }
        }
        else if "fortigate" in [tags] {
                if "traffic" in [LOG_TYPE] {
                        elasticsearch {
                                hosts => "http://192.168.0.17:9200"
                                index => "logstash-fortigate-traffic-index-%{+YYYY.MM.dd}"
                        }
                }
                else if "event" in [LOG_TYPE] {
                        elasticsearch {
                                hosts => "http://192.168.0.17:9200"
                                index => "logstash-fortigate-event-index-%{+YYYY.MM.dd}"
                        }
                }
                else if "utm" in [LOG_TYPE] {
                        elasticsearch {
                                hosts => "http://192.168.0.17:9200"
                                index => "logstash-fortigate-utm-index-%{+YYYY.MM.dd}"
                        }
                }
        }
}

'기술 노트 > logstash' 카테고리의 다른 글

font 깨질 때  (0) 2025.03.24
logstash.conf_230306  (0) 2023.03.06
logstash.conf_23.02.20  (0) 2023.02.20
logstash 파일 파싱하기  (0) 2023.02.20
logstash 설정  (0) 2023.02.20

유입되는 ap 로그 중 grok 패턴을 기존보다 상세히 하여 ap에 로그인한 것과 로그인 실패한 것을 구분

elastalert rule example 참조

- https://dirt-spoon.tistory.com/24

 

elastalert rules example 1

아래 룰은 AP의 콘솔 접속 시 발송하는 메시지 룰입니다. # elasticsearch host es_host: 192.168.0.00 # elasticsearch port es_port: 9200 # 로그는 탐지하는 타입 type: any # 얼마나 자주 탐지 run_every: seconds: 10 # 메시지

dirt-spoon.tistory.com

- https://dirt-spoon.tistory.com/25

 

elastalert rule example 2

아래 룰은 AP의 콘솔 접속 시 발송하는 메시지 룰입니다. es_host: 192.168.0.17 es_port: 9200 type: any run_every: seconds: 10 buffer_time: minutes: 1 index: "logstash-ap*" filter: - query_string: query: reason:"logon failed for invalid us

dirt-spoon.tistory.com

 

 

grok의 pattern_dir에 있는 pattern은 제가 작성했던 pattern을 그대로 사용하시면 됩니다.

 

 

grok 패턴 설명 1

원문 Mar 11 01:48:26 PnP [2357]: pnp_platform.discovery.lease_parser INFO Retrieving lease option strings for vendor-specific-information ipv4
쿼리 %{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{IPORHOST:process}\[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}

 

grok 패턴 설명 2

원문 Mar 10 17:53:10 192.168.10.2 hostapd[2358]: trying to update accounting statistics, station 5e:70:cb:61:b7:a5 not found
쿼리 %{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} \[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}

 

grok 패턴 설명 3

원문 Mar  6 16:15:20 192.168.10.2 syslog: User: admin last logged successfully in 2023-Mar-6#012, to 192.168.10.2, from 192.168.0.54 using HTTPS
쿼리 %{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{WORD:process}\: User\: %{GREEDYDATA:user} last logged %{GREEDYDATA:access_result} in %{GREEDYDATA:access_day}\#%{NUMBER:deauthentication_reason_code}, to %{IP:destination_ip}, from %{IP:source_ip} using %{WORD:access_method}

 

grok 패턴 설명 4

원문 Mar  6 16:14:35 192.168.10.2 syslog: User: logon failed for invalid username in 2023-Mar-6#012, to 192.168.10.2, from 192.168.0.54 using HTTPS
쿼리 %{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{WORD:process}\: User\: %{GREEDYDATA:reason} in %{GREEDYDATA:access_day}\#%{NUMBER:deauthentication_reason_code}, to %{IP:destination_ip}, from %{IP:source_ip} using %{WORD:access_method}

 

input {
        file {
                path => "/var/log/rsyslog/192.168.10.2/*.log"
                start_position => "beginning"
                tags => ["ap1"]
        }
        file {
                path => "/var/log/rsyslog/192.168.10.3/*.log"
                start_position => "beginning"
                tags => ["ap2"]
        }
        file {
                path => "/var/log/rsyslog/192.168.10.4/*.log"
                start_position => "beginning"
                tags => ["ap3"]
        }
        file {
                path => "/var/log/rsyslog/192.168.10.5/*.log"
                start_position => "beginning"
                tags => ["ap4"]
        }
}

filter {
        if "ap1" in [tags] or "ap2" in [tags] or "ap3" in [tags] or "ap4" in [tags]  {
                grok {
                        patterns_dir => ["/etc/logstash/pattern.d"]
                                match => {
                                        "message" => [
                                                "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{IPORHOST:process}\[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}",
                                                "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} \[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}",
                                                "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{WORD:process}\: User\: %{GREEDYDATA:user} last logged %{GREEDYDATA:access_result} in %{GREEDYDATA:access_day}\#%{NUMBER:deauthentication_reason_code}, to %{IP:destination_ip}, from %{IP:source_ip} using %{WORD:access_method}",
                                                "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip_or_host} %{WORD:process}\: User\: %{GREEDYDATA:reason} in %{GREEDYDATA:access_day}\#%{NUMBER:deauthentication_reason_code}, to %{IP:destination_ip}, from %{IP:source_ip} using %{WORD:access_method}"
                                        ]
                                }
                }
                mutate { remove_field => [ "message" ] }
        }
}

output {
        if "ap1" in [tags] {
                elasticsearch {
                        hosts => ["http://192.168.0.17:9200"]
                        index => "logstash-ap1-index-%{+YYYY.MM.dd}"
                }
        }
        else if "ap2" in [tags] {
                elasticsearch {
                        hosts => ["http://192.168.0.17:9200"]
                        index => "logstash-ap2-index-%{+YYYY.MM.dd}"
                }
        }
        else if "ap3" in [tags] {
                elasticsearch {
                        hosts => ["http://192.168.0.17:9200"]
                        index => "logstash-ap3-index-%{+YYYY.MM.dd}"
                }
        }
        else if "ap4" in [tags] {
                elasticsearch {
                        hosts => ["http://192.168.0.17:9200"]
                        index => "logstash-ap4-index-%{+YYYY.MM.dd}"
                }
        }
}

 

'기술 노트 > logstash' 카테고리의 다른 글

font 깨질 때  (0) 2025.03.24
logstash.conf_230405  (0) 2023.04.05
logstash.conf_23.02.20  (0) 2023.02.20
logstash 파일 파싱하기  (0) 2023.02.20
logstash 설정  (0) 2023.02.20

저는 syslog 서버를 기준으로 구축을 한다고 밝혔습니다.

그래서 ap에서 받은 syslog를 기준으로 설명을 진행하겠습니다.

 

rsyslog의 설정을 제가 한 대로 따라하셨다면, 해당 폴더에 syslog 를 수신한 폴더와 내부에 파일이 보일 겁니다.

[root@tmplogsvr rsyslog]# pwd

/var/log/rsyslog

[root@tmplogsvr rsyslog]# ls -al

합계 4

drwx------.  7 root root  106  2월 15 14:45 .

drwxr-xr-x. 17 root root 4096  2월 19 00:00 ..

drwx------.  2 root root  138  2월 20 00:12 192.168.0.54

drwx------.  2 root root  138  2월 20 00:00 192.168.10.2

drwx------.  2 root root  138  2월 20 08:23 192.168.10.3

drwx------.  2 root root  138  2월 20 08:31 192.168.10.4

drwx------.  2 root root  138  2월 20 01:39 192.168.10.5

[root@tmplogsvr rsyslog]#

해당 폴더에 들어가면 수신날자.log 형태의 파일을 확인할 수 있습니다.

[root@tmplogsvr 192.168.10.2]# ls -al

합계 3072

drwx------. 2 root root     138  2월 20 00:00 .

drwx------. 7 root root     106  2월 15 14:45 ..

-rw-------. 1 root root  703219  2월 15 23:57 2023-02-15.log

-rw-------. 1 root root 1129390  2월 16 18:29 2023-02-16.log

-rw-------. 1 root root  176330  2월 17 21:12 2023-02-17.log

-rw-------. 1 root root  504511  2월 18 23:59 2023-02-18.log

-rw-------. 1 root root  542715  2월 19 22:26 2023-02-19.log

-rw-------. 1 root root   71405  2월 20 11:17 2023-02-20.log

[root@tmplogsvr 192.168.10.2]#

 

kibana에서 보고 싶은 파일의 위치를 확인 하신 후 logstash의 설정을 합니다.

우선 먼저 어떻게 파일이 수신되는지 확인을 합니다.

 

저는 logstash의 로그 파싱 설정을 /etc/logstash/conf.d 폴더에 logstash.conf 로 작성하였습니다.

[root@tmplogsvr 192.168.10.2]# cat /etc/logstash/conf.d/logstash.conf
input { # logstash에서 파일을 받아들이겠다는 선언
        file { # 어떤 형태의 입력 type을 받겠다는 선언
                path => "/var/log/rsyslog/192.168.10.2/*.log" # 받아 들일 로그의 위치 선언
                start_position => "beginning" # 파일을 읽는 방식으로 차후 자세한 설명
                tags => ["ap1"] # 해당 경로의 파일인 경우 tag에 "ap1"을 표시
        }
}

output { #logstash에서 파일을 내보내겠다는 선언
        if "ap1" in [tags] { # 로그 중 tags에 "ap1"이 표시되어 있다면
                elasticsearch { # 로그를 elasticsearch에 보내겠다는 선언
                        hosts => ["http://192.168.0.17:9200"] # elasticsearch url 정보
                        index => "logstash-ap1-index-%{+YYYY.MM.dd}" # elasticsearch의 index 입력 방식
                }
        }
}

위의 설정대로 하신 후 logstash를 재시작 합니다.

 

이제 kibana에 접속을 합니다.

 

정상적으로 출력이 됐다면, logstash.conf 파일을 아래와 같이 수정

input {
  file {
    path => "/var/log/rsyslog/192.168.10.2/*.log"
    start_position => "beginning"
    tags => ["ap1"]
  }
}

filter {
  if "ap1" in [tags] { # tags에 "ap1"이 있다면
    grok { # grok 적용해라
      patterns_dir => ["/etc/logstash/patterns.d"] # grok 적용 전 패턴 등록
      match => { "message" => [ "%{SYSLOGTIMESTAMP:access_time} %{IPORHOST:ip} %{IPORHOST:process}\[%{BASE10NUM:process_id}\]\: %{GREEDYDATA:sub_message}" ] }
    }
    mutate { remove_field => [ "message" ] } # message 필드 삭제
  }
}

output {
        if "ap1" in [tags] {
                elasticsearch {
                        hosts => ["http://192.168.0.17:9200"]
                        index => "logstash-ap1-index-%{+YYYY.MM.dd}"
                }
        }
}

수정 후 logstash 재시작 수행

이 후 kibana에서 로그 확인 시 아래와 같이 변경된 것을 볼 수 있습니다.

 

'기술 노트 > logstash' 카테고리의 다른 글

logstash.conf_230306  (0) 2023.03.06
logstash.conf_23.02.20  (0) 2023.02.20
logstash 설정  (0) 2023.02.20
grok pattern  (0) 2023.02.17
logstash 설치  (0) 2023.02.16

설치할 logstash 방식 선택

해당 매뉴얼은 centos에서 설치하여 yum을 선택하였습니다.

https://www.elastic.co/kr/downloads/logstash

 

yum을 이용한 repository 추가

[root@localhost ~]# echo '[logstash-8.x]

name=Elastic repository for 8.x packages

baseurl=https://artifacts.elastic.co/packages/8.x/yum

gpgcheck=1

gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch

enabled=1

autorefresh=1

type=rpm-md' > /etc/yum.repos.d/logstash.repo

[root@localhost ~]#

 

설치 시 elasticsearch의 버전과 동일한 버전을 설치하세요.

 

yum을 이용한 logstash 설치

[root@tmplogsvr ~]# yum install logstash

마지막 메타자료 만료확인 0:06:31 이전인: 2023년 03월 16일 (목) 오전 09시 32분 44초.

종속성이 해결되었습니다.

=============================================================================================================

 꾸러미                  구조                  버전                        레포지터리                   크기

=============================================================================================================

설치 중:

 logstash                x86_64                1:8.6.2-1                   logstash-8.x                311 M

 

연결 요약

=============================================================================================================

설치  1 꾸러미

 

총계 내려받기 크기: 311 M

설치된 크기 : 551 M

진행 할 까요? [y/N]: y

꾸러미 내려받기 중:

logstash-8.6.2-x86_64.rpm                                                     15 MB/s | 311 MB     00:21

-------------------------------------------------------------------------------------------------------------

합계                                                                          15 MB/s | 311 MB     00:21

연결 확인 실행 중

연결 확인에 성공했습니다.

연결 시험 실행 중

연결 시험에 성공했습니다.

연결 실행 중

  준비 중           :                                                                                    1/1

  스크립트릿 실행 중: logstash-1:8.6.2-1.x86_64                                                          1/1

  설치 중           : logstash-1:8.6.2-1.x86_64                                                          1/1

  스크립트릿 실행 중: logstash-1:8.6.2-1.x86_64                                                          1/1

  확인 중           : logstash-1:8.6.2-1.x86_64                                                          1/1

 

설치되었습니다:

  logstash-1:8.6.2-1.x86_64

 

완료되었습니다!

[root@tmplogsvr ~]#

 

logstash 시스템 등록

[root@localhost ~]# systemctl daemon-reload

[root@localhost ~]# systemctl enable logstash.service

Created symlink /etc/systemd/system/multi-user.target.wants/logstash.service → /usr/lib/systemd/system/logstash.service.

 

logstash 시스템 시작 / 중지

/bin/systemctl start logstash.service

/bin/systemctl stop logstash.service

'기술 노트 > logstash' 카테고리의 다른 글

logstash.conf_230306  (0) 2023.03.06
logstash.conf_23.02.20  (0) 2023.02.20
logstash 파일 파싱하기  (0) 2023.02.20
logstash 설정  (0) 2023.02.20
grok pattern  (0) 2023.02.17

+ Recent posts