|
| 1 | +# __author__ = "zok" [email protected] |
| 2 | +# Date: 2020/7/24 Python:3.7 |
| 3 | + |
| 4 | +import requests |
| 5 | +import time |
| 6 | +import random |
| 7 | +import json |
| 8 | +import base64 |
| 9 | +import pyDes |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | + |
| 13 | +class TripleDesUtils: |
| 14 | + |
| 15 | + def encryption(self, data: str, key, iv) -> str: |
| 16 | + """3des 加密 |
| 17 | + """ |
| 18 | + _encryption_result = pyDes.triple_des(key, pyDes.CBC, iv, None, pyDes.PAD_PKCS5).encrypt(data) |
| 19 | + _encryption_result = self._base64encode(_encryption_result).decode() |
| 20 | + return _encryption_result |
| 21 | + |
| 22 | + def decrypt(self, data: str, key, iv) -> str: |
| 23 | + """3des 解密 |
| 24 | + """ |
| 25 | + data = self._base64decode(data) |
| 26 | + _decrypt_result = pyDes.triple_des(key, pyDes.CBC, iv, None, pyDes.PAD_PKCS5).decrypt(data).decode('utf-8') |
| 27 | + return _decrypt_result |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def _base64encode(data): |
| 31 | + try: |
| 32 | + _b64encode_result = base64.b64encode(data) |
| 33 | + except Exception as e: |
| 34 | + raise Exception(f"base64 encode error:{e}") |
| 35 | + return _b64encode_result |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def _base64decode(data): |
| 39 | + try: |
| 40 | + _b64decode_result = base64.b64decode(data) |
| 41 | + except Exception as e: |
| 42 | + raise Exception(f"base64 decode error:{e}") |
| 43 | + return _b64decode_result |
| 44 | + |
| 45 | + |
| 46 | +class WenShu: |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + self.js = None |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def get_now_data(): |
| 53 | + """时间 |
| 54 | + """ |
| 55 | + return datetime.now().strftime('%Y%m%d') |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def random_key(): |
| 59 | + """字符串 |
| 60 | + """ |
| 61 | + random_str = '' |
| 62 | + base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789' |
| 63 | + length = len(base_str) - 1 |
| 64 | + for i in range(24): |
| 65 | + random_str += base_str[random.randint(0, length)] |
| 66 | + return random_str |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def make_id(): |
| 70 | + """id |
| 71 | + """ |
| 72 | + return datetime.now().strftime('%Y%m%d%H%M%S') |
| 73 | + |
| 74 | + def make_cipher_text(self): |
| 75 | + """生成 ciphertext |
| 76 | + """ |
| 77 | + time_13 = str(int(round(time.time() * 1000))) |
| 78 | + key = self.random_key() |
| 79 | + now = self.get_now_data() |
| 80 | + _str = des3.encryption(time_13, key, now) |
| 81 | + _str = key + now + _str |
| 82 | + new_str = '' |
| 83 | + for i in _str: |
| 84 | + if i != 1: |
| 85 | + new_str += " " |
| 86 | + new_str += str(bin(ord(i))[2:]) |
| 87 | + |
| 88 | + msg = """【key生成】: {key}\n【now生成】: {now}\n【_str生成】: {_str}\n【ciphertext生成】: {ciphertext}""".format(key=key, |
| 89 | + now=now, |
| 90 | + _str=_str, |
| 91 | + ciphertext=new_str) |
| 92 | + print(msg) |
| 93 | + |
| 94 | + return new_str.strip() |
| 95 | + |
| 96 | + def make_request(self): |
| 97 | + """生成明文的请求 data 内容 |
| 98 | + 【这里需要根据实际需求修改请求内容】自行抓包研究!! |
| 99 | + """ |
| 100 | + info = { |
| 101 | + "id": self.make_id(), # 年月日时分秒 |
| 102 | + "command": "queryDoc", # 固定 |
| 103 | + "params": { |
| 104 | + "devid": "41d861ffe5b347d28454dc3f07dd4212", # 设备号 |
| 105 | + "devtype": "1", |
| 106 | + "ciphertext": self.make_cipher_text(), |
| 107 | + "pageSize": "20", |
| 108 | + "sortFields": "s50:desc", # 固定 |
| 109 | + "pageNum": "1", |
| 110 | + "queryCondition": [{ |
| 111 | + "key": "s8", |
| 112 | + "value": "02" |
| 113 | + }] # 关键词 + 搜索文本的类型; |
| 114 | + } |
| 115 | + } |
| 116 | + return info |
| 117 | + |
| 118 | + def to_index(self): |
| 119 | + url = 'http://wenshuapp.court.gov.cn/appinterface/rest.q4w' |
| 120 | + headers = { |
| 121 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 122 | + 'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 9; MIX 2 MIUI/V11.0.2.0.PDECNXM)', |
| 123 | + 'Host': 'wenshuapp.court.gov.cn', |
| 124 | + 'Connection': 'Keep-Alive', |
| 125 | + 'Accept-Encoding': 'gzip', |
| 126 | + } |
| 127 | + txt = str(self.make_request()) |
| 128 | + |
| 129 | + request = base64.b64encode(txt.encode('utf-8')).decode('utf-8') |
| 130 | + data = { |
| 131 | + 'request': request |
| 132 | + } |
| 133 | + msg = """【明文请求体】: {txt}\n【密文请求体】: {data}\n【官网速度较慢,耐心等待】....""".format(txt=txt, data=data) |
| 134 | + print(msg) |
| 135 | + response = requests.post(url, headers=headers, data=data) |
| 136 | + if 'HTTP Status 503' in response.text: |
| 137 | + print('【服务器繁忙】 爬的人太多了, 请重试') |
| 138 | + exit() |
| 139 | + data = json.loads(response.text) |
| 140 | + content = data.get('data').get('content') |
| 141 | + key = data.get('data').get('secretKey') |
| 142 | + iv = self.get_now_data() |
| 143 | + msg = """【页面访问结果】: {text}\n【捕获key】:{key}\n【捕获iv】:{iv}\n【捕获content】:{content}""".format(text=response.text, |
| 144 | + key=key, iv=iv, |
| 145 | + content=content) |
| 146 | + print(msg) |
| 147 | + self.parse_html(content, key, iv) |
| 148 | + |
| 149 | + def parse_html(self, content, key, iv): |
| 150 | + _str = des3.decrypt(content, key, iv) |
| 151 | + print("【解密返回结果】:", _str) |
| 152 | + |
| 153 | + |
| 154 | +des3 = TripleDesUtils() |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + """ |
| 158 | + 《入门级安卓逆向 - 文书网app爬虫》 |
| 159 | + https://www.zhangkunzhi.com/index.php/archives/162/ |
| 160 | + """ |
| 161 | + ws = WenShu() |
| 162 | + ws.to_index() |
0 commit comments