fengyun-agent-task 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. fengyun_agent_task-0.1.0/LICENSE +21 -0
  2. fengyun_agent_task-0.1.0/PKG-INFO +383 -0
  3. fengyun_agent_task-0.1.0/README.md +357 -0
  4. fengyun_agent_task-0.1.0/pyproject.toml +70 -0
  5. fengyun_agent_task-0.1.0/setup.cfg +4 -0
  6. fengyun_agent_task-0.1.0/src/fengyun_agent_task.egg-info/PKG-INFO +383 -0
  7. fengyun_agent_task-0.1.0/src/fengyun_agent_task.egg-info/SOURCES.txt +25 -0
  8. fengyun_agent_task-0.1.0/src/fengyun_agent_task.egg-info/dependency_links.txt +1 -0
  9. fengyun_agent_task-0.1.0/src/fengyun_agent_task.egg-info/entry_points.txt +2 -0
  10. fengyun_agent_task-0.1.0/src/fengyun_agent_task.egg-info/requires.txt +2 -0
  11. fengyun_agent_task-0.1.0/src/fengyun_agent_task.egg-info/top_level.txt +1 -0
  12. fengyun_agent_task-0.1.0/src/task/SQLITE_SCHEMA.md +220 -0
  13. fengyun_agent_task-0.1.0/src/task/__init__.py +1 -0
  14. fengyun_agent_task-0.1.0/src/task/analyze.py +96 -0
  15. fengyun_agent_task-0.1.0/src/task/api_repository.py +336 -0
  16. fengyun_agent_task-0.1.0/src/task/cli.py +127 -0
  17. fengyun_agent_task-0.1.0/src/task/client.py +89 -0
  18. fengyun_agent_task-0.1.0/src/task/config.py +55 -0
  19. fengyun_agent_task-0.1.0/src/task/db.py +103 -0
  20. fengyun_agent_task-0.1.0/src/task/detectors.py +59 -0
  21. fengyun_agent_task-0.1.0/src/task/import_sample_data.py +62 -0
  22. fengyun_agent_task-0.1.0/src/task/ingest_wss.py +83 -0
  23. fengyun_agent_task-0.1.0/src/task/json_utils.py +43 -0
  24. fengyun_agent_task-0.1.0/src/task/repository.py +284 -0
  25. fengyun_agent_task-0.1.0/src/task/schema.sql +75 -0
  26. fengyun_agent_task-0.1.0/src/task/submission_repository.py +108 -0
  27. fengyun_agent_task-0.1.0/src/task/submitter.py +73 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CncertAgent contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,383 @@
