1. Fiddler
1. 安装
点击下载
点击安装即可
2. 使用
- 配置https权限
1. 替换JS代码
- devtool F12弹窗
- 将script中src链接的JS文件替换成空的文件
2. 其他使用
2. PyExecJS模块
没有三方JS出现,代码中很少其他JS原生内容意外的内容
1. 安装
- 安装NodeJS
下载地址
下载后,更改安装目录,next安装即可
node -v
npm -v
可以看到版本证明安装成功
2. 下载pyexecjs
pip install pyexecjs -i https://pypi.tuna.tsinghua.edu.cn/simple
下载成功
import execjs
print(execjs.get().name)
- 直接使用execjs.eval()执行JS代码
import execjs
res = execjs.eval("""
//JS代码
"Jack-Mary-Tom-Jary".split("-")
""")
- 使用compile编译代码,然后使用call访问代码中的函数
JS = """
function fn(a,b){
return a + b
}
"""
exe = execjs.compile(JS)
res = exe.call("fn",5,6)
print(res)
案例:百度翻译
-
抓包,找到翻译的api
-
找到需要传的heads和data
import requests
def ts():
url = "https://fanyi.baidu.com/v2transapi?from=en&to=zh"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Referer": "https://fanyi.baidu.com/",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": """BIDUPSID=60005317F05C5F020F42E423561BD481; PSTM=1693587355; BAIDUID=5F3E0D85E6E7DEEB375B752DECAE03EE:FG=1; MCITY=-218%3A; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=VpkbUpJcVVsfkxUY3BJWDcyRnZpa3Y1THZ-S3ZOMGUwYnZ3TXhoZVdhSi1zd3BtSVFBQUFBJCQAAAAAAAAAAAEAAAC7Wz2pcXFfMTE1OTU1ODY4NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH4m42V-JuNla; BDUSS_BFESS=VpkbUpJcVVsfkxUY3BJWDcyRnZpa3Y1THZ-S3ZOMGUwYnZ3TXhoZVdhSi1zd3BtSVFBQUFBJCQAAAAAAAAAAAEAAAC7Wz2pcXFfMTE1OTU1ODY4NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH4m42V-JuNla; H_WISE_SIDS_BFESS=39713_39732_39780_39679_39836_39843_39902_39909_39935_39937_39932_39940_39930_39784; BAIDUID_BFESS=5F3E0D85E6E7DEEB375B752DECAE03EE:FG=1; ZFY=iNGTL2VyG:AG:ALkNJG0wLM6MN0:A4v6HmehLKL303b1wk:C; newlogin=1; H_WISE_SIDS=40171_40079_40300_40366_40379_40415_40446_40465_40457_40317_39662_40488_40510_40513_40398_60037_60028_60047; BDRCVFR[8-v0-fh6g3m]=mk3SLVN4HKm; H_PS_PSSID=40171_40079_40300_40366_40379_40415_40446_40465_40459_40457_39662_40488_40510_40513_40398_60037_60028_60047; BA_HECTOR=812ka48la1052120012ka42gt4n26q1j0lfrg1s; PSINO=6; delPer=0; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; smallFlowVersion=old; RT="z=1&dm=baidu.com&si=80dccff4-38cc-4529-945e-f94a25accd03&ss=luh139b7&sl=5&tt=31j&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=3y6u&ul=6jeq&hd=6jin"; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1711980978; APPGUIDE_10_7_2=1; Hm_lvt_246a5e7d3670cfba258184e42d902b31=1711981023; Hm_lpvt_246a5e7d3670cfba258184e42d902b31=1711981024; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1711981163; ab_sr=1.0.1_MTkxZjYyYzhhYzgyMDUzZmUyODMwNDkzMDgxNzc3OThiYWRjOWE1ZTQ3NGEwOGI0NDdjMWRmMjYxYWNhNjJlOTc1NDMyMjYyNmQ5ZDc4ODdkYWE0MzNmNjdiNTRiNjNjODlhZDliNTZlNzgyZWIzNTgyOTA1NDUxYzA4NDczMDg2NzQ3MDVkMTVlYmJkOTEzZmYzMGRjY2YwNDZjZjI5NzZhYzRkZjljYzYyMjIzMzNlNmU0MWI3NGViYTBkY2VlODc3NDJjOWNhZTVkNDE5YTkwMWZlOWM1OGZmMDBlYjA="""
}
data = {
"from": "en",
"to": "zh",
"query": "i'm Jack", #传入输入
"simple_means_flag": "3",
"sign": "116317.337260",#动态变更,获取JS代码生成
"token": "7d01311ff6381b2b98f6ebdf5a69215e", # JS获取token
"domain": "common",
"ts": "1711981163068"
}
res = requests.post(
url=url,
headers=headers,
data=data
)
print(res.json())
if __name__ == '__main__':
ts()
- 修改需要翻译的句子,发现响应是未知错误,得知参数需要变化
做JS逆向解决
- 找到生成变更数据的JS代码
source中找JS代码
- 在initator中根据发送请求的顺序,找到JS发送翻译请求部分
initiantor找到请求发送顺序
页面发送翻译请求的地方
-
全局搜索参数simple_means_flag (具体原因是搜索结果少,比较好找)
-
找到sign的位置,打断点往下找
-
找到一个JS闭包函数,这就是生成sign的JS代码
-
拷贝到本地
-
python使用execjs运行JS代码
-
代码报错
execjs._exceptions.ProgramError: ReferenceError: window is not defined
- 根据报错信息找到事JS代码中存在window的原因
代码网上翻可以看到
var d = “”.concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107))
Console中输代码可以得到d = ‘gtk’
13. 页面源码中搜索gtk
14. 替换JS中window[d]替换成 “320305.131321201”,运行python报错
ReferenceError: n is not defined
-
找到source中的源码,将n的内容加到JS中
-
运行py,获取sign的值
-
获取token
JS代码中找不到,在页面源代码中找下
last代码
import requests
import execjs
def ts(s,sign):
url = "https://fanyi.baidu.com/v2transapi?from=en&to=zh"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Referer": "https://fanyi.baidu.com/",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": """BIDUPSID=60005317F05C5F020F42E423561BD481; PSTM=1693587355; BAIDUID=5F3E0D85E6E7DEEB375B752DECAE03EE:FG=1; MCITY=-218%3A; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=VpkbUpJcVVsfkxUY3BJWDcyRnZpa3Y1THZ-S3ZOMGUwYnZ3TXhoZVdhSi1zd3BtSVFBQUFBJCQAAAAAAAAAAAEAAAC7Wz2pcXFfMTE1OTU1ODY4NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH4m42V-JuNla; BDUSS_BFESS=VpkbUpJcVVsfkxUY3BJWDcyRnZpa3Y1THZ-S3ZOMGUwYnZ3TXhoZVdhSi1zd3BtSVFBQUFBJCQAAAAAAAAAAAEAAAC7Wz2pcXFfMTE1OTU1ODY4NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH4m42V-JuNla; H_WISE_SIDS_BFESS=39713_39732_39780_39679_39836_39843_39902_39909_39935_39937_39932_39940_39930_39784; BAIDUID_BFESS=5F3E0D85E6E7DEEB375B752DECAE03EE:FG=1; ZFY=iNGTL2VyG:AG:ALkNJG0wLM6MN0:A4v6HmehLKL303b1wk:C; newlogin=1; H_WISE_SIDS=40171_40079_40300_40366_40379_40415_40446_40465_40457_40317_39662_40488_40510_40513_40398_60037_60028_60047; BDRCVFR[8-v0-fh6g3m]=mk3SLVN4HKm; H_PS_PSSID=40171_40079_40300_40366_40379_40415_40446_40465_40459_40457_39662_40488_40510_40513_40398_60037_60028_60047; BA_HECTOR=812ka48la1052120012ka42gt4n26q1j0lfrg1s; PSINO=6; delPer=0; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; smallFlowVersion=old; RT="z=1&dm=baidu.com&si=80dccff4-38cc-4529-945e-f94a25accd03&ss=luh139b7&sl=5&tt=31j&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=3y6u&ul=6jeq&hd=6jin"; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1711980978; APPGUIDE_10_7_2=1; Hm_lvt_246a5e7d3670cfba258184e42d902b31=1711981023; Hm_lpvt_246a5e7d3670cfba258184e42d902b31=1711981024; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1711981163; ab_sr=1.0.1_MTkxZjYyYzhhYzgyMDUzZmUyODMwNDkzMDgxNzc3OThiYWRjOWE1ZTQ3NGEwOGI0NDdjMWRmMjYxYWNhNjJlOTc1NDMyMjYyNmQ5ZDc4ODdkYWE0MzNmNjdiNTRiNjNjODlhZDliNTZlNzgyZWIzNTgyOTA1NDUxYzA4NDczMDg2NzQ3MDVkMTVlYmJkOTEzZmYzMGRjY2YwNDZjZjI5NzZhYzRkZjljYzYyMjIzMzNlNmU0MWI3NGViYTBkY2VlODc3NDJjOWNhZTVkNDE5YTkwMWZlOWM1OGZmMDBlYjA="""
}
data = {
"from": "en",
"to": "zh",
"query": s, #传入输入
"simple_means_flag": "3",
"sign": sign,#动态变更,获取JS代码生成
"token": "7d01311ff6381b2b98f6ebdf5a69215e", # JS获取token
"domain": "common",
"ts": "1711981163068"
}
res = requests.post(
url=url,
headers=headers,
data=data
)
print(res.json())
def get_sign(s):
with open(file="./test.js", mode="r", encoding="utf-8") as f:
js = f.read()
exe = execjs.compile(js)
sign = exe.call("fn", s)
return sign
if __name__ == '__main__':
s = input("请输入需要翻译的句子:")
sign = get_sign(s)
ts(s,sign)