Kaydet (Commit) 628f53e3 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

init

üst
# Created by https://www.gitignore.io/api/python
# Edit at https://www.gitignore.io/?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
### Python Patch ###
.venv/
# End of https://www.gitignore.io/api/python
# Created by https://www.gitignore.io/api/c
# Edit at https://www.gitignore.io/?templates=c
### C ###
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# End of https://www.gitignore.io/api/c
.PHONY: clean
hookify.so: hookify.c
gcc `python-config --cflags` `python-config --includes` -Wl,--export-dynamic -fPIC -shared -o $@ $^ -ldl `python-config --libs`
clean:
rm hookify.so
Helper package to [catlizor](https://github.com/btaskaya/catlizor)
#include <Python.h>
#include <sys/mman.h>
#pragma pack(push, 1)
static struct {
char push_rax;
char mov_rax[2];
char addr[8];
char jmp_rax[2];
}
jumper = {
.push_rax = 0x50,
.mov_rax = {0x48, 0xb8},
.jmp_rax = {0xff, 0xe0}
};
#pragma pack(pop)
#define CATLIZED_SIGN "__catlized"
#define CAPI_METHOD "exc_capi"
extern PyObject* _PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack, Py_ssize_t nargs, PyObject *kwnames);
static int unprotect_page(void* addr) {
return mprotect((char *)((size_t)addr & ~(sysconf(_SC_PAGE_SIZE) -1)), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE | PROT_EXEC);
}
int hookifier(void* target, void* replace) {
/* Some parts based on libhook */
int count;
if(unprotect_page(replace) || unprotect_page(target)) {
return 1;
}
for(count = 0; count < 255 && ((unsigned char*)replace)[count] != 0x90; ++count);
if(count == 255) {
return 1;
}
memmove(replace+1, replace, count);
*((unsigned char *)replace) = 0x58;
memcpy(jumper.addr, &replace, sizeof (void *));
memcpy(target, &jumper, sizeof jumper);
return 0;
}
PyObject *
hookify_PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
Py_ssize_t nargs, PyObject *kwnames)
{
__asm__("NOP");
PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
PyObject *globals = PyFunction_GET_GLOBALS(func);
PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
PyObject *kwdefs, *closure, *name, *qualname;
PyObject **d;
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Py_ssize_t nd;
PyObject *w, *args, *instance, *catlizor, *meth_name, *tracked, *hooks;
int i, n, pre = 0, on_call = 0, post = 0;
assert(PyFunction_Check(func));
assert(nargs >= 0);
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
assert((nargs == 0 && nkwargs == 0) || stack != NULL);
kwdefs = PyFunction_GET_KW_DEFAULTS(func);
closure = PyFunction_GET_CLOSURE(func);
name = ((PyFunctionObject *)func) -> func_name;
qualname = ((PyFunctionObject *)func) -> func_qualname;
if (argdefs != NULL) {
d = &PyTuple_GET_ITEM(argdefs, 0);
nd = PyTuple_GET_SIZE(argdefs);
}
else {
d = NULL;
nd = 0;
}
if (nargs > co->co_argcount) {
n = co->co_argcount;
}
else {
n = nargs;
}
args = PyList_New(n);
for (i = 0; i < n; i++) {
w = stack[i];
Py_INCREF(w);
PyList_SetItem(args, i, w);
}
instance = PyList_GetItem(args, 0);
if (PyObject_HasAttrString(instance, CATLIZED_SIGN)){
catlizor = PyObject_GetAttrString(instance, CATLIZED_SIGN);
meth_name = PyObject_GetAttrString(func, "__name__");
tracked = PyObject_CallMethod(catlizor, "tracked", "(OO)", meth_name, Py_True);
if (PySet_Check(tracked) && PySet_Size(tracked) > 0){
pre = PySet_Contains(tracked, PyLong_FromLong(0));
on_call = PySet_Contains(tracked, PyLong_FromLong(1));
post = PySet_Contains(tracked, PyLong_FromLong(2));
}
}
if (pre)
PyObject_CallMethod(catlizor, CAPI_METHOD, "(iOOO)", 0, func, args, Py_None);
PyObject *result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
stack, nargs,
nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
stack + nargs,
nkwargs, 1,
d, (int)nd, kwdefs,
closure, name, qualname);
if (on_call)
PyObject_CallMethod(catlizor, CAPI_METHOD, "(iOOO)", 1, func, args, result);
if (post)
PyObject_CallMethod(catlizor, CAPI_METHOD, "(iOOO)", 2, func, args, Py_None);
return result;
}
static PyMethodDef module_methods[] = {
{NULL}
};
static struct PyModuleDef hookify =
{
PyModuleDef_HEAD_INIT,
"hookify",
NULL,
-1,
module_methods
};
PyMODINIT_FUNC PyInit_hookify(void) {
__asm__("");
hookifier(_PyFunction_FastCallKeywords, &hookify_PyFunction_FastCallKeywords);
return PyModule_Create(&hookify);
}
from setuptools import setup, Extension
hookify = Extension(
name = 'hookify',
sources = ['hookify.c'],
)
setup(
name = 'hookify',
version = '1.0.0',
author = 'BTaskaya',
author_email = 'batuhanosmantaskaya@gmail.com',
url = "https://github.com/btaskaya/hookify",
description = "Helper package to catlizor v1-extended series.",
ext_modules = [hookify]
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment