2017-04-25 14:44:57f88tw

c-icap 防毒閘道(Squid / ICAP / SSL Bump)

這是一套以 FreeBSD 為核心、採用 Squid ⇄ ICAP ⇄ c-icap ⇄ ClamAV 架構的防毒閘道系統,實際運作於真實網路環境,用於在流量最前端過濾病毒與惡意內容。本文僅分享成功運作的結果畫面,非商業、非教學。
本系統中之 ICAP 為通訊協定,c-icap 則為實際執行 ICAP 服務之伺服器程式,兩者角色不同,非同一元件。

【守護家門的靜謐:一位老工程師的資安禮物】

這不是商業廣告,這是一份來自 8088 時代的老工程師,為這個混亂網路世界準備的一份心意。

● 您是否也常感到不安?

現在的網路環境,詐騙簡訊、惡意病毒、釣魚網頁無孔不入。 即便裝了防毒軟體,病毒還是可能在您沒察覺時潛入。 我一直在想:能不能在病毒「進門」之前,就把它擋下來?

● 我做了一台「網路門神」

我花了一年的時間,利用企業級的技術(FreeBSD / c-icap),打造了一套不需要安裝在電腦裡的防毒系統。

  • 它像是一道過濾網: 在資料進入您的手機、平板、電腦前,就先幫您檢查。

  • 它不打擾您: 您不需要學習操作,它只是安靜地守在最前端。

  • 它沒有後門: 全套使用開放原始碼軟體,純粹、乾淨、無廣告。

● 我想把這份安心,送給有緣的您

我是一個人在作業,精力有限,眼睛也不如以往敏銳。 我無法大量供應,也不想影響商業市場的運作。 如果您真的有強烈需求,希望能獲得這套系統:

  1. 請先聯絡我: 由於每份系統都需手工製作,請先透過【郵政信箱】與我溝通。

  2. 等待確認: 請容許我審核環境是否適合。若我評估後決定為您製作,我會主動聯繫您。

  3. 播種福田: 請不要給我錢。 當我確認要贈送給您時,請您捐款 100元以上(金額隨喜,重在心意)給任何您信任的慈善機構,並將收據傳給我看即可。

「我求的不是名利,而是您在使用網路時的一份安心與快樂。」


聯絡方式: 台北市郵政信箱 35 之 16 號 (請留下您的姓名與電話,若合適,我會親自與您聯繫)


 說明
本頁所顯示之過濾結果,並不代表該網站本身存在問題。
在測試過程中,曾遇到無法判定來源的異常狀況,該狀況無法被任何掃毒軟體明確識別,因此不進行追究。
本系統僅呈現實際過濾結果,是否觸發過濾,取決於當下的比對規則與判斷條件,與網站立場無關。

若造成任何誤解,在此一併說明與致歉

  

1.寫 www.hi38.org 的工作計畫書。

這個結構圖清晰地展示了我們在 FreeBSD 系統上建立的組件位置和它們之間的依賴關係。

總體結構圖 (訴狀式一覽表)/ (根目錄)

├── /mnt/ (原始碼編譯區)
│   ├── c_icap-0.5.x/         <-- c-icap 核心原始碼目錄
│   ├── c_icap-modules-0.5.x/ <-- c-icap 模組原始碼目錄
│   ├── clamav-1.X.x/         <-- ClamAV 原始碼目錄
│   └── squid-6.10/           <-- Squid 原始碼目錄
│
├── /usr/local/ (系統安裝與配置區)
│   ├── etc/
│   │   ├── rc.d/             <-- 啟動腳本目錄
│   │   │   ├── c-icap        <-- 服務啟動腳本 (讓 c-icap 變為服務)
│   │   │   ├── clamd         <-- 服務啟動腳本 (讓 clamd 變為服務)
│   │   │   └── squid         <-- 服務啟動腳本
│   │   │
│   │   ├── clamd.conf        <-- ClamAV 主配置檔 (我們確保 PidFile 在此目錄)
│   │   │
│   │   ├── c-icap/           <-- c-icap 配置子目錄 (如果使用這種結構)
│   │   │   └── c-icap.conf   <-- c-icap 主配置文件 (我們最後成功修改的地方)
│   │   │
│   │   └── squid/
│   │       └── squid.conf    <-- Squid 主配置文件 (我們最後成功添加 ICAP 規則的地方)
│   │
│   └── etc/ (配置檔的另一種常見位置,取決於編譯選項)
│       └── clamav/           <-- (此處可能被忽略,因為我們直接用 /usr/local/etc/clamav)
│
│   └── sbin/
│   │   ├── clamd             <-- ClamAV Daemon (最終運行檔)
│   │   └── c-icap            <-- c-icap 伺服器 (最終運行檔)
│   │
│   └── lib/
│       └── c_icap/           <-- c-icap 模組與服務庫
│           ├── clamd_mod.so  <-- (可能沒用,但存在)
│           └── srv_virus_scan.so <-- c-icap 用來呼叫 ClamAV 的核心模組
│
│   └── var/
│       └── log/
│           ├── server.log    <-- c-icap 服務日誌
│           ├── access.log    <-- c-icap 訪問日誌
│           └── squid/        <-- (Squid 的日誌目錄)
│
└── /var/run/ (運行時數據區)
    └── clamav/               <-- **關鍵:Clamd 寫入 PID 檔的路徑**
        ├── clamd.pid         <-- Clamd 進程 ID 檔案
        └── clamd.sock        <-- Clamd Unix Socket (c-icap 透過它通信)
    
    └── c-icap/               <-- c-icap 運行目錄
        ├── c-icap.pid        <-- c-icap 主進程 ID 檔案 

        └── c-icap.ctl <-- c-icap 控制 Socket成功關鍵點 (您已解決):

