steam动态令牌源码分享(python版本)
由于去年做了一个小项目,需要自动化的程序,所以研究起来steam的东西,steam游戏大家都不陌生,当你绑定手机之后,手机会有一个steam动态令牌,动态令牌是根据时间戳和你账号的SharedSecret参数进行加密的,一般绑定手机之后,他们会在你的手机上存放一个文件,里面就是保存你的SharedSecret参数的值,国外有个大神给搞出来令牌管理器,并且开源了,供大家使用,但是由于我做的项目需要实现steam自动登录自动发货,自动扫货,自动对比价等一些列功能,所以拿国外大神的源码研究一番,大神是用c#写的,但是我用的语言是python,所以我就打算把他的源码给转成python,供自己好实现项目功能,所以需要用的到朋友可以直接拿就好了。不用自己在研究c#代码再自己转换成python浪费时间。
下面放代码:
import base64
import hmac
import json
from hashlib import sha1
import aiohttp
from aiohttp import ClientSession
async def get_steam_server_time():
url = "https://api.steampowered.com/ITwoFactorService/QueryTime/v0001"
headers = {
"Host": "api.steampowered.com",
"Content-Length": "9",
"Expect": "100-continue",
"Connection": "Keep-Alive"
}
data ={
"steamid": "0"
}
conn = aiohttp.TCPConnector(verify_ssl=False)
async with ClientSession(connector=conn) as session:
async with session.post(url=url, headers=headers, data=data, allow_redirects=False,
timeout=10) as response:
response = await response.read()
response = response.decode()
response = json.loads(response)
server_time = response["response"]["server_time"]
return server_time
async def GenerateSteamGuardCodeForTime(time, SharedSecret):
time = int(time)
time = int(time / 30)
token_code = ""
steamGuardCodeTranslations = [50, 51, 52, 53, 54, 55, 56, 57, 66, 67, 68, 70, 71, 72, 74, 75, 77, 78, 80, 81, 82,
84, 86, 87, 88, 89]
key_byte = base64.standard_b64decode(SharedSecret)
code_byte = int(time).to_bytes(length=8, byteorder='big')
hmac_code_tmp = hmac.new(key_byte, code_byte, sha1)
hmac_code = hmac_code_tmp.digest()
b = hmac_code[19] & 0xF
codePoint = (hmac_code[b] & 0x7F) << 24 | (hmac_code[b + 1] & 0xFF) << 16 | (hmac_code[b + 2] & 0xFF) << 8 | (
hmac_code[b + 3] & 0xFF)
for i in range(5):
p = steamGuardCodeTranslations[int(codePoint) % len(steamGuardCodeTranslations)]
codePoint /= len(steamGuardCodeTranslations)
s = hex(p)
t = await hex_to_str(s)
token_code = token_code + str(t)
return token_code
async def hex_to_str(s):
return ''.join([chr(i) for i in [int(b, 16) for b in s.split(' ')]])
async def get_steam_token_main(SharedSecret):
server_time = await get_steam_server_time()
token_code = await GenerateSteamGuardCodeForTime(server_time, SharedSecret)
return token_code
我使用异步写的,所以用的小伙伴注意了 调用时候要带上SharedSecret参数。直接拿来用就好了。
代码其实很简单,第一个get_steam_server_time()就是获取steam服务器时间,拿到时间戳然后下一步进行加密。
到此结束。