Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# WAFfl

[English|[繁體中文](./README_Zh-TW.md)]

A dynamic rule-based Web Application Firewall written in Rust.

- Custom rule engine with configurable security policies.
Expand Down
51 changes: 0 additions & 51 deletions README_Zh-TW.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,3 @@
# WAFfl

[[English](./README.md)|繁體中文]

一個使用 Rust 編寫的動態規則型網頁應用防火牆(WAF)。

- 自訂規則引擎,支援可配置的安全策略。
- 以 Rust 提供輕量且高效的效能。
- 透過 `rules.conf` 彈性定義規則。

## 安全規則

### 範例規則

以下範例規則會拒絕任何在請求 URL 中包含關鍵字 `admin` 的流量。

請在專案根目錄下建立 `rules.conf` 檔案,WAFfl 會在編譯階段加載規則。

```conf
SecRuleEngine On

SecRule REQUEST_URI "@rx admin" "id:1,phase:1,deny,status:401"
```

## 相依套件

`coreruleset`

## 系統需求

- `libmodsecurity` 版本 >= 3.0.13
- `xorg-server-xvfb` 版本 >= 21.1.16

## 測試

### 規則更新

要測試規則更新,可以使用以下指令:

```shell
xvfb-run -a --server-args="-screen 0 1920x1080x24" python3 src/api/main.py
```

## 建置

使用 Cargo 在 Release 模式下編譯 WAFfl:

```shell
cargo build --release
```

## 授權

本專案採用 MIT 授權條款。
102 changes: 48 additions & 54 deletions docs/Nginx_zh-TW.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,27 @@
# Nginx 架設與設定
以Arch Linux、Ubuntu為例

以 Arch、Ubuntu 為例。

## 安裝

Ubuntu

## 在Ubuntu上架設與設定Nginx
### 安裝Nginx
```bash
sudo apt update
sudo apt install nginx -y
```

Arch

```bash
sudo pacman -Syu nginx
```

## Nginx 服務設定

> [!NOTE]
> Arch 跟 Ubuntu 24.04 應該都能用 `systemctl`。

啟動服務:

### 啟動並設定開機時自動開啟
```bash
sudo systemctl enable nginx
sudo systemctl start nginx
```

開機時自動啟動:

### 檢查Nginx狀態
```bash
sudo systemctl enable nginx
sudo systemctl status nginx
```

檢查 Nginx 狀態:

### 撰寫設定
若這個檔案/資料夾不存在,則自行建立一個
#### /etc/nginx/sites-available/default
```bash
sudo systemctl status nginx
sudo vim /etc/nginx/sites-available/default
```

## 防火牆設定

### default

> [!NOTE]
> 若 `/etc/nginx/sites-available/default` 不存在,則自行建立一個
>
> ```bash
> sudo touch /etc/nginx/sites-available/default
> ```

```conf
server {
listen 80;
Expand All @@ -73,9 +44,7 @@ server {
# 2. 接著若請求非惡意,就傳給要保護的伺服器
location /backend {
internal;
rewrite /backend/(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand All @@ -90,27 +59,52 @@ server {
}
}
```

### nginx.conf

> [!NOTE]
> 若 `/etc/nginx/nginx.conf` 不存在,則自行建立一個
>
> ```bash
> sudo touch /etc/nginx/nginx.conf
> ```

#### /etc/nginx/nginx.conf
```bash
sudo vim /etc/nginx/nginx.conf
```
```conf
...
http {
...
include /etc/nginx/sites-available/default;
...
}
```

## 測試與更新

### 測試與更新
```bash
sudo nginx -t
sudo systemctl restart nginx
```

## 在Arch上架設與設定Nginx
### 安裝Nginx
```bash
sudo pacman -Syu nginx
```
### 啟動並設定開機時自動開啟
```bash
sudo systemctl enable nginx
sudo systemctl start nginx
```
### 檢查Nginx狀態
```bash
sudo systemctl status nginx
```
### 撰寫設定
若這個檔案/資料夾不存在,則自行建立一個
/etc/nginx/sites-available/default (參見上方同名檔案範例)
```bash
sudo vim /etc/nginx/sites-available/default
```
/etc/nginx/nginx.conf (參見上方同名檔案範例)
```bash
sudo vim /etc/nginx/nginx.conf
```