配置病毒庫權限與目錄...

組件 目標文件/目錄 成功狀態
clamd /var/run/clamav/clamd.pid 解決:目錄已建,可以寫入 PID
clamd rc.d 腳本 解決service clamd start 成功
c-icap /usr/local/etc/c-icap.conf 解決:核心服務已啟動並監聽 1344 埠。
ClamAV 整合 c-icap.conf 中 ServiceAlias 成功:使用 srv_virus_scan.so 配合 Unix Socket 參數化配置成功
Squid 連接 /usr/local/etc/squid.conf 待辦:請確保您已將 Squid ICAP 規則 貼入 Squid 的配置檔 中。

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

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

/usr/local/lib/c_icap/virus_scan.so: signal 13

✅ [成功] virus_scan.so 已成功橋接至 libclamav

/usr/local/lib/c_icap/clamav_mod.so: signal 13

✅ [成功] clamav_mod.so 已成功橋接至 libclamav

/usr/local/lib/c_icap/clamd_mod.so: signal 13

✅ [成功] clamd_mod.so 已成功橋接至 libclamav

/usr/local/lib/c_icap/srv_content_filtering.so: signal 13

✅ [成功] srv_content_filtering.so 已成功橋接至 libclamav

/usr/local/lib/c_icap/srv_url_check.so: signal 13

✅ [成功] srv_url_check.so 已成功橋接至 libclamav

c-icap-client -i 127.0.0.1 -s avscan -f /tmp/eicar.txt -v

得到的結果代表:

✅ c-icap 已經成功被「觸發」

✅ clamav 引擎已經成功被呼叫

✅ 病毒庫已經被正確載入

✅ 掃毒判定成功(EICAR 被抓到)

root@:~ # sockstat -4 -l | grep 1344

root     c-icap      1665 5   tcp4   *:1344                *:*

root     c-icap      1664 5   tcp4   *:1344                *:*

root     c-icap      1663 5   tcp4   *:1344                *:*

root     c-icap      1662 5   tcp4   *:1344                *:*

透過 ldd 檢查 c-icap 之 virus_scan.so 模組,可確認其已正確連結 ClamAV 核心函式庫(libclamav.so),並同時載入 SSL、壓縮、編碼與 FreeBSD 系統相關之共享函式庫。此結果顯示病毒掃描模組並非僅形式上存在,而是實際具備完整掃描能力,包含 HTTPS(SSL Bump)後之內容分析。

