2 years ago

#69300

test-img

DBenson

Is it possible to hook C module function in python?

I have this code where "clr" and "Assembly" are parts of C library in Linux:

import clr
from System.Reflection import Assembly
import functools

def prefix_function(function, prefunction):
    @functools.wraps(function)
    def run(*args, **kwargs):
        prefunction(*args, **kwargs)
        return function(*args, **kwargs)
    return run

def this_is_a_function():
    print("Hook function called instead of the original!")

# This gives me error "attribute is read-only"
Assembly.GetEntryAssembly = prefix_function(
    Assembly.GetEntryAssembly, this_is_a_function)

# Should print:
# "Hook function called instead of the original!"
Assembly.GetEntryAssembly()

Is it possible to somehow hook calling to static function "GetEntryAssembly()" as well as calls to any functions in C libraries ? I need to modify logic and return result of the original "GetEntryAssembly()". So, in the example above I wish to print "Hook function called" when call to GetEntryAssembly() is made.

I can't subclass, but could accept overwrite kind of "pointers" like in C code.

Also I tried this answer, but read-only attribute error occurs.

python

hook

0 Answers

Your Answer

Accepted video resources