顯示具有 資訊安全 標籤的文章。 顯示所有文章
顯示具有 資訊安全 標籤的文章。 顯示所有文章

2018年6月11日 星期一

NXLog 如何調整Log message內容再送出給Log Server

利用 NXLog 將 Windows DNS Log 送至 OSSIM,卻發現 OSSIM 一直無法將 raw log 進行 parsing,經檢查 OSSIM plugins 設定檔 windns-nxlog.cfg 的 regex 後發現,應該是原本 log 中的中文 (上午|下午) 送到 OSSIM 後無法被辨識。

在嘗試調整 regular expression 無果後,決定從 NXLog 著手,在 NXLog 送出前,先將 (上午|下午) 調整成 (AM|PM),再將 log 送出。

然後,又發現在 SIEM 查詢時,下午的 windns log 都沒有出現,檢查發現在 OSSIM 的 plugin - windns-nxlog.cfg 中,其 date 欄位有先經過 normalize_date() function 處理,而 2018/6/8 06:17:49 PM 這個格式不符其支援的格式,故須修改成 6/8/2018 06:17:49 PM 這種格式。

以下是中文版與英文版所產出之 DNS debugging log。
----------------------------------------------------------------------------------------------------------------------
Windows DNS log from Windows 2008 R2 CHT:

2018/4/16 上午 06:17:49 0B38 PACKET  0000000002134EF0 UDP Rcv 192.168.3.252   3780   Q [0001   D   NOERROR] A      (6)amazon(3)com(0)

2018/4/16 上午 06:17:49 0B38 PACKET  0000000002EA9D30 UDP Snd 168.95.1.1      0fc0   Q [0001   D   NOERROR] A      (6)amazon(3)com(0)

2018/4/16 上午 06:17:49 0B38 PACKET  000000000212DAB0 UDP Rcv 168.95.1.1      0fc0 R Q [8081   DR  NOERROR] A      (6)amazon(3)com(0)

2018/4/16 上午 06:17:49 0B38 PACKET  0000000002134EF0 UDP Snd 192.168.3.252   3780 R Q [8081   DR  NOERROR] A      (6)amazon(3)com(0)
-----------------------------------------------------------------------------------------------------------------------

Windows DNS log from Windows 2008 R2 Eng:

2/19/2015 10:03:57 PM 2AE8 PACKET  00000005CF374F80 UDP Rcv 192.168.0.42    fdd7   Q [0001   D   NOERROR] A      (9)imap-mail(7)outlook(3)com(0)

2/19/2015 10:03:57 PM 2AE8 PACKET  00000005CB426930 UDP Snd 10.255.176.137  0c4c   Q [0001   D   NOERROR] A      (9)imap-mail(7)outlook(3)com(0)

2/19/2015 10:03:57 PM 2AE8 PACKET  00000005D03B4CE0 UDP Rcv 10.255.176.137  0c4c R Q [8081   DR  NOERROR] A      (9)imap-mail(7)outlook(3)com(0)

2/19/2015 10:03:57 PM 2AE8 PACKET  00000005D03B4CE0 UDP Snd 192.168.1.42    fdd7 R Q [8081   DR  NOERROR] A      (9)imap-mail(7)outlook(3)com(0)
-----------------------------------------------------------------------------------------------------------------------


調整 nxlog.conf 設定如下:
======================================================================
< extension json >
    Module      xm_json
< /extension >

## Custom CSV format for the windns-nxlog AlienVault plugin.
< extension transform_alienvault_csv_dns >
    Module          xm_csv
    Fields     $Hostname, $SourceName, $Message
    FieldTypes      string, string, string
    Delimiter       ,
< /extension >

< input dns_logs >
    Module    im_file
    File      "C:\\temp\\DNSLog\\debug.log"
    SavePos  TRUE
    InputType LineBased

    #根據 OSSIM 的 Date Format supported by normalize_date() function,調整 Date Format
    #Convert the Chinese characters into English characters (上午 to AM、下午 to PM)
    #Change the datetime format from "yyyy/mm/dd (上午|下午) hh:mm:ss" to "mm/dd/yyyy hh:mm:ss (AM|PM)"

    Exec if $raw_event =~ /^(\d+)\/(\d+\/\d+)\s(上午\s)(\d+\:\d+\:\d+\s)(.+)/ $raw_event = $2 + '/' + $1 + ' ' + $4 + 'AM ' + $5;
    Exec if $raw_event =~ /^(\d+)\/(\d+\/\d+)\s(下午\s)(\d+\:\d+\:\d+\s)(.+)/ $raw_event = $2 + '/' + $1 + ' ' + $4 + 'PM ' + $5;

    Exec if $raw_event !~ /^\d/ drop();\
    else\
    {\
         $Message = $raw_event;\
         $SourceName = "DNS";\
         $raw_event = to_json();\
    }
< /input >

< output out_alienvault_dns_nxlog >
    Module          om_udp
    Host            192.168.1.2
    Port            514
   
    Exec            if not defined $Message { drop(); }