To change this login announcement, see motd(5).
root@:~ # ldd /usr/local/lib/c_icap/virus_scan.so
/usr/local/lib/c_icap/virus_scan.so:
libclamav.so.11 => /usr/local/lib/libclamav.so.11 (0x32f1a0000000)
libc.so.7 => /lib/libc.so.7 (0x32f19ba6a000)
libclammspack.so.0 => /usr/local/lib/libclammspack.so.0 (0x32f19ed0b000)
libssl.so.12 => /usr/local/lib/libssl.so.12 (0x32f19db09000)
libcrypto.so.12 => /usr/local/lib/libcrypto.so.12 (0x32f1a0855000)
libz.so.6 => /lib/libz.so.6 (0x32f19dfc6000)
libbz2.so.1 => /usr/local/lib/libbz2.so.1 (0x32f19ee54000)
libpcre2-8.so.0 => /usr/local/lib/libpcre2-8.so.0 (0x32f19fa43000)
libxml2.so.16 => /usr/local/lib/libxml2.so.16 (0x32f1a166f000)
libjson-c.so.5 => /usr/local/lib/libjson-c.so.5 (0x32f1a3491000)
libm.so.5 => /lib/libm.so.5 (0x32f1a2235000)
libexecinfo.so.1 => /usr/lib/libexecinfo.so.1 (0x32f1a3103000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x32f1a3fd7000)
librt.so.1 => /lib/librt.so.1 (0x32f1a416e000)
libutil.so.9 => /lib/libutil.so.9 (0x32f1a4a9b000)
libkvm.so.7 => /lib/libkvm.so.7 (0x32f1a5563000)
libmemstat.so.3 => /usr/lib/libmemstat.so.3 (0x32f1a5f25000)
libprocstat.so.1 => /usr/lib/libprocstat.so.1 (0x32f1a68cb000)
libdevstat.so.7 => /lib/libdevstat.so.7 (0x32f1a6ee2000)
libthr.so.3 => /lib/libthr.so.3 (0x32f1a7c74000)
libiconv.so.2 => /usr/local/lib/libiconv.so.2 (0x32f1a8baf000)
liblzma.so.5 => /usr/lib/liblzma.so.5 (0x32f1a9270000)
libelf.so.2 => /lib/libelf.so.2 (0x32f19cded000)
libmd.so.6 => /lib/libmd.so.6 (0x32f1a9d69000)
root@:~ #

Squid SSL Bump ⇄ ICAP ⇄ c-icap virus_scan.so ⇄ libclamav.so
實體連結與模組層級驗證架構圖(FreeBSD)

┌──────────────┐

│        Client / Browser     │

│      (HTTP / HTTPS)      │

└──────────────┘

               │

               ▼

┌─────────────────┐

│            Squid                           │

│  ─ HTTP Proxy                      │

│  ─ SSL Bump (MITM)           │

│  ─ Decrypted HTTPS Content│

└───────────────┘

               │  ICAP protocol

               ▼

┌──────────────┐

│            ICAP                     │

│        (Protocol)                  │

│  Request / Response Mod │

└──────────────┘

               │

               ▼

┌───────────────────┐

│               c-icap                              │

│        (ICAP Server Daemon)          │

│                                                        │

│  ┌───────────────┐      │

│  │    virus_scan.so (Module) │      │

│  │                                          │      │

│  │  ── linked by ldd ──┐   │      │

│  └───────────────┘      │

│                                          │            │

└───────────────┼───┘

                                             │

                                             ▼

┌─────────────────────────┐

│              Shared Libraries (實體證據)           │

│                                                                       │

│  🦠 ClamAV 核心                                        │

│   ├─ libclamav.so.11                                     │

│   └─ libclammspack.so.0                               │

│                                                                        │

│  🔐 SSL / Crypto(HTTPS 掃描成立)     │

│   ├─ libssl.so.12                                             │

│   └─ libcrypto.so.12                                       │

│                                                                         │

│  📦 檔案 / 壓縮 / 編碼                                  │

│   ├─ libbz2.so.1                                              │

│   ├─ liblzma.so.5                                            │

│   ├─ libz.so.6                                                  │

│   ├─ libiconv.so.2                                           │

│                                                                         │

│  📄 內容解析                                                 │

│   ├─ libxml2.so.16                                         │

│   ├─ libjson-c.so.5                                          │

│   └─ libpcre2-8.so.0                                       │

│                                                                         │

│  🧠 FreeBSD 系統深度整合                         │

│   ├─ libmemstat.so.3                                      │

│   ├─ libprocstat.so.1                                       │

│   ├─ libkvm.so.7                                             │

│   └─ libdevstat.so.7                                         │

└─────────────────────────┘


如何驗證 Squid 的 SSL Bump 是否真的生效

(以一台「未安裝任何自訂 CA」的乾淨 Windows 用戶端為例)

為什麼 Chrome / Edge 會出現這個錯誤?

net::ERR_CERT_AUTHORITY_INVALID

這個錯誤只會在一種情況下出現

