-
Notifications
You must be signed in to change notification settings - Fork 1
LogPipeline Development Guide
seungwon9201 edited this page Aug 20, 2025
·
1 revision
์น ์๋ฒ ๋ก๊ทธ์ ์ด์ ํ๋์ ํ์งํ๋ AI ๊ธฐ๋ฐ API ์์คํ ์ ๋๋ค. Isolation Forest ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์ฌ ์ค์๊ฐ์ผ๋ก ๋ก๊ทธ๋ฅผ ๋ถ์ํ๊ณ ์ด์ ์ ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
app/
โโโ main.py # FastAPI ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
โโโ api/
โ โโโ predict.py # ์์ธก API ์๋ํฌ์ธํธ
โโโ core/
โ โโโ model.py # ๋ชจ๋ธ ๋ก๋ฉ ํจ์
โ โโโ scaler.py # ์ ์ ์ค์ผ์ผ๋ง ํจ์
โ โโโ ioc.py # IoC ํจํด ๋ก๋ฉ
โโโ utils/
โ โโโ parser.py # ๋ก๊ทธ ํ์ฑ ์ ํธ๋ฆฌํฐ
โโโ model/
โโโ isolation_model.pkl # ํ๋ จ๋ Isolation Forest ๋ชจ๋ธ
โโโ features.pkl # ํผ์ฒ ๋ฆฌ์คํธ
โโโ method_cols.pkl # HTTP ๋ฉ์๋ ์ปฌ๋ผ ์ ๋ณด
โโโ ioc_keywords.pkl # IoC(Indicator of Compromise) ํค์๋
โโโ scaler_params.json # ์ ์ ์ค์ผ์ผ๋ง ํ๋ผ๋ฏธํฐ
notebook/
โโโ *.ipynb # ๋ชจ๋ธ ํ๋ จ ๋ฐ ์คํ ๋
ธํธ๋ถ
- ์๊ณ ๋ฆฌ์ฆ: Isolation Forest
- ์ ๋ ฅ: ์น ์๋ฒ ๋ก๊ทธ (JSON ํํ)
- ์ถ๋ ฅ: ์ด์ ์ ์ (0-100, ๋์์๋ก ์ ์)
์ถ์ถ๋๋ ์ฃผ์ ํผ์ฒ๋ค:
- URL ๊ด๋ จ: ๊ธธ์ด, ๊น์ด, ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์กด์ฌ ์ฌ๋ถ, ํน์ ๋ฌธ์ ๊ฐ์
- User-Agent ๊ด๋ จ: ๊ธธ์ด
- HTTP ์ํ: ์ํ ์ฝ๋
- ํฌ๊ธฐ: ์๋ต ๋ฐ์ดํธ ์
- Referer: ์กด์ฌ ์ฌ๋ถ
- IoC ํ์ง: URL/User-Agent์์ ์์ฌ์ค๋ฌ์ด ํจํด ๊ฐ์
- HTTP ๋ฉ์๋: One-hot ์ธ์ฝ๋ฉ
์ฌ์ ์ ์๋ ์ ์ฑ ํจํด๋ค์ ํ์ง:
- URL์์ ๋ฐ๊ฒฌ๋๋ ์์ฌ์ค๋ฌ์ด ํค์๋
- User-Agent์์ ๋ฐ๊ฒฌ๋๋ ์์ฌ์ค๋ฌ์ด ํจํด
POST /score
Content-Type: application/json
Content-Encoding: gzip (์ ํ์ฌํญ)
{
"method": "GET",
"url": "/index.html",
"statusCode": 200,
"bytesSent": 1024,
"referer": "https://example.com",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}[
{
"method": "GET",
"url": "/index.html",
"statusCode": 200,
"bytesSent": 1024,
"referer": "https://example.com",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
]{
"score": 85.43
}- 0-60: ์ ์์ ์ธ ํ๋
- 60-70: ์ฝ๊ฐ ์์ฌ์ค๋ฌ์ด ํ๋
- 70-100: ๋งค์ฐ ์ํํ ํ๋
pip install fastapi uvicorn scikit-learn joblib numpy requestsuvicorn app.main:app --host 0.0.0.0 --port 8000curl http://localhost:8000/
# ์๋ต: {"message": "AI Log API is running"}์๋ฒ ์์ ์ ๋ค์ ํ์ผ๋ค์ ๋ก๋:
-
isolation_model.pkl: ํ๋ จ๋ Isolation Forest ๋ชจ๋ธ -
features.pkl: ํผ์ฒ ์์ ์ ๋ณด -
method_cols.pkl: HTTP ๋ฉ์๋ ์-ํซ ์ธ์ฝ๋ฉ ์ปฌ๋ผ ์ ๋ณด
Isolation Forest์ raw decision function ๊ฐ์ 0-100 ์ค์ผ์ผ๋ก ๋ณํ:
def scale_score(raw_score, score_min, score_max):
raw_score = np.clip(raw_score, score_min, score_max)
norm_score = (raw_score - score_min) / (score_max - score_min)
inverted_score = 1 - norm_score # ๋์ ๊ฐ = ์ ์
return round(inverted_score * 100, 2)์ฌ์ ์ ์๋ ์ ์ฑ ํจํด๋ค๊ณผ ๋งค์นญํ์ฌ ์ถ๊ฐ ํผ์ฒ ์์ฑ
์ ์ ๊ณ์ฐ ํ http://127.0.0.1:9000/api/logs๋ก ๊ฒฐ๊ณผ ์ ์ก (์ ํ์ฌํญ)
- 400 Bad Request: ์๋ชป๋ JSON ํ์ ๋๋ ๋ค์ค ๋ก๊ทธ ๋ฐฐ์ด
- 422 Unprocessable Entity: ํ์ ํ๋ ๋๋ฝ
- 500 Internal Server Error: ๋ชจ๋ธ ์์ธก ์คํจ ๋๋ ๋ด๋ถ ์ฒ๋ฆฌ ์ค๋ฅ
๋ชจ๋ ์์ฒญ์ ๋ค์ ํ๋๋ค์ด ๋ฐ๋์ ํฌํจ๋์ด์ผ ํจ:
methodurlstatusCodebytesSentrefereruserAgent
-
notebook/๋๋ ํ ๋ฆฌ์ ๋ชจ๋ธ ํ๋ จ ๊ณผ์ ์ด ๋ด๊ธด Jupyter ๋ ธํธ๋ถ๋ค์ด ์์ - ์๋ก์ด ๋ฐ์ดํฐ๋ก ๋ชจ๋ธ์ ์ฌํ๋ จํ ๋ ํด๋น ๋ ธํธ๋ถ๋ค์ ์ฐธ๊ณ
- ๋ชจ๋ธ๊ณผ ํผ์ฒ ์ ๋ณด๋ ์๋ฒ ์์ ์ ํ ๋ฒ๋ง ๋ก๋
- ์ค๋ ๋ ์์ ์ฑ์ ์ํ Lock ์ฌ์ฉ
- gzip ์์ถ ์ง์์ผ๋ก ๋คํธ์ํฌ ํจ์จ์ฑ ๊ฐ์
- ์๋ก์ด IoC ํจํด์
ioc_keywords.pklํ์ผ์ ์ถ๊ฐ - ์๋ก์ด ํผ์ฒ๋
extract_features()ํจ์์์ ๊ตฌํ - ์ค์ผ์ผ๋ง ํ๋ผ๋ฏธํฐ๋
scaler_params.json์์ ์กฐ์