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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "include/coreruleset"]
path = include/coreruleset
url = https://github.com/coreruleset/coreruleset
[submodule "include/gotestwaf"]
path = include/gotestwaf
url = https://github.com/wallarm/gotestwaf.git
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ SecRule REQUEST_URI "@rx admin" "id:1,phase:1,deny,status:401"

## Dependency

`coreruleset`
### git modules

- `coreruleset`
- `gotestwaf`

Install all modules:

```shell
git submodule update --init --recursive
```

## Requirements

Expand Down
11 changes: 10 additions & 1 deletion README_Zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ SecRule REQUEST_URI "@rx admin" "id:1,phase:1,deny,status:401"

## 相依套件

`coreruleset`
### git modules

- `coreruleset`
- `gotestwaf`

安裝所有的模組:

```shell
git submodule update --init --recursive
```

## 系統需求

Expand Down
3 changes: 2 additions & 1 deletion api_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ debug_flag = false
quantity = 5

[rule]
overwrite = false
overwrite = true
append_type = "mapping"
1 change: 1 addition & 0 deletions include/gotestwaf
Submodule gotestwaf added at 68f243
68 changes: 42 additions & 26 deletions src/api/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,39 @@

class FetchUtil:
"""
負責擷取漏洞情資網站情報

### 成員函數
- fetching() -> str
負責擷取漏洞情資網站情報。

屬性
----------
URL_BASE : str
目標網站的基礎 URL。
ENDPOINT : str
漏洞資訊的 API 端點。
ROUTE : str
個別漏洞頁面的路由。
"""

@staticmethod
def __fetch_page(driver: Driver, url: str, output_name: str, debug_flag: bool) -> str:
"""
使用提供的 driver 抓取單一頁面並返回其 HTML。

如果 `debug_flag` 為 True,則保存 HTML 和截圖。

### 參數
- driver (Driver)
- url (str)
- output_name (str)
- debug_flag (bool)

### 回傳
str: HTML 原始碼
"""使用提供的 driver 抓取單一頁面並返回其 HTML。

如果 `debug_flag` 為 True,則會保存 HTML 和截圖。

參數
----------
driver : Driver
用於抓取的 SeleniumBase Driver 實例。
url : str
要抓取頁面的 URL。
output_name : str
保存 HTML 和截圖檔案的基本名稱。
debug_flag : bool
一個布林標誌,用於啟用保存 HTML 和截圖以進行除錯。

回傳
-------
str
抓取頁面的 HTML 原始碼。
"""
driver.uc_open_with_reconnect(url, 6)
driver.uc_gui_click_captcha()
Expand All @@ -43,17 +55,21 @@ def __fetch_page(driver: Driver, url: str, output_name: str, debug_flag: bool) -

@staticmethod
def fetching(quantity: int = 0, debug_flag: bool = False) -> str:
"""
使用 seleniumbase 開啟網頁,並取得指定數量的原始資料。
"""使用 seleniumbase 開啟網頁,並取得指定數量的原始資料。

若 `debug_flag` 為 True,將會在當前路徑下儲存網頁截圖與 HTML。

### 參數
- quantity (int): 擷取情資頁面數列
- debug_flag (bool): 測試旗標

### 回傳
str: 擷取成功的 HTML 原始碼
參數
----------
quantity : int, optional
擷取情資頁面的數量,預設為 0。
debug_flag : bool, optional
測試旗標,用於啟用保存截圖與 HTML 以進行除錯,預設為 False。

回傳
-------
str
所有成功擷取的 HTML 原始碼的串聯。
"""
raw_data: str = ""

Expand All @@ -68,4 +84,4 @@ def fetching(quantity: int = 0, debug_flag: bool = False) -> str:


if __name__ == "__main__":
FetchUtil.fetching(1, True)
FetchUtil.fetching(1, True)
71 changes: 38 additions & 33 deletions src/api/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@

class Parser:
"""
負責解析 HTML 內容,提取漏洞資料,並整理成 API 格式
負責解析 HTML 內容,提取漏洞資料,並整理成 API 格式。

屬性
----------
manifest : Dict[int, Dict[str, str]]
解析後的漏洞資訊,以索引為鍵,包含漏洞 ID、標題、URL 和日期等資訊的字典。
"""

def __init__(self, html_raw_data: str) -> None:
"""
解析 HTML,將漏洞資料轉換為 API 格式
初始化 Parser 實例,解析 HTML 並將漏洞資料轉換為 API 格式

Parameters
---
html_raw_data: str
HTML 原始碼
參數
----------
html_raw_data : str
包含漏洞資訊的 HTML 原始碼
"""
dates: List[str] = self.__find_date(html_raw_data)
endpoints: List[tuple[str, str]] = self.__find_endpoint(html_raw_data)
Expand All @@ -29,51 +34,51 @@ def __init__(self, html_raw_data: str) -> None:

def __find_endpoint(self, data: str) -> List[tuple[str, str]]:
"""
從 HTML 中擷取漏洞 ID 和標題
從 HTML 中擷取漏洞 ID 和標題

Parameters
---
data: str
HTML 原始碼
參數
----------
data : str
包含漏洞資訊的 HTML 原始碼

Return
---
回傳
-------
List[tuple[str, str]]
包含 (漏洞 ID, 標題) 的列表
包含 (漏洞 ID, 標題) 元組的列表。
"""
pattern: str = r'\/vulnerability\/([A-Z]+-\d{4}-\d{5})">(.+)<\/a><\/h4>'
return re.findall(pattern, data)

def __find_date(self, data: str) -> List[str]:
"""
從 HTML 中擷取漏洞發現日期
從 HTML 中擷取漏洞發現日期

Parameter
---
data: str
HTML 原始碼
參數
----------
data : str
包含漏洞資訊的 HTML 原始碼

Return
---
回傳
-------
List[str]
包含日期的列表,格式為 YYYY/MM/DD
包含日期字串的列表,格式為 YYYY/MM/DD
"""
pattern: str = r"</span>(\d{4}/\d{2}/\d{2})</li>"
return re.findall(pattern, data)

def sort_by_date(self, api_data: Dict[int, Dict[str, str]]) -> Dict[int, list]:
def sort_by_date(self, api_data: Dict[int, Dict[str, str]]) -> Dict[int, Dict[str, List[Dict[str, str]]]]:
"""
依年份與月份整理漏洞資料
依年份與月份整理漏洞資料

Parameters
---
api_data: Dict[int, Dict[str, str]]
原始漏洞資料
參數
----------
api_data : Dict[int, Dict[str, str]]
原始漏洞資料,鍵為索引,值為包含漏洞資訊的字典。

Return
---
Dict[int, list]
依年份、月份整理後的漏洞資料
回傳
-------
Dict[int, Dict[str, List[Dict[str, str]]]]
依年份和月份整理後的漏洞資料,外層鍵為年份,內層鍵為月份(字串格式),值為該年該月漏洞資訊的列表。
"""
sorted_data = defaultdict(lambda: {str(month): [] for month in range(1, 13)})

Expand All @@ -93,4 +98,4 @@ def sort_by_date(self, api_data: Dict[int, Dict[str, str]]) -> Dict[int, list]:
json.dump(parser.manifest, fp, ensure_ascii=False)

with open("api_records_time_sensitive.json", "w") as fp:
json.dump(parser.sort_by_date(parser.manifest), fp, ensure_ascii=False)
json.dump(parser.sort_by_date(parser.manifest), fp, ensure_ascii=False)
Loading