瀏覽器收到了「被重新簽發的 HTTPS 憑證」,
但它 不信任簽發該憑證的 CA


⚠️ 關鍵說明(非常重要)

如果 沒有啟用 SSL Bump
這個錯誤是 不可能 出現的。

原因很簡單:

  • 沒有 SSL Bump
    → 瀏覽器只會看到 原始網站的官方憑證
    → 只會顯示 🔒 綠色鎖頭
    → 不會出現 ERR_CERT_AUTHORITY_INVALID

👉 因此,這個錯誤本身
就是 SSL Bump 已實際介入 HTTPS 的直接證據


真正的「鐵證」在哪?

你可能沒特別注意,但下面這一行
已經是「結構層級、不可否認」的證據:

Via: ICAP/1.0 YourServerName (C-ICAP/0.5.12 srv_content_filtering service), 1.1 No1.f88tw (squid/6.12)

為什麼這一行在 HTTPS 裡是「大事」?

正常的 HTTPS 行為中

  • CONNECT tunnel 建立後

  • Squid 無法看到 HTTP header

  • c-icap 不可能被呼叫

也就是說:

沒有 SSL Bump
→ HTTPS 對 Proxy 來說只是加密的黑盒子


👉 但現在你看到的是什麼?

  • HTTPS 回應標頭中

  • 直接寫明「經過 ICAP 服務」

  • 並標示出 Squid 與 c-icap 的版本與角色

這在架構上代表:

🧠 Squid 已完成 SSL Bump
→ 解開 HTTPS
→ 將內容送入 c-icap
→ 再重新加密後回傳給用戶端

 


只要在未安裝任何自訂 CA 的 Windows 用戶端上,
瀏覽 HTTPS 時出現「憑證不受信任」錯誤,
且 HTTPS 回應中明確標示經過 Squid 與 ICAP,
就代表 SSL Bump 已實際解密 HTTPS,
並將內容送入防毒/內容檢查後再重新加密回傳。










待續

promiscuous mode


我喜歡這隻豬,我有安裝過但是失敗(在資料庫的部分)好想要去買原版的書裡面互帶光碟才能解決問題畢竟自由軟體嗎。找不到地方維修。

##############################################################

OpenBSD
FreeBSD

 Snort 實作入侵偵測系統

前言

在一般企業中通常會設置防火牆作為防止駭客入侵的第一道防線,有些企業甚至認為防火牆即是唯一需要的資安設備,但在實務上,防火牆並無法有效阻擋所有種類的網路攻擊行為。也因此有所謂的入侵偵測系統設備(既然無法阻擋攻擊,那就記錄攻擊行為,以便事後追查,當然目前也有所謂的入侵預防系統 (Intrusion Prevention System, IPS),除了可記錄惡意行為外還可直接阻擋正在進行中的惡意行為)。在本篇文章中,筆者將結合開源碼社群中最有名的入侵偵測軟體 (Snort) 與 Web 介面的管理軟體 (BASE) 實作一個網頁型的入侵偵測系統。

本系統所需套件如下表:

軟體名稱 網址 說明
Fedora 11 https://fedoraproject.org/ Linux 作業系統
Libpcap https://www.tcpdump.org/ 擷取網路封包的程式庫
PCRE https://www.pcre.org/ 正規化表示法的程式庫
Snort https://www.snort.org 網路型入侵偵測系統
PHP https://www.php.net 網頁程式語言
Apache https://www.apache.org 網站伺服器
BASE (Basic Analysis and Security Engine) https://base.secureideas.net 網頁式入侵偵測系統管理程式
MySQL https://www.mysql.com 資料庫軟體
ADOdb https://adodb.sourceforge.net/ PHP 資料庫程式庫

▲表 1

什麼是入侵偵測系統

入侵偵測系統依偵測型式可分為網路型入侵偵測系統(Network-based Intrusion Detection System,以下簡稱為 NIDS)及主機型入侵偵測系統(Host-based Intrusion Detection System,在此我們不多加討論主機型入侵偵測系統)。

NIDS

網路型入侵偵測系統通常部署在網域閘道上 (gateway),通過竊聽 (sniffer) 的方式,即時監測網路上的封包並比對攻擊模式,當發現有疑似入侵行為時能即時提出警告的防禦系統。一般而言 NIDS 網路部署如下圖示:


▲圖 1

