# 110-2 智慧網路與安全技術 Week 13 實習 ###### tags: `Computer Science` `uscc` :::danger === DOCKER ONLY === 因為很重要,所以寫在最前面 使用Docker記得添加-\-cap-add=NET_ADMIN以及[published ports](https://docs.docker.com/config/containers/container-networking/#published-ports) ::: ## 測試服務(擇一,僅用於測試網安服務) 1. ssh (port: 22) - Installation ```sh sudo apt update && sudo apt install -y ssh ``` - Testing 1. Windows: 使用[PuTTY](https://the.earth.li/~sgtatham/putty/latest/w64/putty.exe)連線 2. Linux & Mac: 使用ssh連線 指令:$ ssh <user>@<ip> e.g. ```ssh root@10.0.0.1``` 1. http (port: 80) - Installation ```sh sudo apt update && sudo apt install -y nginx ``` - Testing 使用瀏覽器瀏覽http://<虛擬機ip> 1. ping <虛擬機ip> (maybe a wise choice) ... <!-- :::warning 使用虛擬機做為伺服器(服務提供者),須將網卡設置為橋接(bridge)以方便測試服務 ![](https://i.imgur.com/Mhk4qY0.png =x400) ::: --> ## 防火牆 Firewall ### iptables iptables是運行在**User Space**的應用軟體,通過控制Linux核心netfilter模組,來管理網路封包的處理和轉發。在大部分Linux發行版中,可以通過 [手冊頁](https://linux.die.net/man/8/iptables) 或 man iptables 取得使用者手冊。通常iptables需要核心模組支援才能執行,此處相應的核心模組通常是Xtables。因此,iptables操作需要**Super User**權限,其可執行檔通常位於 /sbin/iptables 或 /usr/sbin/iptables 。 ptables、ip6tables等都使用Xtables框架。存在「**表(tables)**」、「**鏈(chain)**」和「**規則(rules)**」三個層面。 每個「表」指的是不同類型的封包處理流程,如filter表表示進行**封包過濾**,而nat表針對連接進行**位址轉換**操作。每個表中又可以存在多個「鏈」,系統按照預訂的規則將封包通過某個內建鏈,例如將從本機發出的資料通過OUTPUT鏈。在「鏈」中可以存在若干「規則」,這些規則會被逐一進行匹配,如果匹配,可以執行相應的動作,如修改封包,或者跳轉。跳轉可以直接接受該封包或拒絕該封包,也可以跳轉到其他鏈繼續進行匹配,或者從當前鏈返回呼用者鏈。當鏈中所有規則都執行完仍然沒有跳轉時,將根據該鏈的預設策略(「policy」)執行對應動作;如果也沒有預設動作,則是返回呼用者鏈。 - filter表 filter表是預設的表,如果不指明表則使用此表。其通常用於過濾封包。其中的內建鏈包括: - INPUT,輸入鏈。發往本機的封包通過此鏈。 - OUTPUT,輸出鏈。從本機發出的封包通過此鏈。 - FORWARD,轉發鏈。本機轉發的封包通過此鏈。 - nat表 nat表如其名,用於位址轉換操作。其中的內建鏈包括: - PREROUTING,路由前鏈,在處理路由規則前通過此鏈,通常用於目的位址轉換(DNAT)。 - POSTROUTING,路由後鏈,完成路由規則後通過此鏈,通常用於源位址轉換(SNAT)。 - OUTPUT,輸出鏈,類似PREROUTING,但是處理本機發出的封包。 ![](https://i.imgur.com/xUqgSPZ.png) 常用指令: iptables [-t table] -L [chain] # 查看防火牆規則 iptables [-t table] [-FX] [chain] - -F :清除所有的已訂定的規則 - -X :殺掉所有使用者 "自訂" 的 chain iptables [-t table] -P [INPUT | OUTPUT | FORWARD | <chain>] [ACCEPT | DROP] # 定義chain預設策略 iptables [-AI chain] [-io 網路介面] [-p 協定] [-s 來源IP/網域] [-d 目標IP/網域] -j [ACCEPT | DROP | REJECT | <chain> ] # 新增/插入規則 iptables [-t table] -D [chain] [rulenum] # 刪除chain/規則 iptables [-t table] -N [chain] #新增chain :::danger iptables需要核心功能,必須以superuser執行 ::: ### ufw & firewalld - Uncomplicated Firewall,簡稱UFW,是Ubuntu系統上預設的防火牆組件。UFW是為輕量化組態iptables而開發的一款工具。 - e.g. ```# ufw allow 80/tcp``` - firewalld 是一款動態防火牆管理器。 firewalld 通過「網路/防火牆」空間的方式,為不同的網路連線或介面定義其自身的信任等級,通過這種方式達到了動態管理的效果。同時,它支援 IPv4、IPv6 防火牆的設定 、乙太網路網橋以及組態選項可為執行時或永久。另外,它還提供了介面,方便應用添加規則。 - e.g. ```# firewall-cmd --zone=public --add-service=http``` ## 入侵檢測系統 IDS ### Suricata #### Installation ```sh sudo apt update sudo apt install -y software-properties-common # 安裝需要選擇地區,6. Asia, 73. Taipei sudo add-apt-repository ppa:oisf/suricata-stable sudo apt install -y suricata python3-pip pip3 install suricata-update sudo suricata-update ``` #### Setup 請至/etc/suricata/suricata.yaml #L582將eth0修改為正確網卡名稱(使用ip a查看) ```yaml af-packet: - interface: eth0 ``` :::info Docker預設網卡為eth0 ::: #### Startup ```sh sudo systemctl start suricata # DOCKER ONLY service suricata start ``` #### Status ```sh sudo systemctl status suricata # DOCKER ONLY service suricata status ``` > ![](https://i.imgur.com/AQ6xjU5.png) #### Custom Rules 在/etc/suricata/suricata.yaml #L1868添加自定義規則檔案,如:local.rules ```yaml= default-rule-path: /var/lib/suricata/rules rule-files: - suricata.rules - local.rules ``` 於/var/lib/suricata/rules/local.rules撰寫屬於自己的規則 ``` alert icmp any any -> any any (msg:"Do not read gossip during work"; content:"..."; nocase; classtype:policy-violation; sid:1; rev:1;) # alert/drop 協定/服務 來源ip port -> 目的ip port 訊息 內容 ... 類別 sid rev alert ip any any -> any any (msg:"GPL ATTACK_RESPONSE id check returned root"; content:"uid=0|28|root|29|"; classtype:bad-unknown; sid:2100498; rev:7; metadata:created_at 2010_09_23, updated_at 2010_09_23;) ``` #### Restart ```sh sudo systemctl restart suricata # DOCKER ONLY service suricata restart ``` #### Logging ```/var/log/suricata/fast.log``` #### Waching fast.log ```sudo tail -f /var/log/suricata/fast.log``` #### Rule trigger ```curl http://testmyids.com/``` ## Reference - https://linux.vbird.org/linux_server/centos6/0250simple_firewall.php#netfilter_chain - https://suricata.readthedocs.io/en/latest/rule-management/adding-your-own-rules.html