graphql-schema-parse

MIT
Python
windows/mac/linux
· 2022年01月26日 · 4025 次阅读 · 2 条评论

源码:https://github.com/zy7y/graphql-schema-parse
PYPI:https://pypi.org/project/graphql-schema-parse/
详细文档制作中...
已有功能:

  • GraphQL SDL(.graphql) 文件转 JSON
  • GraphQL SDL(.graphql) 文件转 GQL
    SDL (.graphql)

  • GraphQL 服务器 IP:PORT 转 JSON

  • GraphQL 服务器 IP:PORT 转 GQL

  • GraphQL 服务器 IP:PORT 转 TEXT(此为 HTTP 请求源信息,可用于 sqlmap SQL 注入扫描)
    HTTP请求源信息示例

  • GraphQL JSON(.json) 文件转 JSON

  • GraphQL JSON(.json) 文件转 GQL
    GraphQL JSON响应内容

    安装

    pip install graphql-schema-parse
    

    使用

    # depth是个可选参数,作用与生成得query 语句部分中查询字段的查找层级
    #.graphql -> gql
    gql parse schema.graphql --to gql --depth 5  存放目录
    

ip:port -> gql (需要 token 认证才能访问接口文档时,请前往仓库查看)

gql parse http://127.0.0.1:8000 --to gql 测试目录

.json -> gql

gql parse schema.json --to gql --depth 5 存放目录

.json / .graphql -> gql [ 选择 1 | 选择 2 ] 两个里面选一个, (json 文件内容会根据文档填充对应类型的默认参数,最终的 json 文件可直接给 requests 使用)

gql parse [schema.json | schema.graphql ] --depth 5 存放目录

ip:port -> json (需要 token 认证才能访问接口文档时,请前往仓库查看)

gql parse http://127.0.0.1:8000 测试目录

ip:port -> txt (用于 sqlmap -r 指定的 txt, http 请求源信息文件)

gql parse http://127.0.0.1:8000 --to sqlmap 测试目录

# 转之后生成的内容
## json(根据文档中对应类型填充对应的默认参数)
```json
{
  "query": " mutation addUserInput ($user: AddUserInput!){\n        addUserInput (user: $user){\n                  id\n                  username\n                 }\n             \n        }\n        ",
  "variables": {
    "user": {
      "username": "",
      "password": ""
    }
  },
  "operationName": "addUserInput"
}

使用 requests 进行请求

# pip install requests
from requests import post

url = "http://127.0.0.1:8000/graphql"
data = {
    "query": " mutation addUserInput ($user: AddUserInput!){\n        addUserInput (user: $user){\n                  id\n                  username\n                 }\n             \n        }\n        ",
    "variables": {"user": {"username": "", "password": ""}}, "operationName": "addUserInput"}
print(post(url, json=data).json())

txt(sqlmap -r 可用的 sql 注入扫描文件)

POST /graphql HTTP/1.1
HOST: http://127.0.0.1:8000
Authorization: Bearer token
Content-Type: application/json

{"query": " mutation addUserInput ($user: AddUserInput!){\n        addUserInput (user: $user){\n                  id\n                  username\n                 }\n             \n        }\n        ", "variables": {"user": {"username": "*", "password": "*"}}, "operationName": "addUserInput"}
# 安装sqlmap
pip install sqlmap

# 进行SQL注入扫描, sqlmap详细用法前往sqlmap官网学习
sqlmap -r addUserInput.txt --level 5 --risk 3

开始扫描

扫描结果

gql(专门的 graphql 测试工具的 query 语句部分)

mutation addUserInput($user: AddUserInput!) {
  addUserInput(user: $user) {
    id
    username
  }
}

API

gql --help
gql parse --help

output:

Usage: gql parse [OPTIONS] FROM_PATH TO_DIRECTORY

  将Graphql接口文档转成gql文件/Json文件 :param from_path: 接口文档地址, 本地JSON文件地址(.json) 或者 本地
  SDL文件(.schema ), 或者 服务器URL填入(服务器的IP:PORT) :param to: 转换之后的文件类型, 可选
  to_json(.json) / to_gql(.gql) / to_sqlmap(.txt) :param headers: from_type
  为url时可选项,请求头文件地址(.json) :param depth: query语句体中可用查询字段递归深度 :param
  to_directory: 转换之后文件,保存目录 :return:

Arguments:
  FROM_PATH     接口文档地址, 本地JSON文件地址(.json) 或者 本地 SDL文件(.schema ), 或者
                服务器URL填入(服务器的IP:PORT)  [required]
  TO_DIRECTORY  生成文件保存目录,不存在时,自动创建  [required]

Options:
  --headers TEXT          url方式获取接口文档时,可选项传入请求头json文件地址
  --to [json|gql|sqlmap]  [default: ToType.to_json]
  --depth INTEGER         query语句体中可用查询字段递归深度  [default: 1]
  --help                  Show this message and exit.
评论列表
发表于 2022年02月09日

需要生成 test_xx.py 可以直接继承 src 中的 MakeFile 类,实现其中的抽象方法

陈恒捷 发表于 2022年01月27日

看了下,挺实用的工具。详细文档还是看 github 的吧,这里写的有点简略。