@@ -61,9 +61,22 @@ static type_recvfrom orig_recvfrom;
6161static type_write orig_write;
6262static type_read orig_read;
6363static type_close orig_close;
64- static type_SSL_write orig_SSL_write;
65- static type_SSL_read orig_SSL_read;
66- static type_SSL_free orig_SSL_free;
64+
65+ static type_SSL_write orig_SSL_write_libssl = nullptr ;
66+ static type_SSL_read orig_SSL_read_libssl = nullptr ;
67+ static type_SSL_free orig_SSL_free_libssl = nullptr ;
68+
69+ static type_SSL_write orig_SSL_write_conscrypt = nullptr ;
70+ static type_SSL_read orig_SSL_read_conscrypt = nullptr ;
71+ static type_SSL_free orig_SSL_free_conscrypt = nullptr ;
72+
73+ static type_SSL_write orig_SSL_write_ttboringssl = nullptr ;
74+ static type_SSL_read orig_SSL_read_ttboringssl = nullptr ;
75+ static type_SSL_free orig_SSL_free_ttboringssl = nullptr ;
76+
77+ static type_SSL_write orig_SSL_write_flutter = nullptr ;
78+ static type_SSL_read orig_SSL_read_flutter = nullptr ;
79+ static type_SSL_free orig_SSL_free_flutter = nullptr ;
6780
6881struct ScopedHookGuard {
6982 ScopedHookGuard () { g_is_in_hook = true ; }
@@ -104,8 +117,8 @@ std::string get_native_stack_internal() {
104117 uintptr_t offset = (uintptr_t )addr - (uintptr_t )info.dli_fbase ;
105118 const char * lib_name = strrchr (info.dli_fname , ' /' );
106119 lib_name = (lib_name != nullptr ) ? lib_name + 1 : info.dli_fname ;
107- ss << " #" << std::setw (2 ) << (ptr - buffer) << " pc "
108- << std::setw (8 ) << std::setfill (' 0' ) << std::hex << offset
120+ ss << " #" << std::setw (2 ) << (ptr - buffer) << " pc "
121+ << std::setw (8 ) << std::setfill (' 0' ) << std::hex << offset
109122 << " " << lib_name;
110123 if (info.dli_sname ) ss << " (" << info.dli_sname << " )" ;
111124 ss << " \n " ;
@@ -153,7 +166,7 @@ std::string get_cached_socket_info(int fd) {
153166 } else {
154167 return " unknown" ;
155168 }
156-
169+
157170 std::string info = std::string (ip_str) + " :" + std::to_string (port);
158171 std::lock_guard<std::mutex> lock (g_cache_mutex);
159172 g_socket_info_cache[fd] = info;
@@ -208,18 +221,18 @@ bool callback_kotlin(jlong id, bool is_write, const void *buf, size_t len, bool
208221
209222 jbyteArray jData = env->NewByteArray (len);
210223 if (jData == nullptr ) return false ;
211-
224+
212225 env->SetByteArrayRegion (jData, 0 , len, (const jbyte *)buf);
213-
226+
214227 std::string info = (!is_ssl && id > 0 ) ? get_cached_socket_info ((int )id) : " " ;
215228 jstring jInfo = info.empty () ? nullptr : env->NewStringUTF (info.c_str ());
216-
229+
217230 std::string stack = get_cached_stack (id);
218231 jstring jStack = stack.empty () ? nullptr : env->NewStringUTF (stack.c_str ());
219232
220233 bool shouldBlock = env->CallStaticBooleanMethod (
221- gNativeRequestHookClass ,
222- gOnNativeDataMethod ,
234+ gNativeRequestHookClass ,
235+ gOnNativeDataMethod ,
223236 id, is_write, jData, jInfo, jStack, is_ssl
224237 );
225238
@@ -235,7 +248,7 @@ bool callback_kotlin(jlong id, bool is_write, const void *buf, size_t len, bool
235248ssize_t hook_send (int s, const void *buf, size_t len, int flags) {
236249 if (g_is_in_hook) return orig_send (s, buf, len, flags);
237250 ScopedHookGuard guard;
238- if (callback_kotlin ((jlong)s, true , buf, len, false )) return -1 ;
251+ if (callback_kotlin ((jlong)s, true , buf, len, false )) { errno = ECONNRESET ; return -1 ; }
239252 return orig_send (s, buf, len, flags);
240253}
241254
@@ -250,7 +263,7 @@ ssize_t hook_recv(int s, void *buf, size_t len, int flags) {
250263ssize_t hook_sendto (int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) {
251264 if (g_is_in_hook) return orig_sendto (s, buf, len, flags, to, tolen);
252265 ScopedHookGuard guard;
253- if (callback_kotlin ((jlong)s, true , buf, len, false )) return -1 ;
266+ if (callback_kotlin ((jlong)s, true , buf, len, false )) { errno = ECONNRESET ; return -1 ; }
254267 return orig_sendto (s, buf, len, flags, to, tolen);
255268}
256269
@@ -264,13 +277,21 @@ ssize_t hook_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *
264277
265278ssize_t hook_write (int fd, const void *buf, size_t count) {
266279 if (g_is_in_hook || fd <= 2 ) return orig_write (fd, buf, count);
280+ if (fd < 65536 ) {
281+ uint8_t state = g_fd_cache[fd].load (std::memory_order_relaxed);
282+ if (state == 1 ) return orig_write (fd, buf, count);
283+ }
267284 ScopedHookGuard guard;
268- if (callback_kotlin ((jlong)fd, true , buf, count, false )) return -1 ;
285+ if (callback_kotlin ((jlong)fd, true , buf, count, false )) { errno = ECONNRESET ; return -1 ; }
269286 return orig_write (fd, buf, count);
270287}
271288
272289ssize_t hook_read (int fd, void *buf, size_t count) {
273290 if (g_is_in_hook || fd <= 2 ) return orig_read (fd, buf, count);
291+ if (fd < 65536 ) {
292+ uint8_t state = g_fd_cache[fd].load (std::memory_order_relaxed);
293+ if (state == 1 ) return orig_read (fd, buf, count);
294+ }
274295 ScopedHookGuard guard;
275296 ssize_t ret = orig_read (fd, buf, count);
276297 if (ret > 0 ) callback_kotlin ((jlong)fd, false , buf, ret, false );
@@ -280,7 +301,7 @@ ssize_t hook_read(int fd, void *buf, size_t count) {
280301int hook_close (int fd) {
281302 if (g_is_in_hook) return orig_close (fd);
282303 ScopedHookGuard guard;
283-
304+
284305 if (fd >= 0 && fd < 65536 ) {
285306 g_fd_cache[fd].store (0 , std::memory_order_relaxed);
286307 }
@@ -292,34 +313,38 @@ int hook_close(int fd) {
292313 return orig_close (fd);
293314}
294315
295- int hook_SSL_write (void *ssl, const void *buf, int num) {
296- if (g_is_in_hook) return orig_SSL_write (ssl, buf, num);
297- ScopedHookGuard guard;
298- if (buf != nullptr && num > 0 ) {
299- callback_kotlin (reinterpret_cast <jlong>(ssl), true , buf, num, true );
300- }
301- return orig_SSL_write (ssl, buf, num);
302- }
303-
304- int hook_SSL_read (void *ssl, void *buf, int num) {
305- if (g_is_in_hook) return orig_SSL_read (ssl, buf, num);
306- ScopedHookGuard guard;
307- int ret = orig_SSL_read (ssl, buf, num);
308- if (ret > 0 && buf != nullptr ) {
309- callback_kotlin (reinterpret_cast <jlong>(ssl), false , buf, ret, true );
310- }
311- return ret;
316+ #define DEFINE_SSL_HOOKS (suffix, orig_write_ptr, orig_read_ptr, orig_free_ptr ) \
317+ int hook_SSL_write_##suffix(void *ssl, const void *buf, int num) { \
318+ if (g_is_in_hook) return orig_write_ptr (ssl, buf, num); \
319+ ScopedHookGuard guard; \
320+ if (buf != nullptr && num > 0 ) { \
321+ callback_kotlin (reinterpret_cast <jlong>(ssl), true , buf, num, true ); \
322+ } \
323+ return orig_write_ptr (ssl, buf, num); \
324+ } \
325+ int hook_SSL_read_##suffix(void *ssl, void *buf, int num) { \
326+ if (g_is_in_hook) return orig_read_ptr (ssl, buf, num); \
327+ ScopedHookGuard guard; \
328+ int ret = orig_read_ptr (ssl, buf, num); \
329+ if (ret > 0 && buf != nullptr ) { \
330+ callback_kotlin (reinterpret_cast <jlong>(ssl), false , buf, ret, true ); \
331+ } \
332+ return ret; \
333+ } \
334+ void hook_SSL_free_##suffix(void *ssl) { \
335+ if (g_is_in_hook) { orig_free_ptr (ssl); return ; } \
336+ ScopedHookGuard guard; \
337+ { \
338+ std::lock_guard<std::mutex> lock (g_cache_mutex); \
339+ g_stack_cache.erase (reinterpret_cast <jlong>(ssl)); \
340+ } \
341+ orig_free_ptr (ssl); \
312342}
313343
314- void hook_SSL_free (void *ssl) {
315- if (g_is_in_hook) return orig_SSL_free (ssl);
316- ScopedHookGuard guard;
317- {
318- std::lock_guard<std::mutex> lock (g_cache_mutex);
319- g_stack_cache.erase (reinterpret_cast <jlong>(ssl));
320- }
321- orig_SSL_free (ssl);
322- }
344+ DEFINE_SSL_HOOKS (libssl, orig_SSL_write_libssl, orig_SSL_read_libssl, orig_SSL_free_libssl)
345+ DEFINE_SSL_HOOKS (conscrypt, orig_SSL_write_conscrypt, orig_SSL_read_conscrypt, orig_SSL_free_conscrypt)
346+ DEFINE_SSL_HOOKS (ttboringssl, orig_SSL_write_ttboringssl, orig_SSL_read_ttboringssl, orig_SSL_free_ttboringssl)
347+ DEFINE_SSL_HOOKS (flutter, orig_SSL_write_flutter, orig_SSL_read_flutter, orig_SSL_free_flutter)
323348
324349// --- Init ---
325350
@@ -337,14 +362,14 @@ void hook_func(const char *lib_name, const char *sym_name, void *hook_func, void
337362extern " C" JNIEXPORT void JNICALL
338363Java_com_close_hook_ads_hook_gc_network_NativeRequestHook_initNativeHook (JNIEnv *env, jobject thiz) {
339364 env->GetJavaVM (&gJvm );
340-
365+
341366 // 初始化线程 Key
342367 pthread_key_create (&g_thread_key, detach_current_thread);
343368
344369 jclass clazz = env->FindClass (" com/close/hook/ads/hook/gc/network/NativeRequestHook" );
345370 if (!clazz) return ;
346371 gNativeRequestHookClass = (jclass) env->NewGlobalRef (clazz);
347-
372+
348373 gOnNativeDataMethod = env->GetStaticMethodID (clazz, " onNativeData" , " (JZ[BLjava/lang/String;Ljava/lang/String;Z)Z" );
349374 if (!gOnNativeDataMethod ) return ;
350375
@@ -355,27 +380,31 @@ Java_com_close_hook_ads_hook_gc_network_NativeRequestHook_initNativeHook(JNIEnv
355380 shadowhook_init (SHADOWHOOK_MODE_UNIQUE , true );
356381
357382 // Hook Libc
358- hook_func (" libc.so" , " send" , (void *)hook_send, (void **)&orig_send);
359- hook_func (" libc.so" , " recv" , (void *)hook_recv, (void **)&orig_recv);
360- hook_func (" libc.so" , " sendto" , (void *)hook_sendto, (void **)&orig_sendto);
383+ hook_func (" libc.so" , " send" , (void *)hook_send, (void **)&orig_send);
384+ hook_func (" libc.so" , " recv" , (void *)hook_recv, (void **)&orig_recv);
385+ hook_func (" libc.so" , " sendto" , (void *)hook_sendto, (void **)&orig_sendto);
361386 hook_func (" libc.so" , " recvfrom" , (void *)hook_recvfrom, (void **)&orig_recvfrom);
362- hook_func (" libc.so" , " write" , (void *)hook_write, (void **)&orig_write);
363- hook_func (" libc.so" , " read" , (void *)hook_read, (void **)&orig_read);
364- hook_func (" libc.so" , " close" , (void *)hook_close, (void **)&orig_close);
387+ hook_func (" libc.so" , " write" , (void *)hook_write, (void **)&orig_write);
388+ hook_func (" libc.so" , " read" , (void *)hook_read, (void **)&orig_read);
389+ hook_func (" libc.so" , " close" , (void *)hook_close, (void **)&orig_close);
365390
366391 // Hook SSL
367- const char * ssl_libs[] = {
368- " libssl.so" , " libconscrypt_jni.so" ,
369- " libttboringssl.so" , " libflutter.so" , nullptr
370- };
371-
372- for (int i = 0 ; ssl_libs[i] != nullptr ; i++) {
373- hook_func (ssl_libs[i], " SSL_write" , (void *)hook_SSL_write, (void **)&orig_SSL_write);
374- hook_func (ssl_libs[i], " SSL_read" , (void *)hook_SSL_read, (void **)&orig_SSL_read);
375- hook_func (ssl_libs[i], " NativeCrypto_SSL_write" , (void *)hook_SSL_write, (void **)&orig_SSL_write);
376- hook_func (ssl_libs[i], " NativeCrypto_SSL_read" , (void *)hook_SSL_read, (void **)&orig_SSL_read);
377-
378- hook_func (ssl_libs[i], " SSL_free" , (void *)hook_SSL_free, (void **)&orig_SSL_free);
379- hook_func (ssl_libs[i], " NativeCrypto_SSL_free" , (void *)hook_SSL_free, (void **)&orig_SSL_free);
380- }
392+ hook_func (" libssl.so" , " SSL_write" , (void *)hook_SSL_write_libssl, (void **)&orig_SSL_write_libssl);
393+ hook_func (" libssl.so" , " SSL_read" , (void *)hook_SSL_read_libssl, (void **)&orig_SSL_read_libssl);
394+ hook_func (" libssl.so" , " SSL_free" , (void *)hook_SSL_free_libssl, (void **)&orig_SSL_free_libssl);
395+
396+ hook_func (" libconscrypt_jni.so" , " SSL_write" , (void *)hook_SSL_write_conscrypt, (void **)&orig_SSL_write_conscrypt);
397+ hook_func (" libconscrypt_jni.so" , " SSL_read" , (void *)hook_SSL_read_conscrypt, (void **)&orig_SSL_read_conscrypt);
398+ hook_func (" libconscrypt_jni.so" , " SSL_free" , (void *)hook_SSL_free_conscrypt, (void **)&orig_SSL_free_conscrypt);
399+ hook_func (" libconscrypt_jni.so" , " NativeCrypto_SSL_write" , (void *)hook_SSL_write_conscrypt, (void **)&orig_SSL_write_conscrypt);
400+ hook_func (" libconscrypt_jni.so" , " NativeCrypto_SSL_read" , (void *)hook_SSL_read_conscrypt, (void **)&orig_SSL_read_conscrypt);
401+ hook_func (" libconscrypt_jni.so" , " NativeCrypto_SSL_free" , (void *)hook_SSL_free_conscrypt, (void **)&orig_SSL_free_conscrypt);
402+
403+ hook_func (" libttboringssl.so" , " SSL_write" , (void *)hook_SSL_write_ttboringssl, (void **)&orig_SSL_write_ttboringssl);
404+ hook_func (" libttboringssl.so" , " SSL_read" , (void *)hook_SSL_read_ttboringssl, (void **)&orig_SSL_read_ttboringssl);
405+ hook_func (" libttboringssl.so" , " SSL_free" , (void *)hook_SSL_free_ttboringssl, (void **)&orig_SSL_free_ttboringssl);
406+
407+ hook_func (" libflutter.so" , " SSL_write" , (void *)hook_SSL_write_flutter, (void **)&orig_SSL_write_flutter);
408+ hook_func (" libflutter.so" , " SSL_read" , (void *)hook_SSL_read_flutter, (void **)&orig_SSL_read_flutter);
409+ hook_func (" libflutter.so" , " SSL_free" , (void *)hook_SSL_free_flutter, (void **)&orig_SSL_free_flutter);
381410}
0 commit comments