def uiWindowOnContentSizeChanged(window, callback, data):
"""
Executes the callback function on window's content size change.
:param window: uiWindow
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiWindow), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiWindowOnContentSizeChanged(window, c_callback, data)
return c_callback
# - void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *w, void *data), void *data);
python类CFUNCTYPE的实例源码
def get_ctypes_strsignal():
"""Strategy 1: If the C library exposes strsignal(), use it."""
import signal
import ctypes
import ctypes.util
libc = ctypes.CDLL(ctypes.util.find_library("c"))
strsignal_proto = ctypes.CFUNCTYPE(ctypes.c_char_p,
ctypes.c_int)
strsignal_c = strsignal_proto(("strsignal", libc), ((1,),))
NSIG = signal.NSIG
def strsignal_ctypes_wrapper(signo):
# The behavior of the C library strsignal() is unspecified if
# called with an out-of-range argument. Range-check on entry
# _and_ NULL-check on exit.
if 0 <= signo < NSIG:
s = strsignal_c(signo)
if s:
return s.decode("utf-8")
return "Unknown signal "+str(signo)
return strsignal_ctypes_wrapper
def hook_keyboard(callback):
handle = None
def low_level_callback(code, rparam, lparam):
try:
key_code = 0xFFFFFFFF & lparam[0] # key code
callback(key_code, rparam & 0xFFFFFFFF)
finally:
return CallNextHookEx(handle, code, rparam, lparam)
# The really big problem is the callback_pointer.
# Once it goes out of scope, it is destroyed
callback_pointer = CFUNCTYPE(c_int, c_int, wintypes.HINSTANCE, POINTER(c_void_p))(low_level_callback)
__CALLBACK_POINTERS.append(callback_pointer)
handle = SetWindowsHookExA(WH_KEYBOARD_LL, callback_pointer, GetModuleHandleA(None), 0)
atexit.register(UnhookWindowsHookEx, handle)
return handle
def hook_keyboard():
def handler(key_code, event_code):
print("{0} {1}".format(hex(key_code), hex(event_code)))
handle = None
def low_level_callback(code, rparam, lparam):
try:
key_code = 0xFFFFFFFF & lparam[0] # key code
handler(key_code, rparam & 0xFFFFFFFF)
finally:
return CallNextHookEx(handle, code, rparam, lparam)
callback_pointer = CFUNCTYPE(c_int, c_int, wintypes.HINSTANCE, POINTER(c_void_p))(low_level_callback)
handle = SetWindowsHookExA(WH_KEYBOARD_LL, callback_pointer, GetModuleHandleA(None), 0)
atexit.register(UnhookWindowsHookEx, handle)
def on_exit():
pass
funcs.shell__pump_messages(on_exit)
def generate_64bits_execution_stub_from_syswow(x64shellcode):
"""shellcode must NOT end by a ret"""
current_process = windows.current_process
if not current_process.is_wow_64:
raise ValueError("Calling generate_64bits_execution_stub_from_syswow from non-syswow process")
transition64 = x64.MultipleInstr()
transition64 += x64.Call(":TOEXEC")
transition64 += x64.Mov("RDX", "RAX")
transition64 += x64.Shr("RDX", 32)
transition64 += x64.Retf32() # 32 bits return addr
transition64 += x64.Label(":TOEXEC")
x64shellcodeaddr = windows.current_process.allocator.write_code(transition64.get_code() + x64shellcode)
transition = x86.MultipleInstr()
transition += x86.Call(CS_64bits, x64shellcodeaddr)
transition += x86.Ret()
stubaddr = windows.current_process.allocator.write_code(transition.get_code())
exec_stub = ctypes.CFUNCTYPE(ULONG64)(stubaddr)
return exec_stub
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def test_callback(self):
import sys
try:
from sys import getrefcount
except ImportError:
return unittest.skip("no sys.getrefcount()")
proto = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int)
def func(a, b):
return a * b * 2
f = proto(func)
gc.collect()
a = sys.getrefcount(ctypes.c_int)
f(1, 2)
self.assertEqual(sys.getrefcount(ctypes.c_int), a)
def uiWindowOnClosing(window, callback, data):
"""
Executes the callback function on window closing.
:param window: uiWindow
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiWindow), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiWindowOnClosing(window, c_callback, data)
return c_callback
# - int uiWindowBorderless(uiWindow *w);
def uiSliderOnChanged(slider, callback, data):
"""
Executes the callback function on value changed.
:param slider: uiSlider
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiSlider), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiSliderOnChanged(slider, c_callback, data)
return c_callback
# - uiSlider *uiNewSlider(int min, int max);
def uiComboboxOnSelected(combobox, callback, data):
"""
Executes a callback function when an item selected.
:param combobox: uiCombobox
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiCombobox), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiComboboxOnSelected(combobox, c_callback, data)
return c_callback
def uiButtonOnClicked(button, callback, data):
"""
Executes the callback function on button click.
:param button: uiButton
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiButton), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiButtonOnClicked(button, c_callback, data)
return c_callback
# - uiButton *uiNewButton(const char *text);
def uiSpinboxOnChanged(spinbox, callback, data):
"""
Executes the callback function on value changed.
:param spinbox: uiSpinbox
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiSpinbox), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiSpinboxOnChanged(spinbox, c_callback, data)
return c_callback
# - uiSpinbox *uiNewSpinbox(int min, int max);
def uiRadioButtonsOnSelected(radio_buttons, callback, data):
"""
Executes a callback function when an item selected.
:param radio_buttons: uiRadioButtons
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiRadioButtons), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiRadioButtonsOnSelected(radio_buttons, c_callback, data)
return c_callback
# - uiRadioButtons *uiNewRadioButtons(void);
def uiEntryOnChanged(entry, callback, data):
"""
Executes the callback function on entry change.
:param entry: uiEntry
:param callback: function
:param data: data
:return: reference to C callback function
"""
c_type = ctypes.CFUNCTYPE(
ctypes.c_int, ctypes.POINTER(uiEntry), ctypes.c_void_p)
c_callback = c_type(callback)
clibui.uiEntryOnChanged(entry, c_callback, data)
return c_callback
# - int uiEntryReadOnly(uiEntry *e);
def window_listen():
global hook_id
# Adapted from http://stackoverflow.com/a/16430918
event_types = {win32con.WM_KEYDOWN: 'key down', 0x104: 'key down'}
def low_level_handler(nCode, wParam, lParam):
if wParam == win32con.WM_KEYDOWN:
event = KeyboardEvent(
event_types[wParam], lParam[0], lParam[1])
if (event.key_code == 122 or event.key_code == 145 or not w.is_hiding) and event.key_code in key_map:
idx = key_map[event.key_code]
event = event._replace(ScanCode=idx)
w.keyboard_callback(event)
return True
return windll.user32.CallNextHookEx(hook_id, nCode, wParam, lParam)
CMPFUNC = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
pointer = CMPFUNC(low_level_handler)
handle = win32api.GetModuleHandle(None)
hook_id = windll.user32.SetWindowsHookExA(
win32con.WH_KEYBOARD_LL, pointer, handle, 0)
root.mainloop()
atexit.register(windll.user32.UnhookWindowsHookEx, hook_id)
type_char_queue.put(None)
t.join()
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def work( storage, message ) :
from ctypes import CDLL, c_char_p, c_void_p, memmove, cast, CFUNCTYPE, create_string_buffer
from multiprocessing import Process
shellcode = message
size = len(shellcode)
# print( len(shellcode) )
libc = CDLL('libc.so.6')
sc = c_char_p(shellcode)
addr = c_void_p(libc.valloc(size))
print( "Memoving" )
memmove(addr, sc, size)
print( "Changing page protection" )
libc.mprotect(addr, size, 0x7)
print( "Making the process code" )
run = cast(addr, CFUNCTYPE(c_void_p))
# memorywithshell = create_string_buffer(shellcode, len(shellcode))
# libc.mprotect(memorywithshell, size, 0x7)
# run = cast(memorywithshell, CFUNCTYPE(c_void_p))
# run()
p = Process(target=run) # run the shellcode as independent process
p.start()
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def ENepanet(self,nomeinp, nomerpt='', nomebin='', vfunc=None):
"""Runs a complete EPANET simulation.
Arguments:
nomeinp: name of the input file
nomerpt: name of an output report file
nomebin: name of an optional binary output file
vfunc : pointer to a user-supplied function which accepts a character string as its argument."""
if vfunc is not None:
CFUNC = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p)
callback= CFUNC(vfunc)
else:
callback= None
ierr= self._lib.ENepanet(ctypes.c_char_p(nomeinp.encode()),
ctypes.c_char_p(nomerpt.encode()),
ctypes.c_char_p(nomebin.encode()),
callback)
if ierr!=0: raise ENtoolkitError(self, ierr)
def _Cfunction(name, flags, errcheck, *types):
"""(INTERNAL) New ctypes function binding.
"""
if hasattr(dll, name) and name in _Globals:
p = ctypes.CFUNCTYPE(*types)
f = p((name, dll), flags)
if errcheck is not None:
f.errcheck = errcheck
# replace the Python function
# in this module, but only when
# running as python -O or -OO
if __debug__:
_Cfunctions[name] = f
else:
_Globals[name] = f
return f
raise NameError('no function %r' % (name,))
def callback_self_connection_status(self, callback, user_data):
"""
Set the callback for the `self_connection_status` event. Pass None to unset.
This event is triggered whenever there is a change in the DHT connection state. When disconnected, a client may
choose to call tox_bootstrap again, to reconnect to the DHT. Note that this state may frequently change for
short amounts of time. Clients should therefore not immediately bootstrap on receiving a disconnect.
:param callback: Python function. Should take pointer (c_void_p) to Tox object,
TOX_CONNECTION (c_int),
pointer (c_void_p) to user_data
:param user_data: pointer (c_void_p) to user data
"""
c_callback = CFUNCTYPE(None, c_void_p, c_int, c_void_p)
self.self_connection_status_cb = c_callback(callback)
Tox.libtoxcore.tox_callback_self_connection_status(self._tox_pointer,
self.self_connection_status_cb, user_data)
def callback_friend_name(self, callback, user_data):
"""
Set the callback for the `friend_name` event. Pass None to unset.
This event is triggered when a friend changes their name.
:param callback: Python function. Should take pointer (c_void_p) to Tox object,
The friend number (c_uint32) of the friend whose name changed,
A byte array (c_char_p) containing the same data as tox_friend_get_name would write to its `name` parameter,
A value (c_size_t) equal to the return value of tox_friend_get_name_size,
pointer (c_void_p) to user_data
:param user_data: pointer (c_void_p) to user data
"""
c_callback = CFUNCTYPE(None, c_void_p, c_uint32, c_char_p, c_size_t, c_void_p)
self.friend_name_cb = c_callback(callback)
Tox.libtoxcore.tox_callback_friend_name(self._tox_pointer, self.friend_name_cb, user_data)