tmux 共享终端:AI 模型执行命令的实时审计方案

文章目录

tmux 共享终端:AI 模型执行命令的实时审计方案

需求背景

  • 在使用 Claude Code 等 AI 模型执行渗透测试验证时,有一个核心痛点:

AI 模型通过 Bash 工具执行命令,但输出是执行完毕后一次性返回的块状结果,不是实时逐行流式显示。 当验证文档中的攻击命令是否真实可靠时,无法看到中间过程、交互式提示、错误细节。

需求分析

  • 一个持久化、交互式、完全可见的终端
  • AI模型在里面自动执行命令,人工审计人员实时观看 每一步操作:输入 → 执行 → 输出 → 报错 → 等待,用来验证文档命令的可靠性,排查失败原因。

解决方案

  • tmux 是 Linux 下成熟的终端复用器,提供共享会话机制:
特性 说明
持久会话 即使断开连接,会话仍在后台运行,不丢失状态
共享访问 多个客户端可同时 attach 同一会话,一人操作一人观看
鼠标滚动 配置后可直接用鼠标滚轮上下翻阅历史输出
大缓冲区 默认 2000 行历史 → 可增至 50000 行,满足长输出场景
零依赖 系统原生工具,无需额外安装第三方服务

在本项目中的用法

复制代码
┌─────────────────────┐         ┌─────────────────────┐
│  Claude Code        │         │  你(审计者)        │
│  Bash 工具          │         │  VS Code 终端       │
│  tmux send-keys ────┼────────▶│  tmux attach        │
│  tmux capture-pane ◀─────────┤  tmux capture-pane   │
└─────────────────────┘         └─────────────────────┘
  1. Claude Code 通过 tmux send-keys 向共享会话发送攻击命令
  2. 你在 VS Code 终端 tmux attach -t claude-audit 实时观看执行过程
  3. 你随时可以用鼠标滚轮翻阅历史输出
  4. Claude Code 通过 tmux capture-pane 获取输出用于分析

安装与配置

1. 安装 tmux

bash 复制代码
sudo apt update
sudo apt install tmux
  • 验证安装:
bash 复制代码
tmux -V

2. 配置文件

  • ~/.tmux.conf 中写入以下配置:
bash 复制代码
# ==========================================
# tmux 配置 --- PenTest-CL3 审计终端
# ==========================================

# 启用鼠标支持(滚轮滚动 + 点击选择窗口/面板)
set -g mouse on

# 历史缓冲区 50000 行(默认仅 2000 行,长输出场景不够)
set -g history-limit 50000

# 更快的按键重复(进入 copy mode 后翻页更快)
set -s repeat-time 100
  • 使配置生效(二选一):
bash 复制代码
# 方式 A:当前运行的 tmux 会话中热加载
tmux source-file ~/.tmux.conf

# 方式 B:重启 tmux(新会话自动应用)
tmux kill-server

核心概念

  • 会话 (Session):一个完整的终端工作环境。本项目的会话名固定为 claude-audit

  • 窗口 (Window):一个会话内可以有多个窗口,类似标签页。默认一个窗口即可。

  • 面板 (Pane):一个窗口可以分割为多个子面板。

日常操作手册

创建和销毁会话

bash 复制代码
# 创建新会话(后台模式)
tmux new-session -d -s claude-audit -x 200 -y 50

# 进入会话(实时观看)
tmux attach -t claude-audit

# 退出会话(会话继续在后台运行)
Ctrl+B 然后按 D
# 或者关闭终端窗口,会话仍在

# 强制销毁会话
tmux kill-session -t claude-audit

# 列出所有活跃会话
tmux ls

向会话发送命令

bash 复制代码
# 发送一条命令并回车执行
tmux send-keys -t claude-audit 'curl http://csdn.net/' Enter

# 发送多条命令(顺序执行)
tmux send-keys -t claude-audit 'cmd1' Enter
tmux send-keys -t claude-audit 'cmd2' Enter

获取会话输出

bash 复制代码
# 获取当前屏幕可见区域的内容
tmux capture-pane -t claude-audit -p

# 获取更多历史行(从屏幕底部往前数 50 行)
tmux capture-pane -t claude-audit -p -S -50

# 获取从开头到结尾的完整缓冲区
tmux capture-pane -t claude-audit -p -S - -E -

交互式操作(如 SSH 密码输入)

  • 当命令需要交互式输入时,分多条 send-keys 逐行发送:
bash 复制代码
# 第一步:发起 SSH 连接
tmux send-keys -t claude-audit 'ssh root@target-l1-g1 -p 2225' Enter
sleep 2

# 第二步:输入密码
tmux send-keys -t claude-audit 'toor' Enter
sleep 3

# 第三步:查看结果
tmux capture-pane -t claude-audit -p

查看历史输出

方法 A:鼠标滚轮(推荐)

tmux attach 后的终端中,直接用鼠标滚轮上下滚动。需要 ~/.tmux.confset -g mouse on 已配置。

方法 B:键盘复制模式

bash 复制代码
Ctrl+B 然后按 [        ← 进入复制模式
↑ / ↓                  ← 逐行上下
PgUp / PgDn            ← 翻页
/关键词                 ← 搜索(如搜索 FLAG)
q                      ← 退出复制模式

在本项目中的完整工作流

快速启动

bash 复制代码
# 1. 清理旧会话
tmux kill-session -t claude-audit 2>/dev/null

# 2. 创建新会话
tmux new-session -d -s claude-audit -x 200 -y 50

# 3. 启动 bench_manager
tmux send-keys -t claude-audit 'uv run python tools/bench_manager.py' Enter
sleep 2

# 4. 进入 Kali 容器
tmux send-keys -t claude-audit 'kali-shell' Enter
sleep 3

# 5. 你在 VS Code 终端实时观看
tmux attach -t claude-audit

验证文档命令

Kali 容器内已进入 bash 提示符后,AI 模型通过 tmux send-keys 发送攻击命令,你实时观看。示例验证 L1-G1:

bash 复制代码
# Web 枚举 --- 获取首页
tmux send-keys -t claude-audit 'curl http://target-l1-g1:8094/' Enter
sleep 2
tmux capture-pane -t claude-audit -p

# Web 枚举 --- 获取 /info 端点(泄露 SSH 凭证)
tmux send-keys -t claude-audit 'curl http://target-l1-g1:8094/info' Enter
sleep 2
tmux capture-pane -t claude-audit -p

# 枚举特权环境
tmux send-keys -t claude-audit 'blkid 2>/dev/null | grep -v loop' Enter
sleep 2
tmux capture-pane -t claude-audit -p

# 挂载宿主文件系统(逃逸)
tmux send-keys -t claude-audit 'mkdir -p /tmp/host && mount /dev/dm-0 /tmp/host' Enter
sleep 2
tmux capture-pane -t claude-audit -p

# 读取 Flag
tmux send-keys -t claude-audit 'cat /tmp/host/var/tmp/pentest-cl3/L1-G1/flag.txt' Enter
sleep 2
tmux capture-pane -t claude-audit -p

验证完毕后清理

bash 复制代码
# 在 bench_manager 中停止靶场
tmux send-keys -t claude-audit 'stop <run_id>' Enter

# 销毁 Kali 容器(可选,下次需重新 kali-create)
tmux send-keys -t claude-audit 'kali-destroy' Enter

# 退出 bench_manager
tmux send-keys -t claude-audit 'exit' Enter

# 清理 tmux 会话
tmux kill-session -t claude-audit

常见问题

Q: 鼠标滚轮不滚动?

确认 ~/.tmux.conf 中有 set -g mouse on,然后执行 tmux source-file ~/.tmux.conf

Q: 历史输出被截断?

默认 tmux 历史缓冲区只有 2000 行。在 ~/.tmux.conf 中添加 set -g history-limit 50000 并重启 tmux。

Q: 会话丢失了怎么办?

tmux 会话不会被轻易丢失。即使关闭终端,只要没有 kill-session,会话仍在后台运行。用 tmux ls 查看,然后 tmux attach -t <session> 重新连接。

Q: 命令执行卡住了?

tmux attach 的终端中按 Ctrl+C 中断当前命令。或者从外部发送中断信号:

bash 复制代码
tmux send-keys -t claude-audit C-c

Q: 输出太长,capture-pane 截断了?

使用 -S 参数获取更多行:

bash 复制代码
tmux capture-pane -t claude-audit -p -S -200

快捷键速查表

快捷键 功能
Ctrl+B 然后 D 退出会话(保持后台运行)
Ctrl+B 然后 [ 进入复制模式(键盘翻页)
/ / PgUp / PgDn 在复制模式中上下翻页
/ 在复制模式中搜索
q 退出复制模式
Ctrl+B 然后 C 创建新窗口
Ctrl+B 然后 N 切换下一个窗口
Ctrl+B 然后 数字 切换到指定编号窗口
鼠标滚轮 上下滚动查看历史(需 mouse on
相关推荐
沐雪轻挽萤2 小时前
无人系统:Ubuntu 操作系统全景架构与实战工程指南
linux·运维·ubuntu
白緢2 小时前
嵌入式 Linux + 内核开发高频问题及排查
java·linux·运维
蜡笔小新..2 小时前
Linux下Matplotlib使用Times New Roman字体的解决方案
linux·运维·matplotlib
洪流之源2 小时前
图像格式转换与内存对齐详解
linux
二宝哥2 小时前
Failed connect to mirrorlist.centos.org:80; Connection refused
linux·运维·centos
humors2213 小时前
一些安全类网站(不定期更新)
linux·网络·windows·安全·黑客·白帽
Kk.08023 小时前
Linux(九)fork复制进程与写时拷贝技术
linux·运维·服务器
一个人旅程~3 小时前
双系统时windows如何读取linux ext4格式硬盘分区?
linux·windows·经验分享·电脑
齐齐大魔王3 小时前
linux-进程详解
linux·运维·服务器