## Replace newlines, tabs and carriage returns with blanks:
    Exec            $Message = replace($Message, "\t", " "); $Message = replace($Message, "\n", " "); $Message = replace($Message, "\r", " ");
    
## Ensure that commonly undefined values are set:
    Exec            if not defined $AccountName { $AccountName = "-"; }
    Exec            if not defined $AccountType { $AccountType = "-"; }
    Exec            if not defined $Domain { $Domain = "-"; }

## Ensure we send in the proper format:
    Exec     $Hostname = hostname_fqdn();
    Exec            transform_alienvault_csv_dns->to_csv(); $raw_event = $Hostname + ' DNS-NXLOG: ' + $raw_event;
< /output >

## Route for dns nxlog logs:
< route route_dns_nxlog >
    Path        DNS_Logs => out_alienvault_dns_nxlog
< /route >

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


OSSIM中查看rawlog,原本輸出為

Jun  8 00:07:48 DNS.test.corp DNS-NXLOG: "DNS.test.corp","DNS","2018/6/8 ?W?? 12:09:02 0B38 PACKET  00000000029DDCE0 UDP Rcv 192.168.3.31    7f19   Q [0001   D   NOERROR] A      (9)resolver1(5)ofind(6)ctmail(3)com(0)"
Jun  8 00:07:48 DNS.test.corp DNS-NXLOG: "DNS.test.corp","DNS","2018/6/8 ?W?? 12:09:02 0B38 PACKET  00000000029DDCE0 UDP Snd 192.168.3.31    7f19 R Q [8081   DR  NOERROR] A      (9)resolver1(5)ofind(6)ctmail(3)com(0)"


進行 Pre-parsing 後變為

Jun  8 15:43:09 DNS.test.corp DNS-NXLOG: "DNS.test.corp","DNS","6/8/2018 03:42:58 PM 0B34 PACKET  00000000020B9D10 UDP Rcv 192.168.3.31    afdd   Q [0001   D   NOERROR] A      (13)gmail-smtp-in(1)l(6)google(3)com(0)"
Jun  8 15:43:09 DNS.test.corp DNS-NXLOG: "DNS.test.corp","DNS","6/8/2018 03:42:58 PM 0B34 PACKET  00000000020B9D10 UDP Snd 192.168.3.31    afdd R Q [8081   DR  NOERROR] A      (13)gmail-smtp-in(1)l(6)google(3)com(0)"




再回到 OSSIM web介面,ANALYSIS \ SECURITY EVENTS (SIEM) 下,DATA SOURCE 下拉選項出現 windns,表示 DNS Log 已成功寫入資料庫了!!


備註:

OSSIM Date Format supported  by normalize_date() function:
https://www.alienvault.com/documentation/usm-appliance/plugin-management/supported-formats-normalize_date.htm


技術參考來源: http://nxlog-ce.sourceforge.net/nxlog-docs/en/nxlog-reference-manual.html

Rewriting and modifying messages
There are many ways to modify log messages. A simple method which does not always work is to modify the $raw_event field (in case of syslog) without parsing the message. This can be done with regular expressions using capturing, for example:

if $raw_event =~ /^(aaaa)(replaceME)(.+)/ $raw_event = $1 + 'replaceMENT' + $3;

The more complex method is to parse the message into fields, modify some fields and finally reconstruct the message from the fields. The conditional rewrite of the syslog facility example shows such a syslog message modification method.

2018年3月8日 星期四

Graylog Server 安裝及設定


Graylog Server 安裝及設定
日期: 2018/3/7

建立Graylog Server集中管理網路裝置Log,並建立相關監控機制,以增進公司資訊安全。

一.  CentOS 7.3 安裝 (VM on vSphere 6.0)
1.      Taking a minimal server setup
因自動配置partition會將空間分配給 /home/ 50G,造成Elasticsearch Indice空間不夠使用,故建議自行配置硬碟空間。


2.      系統更新
# yum update -y
3.      安裝 VMware Tools (安裝開源 open-vm-tools 即可)
# yum install open-vm-tools -y
4.      安裝pwgenperl-Digest-SHA輔助工具,設定Graylog時用來生成密碼
# yum install epel-release -y
# yum install pwgen perl-Digest-SHA -y



二.  安裝Java
Elasticsearch執行於JVM上,因此需安裝Java 7 update 55或以上版本,並設定有JAVA_HOME環境變數。
l  早期的Java 7版本存在造成資料毀損與遺失的bugElasticsearch在啟動時會檢查過期的Java版本並無法成功啟動。
l  建議使用Java 1.8.0_131以上,因最近Java的重要update會提升JVM的記憶體使用效能。
(Elasticsearch
官方建議不要使用JDK9請使用JDK8)
l  使用JVM Server mode (x64版本為server mode),將更加有效的利用記憶體

安裝指令如下--
# yum install java-1.8.0-openjdk-headless.x86_64




三.  安裝MongoDB
1.     建立MongoDB
# touch /etc/yum.repos.d/mongodb-org-3.6.repo
加入以下內容
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

2.     安裝MongoDB
# yum install -y mongodb-org