1
+ Metadata-Version: 2.4
2
+ Name: fengyun-agent-task
3
+ Version: 0.1.0
4
+ Summary: CncertAgent local WSS log cache, offline analysis, submission, and API adapter toolkit.
5
+ Author: CncertAgent contributors
6
+ License-Expression: MIT
7
+ Keywords: fengyun,security,logs,sqlite,wss
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Environment :: Console
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Security
19
+ Classifier: Topic :: System :: Logging
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: requests>=2.31.0
24
+ Requires-Dist: websocket-client>=1.7.0
25
+ Dynamic: license-file
26
+
27
+ # task 本地 WSS 缓存与离线分析工具
28
+
29
+ 该目录是独立实现。
30
+
31
+ ## 目标
32
+
33
+ - WSS 接收大量日志时优先本地落盘,避免在线分析拖慢接收。
34
+ - SQLite WAL 保存结构化缓存,支持多进程读写。
35
+ - JSONL 保存原始 WSS 消息和原始日志,便于灾备和复盘。
36
+ - 告警与攻击链先进入本地 outbox,再由提交命令统一提交。
37
+
38
+ ## 什么是 `--dry-run`
39
+
40
+ `--dry-run` 表示“试运行 / 演练模式”。
41
+
42
+ 适用命令:
43
+
44
+ - `submit-alerts`
45
+ - `submit-chains`
46
+ - `replay`
47
+
48
+ 开启 `--dry-run` 后会:
49
+
50
+ - 按 `--status`、`--kind`、`--limit` 等参数筛选本地待提交记录。
51
+ - 打印将要提交的 payload,便于检查内容。
52
+ - 不向远端服务端发送提交请求。
53
+ - 不更新 `submission_records` 中的提交状态。
54
+ - 不记录提交尝试。
55
+ - 不写入 `submitted_alerts.jsonl` / `submitted_chains.jsonl`。
56
+
57
+ 例如:
58
+
59
+ ```bash
60
+ python -m task.cli replay --kind all --status failed --dry-run
61
+ ```
62
+
63
+ 含义是:从本地提交记录中找出 `failed` 状态的告警和攻击链,打印将要重新提交的 payload,但不实际提交、不改变本地提交状态。
64
+
65
+ ## 命令总览
66
+
67
+ | 命令 | 用途 | 是否修改本地数据 | 是否提交到服务端 |
68
+ |---|---|---|---|
69
+ | `init-db` | 初始化 SQLite WAL 数据库 | 是 | 否 |
70
+ | `reset-db` | 删除并重建 SQLite 数据库 | 是,破坏性操作 | 否 |
71
+ | `status` | 查看本地缓存统计 | 否 | 否 |
72
+ | `receive` | 接收 WSS 日志并写入 JSONL + SQLite | 是 | 否 |
73
+ | `import-sample-data` | 导入样例日志 | 是 | 否 |
74
+ | `analyze-alerts` | 分析日志并生成本地告警候选 | 是 | 否 |
75
+ | `analyze-chains` | 基于告警候选生成攻击链候选 | 是 | 否 |
76
+ | `submit-alerts` | 提交本地告警 outbox | 会更新提交状态 | 是,除非 `--dry-run` |
77
+ | `submit-chains` | 提交本地攻击链 outbox | 会更新提交状态 | 是,除非 `--dry-run` |
78
+ | `replay` | 重放 pending/failed/submitted 提交记录 | 会更新提交状态 | 是,除非 `--dry-run` |
79
+ | `show-log` | 打印本地日志原文 | 否 | 否 |
80
+ | `show-chain` | 打印本地攻击链日志列表 | 否 | 否 |
81
+
82
+ ## 命令参考
83
+
84
+ 所有命令都通过以下入口执行:
85
+
86
+ ```bash
87
+ python -m task.cli <command> [args]
88
+ ```
89
+
90
+ ### `init-db`
91
+
92
+ 初始化本地 SQLite 数据库,并启用 WAL 模式。
93
+
94
+ ```bash
95
+ python -m task.cli init-db
96
+ ```
97
+
98
+ 参数:无。
99
+
100
+ ### `reset-db`
101
+
102
+ 删除并重建本地 SQLite 数据库。
103
+
104
+ ```bash
105
+ python -m task.cli reset-db --yes
106
+ ```
107
+
108
+ 参数:
109
+
110
+ | 参数 | 默认值 | 说明 |
111
+ |---|---:|---|
112
+ | `--yes` | `False` | 确认删除旧 SQLite 数据库;不提供会拒绝执行。 |
113
+
114
+ 注意:这是破坏性操作,会删除:
115
+
116
+ - `task/data/log_cache.sqlite3`
117
+ - `task/data/log_cache.sqlite3-wal`
118
+ - `task/data/log_cache.sqlite3-shm`
119
+
120
+ 不会删除 JSONL 原始备份文件。
121
+
122
+ ### `status`
123
+
124
+ 查看本地缓存统计,输出 JSON 格式统计信息。
125
+
126
+ ```bash
127
+ python -m task.cli status
128
+ ```
129
+
130
+ 参数:无。
131
+
132
+ ### `receive`
133
+
134
+ 接收 WSS 日志流,写入 JSONL 原始备份和 SQLite 本地缓存。
135
+
136
+ ```bash
137
+ python -m task.cli receive
138
+ python -m task.cli receive --max-batches 1
139
+ ```
140
+
141
+ 参数:
142
+
143
+ | 参数 | 类型 | 默认值 | 说明 |
144
+ |---|---|---:|---|
145
+ | `--max-batches` | int | `0` | 最多接收多少个 `log_batch`;`0` 表示不限。 |
146
+
147
+ 说明:该命令只负责接收和落盘,不做告警分析,也不提交到服务端。
148
+
149
+ ### `import-sample-data`
150
+
151
+ 导入样例 JSON 日志到 SQLite。
152
+
153
+ ```bash
154
+ python -m task.cli import-sample-data
155
+ python -m task.cli import-sample-data --data-dir data
156
+ python -m task.cli import-sample-data --data-dir data --no-raw-backup
157
+ ```
158
+
159
+ 参数:
160
+
161
+ | 参数 | 类型 | 默认值 | 说明 |
162
+ |---|---|---:|---|
163
+ | `--data-dir` | string | `data` | 样例数据目录。 |
164
+ | `--no-raw-backup` | flag | `False` | 只导入 SQLite,不追加写入 `raw_messages.jsonl` / `raw_logs.jsonl`。 |
165
+
166
+ 默认会同时追加写入 `raw_messages.jsonl` 和 `raw_logs.jsonl` 原始备份。
167
+
168
+ ### `analyze-alerts`
169
+
170
+ 分析未处理日志,生成本地告警候选。
171
+
172
+ ```bash
173
+ python -m task.cli analyze-alerts
174
+ python -m task.cli analyze-alerts --limit 10000
175
+ ```
176
+
177
+ 参数:
178
+
179
+ | 参数 | 类型 | 默认值 | 说明 |
180
+ |---|---|---:|---|
181
+ | `--limit` | int | `1000` | 本轮最多分析多少条未处理日志。 |
182
+
183
+ 说明:该命令只生成本地告警候选,不直接提交到服务端。
184
+
185
+ ### `analyze-chains`
186
+
187
+ 基于本地告警候选生成攻击链候选。
188
+
189
+ ```bash
190
+ python -m task.cli analyze-chains
191
+ python -m task.cli analyze-chains --max-logs-per-chain 2000
192
+ ```
193
+
194
+ 参数:
195
+
196
+ | 参数 | 类型 | 默认值 | 说明 |
197
+ |---|---|---:|---|
198
+ | `--max-logs-per-chain` | int | `2000` | 每条候选攻击链最多纳入多少条日志。 |
199
+
200
+ 说明:该命令只生成本地攻击链候选,不直接提交到服务端。
201
+
202
+ ### `submit-alerts`
203
+
204
+ 提交本地告警 outbox。
205
+
206
+ ```bash
207
+ python -m task.cli submit-alerts
208
+ python -m task.cli submit-alerts --dry-run --limit 10
209
+ python -m task.cli submit-alerts --status failed --limit 100
210
+ ```
211
+
212
+ 参数:
213
+
214
+ | 参数 | 类型 | 默认值 | 可选值 | 说明 |
215
+ |---|---|---:|---|---|
216
+ | `--limit` | int | `100` | 任意整数 | 本次最多处理多少条告警提交记录。 |
217
+ | `--status` | string | `pending` | `pending` / `failed` / `submitted` | 筛选要处理的提交状态。 |
218
+ | `--dry-run` | flag | `False` | - | 只打印 payload,不实际提交、不更新提交状态。 |
219
+
220
+ ### `submit-chains`
221
+
222
+ 提交本地攻击链 outbox。
223
+
224
+ ```bash
225
+ python -m task.cli submit-chains
226
+ python -m task.cli submit-chains --dry-run
227
+ python -m task.cli submit-chains --status failed --limit 50
228
+ ```
229
+
230
+ 参数:
231
+
232
+ | 参数 | 类型 | 默认值 | 可选值 | 说明 |
233
+ |---|---|---:|---|---|
234
+ | `--limit` | int | `100` | 任意整数 | 本次最多处理多少条攻击链提交记录。 |
235
+ | `--status` | string | `pending` | `pending` / `failed` / `submitted` | 筛选要处理的提交状态。 |
236
+ | `--dry-run` | flag | `False` | - | 只打印 payload,不实际提交、不更新提交状态。 |
237
+
238
+ ### `replay`
239
+
240
+ 重放已保存在本地 outbox 中的提交 payload。
241
+
242
+ ```bash
243
+ python -m task.cli replay
244
+ python -m task.cli replay --kind all --status failed --dry-run
245
+ python -m task.cli replay --kind alert --status failed --limit 20
246
+ python -m task.cli replay --kind chain --status pending
247
+ ```
248
+
249
+ 参数:
250
+
251
+ | 参数 | 类型 | 默认值 | 可选值 | 说明 |
252
+ |---|---|---:|---|---|
253
+ | `--kind` | string | `all` | `all` / `alert` / `chain` | 重放类型:全部、仅告警、仅攻击链。 |
254
+ | `--status` | string | `failed` | `pending` / `failed` / `submitted` | 选择要重放的提交状态。 |
255
+ | `--limit` | int | `100` | 任意整数 | 每类最多处理多少条;`--kind all` 时 alert 和 chain 分别最多处理该数量。 |
256
+ | `--dry-run` | flag | `False` | - | 只打印将要重放的 payload,不实际提交、不更新状态。 |
257
+
258
+ 说明:`replay` 是“重试/重放已保存的提交 payload”,不是重新接收日志,也不是重新分析日志。
259
+
260
+ 常用失败重放流程:
261
+
262
+ ```bash
263
+ python -m task.cli replay --kind all --status failed --dry-run
264
+ python -m task.cli replay --kind all --status failed
265
+ ```
266
+
267
+ 第一条先预览将要重新提交的内容;第二条才真正提交。
268
+
269
+ ### `show-log`
270
+
271
+ 打印本地日志原文 JSON。
272
+
273
+ ```bash
274
+ python -m task.cli show-log WAF0000001
275
+ ```
276
+
277
+ 参数:
278
+
279
+ | 参数 | 类型 | 说明 |
280
+ |---|---|---|
281
+ | `log_id` | positional string | 要查看的本地日志 ID。 |
282
+
283
+ ### `show-chain`
284
+
285
+ 打印本地攻击链关联日志列表。
286
+
287
+ ```bash
288
+ python -m task.cli show-chain CHAIN_LOCAL_xxx
289
+ ```
290
+
291
+ 参数:
292
+
293
+ | 参数 | 类型 | 说明 |
294
+ |---|---|---|
295
+ | `chain_id` | positional string | 要查看的本地攻击链 ID。 |
296
+
297
+ 输出内容包括时间、日志源、日志 ID。
298
+
299
+ ## 推荐使用流程
300
+
301
+ ### 1. 初始化
302
+
303
+ ```bash
304
+ python -m task.cli init-db
305
+ python -m task.cli status
306
+ ```
307
+
308
+ ### 2. 导入样例数据
309
+
310
+ ```bash
311
+ python -m task.cli import-sample-data --data-dir data
312
+ ```
313
+
314
+ 如果只想导入 SQLite,不追加 JSONL 备份:
315
+
316
+ ```bash
317
+ python -m task.cli import-sample-data --data-dir data --no-raw-backup
318
+ ```
319
+
320
+ SQLite schema 和字段含义见:
321
+
322
+ ```text
323
+ task/SQLITE_SCHEMA.md
324
+ ```
325
+
326
+ ### 3. 接收日志
327
+
328
+ ```bash
329
+ python -m task.cli receive
330
+ ```
331
+
332
+ 联调只接收一批:
333
+
334
+ ```bash
335
+ python -m task.cli receive --max-batches 1
336
+ ```
337
+
338
+ ### 4. 分析和提交
339
+
340
+ ```bash
341
+ python -m task.cli analyze-alerts --limit 10000
342
+ python -m task.cli submit-alerts --dry-run --limit 10
343
+ python -m task.cli submit-alerts --limit 200
344
+
345
+ python -m task.cli analyze-chains
346
+ python -m task.cli submit-chains --dry-run
347
+ python -m task.cli submit-chains
348
+ ```
349
+
350
+ ### 5. 复盘查询
351
+
352
+ ```bash
353
+ python -m task.cli show-log WAF0000001
354
+ python -m task.cli show-chain CHAIN_LOCAL_xxx
355
+ ```
356
+
357
+ ## 常用环境变量
358
+
359
+ ```bash
360
+ export GX_SERVER=https://172.17.35.21:18080
361
+ export GX_TEAM_ID=TEAM001
362
+ export GX_TOKEN=team001-demo-token
363
+ export GX_VERIFY_CERT=0
364
+ export TASK_DATA_DIR=task/data
365
+ ```
366
+
367
+ ## 代码结构
368
+
369
+ - `config.py`:环境变量、默认路径、接口 endpoint 和日志源映射。
370
+ - `db.py`:SQLite 连接、初始化、轻量迁移和重置。
371
+ - `json_utils.py`:JSON/JSONL 序列化、容错解析和原始备份写入。
372
+ - `repository.py`:本地 SQLite 仓储主入口,负责日志入库/查询、攻击链元数据、payload 构造和 CLI/离线流程兼容导出。
373
+ - `submission_repository.py`:`submission_records` outbox、提交记录和重试状态。
374
+ - `analyze.py`:本地黑日志分析和攻击链聚合入口。
375
+ - `api_repository.py`:Flask 后端和前端 API 兼容适配层。
376
+ - `cli.py`:命令行入口,包含 replay、show-log、show-chain 等 CLI 辅助命令。
377
+
378
+ ## 重要说明
379
+
380
+ - `raw_messages.jsonl` 和 `raw_logs.jsonl` 是 append-only 原始备份,不会自动删除。
381
+ - WSS 接收命令只做落盘,不做网络提交。
382
+ - 每个命令单独打开 SQLite 连接,不要跨进程共享连接。
383
+ - 第一版检测和攻击链逻辑是基线启发式,后续可以在 `detectors.py` 和 `analyze.py` 中增强。