GraphQL Cop:高效安全审计工具,保护您的GraphQL API免受攻击
GraphQL Cop是一款专为GraphQL API设计的Python安全审计工具,它能够执行一系列常见的安全检测,包括DoS攻击、CSRF漏洞、信息泄露等。这款轻量级工具不仅易于集成到CI/CD流程中,还支持通过cURL命令重现漏洞,极大地方便了安全研究人员的工作。本文详细介绍了GraphQL Cop的功能、安装方法、使用方法以及运行演示,是网络安全专业人士必备的工具之一。
核心内容提要:GraphQL Cop工具使用指南、GraphQL API安全最佳实践、如何防止GraphQL DoS攻击、GraphQL CSRF漏洞修复、GraphQL信息泄露风险评估、Python编写的安全审计脚本、自动化GraphQL安全测试流程
简介
GraphQL Cop是一个小型 Python 实用程序,用于针对 GraphQL API 运行常见的安全测试。GraphQL Cop非常适合在 GraphQL 中运行 CI/CD 检查。
功能介绍
当前版本的GraphQL Cop支持执行下列检测:
- 别名过载(DoS);
- 批量查询(DoS);
- 基于 GET 的查询(CSRF);
- 使用 urlencoded 负载的 POST 查询(CSRF);
- GraphQL 跟踪/调试模式(信息泄漏);
- 字段重复(DoS);
- 字段建议(信息泄露);
- GraphiQL(信息泄露);
- 自省(信息泄露);
- 指令重载(DoS);
- 使用内省(DoS)的循环查询;
- 通过 GET 方法支持变异(CSRF);
工具要求
- requests==2.25.1
- simplejson==3.17.6
- termcolor==2.2.0
- PySocks==1.7.1
工具安装
由于该工具基于Python 3开发,因此我们首先需要在本地设备上安装并配置好最新版本的Python 3环境。
接下来,广大研究人员可以直接使用下列命令将该项目源码克隆至本地:
git clone https://github.com/dolevf/graphql-cop.git
然后切换到项目目录中,使用下列命令在指定的目录中创建一个虚拟环境,然后激活虚拟环境,并安装requirements.txt 中列出的所有 Python 包:
cd graphql-cop
python3 -m venv path/to/venv
source path/to/venv/bin/activate
python3 -m pip install -r requirements.txt
工具使用
$ python graphql-cop.py -h
Usage: graphql-cop.py -t http://example.com -o json
Options:
-h, --help 显示此帮助消息并退出
-t URL, --target=URL 目标URL地址,如果没有提供GraphQL路径,GraphQL Cop将迭代一系列常见GraphQL路径
-H HEADER, --header=HEADER 将Header(s) 添加到请求中
-o FORMAT, --output=FORMAT 输出json格式
-f, --force 在无法检测到GraphQL时强制扫描
-d, --debug 为调试添加一个带有测试名称的Header
-x PROXY, --proxy=PROXY HTTP(S)代理URL,格式为http://user:pass@host:port
-v, --version 打印工具当前版本
-T, --tor 启用Tor代理
工具运行演示
测试网站:
$ python3 graphql-cop.py -t https://mywebsite.com/graphql
GraphQL Cop 1.1 Security Auditor for GraphQL
Dolev Farhi & Nick AleksStarting...
[HIGH] Introspection Query Enabled (Information Leakage)
[LOW] GraphQL Playground UI (Information Leakage)
[HIGH] Alias Overloading with 100+ aliases is allowed (Denial of Service)
[HIGH] Queries are allowed with 1000+ of the same repeated field (Denial of Service)
测试网站,转储为可解析的 JSON 输出,cURL 复现命令:
{
'curl_verify': 'curl -X POST -H "User-Agent: graphql-cop/1.2" -H ' ' "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H ' ' "Connection: keep-alive" -H "Content-Length: 33" -H ' ' "Content-Type: application/json" -d \'{"query": "query { ' '__typename }"}\' \'http://localhost:5013/graphql\'',
'description': 'Tracing is Enabled',
'impact': 'Information Leakage',
'result': False,
'severity': 'INFO',
'color': 'green',
'title': 'Trace Mode'
},
{
'curl_verify': 'curl -X POST -H "User-Agent: graphql-cop/1.2" -H ' ' "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H ' ' "Connection: keep-alive" -H "Content-Length: 64" -H ' ' "Content-Type: application/json" -d \'{"query": "query { ' '__typename @aa@aa@aa@aa@aa@aa@aa@aa@aa@aa }"}\' ' "'http://localhost:5013/graphql'"',
'description': 'Multiple duplicated directives allowed in a query',
'impact': 'Denial of Service',
'result': True,
'severity': 'HIGH',
'color': 'red',
'title': 'Directive Overloading'
}
graphql-cop通过代理(例如,监听 127.0.0.1:8080 的 Burp Suite)和自定义标头(例如,授权)测试网站:
$ python3 graphql-cop.py -t https://mywebsite.com/graphql --proxy=http://127.0.0.1:8080 --header '{"Authorization": "Bearer token\_here"}'
GraphQL Cop 1.2 Security Auditor for GraphQL
Dolev Farhi & Nick AleksStarting...
[HIGH] Introspection Query Enabled (Information Leakage)
[LOW] GraphQL Playground UI (Information Leakage)
[HIGH] Alias Overloading with 100+ aliases is allowed (Denial of Service)
[HIGH] Queries are allowed with 1000+ of the same repeated field (Denial of Service)
许可证协议
本项目的开发与发布遵循MIT开源许可协议。
项目地址
GraphQL Cop GitHub传送门:https://github.com/dolevf/graphql-cop