Exetools  

Go Back   Exetools > General > Source Code

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 12-28-2023, 19:21
vic4key's Avatar
vic4key vic4key is offline
Family
 
Join Date: Apr 2010
Posts: 62
Rept. Given: 5
Rept. Rcvd 24 Times in 10 Posts
Thanks Given: 62
Thanks Rcvd at 98 Times in 23 Posts
vic4key Reputation: 24
Arrow Python Hooking - Write Less Do More

Sometimes, you need to manipulate low-level functionality (C APIs, etc) from high-level languages (Python).
Eg. Force the low-level side calls directly to your own Python override functions. This repo will help you do that.

[x] Windows
[x] Linux
[x] MacOS
[x] iOS
[x] Android

To hook & unhook a function, you only need to write codes as the following

Code:
import ctypes
from PyHooking import PyHooking, load_external_shared_library, arch, bits

# define the hook function

mylib = load_external_shared_library(f"Examples/mylib_{arch}_{bits}")

@PyHooking.CPrototype(ctypes.CFUNCTYPE(None, ctypes.c_char_p))
def hk_print_message(message):
    message = f"Invoked `hk_print_message('{message.decode('utf-8')}')`"
    PyHooking().invoke(mylib.print_message, message.encode())

# perform hooking

PyHooking().hook(mylib.print_message, hk_print_message)

mylib.print_message(b"This is a string from Python code")
mylib.c_invoke_print_message()

# perform unhooking

PyHooking().unhook(mylib.print_message)

mylib.print_message(b"This is a string from Python code")
mylib.c_invoke_print_message()

# result

'''
Invoked `hk_print_message('This is a string from Python code')`
Invoked `hk_print_message('This is a string from C code')`
This is a string from Python code
This is a string from C code
'''
The repository @ https://github.com/vic4key/py-hooking.git

Follow me on GitHub @ https://github.com/vic4key

Regards,
Vic P.

Last edited by vic4key; 01-02-2024 at 11:03.
Reply With Quote
The Following 2 Users Gave Reputation+1 to vic4key For This Useful Post:
ahmadmansoor (12-29-2023), chants (12-29-2023)
The Following 6 Users Say Thank You to vic4key For This Useful Post:
ahmadmansoor (12-29-2023), blue_devil (12-28-2023), chants (12-29-2023), MarcElBichon (12-29-2023), nimaarek (04-02-2024), tonyweb (01-02-2024)
  #2  
Old 12-29-2023, 04:48
ahmadmansoor's Avatar
ahmadmansoor ahmadmansoor is offline
Coder
 
Join Date: Feb 2006
Location: Syria
Posts: 1,047
Rept. Given: 515
Rept. Rcvd 374 Times in 142 Posts
Thanks Given: 378
Thanks Rcvd at 410 Times in 119 Posts
ahmadmansoor Reputation: 300-399 ahmadmansoor Reputation: 300-399 ahmadmansoor Reputation: 300-399 ahmadmansoor Reputation: 300-399
@vic4key are you the same guy from GitHub, or u have the same name?
__________________
Ur Best Friend Ahmadmansoor
Always My Best Friend: Aaron & JMI & ZeNiX
Reply With Quote
  #3  
Old 12-29-2023, 10:06
vic4key's Avatar
vic4key vic4key is offline
Family
 
Join Date: Apr 2010
Posts: 62
Rept. Given: 5
Rept. Rcvd 24 Times in 10 Posts
Thanks Given: 62
Thanks Rcvd at 98 Times in 23 Posts
vic4key Reputation: 24
@ahmadmansoor Yep. It's me, bro. I believe that my premature-nickname is unique. 😂

P.S Corrected the mistake of copy-paste link from https://github.com/vic4key/cpp-hooking.git to https://github.com/vic4key/py-hooking.git
Reply With Quote
The Following User Says Thank You to vic4key For This Useful Post:
ahmadmansoor (12-29-2023)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On



All times are GMT +8. The time now is 04:38.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( 1998 - 2024 )