curl
2026/5/29工具工具Web安全curl大约 5 分钟
curl
是什么
curl(Client URL)是一款功能强大的命令行工具,用于与服务器进行数据传输。它支持超过 25 种协议,包括 HTTP、HTTPS、FTP、FTPS、SCP、SFTP、TFTP、DICT、TELNET、LDAP 等。curl 是几乎所有 Linux/macOS 系统的标配工具,也是 CTF Web 类题目中最基础、最常用的工具之一。
核心特性:
- 支持几乎所有 HTTP 方法(GET、POST、PUT、DELETE、PATCH 等)
- 支持自定义请求头和 Cookie
- 支持表单数据和文件上传
- 支持 SSL/TLS 连接
- 支持代理设置
- 支持认证(Basic、Digest、NTLM、Bearer 等)
- 支持跟随重定向
- 支持输出控制和调试
安装与配置
安装方法
# Linux
# Ubuntu/Debian
sudo apt update && sudo apt install curl
# CentOS/RHEL
sudo yum install curl
# Arch Linux
sudo pacman -S curl
# macOS(通常已预装)
brew install curl
# Windows
# Windows 10/11 自带 curl(基于 libcurl)
# 或使用 Chocolatey
choco install curl
# 验证安装
curl --version输出样式配置
# 设置默认配置文件 ~/.curlrc
echo "-s" > ~/.curlrc # 默认静默模式libcurl 版本信息
curl --version
# 输出包含:curl x.x.x、支持的协议、SSL 库版本等基本用法
GET 请求
# 最简单的 GET 请求
curl http://example.com
# 带查询参数
curl "http://example.com/search?q=ctf&page=1"
# 只显示响应体(不显示进度条)
curl -s http://example.com
# 显示响应头
curl -i http://example.com
# 显示详细调试信息
curl -v http://example.com
# 只输出 HTTP 状态码
curl -o /dev/null -s -w "%{http_code}" http://example.comPOST 请求
# POST 表单数据
curl -X POST http://example.com/login \
-d "username=admin&password=123456"
# POST JSON 数据
curl -X POST http://example.com/api/data \
-H "Content-Type: application/json" \
-d '{"name":"test","value":42}'
# POST JSON 文件
curl -X POST http://example.com/api/data \
-H "Content-Type: application/json" \
-d @data.json
# POST 表单(application/x-www-form-urlencoded)
curl -X POST http://example.com/form \
--data-urlencode "name=admin" \
--data-urlencode "pass=hello world"其他 HTTP 方法
# PUT 请求
curl -X PUT http://example.com/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"updated"}'
# DELETE 请求
curl -X DELETE http://example.com/api/users/1
# PATCH 请求
curl -X PATCH http://example.com/api/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"patched"}'
# HEAD 请求(只获取响应头)
curl -I http://example.com
# OPTIONS 请求
curl -X OPTIONS http://example.com -i设置请求头
# 自定义 Header
curl http://example.com \
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)" \
-H "Accept: text/html" \
-H "X-Forwarded-For: 127.0.0.1"
# 修改 Host 头
curl http://192.168.1.1/ \
-H "Host: www.target.com"
# 移除默认 Header
curl http://example.com \
-H "Accept:"Cookie 操作
# 发送 Cookie
curl http://example.com/dashboard \
-b "session=abc123; user=admin"
# 从文件读取 Cookie
curl http://example.com/dashboard \
-b cookies.txt
# 保存响应中的 Cookie 到文件
curl http://example.com/login \
-d "user=admin&pass=123" \
-c cookies.txt
# 同时保存和发送 Cookie
curl http://example.com/dashboard \
-b cookies.txt -c cookies.txt
# 使用 Netscape 格式的 Cookie 文件
curl http://example.com \
-b "Netscape HTTP Cookie File"认证
# Basic 认证
curl -u username:password http://example.com/admin
# Digest 认证
curl --digest -u username:password http://example.com/admin
# Bearer Token 认证
curl http://example.com/api/data \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..."
# NTLM 认证
curl --ntlm -u username:password http://example.com/重定向处理
# 跟随重定向
curl -L http://example.com/redirect
# 限制重定向次数
curl -L --max-redirs 5 http://example.com/redirect
# 显示重定向链
curl -L -v http://example.com/redirect 2>&1 | grep "Location:"文件操作
# 下载文件
curl -O http://example.com/file.zip
# 下载并重命名
curl -o myfile.zip http://example.com/file.zip
# 断点续传
curl -C - -O http://example.com/largefile.zip
# 上传文件(表单)
curl -X POST http://example.com/upload \
-F "file=@/path/to/file.txt"
# 上传多个文件
curl -X POST http://example.com/upload \
-F "file1=@a.txt" \
-F "file2=@b.txt"
# 指定上传文件名和类型
curl -X POST http://example.com/upload \
-F "file=@shell.php;filename=image.jpg;type=image/jpeg"输出控制
# 静默模式(不显示进度条和错误信息)
curl -s http://example.com
# 只显示错误
curl -S http://example.com
# 输出到文件
curl -o output.html http://example.com
# 追加到文件
curl -a output.html http://example.com/another
# 显示响应头和响应体
curl -i http://example.com
# 只显示响应头
curl -I http://example.com
# 格式化输出(配合 jq)
curl -s http://example.com/api/data | jq .
# 自定义输出格式
curl -o /dev/null -s -w "HTTP Code: %{http_code}\nTime: %{time_total}s\nSize: %{size_download} bytes\n" http://example.com代理设置
# HTTP 代理
curl -x http://127.0.0.1:8080 http://example.com
# HTTPS 代理
curl -x http://127.0.0.1:8080 https://example.com
# SOCKS5 代理
curl --socks5 127.0.0.1:1080 http://example.com
# 使用环境变量
export http_proxy=http://127.0.0.1:8080
export https_proxy=http://127.0.0.1:8080
curl http://example.comSSL/TLS 选项
# 忽略 SSL 证书验证
curl -k https://self-signed.example.com
# 使用指定 CA 证书
curl --cacert /path/to/ca.pem https://example.com
# 客户端证书
curl --cert client.pem --key client-key.pem https://example.com
# 指定 TLS 版本
curl --tlsv1.2 https://example.comCTF常用技巧
探测目录和文件
# 探测常见路径
curl -s -o /dev/null -w "%{http_code}" http://challenge.ctf.com/flag
curl -s -o /dev/null -w "%{http_code}" http://challenge.ctf.com/admin
curl -s -o /dev/null -w "%{http_code}" http://challenge.ctf.com/robots.txt
curl -s -o /dev/null -w "%{http_code}" http://challenge.ctf.com/.git/config
# 批量探测(配合字典)
for path in $(cat wordlist.txt); do
code=$(curl -s -o /dev/null -w "%{http_code}" "http://challenge.ctf.com/$path")
echo "$path -> $code"
done绕过 Referer 检查
curl http://challenge.ctf.com/secret \
-H "Referer: http://challenge.ctf.com/"绕过 IP 限制
# 伪造 X-Forwarded-For
curl http://challenge.ctf.com/admin \
-H "X-Forwarded-For: 127.0.0.1"
# 伪造其他常见 IP 头
curl http://challenge.ctf.com/admin \
-H "X-Real-IP: 127.0.0.1" \
-H "X-Client-IP: 127.0.0.1" \
-H "X-Originating-IP: 127.0.0.1"修改 User-Agent
# 模拟搜索引擎爬虫
curl http://challenge.ctf.com/ \
-H "User-Agent: Googlebot/2.1 (+http://www.google.com/bot.html)"
# 模拟移动端
curl http://challenge.ctf.com/ \
-H "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)"发送 XML 数据(XXE 测试)
curl -X POST http://challenge.ctf.com/api/xml \
-H "Content-Type: application/xml" \
-d '<?xml version="1.0"?><!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><root>&xxe;</root>'SSTI 测试
curl "http://challenge.ctf.com/search?q={{7*7}}"
curl "http://challenge.ctf.com/search?q=\${7*7}"批量请求脚本
#!/bin/bash
# 批量测试参数
for i in $(seq 1 100); do
result=$(curl -s "http://challenge.ctf.com/api/user?id=$i" | jq -r '.name')
echo "id=$i -> $result"
doneHTTP 请求走私
# 手动构造 HTTP 请求(使用 --raw 或配合 netcat)
curl --raw "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 6\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\nGET /admin HTTP/1.1\r\nHost: example.com\r\n\r\n"常见问题
SSL 证书验证失败
解决:
curl -k https://example.com # 跳过证书验证中文乱码
解决:
# 设置编码
curl -s http://example.com | iconv -f gbk -t utf-8请求被重定向
解决:
curl -L http://example.com # 跟随重定向长 URL 截断
解决:
# 使用 --data-urlencode 编码特殊字符
curl --data-urlencode "param=value with spaces&special=chars" http://example.com调试困难
解决:
curl -v http://example.com # 详细输出
curl --trace trace.txt http://example.com # 完整跟踪
curl --trace-ascii trace.txt http://example.com # ASCII 跟踪