Skip to content

Instantly share code, notes, and snippets.

@trangnth
Last active November 12, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save trangnth/3db5cf3e2841bfeb9fc00bfc5a4ed9a1 to your computer and use it in GitHub Desktop.
Save trangnth/3db5cf3e2841bfeb9fc00bfc5a4ed9a1 to your computer and use it in GitHub Desktop.
Wazuh auto scan and alert IP and port with Nmap

Cấu hình wazuh - nmap auto scan

Mô tả

  • Cảnh báo mỗi một host mới tham gia hoặc không tham gia vào dải ip được quét
  • Cảnh báo mỗi khi một port bất kỳ của host mở hoặc đóng (thay đổi trạng thái)
  • Thực hiện cấu hình trên wazuh server

Tham khảo

  • Sử dụng lại script tại:

https://github.com/m0zgen/nmap-autoscan

Bước 1: Cài đặt sẵn wazuh đang hoạt động bình thường

Bước 2: Cấu hình file config của ossec

  • Thêm đoạn sau vào file cấu hình /var/ossec/etc/ossec.conf
<ossec_config>
...
  <localfile>
    <log_format>full_command</log_format>
    <command>bash /root/nmap-autoscan.sh 192.168.40.71-128</command>
    <frequency>60</frequency>
    <alias>nmap scan ports</alias>
  </localfile>
...
</ossec_config>

Bước 2: Cấu hình thêm rule để tạo alert

  • Thêm đoạn cấu hình dưới đây vào file /var/ossec/etc/rules/local_rules.xml
  <rule id="100003" level="7">
    <if_sid>530</if_sid>
    <match>ossec: output: 'nmap scan ports</match>
    <check_diff />
    <description>Nmap host info has changed</description>
    <group>pci_dss_10.2.7,pci_dss_10.6.1,gpg13_10.1,gdpr_IV_35.7.d,hipaa_164.312.b,nist_800_53_AU.14,nist_800_53_AU.6,</group>
  </rule>
  • Lưu ý phần rule id=100003, id này là tùy chọn trong khoảng 100000 - 109999 (User defined rules) nhưng không được trùng với các rule cũng được người dùng tạo trước đó.

Bước 3: Tạo script

  • Tạo một file /root/nmap-autoscan.sh với nội dung như sau:
#!/bin/bash

# 
# EDITED from https://github.com/m0zgen/nmap-autoscan/blob/master/nmap-autoscan.sh
# Edit by trangnth
# Date: 12/2019
# Nmap scan range IP detect host or port added(opened)/removed(closed)/changed status
#



#
# ---------------------------------------------------------- ENVs #
#
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
# Determine script location
SCRIPT_PATH=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
# Script name
#me=`basename "$0"`

# ---------------------------------------------------------- VARs #
#
IP=$1
dta=$(date +%d-%m-%Y-%H-%M-%S)
# dta=$(date +%d-%m-%Y)

SCANFOLDER=$SCRIPT_PATH/scans
SCANPREV=$SCANFOLDER/prev.xml
SCANCURR=$SCANFOLDER/scan-$dta

# ---------------------------------------------------------- CHKs #
#
if [[ -z $IP ]]; then
  #statements
  echo "Please add IP to scan"
  exit
fi

if [[ ! -d $SCANFOLDER ]]; then
  mkdir $SCANFOLDER
fi

# ---------------------------------------------------------- ACTs #
#
cd $SCANFOLDER

nmap -T4 -F -Pn $IP -oX $SCANCURR.xml > /dev/null

if [[ -L $SCANPREV ]]; then
    ndiff $SCANPREV $SCANCURR.xml | egrep -v "^$|^.*Not shown:.*$|^.*Nmap.*scan initiated.*$" > tmpfile

    #if [[ -n `grep "open" diff-result` ]]; then
    #  #cat /root/scans/diff-result
    #  echo auca >>/dev/null
    #fi

    if [[ -n `grep "PORT" tmpfile` ]]; then
        #sed -i '/^$/d' diff-result
        #sed -i '1,2d' diff-result
        #sed -i '/^.*Not shown:.*$/d' diff-result
        cat tmpfile > diff-result
        sed -i 's/^-Host is up.$/-Host is down./g' diff-result
        sed -i '/^-.*open.*/ s/open/close/' diff-result
    fi
    cat diff-result
fi

ln -sf $SCANCURR.xml $SCANPREV
cd - >> /dev/null
  • Mô tả về script:

    • Dùng nmap để scan range ip, kết quả sẽ được lưu dưới dạng file xml trong thư mục scan/
    • Dùng ndiff để so sánh kết quả của hai lần quét ghi vào file tmpfile, nếu hai lần có kết quả các nhau (phát hiện thông tin về một host thay đổi: rời khởi mạng, ko sử dụng ip đó nữa, đóng port, mở port,...), tức là trong file tmpfile có dữ liệu về một host bất kỳ, thì ghi nội dung khác biệt giữ hai kết quả của hai lần check (host thay đổi thông tin) vào file diff-scan
    • In ra màn hình nôi dung file diff-scan
  • Wazuh flow:

    • Nếu output khi thực hiện tool (nội dung của file diff-scan) giữa hai lần chạy tool khác nhau thì wazuh sẽ tạo một cảnh báo (có thể xem trên kibana hoặc trong file log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment