温馨提示×

linux strings与grep怎么配合

小樊
51
2026-10-20 01:07:07
栏目: 智能运维

在 Linux 中,strings 和 grep 经常配合使用,用来从二进制文件或数据中抽取可打印字符串,并筛选感兴趣的内容。下面从常见用法到实战示例逐步说明。


一、基本配合方式

1️⃣ 管道方式(最常用)

strings file | grep "keyword"

示例:在二进制程序中查找字符串 password

strings /bin/ls | grep password

2️⃣ 多个关键字(OR)

strings file | grep -E "user|pass|token"

3️⃣ 忽略大小写

strings file | grep -i "error"

4️⃣ 显示行号(在 strings 输出中的位置)

strings file | grep -n "main"

二、结合文件路径批量处理

1️⃣ 对目录下所有文件

strings * | grep "API_KEY"

或更安全的方式:

for f in *; do
  echo "== $f =="
  strings "$f" | grep "secret"
done

2️⃣ 配合 find

find . -type f -exec strings {} \; | grep "DEBUG"

或只搜索可执行文件:

find . -type f -executable -exec strings {} \; | grep "version"

三、提取上下文信息

1️⃣ 显示匹配前后内容

strings file | grep -A3 -B2 "config"

2️⃣ 只输出匹配字符串(不显示其他)

strings file | grep -o "http://[a-zA-Z0-9./]+"

四、高级用法

1️⃣ 指定最小字符串长度

strings -n 6 file | grep "token"

2️⃣ 从内存或设备文件读取

strings /proc/kcore | grep "root"

(⚠️ 需要权限,且可能很大)


3️⃣ 分析恶意样本(实战)

strings malware.bin | grep -E "http|ftp|\.exe|cmd"

五、典型应用场景

场景 命令示例
分析二进制配置 `strings app
提取 URL `strings file
逆向辅助 `strings lib.so
日志/内存分析 `strings core.dump

如果你有具体文件类型或想找的内容(比如 ELF、固件、内存 dump、恶意样本),我可以给你更精确的命令。

0 踩