CTF WEB 解题技能
·
HTTP 请求方法不止 GET/POST,自定义方法也能携带发送,常见三种实现方式:
curl -X CTFB http://目标地址/index.php
-X指定自定义请求方法为CTFB
服务器设置了 admin=0 cookie,我们只需要把它改成 admin=1 来实现管理员已登录
curl -sk -b "admin=1" http://challenbaadf.sandbox.com:10086
常用参数速查表
| 参数 | 作用 |
|---|---|
| -X | 指定请求方法 POST/PUT/DELETE |
| -H | 添加请求头 |
| -d | 提交表单 /json 数据 |
| -F | 文件上传 |
| -o/-O | 输出到文件 |
| -sS | 静默输出,只打印错误 |
| -v | 完整调试日志 |
| -L | 跟随 3xx 重定向 |
| -k | 跳过 SSL 证书校验 |
| -c/-b | 写入 / 读取 Cookie |
| -u | Basic 账号密码认证 |
| -m | 请求总超时 |
| -x | 设置代理 |
| -w | 自定义输出响应信息 |
Linux curl 命令详解
一、基础介绍
curl 是 Linux / Mac / Windows 通用的命令行数据传输工具,支持 HTTP/HTTPS/FTP/SFTP/TCP/SOCKS5 等几十种协议,常用于接口测试、文件下载、爬虫、自动化脚本。 全称:Client URL,默认不安装,CentOS/Debian 安装命令:
# CentOS/RHEL
yum install curl -y
# Debian/Ubuntu
apt install curl -y
二、通用语法
curl [选项] URL
三、高频常用参数(分类整理)
1. 输出控制
-o:输出到文件(覆盖)
# 把网页保存到 index.html
curl -o index.html https://www.baidu.com
-O(大写):自动使用远程文件名保存
# 自动保存为 test.zip
curl -O https://xxx.com/test.zip
-s / --silent:静默模式,隐藏进度条、错误输出
脚本中必用
curl -s https://api.example.com
-S:配合 -s,只显示错误信息
curl -sS https://api.example.com
-v / --verbose:打印完整通信日志(调试接口神器)
curl -v https://www.baidu.com
# 更详细追踪 TCP 握手、SSL
curl -vvv https://www.baidu.com
-#:简洁进度条代替默认进度输出
curl -# -O https://xxx/file.zip
2. 请求方法(GET/POST/PUT/DELETE)
默认 GET
curl https://api.com/get?name=test
-X 指定请求方法
# POST
curl -X POST https://api.com/login
# DELETE
curl -X DELETE https://api.com/user/1
# PUT
curl -X PUT https://api.com/user/1
3. 请求头 Headers -H
多 Header 写多个 -H
# 单个 header
curl -H "Content-Type: application/json" https://api.com
# 多个 header
curl -H "Token: abc123" -H "User-Agent: myscript/1.0" https://api.com
4. 传递请求数据(POST 参数)
1)-d / --data 表单提交 application/x-www-form-urlencoded
# 表单参数 name=zhangsan&age=18
curl -X POST -d "name=zhangsan&age=18" https://api.com/login
# 从文件读取参数
curl -X POST -d @postdata.txt https://api.com
2)JSON 请求(后端最常用)
搭配 -H "Content-Type: application/json"
curl -X POST \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"123456"}' \
https://api.com/login
3)--data-urlencode 自动编码特殊字符(空格、中文)
curl -d "search=$(echo "测试内容")" --data-urlencode "search=测试内容" https://api.com
5. Cookie 相关
-c:把响应 Cookie 保存到文件
curl -c cookie.txt https://login.example.com
-b:读取文件中的 Cookie 发送请求(登录态保持)
curl -b cookie.txt https://api.example.com/userinfo
直接携带 Cookie 字符串
curl -b "sessionid=xxx; token=yyy" https://api.com
6. 模拟浏览器(User-Agent)
-A / --user-agent 指定 UA
# 模拟 Chrome
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36" https://api.com
7. 跳转跟随 3xx 重定向 -L
很多网站登录、下载会 302 跳转,不加 -L 只会返回跳转页面
# 自动跟随全部重定向
curl -L https://short.xxx.com
# 限制最多跳转 5 次
curl -L --max-redirs 5 https://short.xxx.com
8. 超时控制(脚本防卡死)
-m / --max-time 总超时(秒)
# 超过10秒直接断开
curl -m 10 https://api.com
--connect-timeout 连接超时
curl --connect-timeout 3 https://api.com
9. 代理访问
HTTP 代理
curl -x http://127.0.0.1:7890 https://www.google.com
# 带账号密码代理
curl -x user:pass@127.0.0.1:7890 https://www.google.com
socks5 代理
curl -x socks5://127.0.0.1:1080 https://www.google.com
10. HTTPS 证书跳过校验(内网测试用,生产不推荐)
-k / --insecure 忽略 SSL 证书报错(自签名证书)
curl -k https://127.0.0.1:8443/api
11. 文件上传
-F 表单上传文件(multipart/form-data)
# 上传本地 test.jpg,字段名 file
curl -X POST -F "file=@test.jpg" https://api.com/upload
# 同时传参数
curl -F "file=@test.jpg" -F "desc=图片测试" https://api.com/upload
12. 基础认证 Basic Auth
-u 账号:密码
curl -u admin:123456 https://api.com/auth
# 只输账号,执行时交互式输入密码
curl -u admin https://api.com/auth
13. 限定 IP 访问、自定义请求端口
绑定本机出口 IP
curl --interface 192.168.1.100 https://api.com
指定 Host 模拟域名解析(本地调试)
--resolve 域名:端口:IP
# 强制把 api.test.com 解析到 127.0.0.1:8080
curl --resolve api.test.com:8080:127.0.0.1 http://api.test.com:8080
四、实用完整示例
示例 1:标准 JSON 接口调用(日常开发最多)
curl -s -X POST \
-L \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" \
-d '{
"page": 1,
"size": 10
}' \
https://api.example.com/list
示例 2:登录并保持 Cookie 获取用户信息
# 1. 登录保存cookie
curl -c cookie.txt -d "username=admin&password=123456" https://api.com/login
# 2. 携带cookie访问个人中心
curl -b cookie.txt https://api.com/user
示例 3:静默检测接口是否存活(shell 脚本健康检查)
# 状态码 200 正常,非200异常
if curl -s -m 5 -o /dev/null -w "%{http_code}" https://api.com/health | grep 200; then
echo "服务正常"
else
echo "服务异常"
fi
示例 4:只输出 HTTP 响应状态码
-w "%{http_code}" 格式化输出变量
curl -s -o /dev/null -w "%{http_code}" https://www.baidu.com
常用输出变量:
%{http_code}HTTP 状态码%{time_total}总耗时秒%{remote_ip}服务端 IP%{url_effective}最终访问 URL
示例 5:下载文件,显示进度条,超时 30 秒
curl -# -m 30 -O https://xxx.com/software.zip
示例 6:跳过 SSL 证书访问内网 https 服务
curl -k -v https://192.168.1.100:8443
五、常见返回状态码说明
- 200:请求成功
- 301/302:永久 / 临时重定向(需要
-L) - 400:参数错误
- 401:未登录 /token 失效
- 403:权限拒绝
- 404:接口不存在
- 405:请求方法不允许(POST 接口用 GET 访问)
- 500:服务端内部错误
六、避坑要点
- JSON 参数带换行:shell 中单引号
'包裹 json,避免双引号转义麻烦; - 接口超时卡死:脚本必须加
-m最大超时; - 自签名 HTTPS:内网测试加
-k,公网环境不要使用; - 重定向接口:一定要加
-L,否则拿不到最终页面数据; - 脚本输出杂乱:组合
-sS静默模式; - 文件上传
@不能丢:-F "file=@a.jpg",少 @不会上传文件。
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐

所有评论(0)