NIDS 部署在網域閘道上 (gateway) 監控所有的封包並與攻擊模式資料庫比對,一旦發現有符合攻擊樣式的網路封包,即觸發相關事件來通知管理者,相信細心的讀者一定會發現上圖中的入侵偵測系統 (Snort) 是接在 switch 裝置前的 hub,而不是與 switch 裝置直接連接,這是因為封包在這兩種裝置上傳遞方式不同的緣故。在 hub 上,封包是採用廣播的方式,當封包進入到 hub 時,將會以廣播的方式將封包傳遞給 hub 上所有的主機,但只有目的主機會接收此封包,也就是說,在 hub 上連接的主機都會接收到封包,只是沒收下來而已,在這個情況下,如果我們讓網卡進入所謂的混亂 (promiscuous) 模式,那網卡就會強制的將同一 hub 上的所有封包資訊接收下來(這也是一些以竊取資料為目的木馬程式的特徵),所以有些網管人員會特別注意相關 log 檔案是否有類似 (promiscuous) 的字串,不過 Snort 在啟動後,也會強迫網卡進入混亂 (promiscuous) 模式,藉此取得其它主機的封包。以 Linux 系統為例;讀者可利用 ifconfig 指令查看目前網卡的狀況,如下圖為一個正常模式的網卡,在此模式下,網卡僅會處理與本身相關的封包。


▲圖 2

讀者可利用 ifconfig eth0 promisc 指令,讓網卡進入混亂 (promiscuous) 模式,如下圖即為一個進入混亂 (promiscuous) 模式的網卡,在此模式下,網卡會處理流經 hub 的所有封包,Snort 即是利用此特性處理網域內的所有封包並比對是否有惡意樣式的封包,所以 Snort 程式在執行時,會將網卡設定成混亂 (promiscuous) 模式。


▲圖 3

而在 switch 裝置上,則是會保存一張對應表,對應接在 switch 上的每台主機埠口(即 switch 裝置上的埠會對應連接此實體埠主機的 MAC 資訊),一旦封包流進此 switch 即會先查詢此對應表,並直接將封包直接傳遞給目的主機,而其他不相關的主機無法接收到封包。因此,若無經過特殊的處理,Snort 直接連在 switch 上是無法取得其它主機的封包。這也是為什麼 Snort 主機不能直接接在 switch 上來從事監控作業,而必須接在 hub 上的原因。除非 switch 有提供所謂的 mirror 功能,可將 switch 上其它埠所接收到的封包複製一份到 Snort 主機所在的埠口上,否則 Snort 接在 switch 上是無法取得其它主機的封包(僅可取得流經自己主機的封包)。

NIDS 偵測的方式可分為特徵比對 (Signature-based) 方式與異常偵測 (Anomaly-based) 方式,如下所述:

特徵比對 (Signature-based)

特徵比對 (Signature-based) 又稱為「不當行為偵測 (Misuse detection)」,系統會先針對入侵特徵建立一「異常特徵資料庫」,只要 NIDS 偵測到的封包內容與資料庫的某個特徵相符,系統即會判別為入侵。此種方式的優點是不易誤判,因為個別的攻擊行為通常擁有特殊的特徵符號。就如同病毒碼一般可精確比對出攻擊模式。但就如上所言,此種方式是否能完整的檢測出惡意的封包,取決於「異常特徵資料庫」的完整性,異常特徵資料庫越完整,檢測出惡意封包的機率就越大,所以此種方式並無法檢測出未知的攻擊方式,因為需要先有攻擊行為才會有攻擊特徵,「異常特徵資料庫」才能加入此種攻擊特徵,最後 NIDS 才可藉此掃描出惡意封包。Snort 所採用的偵測方式,即屬於特徴比對的方式。

異常偵測 (Anomaly-based)

運用統計分析的方式,先定義出正常的系統模式(以下簡稱正常模式),而後當 NIDS 檢測出不符合正常模式的流量時,即判別為異常,此種模式的優點在於可偵測未知的攻擊行為,因為攻擊行為常會造成系統偏離正常模式而被檢測出來,但缺點是很難界定所謂的「正常的系統模式」,所以經常會有誤判的情況,也因此大部份的入侵偵測系統大都採用特徵比對的方式。

安裝 Snort

Snort 是一種以攻擊特徵碼為基礎的入侵偵測系統,利用事先建立好的已知攻擊資料特徵碼,來比對接收到的封包內容是否含有攻擊行為。若符合特徵碼即觸發相對應的動作。相關架構圖如下所示:


