151 lines
7.5 KiB
Markdown
151 lines
7.5 KiB
Markdown
---
|
|
title: anomaly-detect (VictoriaLogs + ollama 기반 이상 트래픽 감지)
|
|
updated: 2026-04-08
|
|
tags: [security, crowdsec, victorialogs, ollama, gemma, anomaly]
|
|
---
|
|
|
|
> 코드: `gitea.inouter.com/kaffa/anomaly-detect` (private)
|
|
|
|
# anomaly-detect
|
|
|
|
[[crowdsec-safeline#~~ddos-detect (AI 행위 분석)~~ — 폐기 (2026-04-08)|폐기된 ddos-detect]] 후속. [[victorialogs|VictoriaLogs]]에 적재된 K3s 서울 APISIX access log를 5분마다 분석하여 봇/공격성 IP를 [[crowdsec-safeline|CrowdSec]]에 자동 ban으로 등록한다.
|
|
|
|
## 위치 / 사양
|
|
|
|
| 항목 | 값 |
|
|
|------|-----|
|
|
| 호스트 | incus-hp2 |
|
|
| 컨테이너 | `anomaly-detect` (default 프로젝트, Debian 13 trixie) |
|
|
| IP | 10.100.2.164 |
|
|
| 사양 | 1 vCPU, 512MB RAM, 5GB |
|
|
| 설치 경로 | `/opt/anomaly-detect/{venv,analyzer.py}`, `/etc/anomaly-detect/lapi.yaml`, `/var/lib/anomaly-detect/dedup.json` |
|
|
| systemd | `anomaly-detect.service` (oneshot) + `anomaly-detect.timer` (`OnCalendar=*:0/5`, `Persistent=true`, `RandomizedDelaySec=20`) |
|
|
|
|
## 데이터 흐름
|
|
|
|
```
|
|
[5분 주기 systemd timer]
|
|
↓
|
|
analyzer.py
|
|
├─ 1) https://vl.inouter.com — LogsQL: program:apisix log_type:access 지난 5분
|
|
├─ 2) per-IP 통계 게이트 (count/4xx/5xx/499/distinct paths)
|
|
├─ 3) 후보 N개 (default max 5)
|
|
├─ 4) 각 후보 → http://100.87.221.126:11434/api/generate (kaffa-macmini ollama)
|
|
│ 모델: gemma4:e4b (Q4_K_M, 8.0B), format=json
|
|
├─ 5) verdict=yes → CrowdSec LAPI alert POST
|
|
│ http://10.253.100.240:8080/v1/alerts
|
|
│ profiles.yaml의 default_ip_remediation이 자동 ban 생성
|
|
└─ 6) dedup.json에 처리 IP + 타임스탬프 기록 (24h 내 재처리 안 함)
|
|
```
|
|
|
|
## CrowdSec LAPI 등록
|
|
|
|
`anomaly-detect`라는 watcher machine을 jp1 crowdsec에 등록하고, credentials를 컨테이너 안 `/etc/anomaly-detect/lapi.yaml`에 저장:
|
|
|
|
```yaml
|
|
url: http://10.253.100.240:8080
|
|
login: anomaly-detect
|
|
password: <vault: secret/apps/anomaly-detect>
|
|
```
|
|
|
|
> [!warning] cscli machines add 함정
|
|
> `cscli machines add NAME --auto`는 default로 `/etc/crowdsec/local_api_credentials.yaml`을 덮어씀 — 이건 jp1 crowdsec **daemon 자체의 LAPI 클라이언트 설정**이라 덮어쓰면 daemon이 새 password로 LAPI 인증을 시도하면서 동기화가 깨짐. 반드시 `--file <별도 경로>` 옵션을 줘야 한다. 만약 실수로 덮어썼다면 `cscli machines add <default-machine-name> --auto --force --file /etc/crowdsec/local_api_credentials.yaml`로 default machine 새 credentials 발급 후 `systemctl reload crowdsec`로 복구.
|
|
|
|
## 통계 게이트 (환경변수로 조정)
|
|
|
|
| 변수 | default | 의미 |
|
|
|------|---------|------|
|
|
| `WINDOW_MIN` | 5 | LogsQL 윈도우 (분) |
|
|
| `GATE_MIN_REQS` | 30 | 윈도우 내 최소 요청 수 |
|
|
| `GATE_MIN_4XX_RATIO` | 0.5 | 4xx 비율 (count >= MIN_REQS와 AND) |
|
|
| `GATE_MIN_5XX_COUNT` | 10 | 5xx 최소 발생 |
|
|
| `GATE_MIN_499_COUNT` | 15 | 499 최소 발생 |
|
|
| `GATE_MIN_DISTINCT_PATH` | 20 | 서로 다른 path 최소 |
|
|
| `MAX_CANDIDATES` | 5 | 한 사이클에 ollama로 분석할 IP 상한 |
|
|
| `BAN_DURATION` | 4h | profiles.yaml이 아닌 alert 자체에서 전달하는 ban 기간 |
|
|
| `DEDUP_HOURS` | 24 | 같은 IP 재분석 방지 윈도우 |
|
|
|
|
게이트 통과 조건 (OR):
|
|
1. count ≥ MIN_REQS AND 4xx 비율 ≥ MIN_4XX_RATIO
|
|
2. 5xx ≥ MIN_5XX_COUNT
|
|
3. 499 ≥ MIN_499_COUNT
|
|
4. distinct path ≥ MIN_DISTINCT_PATH
|
|
|
|
사설망 IP(10/8, 127/8, 192.168/16, 172.16/12)는 자동 제외.
|
|
|
|
## ollama prompt
|
|
|
|
`format=json`으로 강제 + 명확한 schema:
|
|
|
|
```json
|
|
{"verdict": "yes" | "no", "reason": "<한 문장 한국어>"}
|
|
```
|
|
|
|
판단 근거는 system prompt에 명시 (반복 패턴, 머신 속도, 4xx/5xx 비율, path 열거, 알려진 스캐너 UA, 로그인 brute force, 비정상 rate). 정상 브라우저 패턴은 "no"로 분류.
|
|
|
|
## CrowdSec 자동 ban
|
|
|
|
profile 기반:
|
|
|
|
```
|
|
alert (machine=anomaly-detect, source.scope=Ip, remediation=true)
|
|
→ /etc/crowdsec/profiles.yaml의 default_ip_remediation에 매치
|
|
→ 자동으로 ban decision 4h 생성
|
|
→ bouncers (BunnyCDN, APISIX 서울/오사카, netbis-cf 등)가 다음 pull에 적용
|
|
```
|
|
|
|
## 운영 명령
|
|
|
|
```bash
|
|
# 컨테이너 진입
|
|
ssh incus-hp2 'incus exec anomaly-detect -- bash'
|
|
|
|
# 수동 1회 실행
|
|
incus exec anomaly-detect -- /opt/anomaly-detect/venv/bin/python /opt/anomaly-detect/analyzer.py
|
|
|
|
# 상태
|
|
incus exec anomaly-detect -- systemctl status anomaly-detect.timer
|
|
incus exec anomaly-detect -- journalctl -u anomaly-detect.service --since "30 minutes ago"
|
|
|
|
# dedup 초기화 (모든 IP 재분석 허용)
|
|
incus exec anomaly-detect -- sh -c 'echo "{}" > /var/lib/anomaly-detect/dedup.json'
|
|
|
|
# 게이트/모델/주기 변경 → /etc/systemd/system/anomaly-detect.service의 Environment=
|
|
```
|
|
|
|
## 검증 (최초 배포)
|
|
|
|
- `cscli machines list` → `anomaly-detect` 등록 확인
|
|
- `curl https://vl.inouter.com/select/logsql/query?query=program:apisix\&limit=1` 200 OK (컨테이너 내부)
|
|
- `curl http://100.87.221.126:11434/api/tags` → `gemma4:e4b` 노출
|
|
- `curl http://10.253.100.240:8080/v1/decisions` → 403 (인증 필요, 네트워크 OK)
|
|
- 더미 IP `198.51.100.99`로 alert POST → 201 + decision 등록 → cleanup 확인 (smoke test)
|
|
|
|
## 초기 리뷰 수정 (2026-04-08)
|
|
|
|
코드 리뷰 결과 다음 버그/개선을 반영:
|
|
|
|
1. **LAPI POST 실패 시 dedup 선기록 버그**: `dedup[ip] = now_ts`가 LAPI alert POST 이전에 설정되어, LAPI가 일시적으로 죽으면 해당 공격 IP가 24h 동안 재ban되지 않던 문제 수정. 성공/`no` 판정일 때만 dedup에 기록하고, LAPI 예외는 다음 사이클 재시도.
|
|
2. **XFF CSV 파싱**: `xff="1.2.3.4, 5.6.7.8"` 같은 CSV를 그대로 IP로 쓰던 버그 수정. `extract_client_ip()` 헬퍼로 첫 번째 IP만 사용.
|
|
3. **사설망/Tailscale 필터 개선**: `ip.startswith(("10.", "172.16.", ...))` 문자열 매칭 → `ipaddress.ip_address().is_private` + Tailscale CGNAT `100.64.0.0/10` 제외.
|
|
4. **dedup.json 원자적 쓰기**: `tempfile` + `os.replace`로 크래시 시 파일 절단 방지.
|
|
|
|
수정본은 `gitea.inouter.com/kaffa/anomaly-detect` main 브랜치에 커밋됨 (`d5310f0`).
|
|
|
|
## 향후 작업
|
|
|
|
- [ ] 처음 1주는 dry_run 없이 자동 ban이지만 임계값 조정 필요 시 보수적으로 시작 → 모니터링 후 점진 강화
|
|
- [ ] **[Medium]** Discord webhook 알림 추가 (`secret/apps/discord` Vault에서 가져오기) + systemd `OnFailure=` drop-in
|
|
- [ ] **[Medium]** ollama 장시간 장애 시 하드 게이트 fallback (예: 5분 1000+ reqs + 4xx>80% 자동 ban)
|
|
- [ ] **[Medium]** `LLM no 판정` IP의 dedup 짧게 (1h) 따로 관리해 false negative 회수
|
|
- [ ] **[Low]** CrowdSec alert `origin`을 `"crowdsec"` → `"anomaly-detect"`로 태깅
|
|
- [ ] **[Low]** sample 10건을 "처음 만난 10건" → "최근 10건"으로 변경 (LogsQL `_time` desc 정렬)
|
|
- [ ] **[Low]** `scenario_hash` 고정 해시 지정
|
|
- [ ] gemma4:e4b 한국어 reason 품질 평가 → 모델 변경 검토 (`gemma3:12b`, `qwen2.5:7b`, `llama3.1:8b` 등)
|
|
- [ ] 게이트 통과 후 후보 0건이 며칠 지속되면 임계값 완화
|
|
- [x] 코드를 Gitea repo로 분리 (`gitea.inouter.com/kaffa/anomaly-detect`) — 2026-04-08 완료
|
|
|
|
## 폐기된 전임자
|
|
|
|
- [[crowdsec-safeline#~~ddos-detect (AI 행위 분석)~~ — 폐기 (2026-04-08)|ddos-detect]] (Go, jp1 crowdsec 컨테이너 안, 60s 폴링, Claude CLI sonnet 호출). 폐기 사유: 60s 폴링 + 동기 Claude CLI 구조 한계. 이번 anomaly-detect는 5분 주기 + 통계 게이트 + 로컬 LLM(ollama gemma4)으로 비용/지연 동시 개선.
|