Files
coarena-autoreg/gh_oauth_check.py
T

83 lines
3.5 KiB
Python

"""Отбраковать GitHub-пул по пригодности для OAuth третьей стороны.
Зачем: `POST github.com/session` даёт 302 у всех живых аккаунтов, включая
негодные. Единственный честный критерий — дойти до consent-страницы целевого
приложения. Флагнутый аккаунт логинится, открывает /dashboard и отвечает
«This account is flagged, and therefore cannot authorize a third party
application» — такой OAuth не отдаст никогда.
Использование:
python gh_oauth_check.py <pool.json> <client_id> [limit] [out.json]
Формат пула: любой список словарей с полями login/password (totp не нужен —
2FA-аккаунты помечаются `2fa-needed` и остаются в out для отдельного прогона
браузером).
Категории в выводе:
clean — доходит до формы authorize, годится
FLAGGED — флаг, выбросить
suspended — логин уводит на /suspended
2fa-needed — нужен TOTP, HTTP-проверка не решает
other/ERR — всё прочее (чужой пароль, капча, сеть)
"""
import json
import re
import sys
import requests
UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36")
def check(sess, login, password, client_id):
sess.cookies.clear()
r = sess.get("https://github.com/login", timeout=25)
m = re.search(r'name="authenticity_token" value="([^"]+)"', r.text)
if not m:
return "no-token", ""
rr = sess.post("https://github.com/session", timeout=30, allow_redirects=True, data={
"login": login, "password": password,
"authenticity_token": m.group(1), "commit": "Sign in"})
if "two-factor" in rr.url:
return "2fa-needed", rr.url
if "suspended" in rr.url:
return "suspended", rr.url
au = sess.get(
"https://github.com/login/oauth/authorize?client_id=%s&state=probe&scope=user:email" % client_id,
timeout=30, allow_redirects=True)
body = au.text
if "flagged" in body:
return "FLAGGED", au.url
if "authorize" in au.url or "Authorize" in body:
return "clean", au.url
return "other", au.url
def main():
pool_path, client_id = sys.argv[1], sys.argv[2]
limit = int(sys.argv[3]) if len(sys.argv) > 3 else 25
out_path = sys.argv[4] if len(sys.argv) > 4 else "gh_oauth_candidates.json"
pool = json.load(open(pool_path, encoding="utf-8"))
sess = requests.Session()
sess.headers.update({"User-Agent": UA})
out = []
for acc in pool[:limit]:
login = acc.get("login")
try:
status, url = check(sess, login, acc.get("password", ""), client_id)
except Exception as e:
status, url = "ERR", repr(e)[:70]
print("%-32s %-11s %s" % (login, status, url[:60]), flush=True)
if status in ("clean", "2fa-needed", "other"):
item = dict(acc)
item["status"] = status
out.append(item)
json.dump(out, open(out_path, "w", encoding="utf-8"), ensure_ascii=False, indent=1)
clean = sum(1 for a in out if a.get("status") == "clean")
print("saved %s: clean %d of %d checked" % (out_path, clean, min(limit, len(pool))))
if __name__ == "__main__":
main()