▲圖 4

Packet Decoder(封包解碼器)

當 Snort 取得各種不同通訊協定的網路封包後,第一件事即將封包置入「封包解碼器」模組中做第一階段的封包處理與分析,以作為下一個階段(前處理器與偵測引擎)的資料來源。

Preprocessors(前處理器)

Snort 的前處理器為外掛程式 (plugin) 的架構,主要功能在於重新標準化網路流量(如重組封包、分段與重組 TCP stream、編碼的轉換等等),以使得網路流量能精確的被偵測引擎 (Detection Engine) 解析及匹配特徵碼。

Detection Engine(偵測模組)

Detection Engine(偵測模組)主要功能在於規則分析與特徵偵測,Detection Engine(偵測模組)將 Snort 的規則文件引入,並按照規則文件中的規則,逐一比對並分析網路封包,一旦發現封包有符合規則文件中定義的行為,即觸發該規則文件中所定義的處理方式,當所有的規則都不符合時,即會丟棄該封包。Snort 的規則文件分為兩個部份:
1. 規則表頭 (Rule-Header) 規則表頭是規則中第一個圓括孤之前的部份,通常是用來決定封包比對來源範圍(如:限定比對那些範圍的 IP)及比對成功時的動作(如:用 log 或是直接丟棄)。
2. 規則選項 (Rule-Options) 規則選項主要利用一至多個關鍵字設定欲偵測的流量特徵,規則選項依功能可區分為下列四個部份:
(1) Meta-data 用來設定欲顯示的相關訊息,如當規則觸發時所要產生的訊息或弱點的相關參考訊息。
(2) Payload 用來比對封包內容的規則,如比對是否需區分大小寫或特定字串等等。
(3) Non-Payload 用來比對各種協定的欄位值。
(4) Post-Detection 當封包內容與規則符合時,除了在規則表頭 (Rule-Header) 所定義的動作外,另外會觸發的動作指令如下規則範例:
alert tcp any any → any 5432 (msg:"someone access PSQL command:SELECT"; content:"select";)
規則描述如下表:

alert【處理方式】 產生警示的 log
tcp【來源通訊協定】 偵測 TCP 的封包
any【來源 IP】 偵測任何的來源 IP
any【來源 port】 偵測任何的來源埠
any【目的 IP】 偵測任何的目的 IP
5432【目的 port】 僅偵測 5432 埠的封包
【進階處理內容】 若符合內容含有「select」的字串,則將 msg 後的字串記錄起來。

▲表 2

上述規則的意義為:
當主機上的 5432 埠,如果有接收到內含有 select 字串的封包,則記錄一筆警示記錄(記錄內容為「someone access PSQL command:SELECT」)。

Logging and Alerting:當 Snort 偵測出惡意封包時,可以將該訊息記錄起來並觸發相關警告事件。
Output Modules:可將相關資訊輸出到檔案或資料庫等儲存媒介,在本解決方案中,我們會將相關的資訊儲存至資料庫中。

接下來,繼續安裝 Snort,假設讀者已安裝完成 Apache 與 MySQL,請讀者依序以下列步驟安裝相關套件:
1.   安裝 Libpcap 及 PCRE
yum install libpcap*
yum install libpcre*
2.   設定 ADOdb
至 https://adodb.sourceforge.net/ 取得最新的版本,解開後,將相關檔案置於 /usr/local/adodb 目錄即可
3.   安裝 Snort 的資料庫
(1)  新建一個名稱為 snort 的資料庫
(2)  在 Snort 原始碼的 schemas 目錄下有一個 create_mysql 檔案
利用 mysql –u [USER] –p [Password] snort 
(新建立 Snort 所需要的資料庫表格)
4.   安裝 Snort
(1)  至 www.snort.org 取得最新版本的 Snort 解壓縮後,如下指令:
./configure && make && make install 即可安裝完成
(2)  接下來即為建立 Snort 所需的相關目錄:
mkdir –p  /opt/snort/etc   #放置 config 檔案目錄
mkdir –p  /opt/snort/rules   #放置規則檔的目錄
mkdir –p  /var/log/snort   #放置 log
(3)下一步即為取得 Snort 的規則集(目前 Snort 是必需付費方可取得最新的規則集,但讀者可至官方網站簡單註冊後,即可取得未註冊的版本)將相關的規則集檔案解壓縮後置於 /opt/snort/rules 即可。
(4)再來即為設定 Snort 的組態檔 (snort.conf),基本上僅需設定下列選項即可:
var HOME_NET any    #設定欲監控的主機(any 表示任意的主機均符合)
var EXTERNAL_NET any #設定外部主機範圍
output database: log, mysql, user =【資料庫使用者帳號】
password =【資料庫密碼】 dbname=snort host=localhost
#MySQL 資料庫的支援