3.     啟動MongoDB
# systemctl start mongod

4.     設定開機啟動MongoDB
# systemctl enable mongod



四.  安裝Elasticsearch
Graylog 2.4不支援Elasticsearch 6.x,故安裝5.x版。

1.     安裝Elasticsearch GPG密鑰
# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

2.     建立Elasticsearch
# touch /etc/yum.repos.d/elasticsearch.repo
加入以下內容
[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

3.     安裝Elasticsearch
# yum install -y elasticsearch

4.     修改elasticsearch設定檔
# vi /etc/elasticsearch/elasticsearch.yml
### 設定cluster.namegarylog ###
cluster.name: graylog

5.     啟動elasticsearch
# systemctl start elasticsearch

6.     設定開機啟動elasticsearch
# systemctl enable elasticsearch

7.     檢查Elasticsearch健康狀況
# curl –XGET localhost:9200/_cluster/health?pretty=true
若一切正常,應該會得到以下訊息內容--
{
  "cluster_name" : "graylog",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}



五.  安裝Graylog Server
1.     下載Graylog RPM軟件包
# rpm -Uvh https://packages.graylog2.org/repo/packages/graylog-2.4-repository_latest.rpm

2.     安裝graylog-server
# yum install -y graylog-server

3.     設置Graylog管理員的密鑰
利用pwgen輔助工具產生密碼for server.conf中的password_secret
# SECRET=$(pwgen -s 96 1)
# sudo -E sed -i -e 's/password_secret =.*/password_secret = '$SECRET'/' /etc/graylog/server/server.conf

查看 /etc/graylog/server/server.conf 內容,password_secret參數如下



4.     設定Graylog管理員admin的密碼
由於密碼使用SHA雜湊演算法,我們需要把明文密碼轉換為hash值,然後賦值給root_password_sha2參數。

例如,以下列命令產生密碼為test1234hash值。
# echo -n test1234 | sha256sum | awk '{print $1}'


將以上命令產出之Hash值,填入到server.confroot_password_sha2參數.
# vi /etc/graylog/server/server.conf



5.     設定Graylog Web接口和Server的溝通方式
預設Graylog只聽localhost,故若需以Web介面進行管理,須設定以下參數---
# vi /etc/graylog/server/server.conf
rest_listen_uri = http://192.168.x.x:9000/api/  (192.168.x.xGraylog Server IP)
web_listen_uri = http://192.168.x.x:9000/
elasticsearch_cluster_name = graylog  (需與elasticsearch設定檔的cluster.name參數同)

6.     設定Graylog其他參數
# vi /etc/graylog/server/server.conf
# The email address of the root user.
# Default is empty
#root_email = ""
root_email = "cyp@test.com.tw"

# The time zone setting of the root user. See http://www.joda.org/joda-time/timezones.html for a list of valid time zones.
# Default is UTC
#root_timezone = UTC
重要,若不設定為ROC,則進來Logtimestamp會變成以UTC時間紀錄,
會影響到搜尋結果
root_timezone = ROC


# 允許wildcard搜尋語法
# 例如 AND EventID:4771 AND NOT TargetUserName:*?
# 新增下列設定
allow_leading_wildcard_searches = true

7.     設定防火牆
# sudo firewall-cmd --permanent --zone=public --add-port=9000/tcp
# sudo firewall-cmd --permanent --zone=public --add-port=12201/udp
# sudo firewall-cmd --reload

8.     啟動graylog-server
# systemctl start graylog-server.service

9.     設定開機啟動graylog-server
# systemctl enable graylog-server.service

10. 設定SELinux
安裝SELinux管理工具
# yum install -y policycoreutils-python
  
允許Web Server存取網路
# sudo setsebool -P httpd_can_network_connect 1

若沒有設定上列policy,可以下列命令針對需求對不同http port設定存取規則。
Graylog REST API and web interface:
# sudo semanage port -a -t http_port_t -p tcp 9000

Elasticsearch (only if the HTTP API is being used):
# sudo semanage port -a -t http_port_t -p tcp 9200

允取使用MongoDB default port (27017/tcp):
# sudo semanage port -a -t mongod_port_t -p tcp 27017



六.  登錄Graylog Web
使用瀏覽器訪問 http://graylog_public_IP_domain:9000/

你應該可以看到一個登錄介面,使用帳號 admin 和前面設置的密碼登入。



七.  設定INPUT接收Windows Log
Windows端選擇以NXLog協助發送LogGraylog Server,請至下載網頁下載安裝。

NXLog設定檔 (C:\Program Files (x86)\nxlog\conf\nxlog.conf) 修改如下---



設定後啟動nxlog服務。






回到Graylog Web介面---
1.      點擊下拉 System / Inputs 功能選單,選擇 Inputs


2.      選擇 GELF UDP後,點擊 Launch new input


3.      填入相關設定值,完成後按 Save
Node: 下拉選擇Node
Title: Windows Event - UDP
Bind address: Graylog Server IP
Port: 12201 (default value)



設定完成後,即可看到一個Local Inputs開始接收messages