Let's suppose some code like this:
class X:
def myfunc(self):
print "orig"
def new_myfunc(self):
print "new"
X().myfunc()
X.myfunc = new_myfunc
X().myfunc()
where the new function is injected by a cheater.
Some functions can be altered,others not.
I would like to know how i can detect this code change.
For example i could make an dict that contain original function codes( with "func_code" ) and then check if they are changed
but how i can run the check at every "import"? there is a way to edit the autoloader in python?
edit: this is what i would like to do, but automatically for every import,how?
protection = {'X':'myfunc'}
f = {}
class X:
def myfunc(self):
print "orig"
def new_myfunc(self):
print "new"
#system check
for key,value in protection.iteritems():
protectedFunc = getattr(eval(key), value)
f[key] = { value : protectedFunc.func_code}
#cheater code
X.myfunc = new_myfunc
#system check
for key,value in protection.iteritems():
protectedFunc = getattr(eval(key), value)
if f[key][value] != protectedFunc.func_code:
print 'detected'
#call by my app
X().myfunc()