在安裝完成後,即可開始測試 Snort,Snort 提供兩種工作模式:
(1)  監聽 (sniffer) 模式
在此模式下,Snort 僅是 sniffer(如 tcpdump)的功能,監聽所有來往的封包,但不會做攻擊模式的比對。
讀者以 snort –v 進入 sniffer 模式,如下圖示:


▲圖 5

(2)  NIDS(網路型入侵偵測系統)
在此模式下,Snort 不僅會監聽所有來往的封包,並會做攻擊模式的比對。

4.   安裝 BASE 軟體
由於 BASE 軟體有繪圖及 Email 相關功能, 所以我們還必需安裝 PHP 相關的模組。
pear install Mail     #利用安裝 Email                             
pear install Image_Color-1.0.2.tgz  #安裝繪圖所需的模組
pear install Image_Canvas-0.3.0.tgz 
pear install Image_Graph-0.7.2.tgz
安裝完成後,可利用 pear list 來檢查是否有安裝相關模組,如下圖示:


▲圖 6

將 BASE 相關程式置於網站根目錄下的 base 目錄,(在此為 /usr/local/apache2/htdocs/base),後利用瀏覽器設定,如下步驟:
步驟1(檢查相關環境):


▲圖 7

步驟2(設定 ADOdb 的所在目錄):


▲圖 8

步驟3(設定資料庫相關的資訊):


▲圖 9

步驟4(設定從網站登入的帳號及密碼資訊):


▲圖 10

步驟5(產生相關的資料庫表格):


▲圖 11

當一切順利完成後,讀者可利用瀏覽器瀏覽 https://〈base IP〉/base/ 即可瀏覽 BASE 的相關頁面
接下來即請讀者啟動 Snort,如下指令:
/usr/local/snort/bin/snort -c /usr/local/snort/etc/snort.conf  -D
(以常駐程式方式啟動 Snort,並將網卡設定成 promiscuous,讀者可利用 dmesg 查看是否有類似下列的文字「device eth0 entered promiscuous mode」)。
下圖為 Snort 偵測到 Teardrop(一種拒絕服務攻擊的手法)攻擊的畫面:


▲圖 12

至此,讀者已成功建立網頁型入侵偵測系統。可由網頁介面來掌控入侵偵測系統的相關資訊。
###################################


###################################

Linux Journal

Search

Peering Over the Firewall

Security
by Jeffrey L. Taylor
 

When our home LAN graduated to a 24x7 Internet connection, my Linux box became the firewall and the router. I liked the ability to customize the firewall, and by using Snort I could keep an eye on the barbarians at the gates. However, I could not experiment much without disrupting the entire household's Internet access. Adding a DSL/cable broadband router (see Resources) with a built-in firewall took my computer off the critical path and allowed me to experiment with various configurations and operating systems without domestic discord. But, I missed seeing what was going on. I do not want the first sign of someone attacking me to occur when they appear inside the firewall.

Intrusion detection systems (IDSs) are standard practice in the corporate world, but they easily can cost more than an entire home network, including computers. With some free software (Snort), a cheap Ethernet hub and a custom cable, you can have an IDS that is almost as good as a commercial system. The major lack is the pretty reports and graphs necessary to justify a big salary.

A typical home LAN with a broadband router is shown here:

Figure 1. LAN w/Broadband Router

Most broadband routers include a multiport switch. A switch sends packets to only the destination port. In contrast, a hub is a simple repeater and sends each packet to all other ports. With a switch, each computer sees only its own traffic, so computer A cannot monitor attacks on the other computers. Figure 2 shows how to use a hub to peer around the router and see all packets.

Figure 2. LAN w/Broadband Router and Snort Cable

Residential cable and DSL modems run at 1-2Mbps, so a single speed 10Mbps Ethernet hub is fine. These can be found used, reconditioned or dumped as obsolete for as little as $10 at discount outlets.

