搜狗翻译api抓取分析

本文最后更新于:2024年6月7日 中午

最近开学之后,博主更新博客的时间变少了,今天五一劳动节了,随手做一个吧,关于百度翻译api那个,后面我尝试转一下语言,进行处理

那么今天就要进行翻译搜狗的逆向吧


抓取信息

首先打开搜狗翻译的网站点我跳转

右键你键盘上的F12,点击检查->网络 开始抓包,首先我们先翻译一个东西就拿你好,来举个例子

这个resultapi,就是翻译的api,点击payload看看参数,这里先翻译两次,找到变和不变的


分析参数

text,这个好理解,就是翻译的东西

suuid是加密参数,需要解密,其他参数都不变

s参数分析

首先来看s参数,可以看到是引用了这个文件

点进去,如果直接搜s是不行的,不过从s的长度,疑似一个md5摘要算法,那我们搜索一下md5

这里就是md5调用的地方了,先勾上,然后去翻译一个东西,可以得到t=autoen西瓜1109984457

auto为自动检测源语言,en是翻译的目标语言,至于后面的数字我们暂时不知道具体意思,那直接打this.md5这个部分

得不到n这个函数的意义,去其他js文件看看,这个app.js很可疑

点进去

从3162行可以看到,下面的s等于D,同时D也是由V函数和p m l c这四个地方组成的,我们打一下断点

C就是我们想要的地方,同时跟x.secretcode是一样,我们需要找到x函数的位置

这里我苦恼了半天也没找到x的位置,然后翻译了几次,发现数字都没变,应该是个定值

这样就得到了加密公式

1
源语言+翻译语言+翻译文本+109984457

uuid参数分析

根据多次翻译的结果,uuid也是定值


成品

分析完成之后,接下来开始写代码了

TS代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import CryptoJS from 'crypto-js';

const url : string = "https://fanyi.sogou.com/api/transpc/text/result"; // url链接
const tr_text : string = "西红柿"; // 翻译内容
const from_text : string = "auto" //翻译源语言 默认auto
const to_text : string = "en" // 翻译目标语言
const cookie_ : string = "ABTEST=0|1714661977|v17; SNUID=F6B3059AE2E7F69838F8F0FAE2DBB8A9; SUID=1452E778EF53A00A000000006633AA59; wuid=1714661977888; FQV=e1a1827db43089b1cdd50f9f99397d3c; translate.sess=4a702d60-5da6-4219-b3d6-9640f5b73add; SUV=1714661982306; SGINPUT_UPSCREEN=1714661982341"; // coookie
const key : string = CryptoJS.MD5(from_text + to_text + tr_text + "109984457").toString()

var header = {
method: 'POST',
headers: {
'Content-Type': 'application/json', // 自定义请求头
'cookie': cookie_,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
},
body:JSON.stringify({
"from":from_text,
"to":to_text,
"text":tr_text,
"client":"pc",
"fr":"browser_pc",
"needQc":1,
"s":key,
"uuid":"0f8e6ff9-99d9-40c1-99ba-e359f2fa4c16",
"exchange":false
})
}
fetch(url, header)
.then(response => response.json())
.then(data => {
if(data["status"]!=0){
console.log("翻译失败,原因",data["status"]);
}else{

console.log("原文:",tr_text,"译文:",data["data"]["wordCard"]["usualDict"][0]['values'][0])
}
})
.catch(error => {
console.error('There was a problem with your fetch operation:', error);
});

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from_text = "auto" # 翻译的源语言
to_text = "en" # 翻译的目标内容
text = "西红柿"


cookie_ = "ABTEST=0|1714661977|v17; SNUID=F6B3059AE2E7F69838F8F0FAE2DBB8A9; SUID=1452E778EF53A00A000000006633AA59; wuid=1714661977888; FQV=e1a1827db43089b1cdd50f9f99397d3c; translate.sess=4a702d60-5da6-4219-b3d6-9640f5b73add; SUV=1714661982306; SGINPUT_UPSCREEN=1714661982341"

import httpx, hashlib

key = from_text + to_text + text + "109984457"
md5hash = hashlib.md5(key.encode("utf_8")).hexdigest()

try:
r = httpx.post(
url="https://fanyi.sogou.com/api/transpc/text/result",
headers={
'Content-Type': 'application/json',
'cookie': cookie_,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
},
data={
"from":from_text,
"to":to_text,
"text":text,
"client":"pc",
"fr":"browser_pc",
"needQc":1,
"s":md5hash,
"uuid":"0f8e6ff9-99d9-40c1-99ba-e359f2fa4c16",
"exchange":False
}
)
if r["status"] != 0:
print("状态错误")
else:
print(f'原文:{text},译文:{r["data"]["wordCard"]["usualDict"][0]["values"][0]}')
except (httpx.ConnectError, httpx.ConnectTimeout):
print("网络错误,翻译失败")

这期先到这里了


搜狗翻译api抓取分析
http://blog.bingyue.top/2024/05/03/saogou_fanyi/
作者
bingyue
发布于
2024年5月3日
许可协议