tkddnr924/Linux-Analysis

GitHub: tkddnr924/Linux-Analysis

Linux系统日志分析工具,自动解析并分析日志,发现安全风险。

Stars: 1 | Forks: 0

# Linux 分析 解析收集到的 Linux artifacts(日志、系统信息)并将其存储到 SQLite DB 中, 自动分析安全威胁及运营异常迹象。 ## pipeline ``` target/ ← 로그 파일을 구조 상관없이 배치 (하위 디렉토리 포함 재귀 탐색) │ ▼ [1단계: 파싱] parser.db ← 원시 로그 데이터 │ ▼ [2단계: 분석] analysis.db ← 집계·분석 결과 ``` ## 快速开始 ``` # 1. 创建并激活虚拟环境 python3.12 -m venv venv source venv/bin/activate # 2. 安装依赖 pip install -r requirements.txt # 3. 将日志文件放置到 target/ 后运行 python main.py ``` ## 项目结构 ``` Linux-Analysis/ ├── main.py # 진입점 │ ├── parser/ # 로그 파서 │ ├── auditlog.py # audit.log* 파서 │ ├── authlog.py # auth.log* 파서 │ ├── nginxlog.py # nginx access.log* 파서 │ └── utils/ │ ├── files.py # MD5, glob, 압축 해제 │ ├── strings.py # hex 디코딩, key=value 파싱 │ └── times.py # epoch → KST 변환 │ ├── analyzer/ # 분석기 │ ├── auditlog.py # audit 로그 분석 │ ├── authlog.py # SSH/인증 로그 분석 │ ├── cron.py # cron 실행 이력 분석 │ ├── nginxlog.py # nginx 공격·웹쉘 탐지 │ ├── sysinfo.py # 서버 기본 정보 수집 │ └── loginfo.py # 로그 요약 메타정보 │ ├── target/ # 분석 대상 로그 (하위 구조 무관) ├── parser.db # 파싱 결과 (자동 생성) ├── analysis.db # 분석 결과 (실행 시 재생성) └── gui/ # Electron 기반 analysis.db 뷰어 ├── main.js # Electron 메인 프로세스 ├── preload.js # IPC 브릿지 ├── renderer/ # UI (HTML/CSS/JS) └── package.json # Node 22 의존성 ``` ## 支持的日志及解析目标 | 日志文件 | 解析表 | 备注 | |---|---|---| | `audit.log*` | `audit` | 自动解码 hex 编码 | | `auth.log*` | `authlog` | 分类 SSH·sudo·cron·su 事件 | | `access.log*` | `nginx` | 仅保存 2xx 成功请求 | - 自动解压 `.gz` / `.tar.gz` / `.tgz` 压缩文件后解析 - 防止重复解析相同文件(基于 MD5) ## DB schema ### parser.db #### `info` — 解析文件元信息 | 列 | 说明 | |---|---| | `file_name` | 文件名 | | `file_path` | 完整路径 | | `md5` | MD5 校验和(防止重复解析) | | `file_size` | 文件大小(bytes) | | `log_type` | 日志类型(audit / authlog / nginx) | | `parsed_at` | 解析时间 | #### `audit` — Linux Audit 日志 `type`, `date_time`, `sequence`, `pid`, `uid`, `auid`, `ses`, `exe`, `cmd`, `cwd`, `comm`, `proctitle`, `op`, `subj`, `hostname`, `addr`, `terminal`, `msg_res`, `raw_line` 等多个字段 #### `authlog` — 认证日志 `date_time`, `hostname`, `service`, `pid`, `event_type`, `user`, `src_ip`, `port`, `detail`, `raw_line` **`event_type` 分类:** | 分类 | 事件 | |---|---| | SSH | `sshd_accepted_password` `sshd_accepted_publickey` `sshd_failed_password` `sshd_invalid_user` `sshd_conn_closed` 等多个 | | sudo | `sudo_command` `sudo_auth_failure` | | cron | `cron_session_opened` `cron_session_closed` | | su | `su_to` `su_session_opened` `su_session_closed` | #### `nginx` — Nginx 访问日志(2xx) `date_time`, `src_ip`, `method`, `uri`, `status`, `bytes_sent`, `user_agent`, `referer` ### analysis.db #### `info` — 服务器基本信息 *(仅在存在 Volatile/NonVolatile artifacts 时收集)* `hostname`, `internal_ip`, `mac_address`, `os`, `kernel`, `architecture`, `cpu_model`, `cpu_cores`, `disk_total/used/avail/use_pct`, `timezone`, `collected_at`, `booted_at`, `uptime_days`, `last_reboot`, `listen_ports`, `collect_user` #### `log` — 日志摘要 `log_name`, `first_record`, `last_record`, `total_records`, `file_count` #### `authlog_login` — SSH 登录成功 `src_ip`, `user`, `auth_method`, `first_seen`, `last_seen`, `count` #### `authlog_sudo` — sudo 命令执行 `user`, `command`, `first_seen`, `last_seen`, `count` #### `authlog_attack_ip` — 访问尝试 IP 汇总 `src_ip`, `first_seen`, `last_seen`, `total_count`, `success_count`, `fail_count` #### `authlog_su` — su 账号切换 `from_user`, `to_user`, `first_seen`, `last_seen`, `count` #### `audit_login` — 认证·登录事件 `type`, `acct`, `hostname`, `addr`, `terminal`, `res`, `first_seen`, `last_seen`, `count` #### `audit_cmd` — 命令执行历史(EXECVE) `uid`, `auid`, `cmd`, `cwd`, `first_seen`, `last_seen`, `count` #### `audit_file` — 文件访问历史(PATH) `uid`, `exe`, `cwd`, `first_seen`, `last_seen`, `count` #### `cron_info` — cron 执行统计 | 列 | 说明 | |---|---| | `process` | 执行的进程·命令名 | | `user` | 执行用户(uid) | | `first_seen` | 首次执行时间 | | `last_seen` | 最后执行时间 | | `exec_count` | 执行次数 | | `avg_duration_sec` | 平均耗时(秒) | | `total_duration_sec` | 总耗时(秒) | #### `nginx_top_ip` — 检测到攻击的 IP 汇总 `src_ip`, `first_seen`, `last_seen`, `attack_count`, `attack_types` #### `nginx_attack` — 攻击 payload 检测 `date_time`, `src_ip`, `method`, `uri`, `decoded_uri`, `status`, `attack_type`, `matched_str`, `user_agent` **检测到的攻击类型:** `sql_injection` `xss` `path_traversal` `lfi_rfi` `shell_injection` `php_injection` `log4shell` `spring4shell` #### `nginx_webshell` — 可疑 webshell 文件 `file_name`, `file_path`, `src_ip`, `first_seen`, `last_seen`, `access_count`, `bytes_min`, `bytes_max`, `bytes_distinct`, `suspicion_score`, `suspicion_flags` **检测标准:** `known_webshell` `variable_response` `script_in_media` `suspicious_path` `high_freq_access` `persistent_access` ## GUI 查看器 通过桌面应用浏览 `analysis.db` 的结果。 ``` cd gui/ npm install # 최초 1회 (better-sqlite3 자동 재빌드) npm start ``` ## 执行流程 ``` main.py │ ├── [RESET] analysis.db 삭제 후 재생성 │ ├── [1단계] parse_logs() │ ├── target/ 하위 audit.log*, auth.log*, access.log* 탐색 │ ├── .gz 압축 파일 자동 해제 (.decomp/ 임시 디렉토리) │ ├── MD5 확인 → 이미 파싱된 파일 SKIP │ └── 1,000건 단위 배치 INSERT → parser.db │ └── [2단계] analyze_logs() ├── sysinfo → analysis.db :: info (아티팩트 있을 때만) ├── authlog → analysis.db :: authlog_* ├── audit → analysis.db :: audit_* ├── cron → analysis.db :: cron_info ├── nginx → analysis.db :: nginx_* └── loginfo → analysis.db :: log ```
标签:AMSI绕过, PB级数据处理, Python, SQLite, 威胁检测, 安全运维, 数字取证, 无后门, 日志分析, 自动化脚本