A packet sniffer, such as Snort, running in promiscuous mode on the second network interface card (NIC) of computer A can see all traffic, including those packets blocked by the firewall. To keep the second NIC from responding to any IP packets, it needs to be brought up with no IP address, that is, ifconfig eth1 up. If the second NIC already is assigned an IP address, taking it down and bringing it back does not always delete the IP address. Instead, take the interface down, remove the driver module (rmmod ne2k-pci) and bring it back up. If the driver is compiled into the kernel, you probably have to reboot here. Some system configuration tools, such as SuSE's yast, do not let you configure an Ethernet interface without an IP address. In this case, you have to edit the necessary files by hand.

No IP address usually is adequate stealthiness, but the second NIC still can respond to low-level, layer 2 requests, such as ARP requests. To make it totally invisible, use a read-only cable. The wiring diagram is in Figure 3. These cables can be made easily with a length of Cat5 cable, two RJ45 connectors and a crimping tool. First, strip off a half inch of the outer insulation, and insert each wire into a slot on the RJ45 connector and crimp. There does not seem to be a consistent standard for which pin gets which color wire, so eyeball a commercial cable and follow its example. Label this the hub end.

Next, cut two inches off the other end of the cable to get a bit of wire to make the jumper between pins 1 and 2 of the Snort end. You can do this by bending it in half and shoving it in the RJ45 connector. Again, strip a half inch off the outer insulation. Strip a quarter inch of the inner insulation off the wires from pins 1, 2, 3 and 6. With care and solid conductor Cat5 cable, you can push the wires from pins 1 and 3 into the slot for pin 3 and the wires for pins 2 and 6 into slot 6. Be sure all wires are inserted fully into the slots, then crimp the end. See Resources for links to pictures of the result. (A word of warning: trying this with stranded wire is a task for masochists.)

If your cable is stranded or if you can't find a crimper and are handy with a soldering iron, simply cut the patch cord in the middle and connect the appropriate wires together. Use heat shrink tubing to insulate and protect your handiwork.

Figure 3. Snort Read-Only Cable Wiring Diagram

When you plug the cable into the NIC and hub, the status LEDs should light in their usual pattern. If not, check and correct your work. Next, put Snort in packet dumping mode with snort -d -p -v -i eth1. You now should see traffic coming from both directions.

Finally, edit snort.conf and start Snort in dæmon mode:

snort -D -i eth1 -p -c /etc/snort.conf

Most home broadband connections have dynamic IP addresses, but they don't change often. Therefore, configuring HOME_NET in snort.conf can be done by hand each time the address changes. An alternative is to run the whois program on your external IP address. It may tell you the IP address range(s) owned by your ISP. Using this address/netmask for HOME_NET makes for less work, but then Snort may ignore attacks from your electronic neighbors because they are considered to be part of your LAN.

With this setup, I again could see the script kiddies rattling the door knobs and the various worms, including Slammer, trying to propagate.

When I upgraded to Snort 1.9.1 and its more extensive attack signatures, I started seeing a P2P GNUTella alert. I don't use P2P, so this was suspicious. The router does network address translation (NAT) between the internal private addresses and the single external IP address, hiding the culprit. By moving the hub inside the firewall, as shown in Figure 4, you can see the untranslated internal addresses. You have to change HOME_NET in /etc/snort.conf from the external IP address to the internal range, probably something like 192.168.0.0/16, to do this.

Figure 4. Snorting Inside the LAN

Before you move cables around, check whether your broadband router supports configuring a spanning port to get all traffic, that is, it is not switched. If so, you can leave the hub where it is and plug Snort into the spanning port.

The culprits turned out to be AmphetaDesk (an RSS headline aggregator) and PGP/GPG key fetches. The P2P GNUTella signature is overly broad; it flags any HTTP GET request to a non-standard port (not port 80). Below are some rules you can add to local.rules to eliminate the false positives:

pass tcp $HOME_NET any -> $EXTERNAL_NET !80 (flow:to_server,established;   content:"User-Agent: AmphetaDesk"; offset:10;)pass tcp $HOME_NET any -> $EXTERNAL_NET 11371 (flow:to_server,established;   content:"GET /pks/lookup?op=get&search="; offset:0;)

In conclusion, Snort makes it easy to tell what's going on. To stay on top of what's going on, update your Snort rules frequently.

Resources

“A Tale of Two Routers”, UnixReview.com, September 2002.

URLs for read-only Snort cable pictures:

www.snortsam.net/1

www.snortsam.net/2

www.snortsam.net/3