### 測試與更新
```bash
sudo nginx -t
sudo systemctl restart nginx
```
42 changes: 3 additions & 39 deletions src/api/README_Zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,20 @@

### 安裝依賴項

> [!NOTE]
> 以 Arch 為例。

安裝系統依賴項

```shell
sudo pacman -Syu xorg-server-xvfb
```

安裝 Python 第三方擴展元件

```shell
pip install -r requirement.txt
```

安裝 git-submodules
Arch:

```shell
git submodule update --init --recursive
sudo pacman -Syu xorg-server-xvfb && pip install -r requirement.txt
```

> [!NOTE] 一次搞定
>
> ```shell
> sudo pacman -Syu xorg-server-xvfb &&\
> git submodule update --init --recursive &&\
> git submodule update --init --recursive
> ```

### 規則集更新

欲更新規則集,請跟隨以下步驟:

> [!WARNING]
> 假設你輸入 `pwd` 時會跟以下顯示一樣:
>
> ```shell
> $ pwd | grep -iq '/WAFfl$' && echo "Correct directory" || echo "Wrong directory"
> Correct directory
> ```

1. 在專案根目錄 `/WAFfl` 底下新增檔案 `rules.conf`

```shell
touch rules.conf
touch /WAFfl/rules.conf
```

2. 執行以下指令
Expand All @@ -69,8 +38,3 @@ git submodule update --init --recursive
```

3. 等待約30秒即能在專案跟目錄找到 `api_records_type_sensitive.json`
4. 更新 `rules.conf`

```shell
python3 src/api/rules.py
```
2 changes: 0 additions & 2 deletions src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from fetch import FetchUtil
from parser import Parser
from mapping import Directory
from rules import update_rule


def main(config: dict[str, Any]):
Expand Down Expand Up @@ -36,4 +35,3 @@ def main(config: dict[str, Any]):
with open(Directory.CONFIG.value, "rb") as config:
config: dict[str, Any] = tomllib.load(config)
main(config)
update_rule()
8 changes: 4 additions & 4 deletions src/api/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Parser:
def __init__(self, html_raw_data: str) -> None:
"""
解析 HTML,將漏洞資料轉換為 API 格式

Parameters
---
html_raw_data: str
Expand All @@ -35,7 +35,7 @@ def __find_endpoint(self, data: str) -> List[tuple[str, str]]:
---
data: str
HTML 原始碼

Return
---
List[tuple[str, str]]
Expand All @@ -52,7 +52,7 @@ def __find_date(self, data: str) -> List[str]:
---
data: str
HTML 原始碼

Return
---
List[str]
Expand All @@ -64,7 +64,7 @@ def __find_date(self, data: str) -> List[str]:
def sort_by_date(self, api_data: Dict[int, Dict[str, str]]) -> Dict[int, list]:
"""
依年份與月份整理漏洞資料

Parameters
---
api_data: Dict[int, Dict[str, str]]
Expand Down
8 changes: 4 additions & 4 deletions src/api/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class RuleUtil:
---
RuleUtil()
初始化規則管理工具,設定來源路徑與目的路徑

Methods
---
extract_rule()
擷取 `.conf` 規則檔案,可選擇擷取所有規則或僅擷取符合 API 記錄的規則。
rule_dump()
rule_dump()
把規則寫入進規則檔中,如果有 `overwrite` 則不產生備份檔
"""

Expand Down Expand Up @@ -119,7 +119,7 @@ def rule_dump(self, overwrite: bool = False) -> None:
self.rules = []


def update_rule():
def main():
with open(Directory.CONFIG.value, "rb") as config:
config: dict[str, Any] = tomllib.load(config)

Expand All @@ -129,4 +129,4 @@ def update_rule():


if __name__ == "__main__":
update_rule()
main()
2 changes: 1 addition & 1 deletion src/api/v_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, api_record_raw: Dict[str, List[Dict]]) -> None:
---
api_record: Dict[str, List[Dist]]
原始 API 資料

Returns
---
None
Expand Down
Loading