reywechat-hook 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2023-10-17
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : WeChat client control hook method set.
9
+
10
+ Folders
11
+ -------
12
+ data : Data file.
13
+
14
+ Modules
15
+ -------
16
+ rall : All methods.
17
+ rhook : Hook methods.
18
+ """
Binary file
Binary file
reywechat_hook/rall.py ADDED
@@ -0,0 +1,11 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2023-10-19
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : All methods.
9
+ """
10
+
11
+ from .rhook import *
@@ -0,0 +1,335 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2026-07-02
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Hook methods.
9
+ """
10
+
11
+ from typing import Any, Dict, Literal, Callable, Final
12
+ from os.path import exists as os_exists, abspath as os_abspath, split as os_split, join as os_join
13
+ from json import dumps as json_dumps, loads as json_loads
14
+ import ctypes
15
+ from reykit.rbase import throw
16
+ from reykit.rnet import listen_socket, send_socket
17
+ from reykit.rsys import get_sys_bits
18
+
19
+ __all__ = (
20
+ 'SEND_PORT',
21
+ 'RECEIVE_PORT',
22
+ 'WeChatHookLoader',
23
+ 'WeChatHook'
24
+ )
25
+
26
+ SEND_PORT = 49152
27
+ 'Hook program listening port send message.'
28
+ RECEIVE_PORT = 49153
29
+ 'Main program listening port receive message.'
30
+
31
+ _path_dir: Final[str] = os_split(os_abspath(__file__))[0]
32
+ _path_helper_dll: Final[str] = os_join(_path_dir, 'Helper_4.1.2.17.dll')
33
+ _path_loader_dll: Final[str] = os_join(_path_dir, 'Loader_4.1.2.17.dll')
34
+ _socket_id: int | None = None
35
+ _recv_callback: Callable[[int, int, dict], None] | None = None
36
+ def _c_str(text: str | bytes) -> ctypes.c_char_p:
37
+ return ctypes.c_char_p(text.encode('utf-8'))
38
+
39
+ @ctypes.WINFUNCTYPE(None, ctypes.c_void_p)
40
+ def _on_connect(client_id: int) -> None:
41
+ """
42
+ Called when a new WeChat socket client is connected.
43
+
44
+ Parameters
45
+ ----------
46
+ client_id : Socket client ID.
47
+ """
48
+
49
+ # Handle.
50
+ global _socket_id
51
+ _socket_id = int(client_id)
52
+
53
+ @ctypes.WINFUNCTYPE(None, ctypes.c_long, ctypes.c_char_p, ctypes.c_ulong)
54
+ def _on_recv(client_id: int, data: bytes, length: int) -> None:
55
+ """
56
+ Called when WeChat sends a message.
57
+
58
+ Parameters
59
+ ----------
60
+ client_id : Socket client ID.
61
+ data : Raw JSON data pointer.
62
+ length : Data length.
63
+ """
64
+
65
+ # Handle.
66
+ global _recv_callback
67
+ if not data or not _recv_callback:
68
+ return
69
+ try:
70
+ raw = ctypes.string_at(data, length)
71
+ text = raw.decode('utf-8', errors='ignore').rstrip('\x00')
72
+ if not text:
73
+ return
74
+ try:
75
+ packet = json_loads(text)
76
+ except Exception:
77
+ end = text.rfind('}')
78
+ if end == -1:
79
+ return
80
+ packet = json_loads(text[:end + 1])
81
+ _recv_callback(
82
+ int(client_id),
83
+ packet.get('type'),
84
+ packet.get('data'),
85
+ )
86
+ except Exception:
87
+ pass
88
+
89
+ @ctypes.WINFUNCTYPE(None, ctypes.c_ulong)
90
+ def _on_close(client_id: int) -> None:
91
+ """
92
+ Called when a socket client disconnects.
93
+
94
+ Parameters
95
+ ----------
96
+ client_id : Socket client ID.
97
+ """
98
+
99
+ # Handle.
100
+ global _socket_id
101
+ if _socket_id == int(client_id):
102
+ _socket_id = None
103
+
104
+ class WeChatHookLoader(object):
105
+ """
106
+ WeChat client hook loader.
107
+ """
108
+
109
+ OFFSET_SOCKET = 0xB080
110
+ OFFSET_SEND = 0xAF90
111
+ OFFSET_INJECT = 0xCC10
112
+ OFFSET_DESTROY = 0xC540
113
+ OFFSET_UTF8 = 0xC680
114
+
115
+ def __init__(self) -> None:
116
+ """
117
+ Build instance attributes.
118
+ """
119
+
120
+ # Build.
121
+ if not os_exists(_path_loader_dll):
122
+ raise FileNotFoundError(_path_loader_dll)
123
+ dll = ctypes.WinDLL(_path_loader_dll)
124
+ self.base = dll._handle
125
+ self._init_shared_memory()
126
+ self._enable_utf8()
127
+ self._init_socket()
128
+
129
+ def _call(self, offset: int, argtypes: list, restype) -> ctypes._CFuncPtr:
130
+ """
131
+ Call function by offset.
132
+
133
+ Parameters
134
+ ----------
135
+ offset : Function offset relative to the Loader.dll base address.
136
+ argtypes : List of ctypes parameter types.
137
+ restype : ctypes return type.
138
+
139
+ Returns
140
+ -------
141
+ Callable ctypes function object.
142
+ """
143
+
144
+ # Function.
145
+ func = ctypes.WINFUNCTYPE(restype, *argtypes)
146
+ result = func(self.base + offset)
147
+
148
+ return result
149
+
150
+ @staticmethod
151
+ def _init_shared_memory() -> None:
152
+ """
153
+ Initialize create shared memory required by Loader/Helper.
154
+ """
155
+
156
+ # Initialize.
157
+ k32 = ctypes.windll.kernel32
158
+ mapping = k32.CreateFileMappingA(
159
+ -1, None, 0x04, 0, 33, b'windows_shell_global__'
160
+ )
161
+ if not mapping:
162
+ return
163
+ addr = k32.MapViewOfFile(mapping, 0x000F001F, 0, 0, 0)
164
+ if not addr:
165
+ return
166
+ key = b'3101b223dca7715b0154924f0eeeee20'
167
+ ctypes.memmove(addr, ctypes.create_string_buffer(key), len(key))
168
+
169
+ def _enable_utf8(self) -> None:
170
+ """
171
+ Enable UTF-8 communication mode.
172
+ """
173
+
174
+ # Enable.
175
+ self._call(self.OFFSET_UTF8, [], ctypes.c_bool)()
176
+
177
+ def _init_socket(self) -> None:
178
+ """
179
+ Initialize socket callbacks.
180
+ """
181
+
182
+ # Initialize.
183
+ self._call(
184
+ self.OFFSET_SOCKET,
185
+ [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p],
186
+ ctypes.c_bool,
187
+ )(_on_connect, _on_recv, _on_close)
188
+
189
+ def inject(self) -> int:
190
+ """
191
+ Inject `DLL` file into WeChat.
192
+
193
+ Returns
194
+ -------
195
+ Process ID of the injected WeChat process.
196
+ """
197
+
198
+ # Inject.
199
+ process_id = self._call(
200
+ self.OFFSET_INJECT,
201
+ [ctypes.c_char_p],
202
+ ctypes.c_uint32,
203
+ )(_c_str(_path_helper_dll))
204
+
205
+ return process_id
206
+
207
+ def send(self, client_id: int, text: str) -> bool:
208
+ """
209
+ Send raw JSON message to WeChat.
210
+
211
+ Args:
212
+ client_id (int): socket client id
213
+ text (str): JSON string
214
+
215
+ Returns:
216
+ bool: success
217
+ """
218
+ return self._call(
219
+ self.OFFSET_SEND,
220
+ [ctypes.c_uint32, ctypes.c_char_p],
221
+ ctypes.c_bool,
222
+ )(client_id, _c_str(text))
223
+
224
+ def destroy(self) -> bool:
225
+ """
226
+ Release Loader resources.
227
+
228
+ Returns:
229
+ bool
230
+ """
231
+ return self._call(self.OFFSET_DESTROY, [], ctypes.c_bool)()
232
+
233
+ class WeChatHook(object):
234
+ """
235
+ WeChat hook.
236
+ """
237
+
238
+ def __init__(self):
239
+ """
240
+ Build instance attributes.
241
+ """
242
+
243
+ # Check System bits.
244
+ sys_bits = get_sys_bits()
245
+ if sys_bits != 32:
246
+ throw(AssertionError, text='python must be 32-bit')
247
+
248
+ # Build.
249
+ self.loader: WeChatHookLoader | None = None
250
+
251
+ def inject(self) -> bool:
252
+ """
253
+ Inject WeChat hook.
254
+
255
+ Returns
256
+ -------
257
+ Whether it is successful.
258
+ """
259
+
260
+ # Start.
261
+ self.loader = WeChatHookLoader()
262
+ result = bool(self.loader.inject())
263
+
264
+ return result
265
+
266
+ def register_callback(
267
+ self,
268
+ callback: Callable[[int, int, Dict[str, Any]], None],
269
+ ) -> None:
270
+ """
271
+ Register message callback.
272
+
273
+ Parameters
274
+ ----------
275
+ callback : Callback function.
276
+ """
277
+
278
+ # Register.
279
+ global _recv_callback
280
+ _recv_callback = callback
281
+
282
+ def send_payload(self, payload: Dict[str, Any]) -> bool:
283
+ """
284
+ Send raw protocol payload.
285
+
286
+ Parameters
287
+ ----------
288
+ payload : WeChat protocol JSON.
289
+
290
+ Returns
291
+ -------
292
+ Whether it is successful.
293
+ """
294
+
295
+ # Check.
296
+ if not self.loader or not _socket_id:
297
+ return False
298
+
299
+ # Send.
300
+ result = self.loader.send(
301
+ _socket_id,
302
+ json_dumps(payload, ensure_ascii=False),
303
+ )
304
+
305
+ return result
306
+
307
+ def start_socket(self) -> None:
308
+ """
309
+ Start socket, will block.
310
+ """
311
+
312
+ # Receive.
313
+ self.register_callback(
314
+ lambda _, request_type, request_data: send_socket(
315
+ '127.0.0.1',
316
+ RECEIVE_PORT,
317
+ {
318
+ 'type': request_type,
319
+ 'data': request_data
320
+ }
321
+ )
322
+ )
323
+
324
+ # Send.
325
+ def handler(data: Literal['inject'] | Dict[str, Any]) -> None:
326
+ if data == 'inject':
327
+ self.inject()
328
+ else:
329
+ self.send_payload(data)
330
+ listen_socket(
331
+ '127.0.0.1',
332
+ SEND_PORT,
333
+ handler
334
+ )
335
+ print(f'start listening on port {RECEIVE_PORT}')
@@ -0,0 +1,29 @@
1
+ Metadata-Version: 2.4
2
+ Name: reywechat-hook
3
+ Version: 1.0.0
4
+ Summary: WeChat client control hook method set.
5
+ Project-URL: Homepage, https://www.reyxbo.com
6
+ Project-URL: Repository, https://github.com/reyxbo/reywechat-hook-py.git
7
+ Author-email: Rey <reyxbo@163.com>
8
+ License: Copyright 2025 ReyXBo
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ License-File: LICENSE
16
+ Keywords: helper,hook,rey,reyxbo,robot,wechat
17
+ Requires-Python: >=3.12
18
+ Requires-Dist: reykit
19
+ Description-Content-Type: text/markdown
20
+
21
+ # reywechat
22
+
23
+ > WeChat client control method set.
24
+
25
+ ## Install
26
+
27
+ ```
28
+ pip install reywechat
29
+ ```
@@ -0,0 +1,9 @@
1
+ reywechat_hook/__init__.py,sha256=19ThR9rL7tvWp6yGAPfEeJ0-oz8PUmxVvO8ZWHJRORU,281
2
+ reywechat_hook/rall.py,sha256=ttQGp2AV7s5kqBD2uiP81SMbCJaMVtuV3Y19eLBmoUc,176
3
+ reywechat_hook/rhook.py,sha256=hVWkd1wSEZu4Oo8wW21X6pCaVIiuSGwvgvEyubOv-oQ,8501
4
+ reywechat_hook/data/Helper_4.1.2.17.dll,sha256=hM3KSFR5j4SBrtfP8VbUUxiduHa87SUVsI9FXefcRhw,9518080
5
+ reywechat_hook/data/Loader_4.1.2.17.dll,sha256=9EKxiL4NnNJgw4WWU5EMfajBJQSh8kji__5fxt2uDiU,4532224
6
+ reywechat_hook-1.0.0.dist-info/METADATA,sha256=r2wmy8FbmQxjvlYNo7xET8dSI8su4ljjh30UZ_lBBUM,1618
7
+ reywechat_hook-1.0.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
8
+ reywechat_hook-1.0.0.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
9
+ reywechat_hook-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,7 @@
1
+ Copyright 2025 ReyXBo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.