在 Ubuntu 里,strings 通常来自 binutils 包,命令行直接可用。下面从基本用法 → 脚本调用(Shell / Python)→ 常见注意点给你说明。
which strings
strings --version
如果没安装:
sudo apt update
sudo apt install binutils
#!/bin/bash
FILE="/bin/ls"
strings "$FILE" | head -n 20
运行:
chmod +x run_strings.sh
./run_strings.sh
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 <file>"
exit 1
fi
strings -n 6 "$1"
使用:
./run_strings.sh ./myprogram
strings binary_file | grep -i "error"
或脚本中:
strings "$1" | grep -i "password"
import subprocess
def run_strings(path):
result = subprocess.run(
["strings", path],
capture_output=True,
text=True
)
return result.stdout
output = run_strings("/bin/ls")
print(output[:500])
subprocess.run(["strings", "-n", "8", path])
result = subprocess.run(
["strings", path],
capture_output=True,
text=True
)
if result.returncode != 0:
print("strings failed:", result.stderr)
| 参数 | 作用 |
|---|---|
-n N |
只显示长度 ≥ N 的字符串 |
-e |
指定编码(如 s, l, b) |
-f |
显示文件名 |
-a |
扫描整个文件(默认) |
示例:
strings -n 10 -f binary_file
strings /usr/bin/python3 | grep -i "version"
strings unknown.bin | less
如果你有具体使用场景(比如:逆向分析 / CTF / 自动化扫描 / 批量处理),我可以给你更针对性的脚本模板。