Skip to content

Instantly share code, notes, and snippets.

@kagurazakasanae
Created December 4, 2019 12:50
Show Gist options
  • Save kagurazakasanae/a76b01f60c89aa3866dfc689345a3faf to your computer and use it in GitHub Desktop.
Save kagurazakasanae/a76b01f60c89aa3866dfc689345a3faf to your computer and use it in GitHub Desktop.
a script for backing up qq contacts and groups
#coding=utf-8
import requests, json
random_tk = "2143984438"
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
referer = "https://nonexists.qq.com"
def get_gtk(p_skey):
h = 5381
for i in p_skey:
h += (h << 5) + ord(i)
return (h & 0x7fffffff)
r = requests.get("https://localhost.ptlogin2.qq.com:4301/pt_get_uins?callback=ptui_getuins_CB&pt_local_tk=" + random_tk, headers={"User-Agent": ua, "Referer": referer, "Cookie": "pt_local_token=" + random_tk})
if r.status_code == 200:
account_list = json.loads(r.text.strip("var var_sso_uin_list=").strip(";ptui_getuins_CB(var_sso_uin_list);"))
for acc in account_list:
s = requests.Session()
requests.utils.add_dict_to_cookiejar(s.cookies, {"pt_local_token": random_tk})
print("QQ:", acc['account'], acc['nickname'])
r = s.get("https://localhost.ptlogin2.qq.com:4301/pt_get_st?clientuin=" + acc['account'] + "&callback=ptui_getst_CB&pt_local_tk=" + random_tk, headers={"User-Agent": ua, "Referer": referer})
clientuin = s.cookies.get("clientuin")
clientkey = s.cookies.get("clientkey")
if r.status_code == 200:
r = s.get("https://ssl.ptlogin2.qq.com/jump?clientuin=" + acc['account'] + "&keyindex=9&pt_aid=549000912&daid=5&u1=https%3A%2F%2Fnonexists.qq.com&pt_local_tk=" + random_tk + "&pt_3rd_aid=0&ptopt=1&style=40", headers={"User-Agent": ua, "Referer": referer})
if "ptui_qlogin_CB('0'" in r.text:
print("Login qzone req #1: Successed:", acc['account'])
exec("tmp = " + r.text.strip("ptui_qlogin_CB"))
r = s.get(tmp[1], headers={"User-Agent": ua, "Referer": referer}, allow_redirects=False)
if r.status_code == 301 or r.status_code == 302:
print("Login qzone req #2: Successed:", acc['account'])
p_skey = s.cookies.get("p_skey")
skey = s.cookies.get("skey")
print("p_skey:", p_skey)
print("skey:", skey)
g_tk = str(get_gtk(p_skey))
print("g_tk:", g_tk)
r = s.get("https://user.qzone.qq.com/" + acc['account'] + "/infocenter", headers={"User-Agent": ua})
f = r.text.find('window.g_qzonetoken = (function(){ try{return "')
if f != -1:
g_qzonetoken = r.text[f+47:f+47+76]
print("g_qzonetoken:", g_qzonetoken)
r = s.get("https://h5.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_mngfrd_get.cgi?uin=" + acc['account'] + "&g_tk=" + g_tk + "&qzonetoken=" + g_qzonetoken, headers={"User-Agent": ua})
if "_Callback" in r.text:
print("Get friend list successed:" + acc['account'])
friend_list = json.loads(r.text[10:-3])
friend_dict = {}
for gp in friend_list['gpnames']:
friend_dict[gp['gpname']] = []
for friend in friend_list['items']:
if friend['groupid'] == gp['gpid']:
friend_dict[gp['gpname']].append(friend)
with open("friends_list_" + acc['account'] + ".txt", "w", encoding="utf-8") as f:
for gp in friend_dict:
f.write(gp + "\n")
for fr in friend_dict[gp]:
f.write(" " + str(fr['uin']) + " [" + fr['name'] + "]" + " (" + fr['realname'] + ")" + "\n")
f.close()
print("Friend list dumped for QQ:", acc['account'])
else:
print("Failed to get friend list")
else:
print("Could not find g_qzonetoken")
else:
print("Login qzone req #2: Failed:", acc['account'])
else:
print("Login qzone req #1: Failed:", acc['account'])
s = requests.Session()
requests.utils.add_dict_to_cookiejar(s.cookies, {"pt_local_token": random_tk})
requests.utils.add_dict_to_cookiejar(s.cookies, {"clientuin": clientuin, "clientkey": clientkey})
r = s.get("https://ssl.ptlogin2.qq.com/jump?clientuin=" + acc['account'] + "&keyindex=9&pt_aid=715030901&daid=73&u1=https%3A%2F%2Fnonexists.qq.com&pt_local_tk=" + random_tk + "&pt_3rd_aid=0&ptopt=1&style=40", headers={"User-Agent": ua, "Referer": referer})
if "ptui_qlogin_CB('0'" in r.text:
print("Login qun req #1: Successed:", acc['account'])
exec("tmp = " + r.text.strip("ptui_qlogin_CB"))
r = s.get(tmp[1], headers={"User-Agent": ua, "Referer": referer}, allow_redirects=False)
if r.status_code == 301 or r.status_code == 302:
print("Login qun req #2: Successed:", acc['account'])
p_skey = s.cookies.get("p_skey")
skey = s.cookies.get("skey")
print("p_skey:", p_skey)
print("skey:", skey)
g_tk = str(get_gtk(skey))
print("g_tk:", g_tk)
r = s.post("https://qun.qq.com/cgi-bin/qun_mgr/get_group_list", data={"bkn": g_tk}, headers={"User-Agent": ua})
if "[errcode:" not in r.text:
qun_list = json.loads(r.text)
print("Get group list successed:" + acc['account'])
with open("qun_list_" + acc['account'] + ".txt", "w", encoding="utf-8") as f:
try:
f.write("Joined\n")
for q in qun_list['join']:
f.write(" " + str(q['gc']) + " [" + q['gn'] + "]\n")
except:
pass
try:
f.write("Managed\n")
for q in qun_list['manage']:
f.write(" " + str(q['gc']) + " [" + q['gn'] + "]\n")
except:
pass
try:
f.write("Created\n")
for q in qun_list['create']:
f.write(" " + str(q['gc']) + " [" + q['gn'] + "]\n")
except:
pass
f.close()
print("Group list dumped for QQ:", acc['account'])
else:
print("Login qun req #2: Failed:", acc['account'])
else:
print("Login qun req #1: Failed:", acc['account'])
else:
print("ClientKey req failed:", acc['account'])
else:
print("?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment