--- title: 惡意 C&C DNS 網路通訊偵測 tags: - C&C Server - DNS - DNS tunneling - Computer Network - TCP - UDP - 第一組 --- # 惡意 C&C DNS 網路通訊偵測 :::info **做簡報的重點** - 介紹你們針對這個題目去蒐集資料後的了解? - 目前有關的問題與相關研究如何? - 你們覺得這個題目若要變成專題,可行的方向是?理由為何? - 你找到的資料資源有哪些? ::: > 你是惡意程式分析專家。請說明學習並研究「惡意 C&C DNS 網路通訊偵測」會需要那些背景知識? > [name=羅崧瑋] :::info <i class="fa fa-search" aria-hidden="true"></i> **chatGPT 的回答** 學習並研究「惡意 C&C DNS 網路通訊偵測」(Command and Control Domain Name System Network Traffic Detection)需要廣泛的背景知識,涵蓋多個領域。以下是主要的背景知識領域: ### 1. **計算機網路基礎** - **OSI模型**:了解七層模型,尤其是應用層、傳輸層和網絡層。 - **網絡協議**:熟悉TCP/IP協議、UDP、HTTP、HTTPS等基本協議。 - **DNS協議**:深入理解DNS的工作原理,包括DNS查詢、響應、記錄類型(A、MX、CNAME等)。 ### 2. **網絡安全** - **網絡攻擊技術**:了解常見的網絡攻擊手法,如DDoS攻擊、釣魚攻擊、SQL注入等。 - **惡意軟件分析**:理解惡意軟件(如病毒、木馬、蠕蟲、勒索軟件)的工作機制和傳播方式。 - **入侵檢測系統(IDS)和入侵防禦系統(IPS)**:了解這些系統的工作原理及其在網絡安全中的應用。 ### 3. **惡意軟件分析** - **靜態分析**:反編譯、反匯編技術,了解PE、ELF等文件格式。 - **動態分析**:沙盒技術、虛擬機技術,通過動態執行觀察惡意軟件行為。 - **逆向工程**:使用工具(如IDA Pro、Ghidra)對惡意軟件進行逆向分析。 ### 4. **DNS流量分析** - **流量捕捉與分析工具**:熟悉Wireshark、tcpdump等工具,用於捕捉和分析網絡流量。 - **流量模式識別**:了解正常與異常DNS流量的特徵,識別可疑模式。 ### 5. **數據分析與機器學習** - **數據處理**:了解如何處理和分析大規模數據集,使用工具如Pandas、NumPy等。 - **機器學習基礎**:了解監督學習、非監督學習和強化學習的基本概念,熟悉常見的機器學習算法(如決策樹、隨機森林、支持向量機、神經網絡)。 - **特徵提取與選擇**:理解如何從DNS流量中提取和選擇有效特徵,以提高檢測的準確性。 ### 6. **網絡取證** - **取證方法**:了解基本的網絡取證技術,確保證據的完整性和可追溯性。 - **法律與倫理**:熟悉與網絡安全相關的法律法規和倫理標準,確保在合法合規的框架內進行研究。 ### 7. **程式設計** - **編程語言**:熟悉Python、C/C++等常用編程語言,用於開發分析工具和自動化任務。 - **腳本編寫**:能夠編寫腳本自動化處理大量網絡數據,如使用Shell script、Python script。 這些背景知識構成了一個完整的知識體系,有助於有效地學習和研究「惡意 C&C DNS 網路通訊偵測」。在實際學習過程中,可以通過閱讀相關書籍、參與專業培訓課程、參加網絡安全競賽(如CTF)等方式來逐步掌握和應用這些知識。 ### 參考資料 - Stallings, W., & Brown, L. (2018). **Computer Security: Principles and Practice**. Pearson. - Skoudis, E., & Liston, T. (2003). **Counter Hack Reloaded: A Step-by-Step Guide to Computer Attacks and Effective Defenses**. Prentice Hall. - Ligh, M. H., Adair, S., Hartstein, B., & Richard, M. (2010). **Malware Analyst's Cookbook and DVD: Tools and Techniques for Fighting Malicious Code**. Wiley. ::: ## 名詞定義 - **packet 封包** a small segment of a larger message[^packet_def] - **socket** one endpoint of a two-way communication link between two programs running on the network[^socket_def] [^packet_def]: 來源:[https://www.cloudflare.com/zh-tw/learning/network-layer/what-is-a-packet/](https://www.cloudflare.com/zh-tw/learning/network-layer/what-is-a-packet/) [^socket_def]: 來源:[The Java™ Tutorials - What Is a Socket?](https://docs.oracle.com/javase/tutorial/networking/sockets/definition.html) ## C&C Server(C2Server) Command and Control(C&C) Server,又稱 C2 Server,中文翻譯為「命令與控制伺服器」。是一台電腦,用於向被惡意程式感染的裝置發送指令,於遠端控制受感染的裝置。感染後的裝置網路叫做 botnet。 ### 用途 - **Cryptojacking(Crypto-mining)** 破解密碼、挖虛擬貨幣等需要龐大計算資源的行動。 - **Data exfiltration** 偷人家資料,洩漏出去、拿去賣或是自己偷偷用。 - **DDoS attacks** 控制大量連網裝置,對被害者發送大量網路請求。 - **Reboot devices** 重開機別人裝置,讓在該裝置運行的服務被中斷。 - **Network shutdown** 把一個區域的網路全部關閉。 - **Misuse of future resources** 濫用未來資源。可能會進行 C&C 攻擊以破壞合法應用程式,從而對未來資源產生負面影響。這方面的一個例子是 2020 年的 SolarWinds 漏洞,威脅行為者將惡意代碼秘密傳輸到公司的軟體系統中,然後由 SolarWinds 無意中將其作為其軟體更新的一部分發送給客戶。該代碼處於休眠狀態數月,為受害者的機器創建了後門,並被威脅行為者用來監視和安裝其他惡意軟體。 - **Advanced persistent threat(APT)** 會讓惡意軟體潛伏在被害裝置內,看要拿去賣還是繼續感染其他裝置。 ### 感染途徑 - Phishing emails - Malvertising - Vulnerable browser extensions - Malware ### 架構 - Star topology(Centralized) 由一個伺服器向其他裝置發送命令。 - P2P 去中心化,資料將會由每個節點被分,更難被偵測,也可以與星型架構混用。 - Random topology 最難檢測和阻止。 - Multi-server topology - Hierarchical topology ### 通訊方式 #### DGA 使用域名生成算法(DGA,Domain Generation Algorithm)來動態生成大量的域名,防止使用固定域名被發現且封鎖後無發攻擊。DGA生成之域名可能為無效域名,惡意軟體需要嘗試連接(大量的DNS查詢)這些域名以找到當前有效的域名。 #### DNS tunneling ``` +---+ +---+ +---+ ... +---+ +---+ +---+ | |<---| |----| |<-- <--| |<---| |----| | | C | | | | D | | D | | | | B | | & | | F | | N | ... | N | | F | | o | | C | | W | | S | | S | | W | | t | | |----| |--->| |--> -->| |----| |--->| | +---+ +---+ +---+ ... +---+ +---+ +---+ ip spoofed packet ``` :::info DNS隧道通過在DNS封包內嵌入其他數據來傳輸訊息,這種方法可以用來繞過網絡過濾器或防火牆。以下是幾種常見的方式,DNS隧道是如何隱藏訊息在DNS封包內的: 1. **Base32/Base64 編碼**: - 將要傳輸的數據轉換為Base32或Base64編碼,並將其嵌入到DNS請求的查詢名稱(Query Name)中。 - 優點:簡單實現,通過常見的編碼方式將二進制數據轉換為ASCII字符。 - 缺點:編碼後的數據量會增加,需要處理更大的DNS封包。 ```plaintext 編碼後的數據.example.com ``` 2. **子域名分片**: - 將大的數據切分為多個小片段,並將每個片段放在一個子域名中。每個查詢請求中包含一個片段,伺服器再將這些片段重新組合還原原始數據。 - 優點:能夠傳輸更大的數據量,減少單個封包的大小。 - 缺點:需要多個DNS查詢來傳輸完整數據,效率較低。 ```plaintext 片段1.example.com 片段2.example.com ``` 3. **DNS TXT 記錄**: - 使用DNS的TXT記錄類型來傳輸數據。TXT記錄可以存儲任意文本數據,適合用來嵌入需要傳輸的訊息。 - 優點:TXT記錄本來就設計用來存儲文本數據,天然適合傳輸任意訊息。 - 缺點:部分DNS伺服器可能對TXT記錄有大小限制。 ```plaintext "encoded_data" TXT example.com ``` 4. **CNAME 記錄**: - 利用CNAME記錄進行訊息隱藏。CNAME記錄通常用來做域名別名解析,但也可以用來傳輸數據。 - 優點:CNAME記錄廣泛使用,不易被懷疑。 - 缺點:CNAME記錄解析可能會導致額外的DNS查詢,增加延遲。 ```plaintext encoded_data.example.com CNAME target.example.com ``` 5. **A/AAAA 記錄**: - 利用IPv4(A記錄)或IPv6(AAAA記錄)地址進行數據隱藏。將數據編碼為IP地址格式,通過DNS請求查詢這些地址來傳輸數據。 - 優點:A/AAAA記錄常見且解析快速。 - 缺點:受限於IP地址格式,能夠傳輸的數據量較小。 ```plaintext encoded_data.example.com A 127.0.0.1 ``` 6. **MX 記錄**: - 使用郵件交換(Mail Exchanger,MX)記錄來傳輸數據。MX記錄通常用來指向郵件伺服器,但也可以用來隱藏數據。 - 優點:MX記錄不易引起懷疑。 - 缺點:MX記錄解析需要更多的DNS查詢,效率較低。 ```plaintext encoded_data.example.com MX mail.example.com ``` 每種方式都有其優點和限制,選擇哪種方式取決於具體應用場景和網絡環境。在實際應用中,應考慮數據大小、傳輸效率以及安全性等因素。 ::: - Names can only up to 255 bytes ```python! #!/usr/bin/env python3 import base64 def chunk_data(message, chunk_size=20): return [message[i:i+chunk_size] for i in range(0, len(message), chunk_size)] def encode_chunks(chunks): encoded_chunks = [] for chunk in chunks: encoded_chunk = base64.b64encode(chunk.encode('utf-8')) encoded_chunks.append(encoded_chunk.decode('utf-8').strip('=')) return encoded_chunks def create_domain_name(chunk, domain): return f"{chunk}.{domain}" def decode_chunks(encoded_chunks, domain): stripped_chunks = [chunk.strip(domain) for chunk in encoded_chunks] decoded_chunks = [] for chunk in stripped_chunks: decoded_chunk = base64.b64decode( (chunk + '=' * (-len(chunk) % 4)).encode('utf-8')).decode('utf-8') decoded_chunks.append(decoded_chunk) return decoded_chunks def main(): domain = "yuto0226.com" message = "Jerry is fall in love with a guy." chunk_size = 10 chunks = chunk_data(message, chunk_size) encoded_chunks = encode_chunks(chunks) print("\n[*] Chunk and Encode") for i in range(len(chunks)): print(f"{chunks[i]:{chunk_size}} -> {encoded_chunks[i]:{chunk_size}}") qnames = [create_domain_name(chunk, domain) for chunk in encoded_chunks] print("\n[*] Create Domain Names") for qname in qnames: print(qname) print("\n", "#" * 80, sep='') print("Message Tunneling") print("#" * 80) print("\n[*] Decode Chunked Message") decoded_chunks = decode_chunks(qnames, domain) received_message = ''.join(decoded_chunks) for chunk in decoded_chunks: print(chunk) print("\n[*] Original Message") print(received_message) if __name__ == "__main__": main() ``` #### 通訊協定 #### 流量隱蔽 ### 檢測方法 #### 靜態分析 #### 動態分析 - 流量檢測 ### 著名案例 - Mirai Botnet - TrickBot Banking Trojan - Kyle & Stan Malvertising ## 計算機網路基礎 [TryHackMe - Introductory Networking](https://tryhackme.com/r/room/introtonetworking) packet size: 0x0000-0xFFFF 65535 bytes ### IP [RFC 791 - INTERNET PROTOCOL](https://datatracker.ietf.org/doc/html/rfc791) [RFC 6864 - Updated Specification of the IPv4 ID Field](https://datatracker.ietf.org/doc/html/rfc6864) [RFC 2474 - Definition of the Differentiated Services Field (DS Field) in the IPv4 and IPv6 Headers](https://datatracker.ietf.org/doc/html/rfc2474) ``` 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ``` ### TCP [Transmission Control Protocol (TCP)](https://datatracker.ietf.org/doc/html/rfc9293) ``` 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Port | Destination Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgment Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | |C|E|U|A|P|R|S|F| | | Offset| Rsrvd |W|C|R|C|S|S|Y|I| Window | | | |R|E|G|K|H|T|N|N| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum | Urgent Pointer | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | [Options] | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | : : Data : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ``` ### UDP [User Datagram Protocol](https://datatracker.ietf.org/doc/html/rfc768) ``` 0 7 8 15 16 23 24 31 +--------+--------+--------+--------+ | Source | Destination | | Port | Port | +--------+--------+--------+--------+ | | | | Length | Checksum | +--------+--------+--------+--------+ | | data octets ... +---------------- ... ``` ### HTTP/HTTPS **HTTP/1.0** [RFC 1945 - Hypertext Transfer Protocol -- HTTP/1.0](https://datatracker.ietf.org/doc/html/rfc1945) **HTTP/1.1** - [RFC 2068 - Hypertext Transfer Protocol -- HTTP/1.1](https://datatracker.ietf.org/doc/html/rfc2068) - [RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1](https://datatracker.ietf.org/doc/html/rfc2616) - [RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing](https://datatracker.ietf.org/doc/html/rfc7230) - [RFC 7231 - Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content](https://datatracker.ietf.org/doc/html/rfc7231) - [RFC 7232 - Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests](https://datatracker.ietf.org/doc/html/rfc7232) - [RFC 7233 - Hypertext Transfer Protocol (HTTP/1.1): Range Requests](https://datatracker.ietf.org/doc/html/rfc7233) - [RFC 7234 - Hypertext Transfer Protocol (HTTP/1.1): Caching](https://datatracker.ietf.org/doc/html/rfc7234) - [RFC 7235 - Hypertext Transfer Protocol (HTTP/1.1): Authentication](https://datatracker.ietf.org/doc/html/rfc7235) ### DNS [RFC 1034 - DOMAIN NAMES - CONCEPTS AND FACILITIES](https://datatracker.ietf.org/doc/html/rfc1034) [RFC 1035 - DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION](https://datatracker.ietf.org/doc/html/rfc1035) Message format ``` +---------------------+ | Header | +---------------------+ | Question | the question for the name server +---------------------+ | Answer | RRs answering the question +---------------------+ | Authority | RRs pointing toward an authority +---------------------+ | Additional | RRs holding additional information +---------------------+ ``` ##### Header ``` 1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA| Z | RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ ``` ##### Qusetion ``` +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | / QNAME / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QTYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QCLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ ``` ##### Answer/Authority/Additional ``` +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | / / / NAME / | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | TYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | CLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | TTL | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | RDLENGTH | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| / RDATA / / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ ``` <!-- ## 網路安全基礎 --> <!-- ## 惡意軟體(Malware)知識 ### 惡意軟體 帶有惡意目的的所有類型軟體的統稱。 1. **病毒(Virus)** 會感染程式檔案或個人檔案。 2. **蠕蟲(Worm)** 在網路中自我複製與散播的惡意軟體。 3. **木馬程式(Trojan)** 看似合法程式,但會破壞電腦。 4. **間諜軟體(Spyware)** 會蒐集個人資料。 5. **廣告軟體(Adware)** 會使用合法的線上廣告來散播惡意軟體。 6. **殭屍網路(Botnet)** --> ## 文獻查詢 [A comprehensive survey on DNS tunnel detection](https://www.sciencedirect.com/science/article/pii/S1389128621003248) **iterative Query** 根域名不會直接回傳查詢的 IP 而是回傳 TDL 伺服器的 IP。 再由 Recursive Resolver 根據根域名伺服器的回應重新向 TDL 伺服器發出新的 query。 TDL 伺服器繼續回傳下一級伺服器的 IP 直到找到 Authoritative Name Server Recursive Resolver 從 Authoritative Name Server 獲取所需 IP 回傳給 user。 > Recursive Resolver 遞歸解析器,負責外部網路的完整查詢過程。 > Authoritative Name Server 權威域名伺服器,保存有關特定域名的所有紀錄,且當 DNS 伺服器向他發 query 時提供域名的最終解析結果 > local name server 本地名稱伺服器是 Recursive Resolver 的一部分,但主要處理內部網路。 **recursive Query** 本地解析器收到 query 後不再將請求發送回客戶端,而是完全接管此次 query 過程,直到獲得最終 IP。 ![](https://ars.els-cdn.com/content/image/1-s2.0-S1389128621003248-gr2_lrg.jpg) ## DNS 及網路流量分析 ## 工具 **Nmap** - [TryHackMe - Nmap](https://tryhackme.com/r/room/furthernmap) - [Nmap 筆記](https://hackmd.io/@yuto0226/SJN1tuaHA) ## 參考資料 **C&C Server** - [Command-and-Control Servers Explained. Techniques and DNS Security Risks](https://heimdalsecurity.com/blog/command-and-control-servers-explained/) - [command-and-control server (C&C server)](https://www.techtarget.com/whatis/definition/command-and-control-server-CC-server) - [What is a Command-and-Control Server?](https://sysdig.com/learn-cloud-native/detection-and-response/what-is-a-command-and-control-server/) **Detect traffic** - [How to Leverage Log Services to Analyze C&C Traffic](https://securityintelligence.com/how-to-leverage-log-services-to-analyze-cc-traffic/) - [How to Detect Malware C2 with DNS Status Codes](https://www.socinvestigation.com/how-to-detect-malware-c2-with-dns-status-codes/) - [Basic classifiers for DNS tunneling detection](https://ieeexplore.ieee.org/abstract/document/6755060) - [A comprehensive survey on DNS tunnel detection](https://www.sciencedirect.com/science/article/pii/S1389128621003248) - **DNS tunneling** - [DNS Response Size](https://www.netmeister.org/blog/dns-size.html) **proxy** - [How to Create a Proxy Server in Python Using Proxy.py](https://scrapingant.com/blog/python-proxy-server-proxy-py) **相關研究** - [OnBotnets that use DNS for Command and Control](https://norbert-pohlmann.com/app/uploads/2015/08/279-On-Botnets-that-use-DNS-for-Command-and-Control-Prof-Norbert-Pohlmann.pdf)