## Malware
Malicious software is a generic term that refers to hostile executable code.
### type
- 自我複製並寄生於主機代碼(病毒)。
Self-replicating and live in host code: virus.
- 獨立運行(蠕蟲)。
Freestanding: worm.
- 不自我複製(木馬)。
Non-self-replicating: Trojan.
Pretend to be good software or install itself without user knowing

### Basic challenges
It’s hard or impossible to determine how a program will
behave. -> halting problem
### creeper vs reaper


### virus
A computer virus infects other programs.
- Infected program must **be executed** to invoke virus
- Virus self-replicates (as opposed to Trojan)
- Installed without user agreement
#### Self-replication can exploit many different vectors
- 感染存儲的可執行文件
Infect stored executables.
- 感染操作系統例程以保持駐留內存中
Infect OS routines to remain in memory.
- 感染磁碟的啟動扇區
Infect disk boot sectors.w
### Anti-virus
identifies and attempts to purge malware of any type (not just viruses
- Simple anti-virus software looks for **signatures**(code fragment for a known virus).
### Polymorphic Viruses
- 病毒的功能保持不變,但代碼會發生改變。
Code changes while virus functionality doesn’t.
- 由短頭部 (Short Head) 和加密的主體或負載 (Encrypted Body or Payload) 組成。
The virus contains a short head and an encrypted body or payload.
- 當病毒被執行時,頭部負責解密主體。
When the virus is executed, the head decrypts the payload.
- 加密密鑰可以變化:
Encryption key can change.
- 頭部簡短,易於更改:
Head is short, so it can be easily changed too.
- Defeats basic signature checking.

### Anti-virus defense: emulation
- 模擬可能受感染代碼的執行:
Emulate execution of potentially infected code.
- 檢查解密後的負載的特徵碼:
Look for signature of decrypted payload.
- 可以有效對抗多態性病毒:
Defeats polymorphism!
- 如果病毒深度嵌入到主機可執行文件中,模擬過程可能需要過長時間。
Doesn’t work if the virus is deeply embedded in the host executable; emulation takes too long.
### Metamorphic Viruses
Mutate the payload(變異payload)
#### HOW



#### Anti-virus defense?
1.尋找共同的子序列 (Common Subsequences)
- 對變形病毒的不同版本,嘗試找到它們代碼中的共同特徵(以粗體標記)。
Attempt to identify shared subsequences in different variants of the metamorphic virus, marked in bold.
2.行為檢測 (Behavioral Detection)
- 基於病毒行為進行檢測,而不是僅依賴靜態特徵。
Detect based on the virus's behavior rather than static characteristics.
- 行為檢測可以識別變形病毒的運行模式,但有誤報風險。
Behavioral detection identifies the virus's runtime patterns but incurs false positives.
- 誤報問題 (False Positives)
對於消費者而言,誤報率過高是不可接受的,可能導致正常程序被錯誤標記為病毒。
False positives are unacceptable for consumers as they may flag legitimate programs incorrectly.
### Drive-by downloads/installations
- 驅動式下載網站專門針對瀏覽器和瀏覽器插件的漏洞。
A drive-by download site targets vulnerabilities in web browsers and browser add-ons
- 具有漏洞的計算機用戶僅需訪問該網站,即可自動感染惡意軟件,無需主動下載任何內容。
Users with vulnerable computers can be infected with malware automatically by visiting such a website; they don’t need to attempt to download anything.
- 通常寄宿於被入侵的合法網站 (Usually Hosted on Legitimate Websites):
- 通過入侵(Intrusion)或用戶貢獻內容(例如跨站腳本 (XSS))進行攻擊。
Compromised by intrusion or user-contributed content (e.g., cross-site scripting, XSS).
- Exploit code is usually hosted on a different website and exposed through a compromised webpage using a technique like a URL embedded in malicious script code or an iFrame.

### Mobile malware
與 PC 惡意軟件相比,行動惡意軟件的普及率和複雜度仍然較低
大部分都是安卓被搞
- 在前十大威脅中,有四個與間諜軟件有關。
Four of the top ten threats are spyware.
## Privacy
### Anonymization and pseudonymization
- Anonymization (匿名化)
Remove name and any real-world ID
- Pseudonymization (假名化)
Replace real ID with a consistent random ID
#### problem
- Linkage attacks
- An adversary with a little bit of knowledge (e.g., age, gender, postcode) can match to the database and identify people.
擁有少量信息(如年齡、性別、郵編)的攻擊者可以匹配數據庫並識別個人。
- Exploitation without real-world identification
- information can be exploited without real-world identification (e.g., tracking cookies on websites).
### K-anonymity
- Replace an attribute by ranges of values (or representative values)
將屬性替換為值的範圍(或代表值)
- In groups of at least k
每組至少包含 k 個成員

使得每位個體至少與 k-1 名其他成員共享相同的非敏感屬性值,以增加隱私保護。

### Privacy vs Security
- Security (安全):
Prevent unauthorized users from reading data
防止未授權用戶訪問數據
- Privacy (隱私):
Prevent authorized users from learning more than they should
防止授權用戶了解超出其權限的信息
- Data should still be useful for intended tasks
數據應該仍然對預定的任務有用
### Aggregate functions
did not release the dataset with individual values
only released an aggregate value
- e.g., average, sum, high-d average, count…
例如:平均值、總和、高維平均值、計數等
A function 𝑓(𝐷) computed on a dataset 𝐷
#### Differencing attack
- Datasets with and without an individual. E.g., average before and after employee joins
比較包含和不包含某個個體的數據集。例如,員工加入前後的平均值變化。
#### Membership inference attacks
- Find if a known person was present in the dataset
判斷某個已知個體是否存在於數據集中。
#### Data reconstruction attack
- Deduce part of the dataset from aggregate
從聚合值去反推未公開的部分數據
### ML
machine learning models is also a kind of Aggregate function
$w = f(D)$
模型參數 𝑤 是基於數據集 𝐷
會受到上述的攻擊
#### IDEA

---
### knowledge
攻擊者可能擁有的輔助信息:
例如,攻擊者可能擁有與目標數據集相關的其他數據集,或者已經掌握了一部分數據點的信息。
通過將已知數據與目標數據集的輸出結果進行關聯(Linkage Attack),攻擊者可能推斷出隱藏的個體數據。
Knowledge can be tricky
身高例子
- Knowledge can be seen as probabilistic (知識可以被視為概率性的):
- Looking at the data, you can make a better guess than before
通過查看數據,你可能比以前做出更好的猜測。
#### Idea 2

**prvent Membership inference attacks**
### Differential privacy




ϵ 衡量了由於公開算法 𝐴 的輸出而導致的隱私損失。
更小的 𝜖 表示:
輸出結果更難被攻擊者利用來推斷個體數據的存在。
算法對輸入數據的「不敏感性」更高
#### subset version

### example
假設有一場會議。我們希望公佈會議中的人數統計,公佈的人數統計必須滿足 𝜖-差分隱私的要求,通過觀察統計結果,攻擊者不應該容易推斷某個特定的人是否參加了會議。
real count=n
Publish $n+y$, where y is noise (a random number)
使用拉普拉斯分佈生成y
$\Pr[y] = \frac{1}{2b} e^{-\frac{|y - \mu|}{b}}$
$\mu$ is the mean, $2b^2$ is variance
$Lap(b)$ is Laplace distribution when mean=0 and variance=$2b^2$


#### publish n+y
- where $y$ ~~ $Lap(\frac{1}{ϵ})$
This is the ϵ-Differential privacy
### Adversarial model
Adversary
- Knows the algorithm
- Knows the distribution
- Does not know the noise y
DP assume the adversary know the entire data set,
but does not know if individual x’s data is in the computation
since the definition says nothing about its value
#### replacement version
if even the presence of x is protected, its value is also protected
**if x has v**
- removing x cannot be detected in the
output (has ϵ-differential privacy)
- can also make two undetectable moves
- remove v and insert $\mu$
- Has 2ϵ-DP
DP is often defined with the value replacement version instead of the removal version
- Easier to see how DP protects values
### Sensitivity
當單一輸入會很大程度導致輸出中的重大變化 -> privacy is hard
:::info
敏感度
$\Delta f = \max \left| f(D) - f(D') \right|$
for all possible neighboring datasets
:::
噪聲的幅度與$\Delta f$成正比,與$𝜖$成反比


## Software-vulnerability
### determine the quality of a program
just testing
- software with more faults is likely to
have even more
### Penetrate and Patch
- 特殊團隊測試程式,尋找錯誤。(Special teams test programs and find faults)
- 如果未發現攻擊,程式被認為是安全的。(If no attack found, the program was OK)
- 如果發現攻擊,進一步頻繁測試。(Otherwise, not – More frequently)
- 然後修補漏洞。(Then fix faults)
#### Issue
become less secure
- 只關注修復漏洞本身,而忽略其上下文
- 漏洞修復可能引發其他地方的副作用
- 修復一個漏洞可能導致新的漏洞
- 修復漏洞可能影響系統的功能或性能
### 5% bug per line of code
### Memory Exploits
Buffer is a data storage area inside computer memory (stack or heap)
- hold pre defined amount of data
if executable code is **offered as data**, it may be excuted by fooling the machine and attacker can get access of the machine
### stack buffer

當函數 func 返回時(When func returns):
- 局部變數從堆疊中彈出。
- 堆疊框架指針(sfp)的舊值被恢復。
- 返回地址(ret addr)被取回。
- 堆疊框架(stack frame)被彈出。
- 程式執行繼續從返回地址(調用函數的下一行)開始。
### buffer Over-stuffed

#### Stack Smashing
- 假設緩衝區包含attacker-created string
- 攻擊者將實際的組合指令嵌入其輸入字符串中
- 在溢位中,緩衝區中的指針出現在系統期望的返回地址位置
- 當函數結束時,緩衝區中的代碼將被執行

- Stack memory is supposed to contain only data, but excutable code is dealed as data
- attacker must guess stack position correctly
- 溢位部分必須在返回地址(RET)的存儲位置包含攻擊代碼的正確地址
- 返回地址(RET)的值必須指向緩衝區中攻擊代碼的起始位置
- otherwise segmentation violation
##### No Range Checks
沒檢查範圍
- strcpy(char *dest, const char *src)
- strcat(char *dest, const char *src)
- gets(char *s)
- scanf(const char *format, …)
- printf(const char *format, …)
##### check range
```
strncpy(char *dest, const char *src, size_t n)
```
Change from
```
strcpy(record, user);
strcat(record, ":");
strcat(record, cpw);
```
To
```
strncpy(record, user, MAX_STRING_LEN-1);
strcat(record, ":");
strncat(record, cpw, MAX_STRING_LEN-1);
```
但是
strncpy 和 strncat 只限制單次操作的大小,但不追踪整個緩衝區的總大小。
如果 record 的大小小於 user + : + cpw 的總長度,仍可能導致溢位。
### Variable Overflow
用BUF OVERFLOW去改變SENTATIVE的變數內容

### Alter Pointer Variables

- 在輸入參數 s 中注入惡意代碼:
Send malicious code in s
- 通過 gets 提供超過 80 字節的輸入:
Pass more than 80 bytes in gets.
- 覆蓋函數指標 fnptr 的值:
Overflow fnptr.
- fnptr 現在指向惡意代碼:
fnptr now points to malicious code.
- 當 fnptr 被執行時,惡意代碼也將被執行:
When fnptr is executed, malicious code is executed
### Frame Pointer



### Integer Overflow
```
static int getpeername1(p, uap, compat) {
struct sockaddr *sa; // socket 地址結構
len = MIN(len, sa->sa_len); // 確保 len 不會超出範圍
copyout(sa, (caddr_t)uap->asa, (u_int)len); // 將 len 字節複製到用戶空間
}
```
如果LEN是負數(SIGNED),負數 len 會通過這個檢查,copyout會把其解釋成很大的數,結果最多可複製 4GB 的核心內存到用戶空間
### Time of Check to Time of Use
Concurrency issue
- 連續指令可能不會順序執行。
Successive instructions may not execute serially.
- 其他進程可能會在此期間獲得控制權。
Other processes may be given control
TOCTTOU
- 在存取控制檢查與實際存取操作之間,控制權被交給了其他進程。
Control is given to other process between access control check and access operation.
---
正常檔案的path通過檢查後,利用Concurrency issue趁還沒有開啟時把path代表的檔案換成惡意的

#### Defense
- 確保關鍵參數在進程切換期間不被暴露
Ensure critical parameters are not exposed during pre-emption.
- 確保操作的順序完整性
Ensure serial integrity
- ```openfile``` is **atomic**
- No pre-emption during its execution(不允許進程切換)
- 驗證關鍵參數的完整性
Validate critical parameters.
- 在進程切換之間驗證路徑的checksum
### Incomplete Mediation
程式或系統未對用戶輸入進行充分的檢查與過濾時,可能導致錯誤或漏洞。
FIX
- 在客戶端編寫代碼檢查輸入的正確性
- 限制用戶的選擇,只能選擇有效的選項(例如,使用下拉選單)。
BUT STILL vulnerable
前端驗證結果可通過 URL 獲取
惡意用戶可以訪問並修改這些字段
只有當修改完成後才會發送給服務器
服務器無法區分 URL 是來自用戶瀏覽器還是惡意用戶
### COMBINATION
Use in Combination, can be used
Example: Attacker can
- Use buffer overflow to disrupt code execution
- Use TOCTTOU to add a new user to system
- Use incomplete mediation to achieve privileged
status
## NetWork

---



---

### Promiscuous Mode
在Promiscuous Mode下,網卡(Network Interface Card,NIC)會接收網絡中所有的封包,而不僅僅是那些發送給自己的封包。
可以用作網路監聽,分析封包,攔截密碼
#### Wireless
easy to sniff
- 訊號散播 不受物理線材的限制
Wardriving(尋找開放式網絡)指駕車或步行通過移動設備尋找開放式無線網絡的行為
#### defense
很難遠程檢測
- Baiting(誘餌): 發送具有誘餌特性的封包,測試是否被攻擊者捕獲。
- Ping Delay(延遲測試): 觀察回應延遲,判斷網卡是否處於異常狀態(如可能沒有傳輸線纜的情況)
- 主機檢測
- ifconfig or cpm.c
- big log file or CPU load
- Routing and Bridging 限制傳輸範圍
- Switches/VLANs instead of Hubs
- 交換機(Switches):與集線器(Hubs)不同,交換機只將數據轉發給目標設備,大幅降低被嗅探的可能性。
- VLAN: 將網絡劃分為不同的邏輯子網,限制封包的傳播範圍。
- 加密
#### Sniffer Baiting(誘餌策略)
在網絡中傳輸「誘人的」封包,例如帶有明文密碼的登錄信息。
將誘餌數據編碼在特定字段中,例如 password。
等待攻擊者使用這些數據登錄到蜜罐(Honeypot),然後進行分析。


### ARP Poisoning
ARP(Address Resolution Protocol)
ARP 用於將 IP 地址映射到 MAC 地址。
當某設備需要發送封包給同一局域網內的其他設備時,若沒有對應的 MAC 地址,會廣播一個 ARP 請求來獲取。

**Man-in-the-Middle Attack**
攔截流量後修改通信內容,例如網頁內容
### tcpdump
- 一個命令行工具,用於分析網絡或協議問題。
- 通過捕獲和顯示網絡數據包的信息,幫助診斷網絡問題或監控網絡流量。
- 適合進行輕量級的網絡監控和數據分析。
#### 特性
- 可以通過命令行選項選擇需要監控的特定協議,例如 TCP、UDP、ICMP 等
- 數據包都可以以 十六進制格式(Hex) 顯示
### IP impersonation on a LAN
- 本地 IP 地址要求:
攻擊者需要在局域網內使用一個本地的 IP 地址進行偽裝。
這樣可以欺騙其他主機,使它們的流量被重定向到攻擊者。
IP 地址的配置:
- 通過手動配置,可以輕鬆更改自己的 IP 地址以匹配目標設備的 IP 地址。
- 攻擊方式:
拒絕服務攻擊(DoS): 創建一個虛假的 IP 包,偽造源地址並直接寫入乙太網驅動程序,干擾目標通信。
### IP Vulnerabilities


### IP Fragmentation Attacks
當數據包的大小超過 MTU(最大傳輸單元)時,IP 協議會將其分片,以便通過支持較小數據包的網絡鏈路。
Fragment Offset(分片偏移量):
- 指示該分片在原始數據中的位置。
Flags(標誌位):
- 包括 MF(More Fragments,還有更多分片)和 DF(Don't Fragment,不進行分片)標誌。



### IP Source Routing
允許發送方在數據包中指定其傳輸路徑(經由哪些路由器和網絡到達目標)(TO/FROM HOST)
#### Source Address Spoofing
- remote hacker spoofs source address to that of trusted internal host
- internal hosts thinks it's a local (trusted) host, but source routing routes packet back to hacker's machine
1.攻擊者冒充受信任的主機,與目標設備進行通信
2.攻擊者指定數據包的源路由
3.回傳數據流量按照指定的路徑返回攻擊者設備,而不是正確的受信任主機

#### Counter method
- routers can (should) be configured to drop source routed packets
- tcpwrappers (host-based network ACLs) also drop such packets(網絡訪問控制列表)
### TCP
Three way handshake
- SYN flooding -- denial of service
consumes server resources
- Land.c attack SYN with src and dst IP
the same
- Send FIN or RST to break a connection
- need to get sequence number right
- Do port scans to find services (nmap)
### Sequence number guessing (TCP)
- TCP 協議使用序列號來跟踪數據包的順序,確保數據的可靠傳輸。
- 每次建立新連接時,通信雙方會交換初始序列號(ISN,Initial Sequence Number),並隨後根據數據包的大小遞增。



- 探測目標(Probe Target):
- 攻擊者向目標發送多個 TCP SYN 請求,記錄返回的 SYN-ACK 中的初始序列號。
- 推測序列號(Guess Sequence Number):
- 通過分析序列號的增量規律,推測下一個序列號。
- 偽裝受信任主機(Spoof Trusted Host):
- 攻擊者偽造受信任主機的 IP 地址,向目標主機發送帶有正確序列號的 TCP 包。
- 建立偽裝連接:
- 如果序列號正確,目標主機會接受連接,攻擊者即可進行未授權操作。
- 會話預測(Session Flow Prediction):
- 攻擊者需要預測會話的完整數據流,因為其無法直接接收服務器的回應包。
#### Counter method
- new OS's, random seq. number
- router blocks local from external
### Session Hijacking(TCP)
- 攻擊者位於通信路徑中(Man-in-the-Middle,MITM):
- 攻擊者監視初始的會話建立過程(例如 TCP 三次握手)。
- 嗅探初始數據(Sniff Initial Session Establishment):
- 攻擊者捕獲用戶和服務器之間交換的序列號和其他通信參數。
- 重置客戶端(Reset Client):
- 攻擊者向客戶端發送 TCP RST(重置包),使其與服務器斷開連接。
- 接管會話(Take Over Session):
- 攻擊者通過模擬客戶端的身份與服務器通信,並完全接管會話。
即使是基於strong-authenticated session(如 SKey、SecureID)的會話,攻擊者也可以在認證完成後劫持會話。
#### counter method
使用加密的通信協議(如 SSH),防止攻擊者嗅探數據
### UDP
- fraggle (brother of smurf) attack
- 原理:
- 利用 UDP 廣播,將數據包發送到端口 7(echo)、19(chargen)或 135(Windows RPC)。
- 數據包的源地址偽裝為受害者 IP 地址。
- 影響:
- 廣播引發大量回應,淹沒受害者的網絡(類似於 DDoS
- UDP Bomb
- 創建非法的 UDP 數據包(如數據包長度小於 IP 頭部長度)
- 某些舊操作系統無法處理這種非法數據包,導致 IP STACK 崩潰。
### DNS
域名轉換IP地址
- DNS 中毒(DNS Poisoning):
- 攻擊者向 DNS 緩存注入惡意數據,使用戶訪問錯誤的 IP 地址。
- 攔截與回應(First Responder):
- 攔截 DNS 請求並提供偽造的響應,將用戶引導至惡意網站。
- 冒充可信主機(Impersonation):
- 假冒合法的 DNS 伺服器或域名,竊取用戶敏感數據。
- 拒絕服務攻擊(Denial of Service, DoS):
- 向 DNS 伺服器發送大量請求,導致其無法處理正常流量
#### DNS poisoning




### Denial of Service (DoS) Attacks
- Flooding or “poison packet”
- overload service/net, e.g. SYN attack
- crash server or your machine
- overload DNS, routers, servers
- usually done with bogus(偽造) source IP address(es)
- difficult to block/filter
#### 2nd order denial of service
- 攻擊者設計惡意流量,觸發目標系統的自動防禦機制(如自動阻止 IP 或關閉服務),將這些合法 IP 加入黑名單。
- 防禦系統誤判,將合法用戶的訪問也阻止,進而對合法用戶產生拒絕服務的效果
### Distributed denial of service attacks (DDoS)
Botnets(殭屍網路)


#### counter method
hard:
流量必須是活躍狀態,才能進行追踪。
必須進入目標網絡路由器,並逐層回溯到來源。
需要跨越管理邊界與國家邊界
