如何使用Python检测GraphQL端点

2024-06-06 18:45:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python语言检测graphql端点。我是一个绝对的初学者,但我已经尝试过编写代码。 你能建议一些改变和更好的方法吗? 代码:

import requests,urllib,urllib.request
import string
consoleDict = [
    "",
    "/graphql",
    "/graphql/console",
    "graphql.php",
    "graphiql",
    "explorer",
    "altair",
    "/playground"
          ]
for endpoint in consoleDict:
    ep = ' http://159.100.248.211 '
    response = requests.get(ep)
    if response.status_code in [200,403]:
        print("It is a GraphQL endpoint",endpoint)

谢谢:)


Tags: 方法代码inimport语言responseurllib端点
1条回答
网友
1楼 · 发布于 2024-06-06 18:45:56

即使使用gql,您也需要模式来请求任何东西。如果您不知道,可以使用内省查询:

{
  __schema {
    types {
      name
    }
  }
}

某些端点可能已禁用此功能,但如果您不知道模式,则这是一个很好的起点。 试试这样的方法:

import json
import requests
from urllib import parse

paths = [
    "",
    "/graphql",
    "/graphql/console",
    "graphql.php",
    "graphiql",
    "explorer",
    "altair",
    "/playground"
]

query = """{
  __schema {
    types {
      name
    }
  }
}
"""

for path in paths:
    hostname = 'http://159.100.248.211'
    endpoint = parse.urljoin(hostname, path)
    try:
        print(f"Attempt: {endpoint}")
        response = requests.post(endpoint, json={'query': query}, timeout=0.1)
    except Exception:
        print("No GraphQL endpoint found")
    else:
        if response.status_code == 200:
            json_data = json.loads(response.text)
            if json_data.get('data'):
                print("It is a GraphQL endpoint",endpoint)

让mw知道这是否有效

相关问题 更多 >