python 钩子函数

简介: python 在windows下监听键盘按键使用到的库ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数)使用的Win32APISetWindowsHookEx(), 将用户定义的钩子函数添加到钩子链中, 也就是我们的注册钩子函数UnhookWindowsH...

python 在windows下监听键盘按键

使用到的库

  • ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数)

使用的Win32API

  • SetWindowsHookEx(), 将用户定义的钩子函数添加到钩子链中, 也就是我们的注册钩子函数
  • UnhookWindowsHookEx(), 卸载钩子函数
  • CallNextHookEx()在我们的钩子函数中必须调用, 这样才能让程序的传递消息

在没有钩子函数的情况下windows程序运行机制

  • 键盘输入 --> 系统消息队列 --> 对应应用程序的消息队列 --> 将消息发送到对应的窗口中

在有了钩子函数的情况下windows程序运行机制

  • 键盘输入 --> 系统消息队列 --> 对应应用程序消息队列 --> 将消息发送到钩子链中 --> 消息一一调用完毕所有的钩子函数(需要调用CallNextHookEx函数才能将消息传递下去) --> 将消息发送到对应的窗口中

示例程序

  • 注意:
    • 在程序中, 我们通过CFUNCTYPE返回一个类对象, 通过该类对象可以实例化出我们需要的c类型的函数, 但是如果不将他放在全局的话则会失去效果, 因为在C语言中函数是全局的
# -*- coding: utf-8 -*-
import os
import sys
from ctypes import *
from ctypes.wintypes import *


"""
define constants
"""
WH_KEYBOARD = 13
WM_KEYDOWN = 0x0100
CTRL_CODE = 162


class JHKeyLogger(object):

    def __init__(self, user32, kernel32):
        """
        Description:
            Init the keylogger object, the property 'hook_' is the handle to control our hook function
        
        Args:
            @(dll)user32: just put windll.user32 here
            @(dll)kernel32: just put windll.kernel32 here

        Returns:
            None
        """
        self.user32_ = user32
        self.kernel32_ = kernel32
        self.hook_ = None
        
    def install_hookproc(self, hookproc):
        """
        Description:
            install hookproc function into message chain

        Args:
            @(c type function)hookproc: hookproc is the hook function to call

        Returns:
            @(bool):
                if SetWindowHookExA() function works successfully, return True
                else return False
        """
        self.hook_ = self.user32_.SetWindowsHookExA(
                                      WH_KEYBOARD,
                                      hookproc,
                                      self.kernel32_.GetModuleHandleW(None),
                                      0)
        if not self.hook_:
            return False
        return True

    def uninstall_hookproc(self):
        """
        Description:
            uninstall the hookproc function which means pick the hookproc pointer off the message chain
        Args:
            None
        Returns:
            None
        """
        if not self.hook_:
            return
        self.user32_.UnhookWindowsHookEx(self.hook_)
        self.hook_ = None

    def start(self):
        """
        Description:
            start logging, just get the message, the current thread will blocked by the GetMessageA() function
        
        Args:
            None
        Returns:
            None
        """
        msg = MSG()
        self.user32_.GetMessageA(msg, 0, 0, 0)

    def stop(self):
        self.uninstall_hookproc()


def hookproc(nCode, wParam, lParam):
    """
    Description:
        An user-defined hook function

    Attention:
        here we use the global variable named 'g_keylogger'
    """
    if wParam != WM_KEYDOWN:
        return g_keylogger.user32_.CallNextHookEx(g_keylogger.hook_, nCode, wParam, lParam)

    pressed_key = chr(lParam[0])
    print pressed_key,
    # hit ctrl key to stop logging
    if CTRL_CODE == lParam[0]:
        g_keylogger.stop()
        sys.exit(-1)
    return g_keylogger.user32_.CallNextHookEx(g_keylogger.hook_, nCode, wParam, lParam)


# Attention: pointer must be defined as a global variable
cfunctype = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
pointer = cfunctype(hookproc)

g_keylogger = JHKeyLogger(windll.user32, windll.kernel32)

def main():
    if g_keylogger.install_hookproc(pointer):
        print 'install keylogger successfully!'
    g_keylogger.start()
    print 'hit ctrl to stop'
    
if __name__ == '__main__':
    main()
目录
相关文章
|
3天前
|
数据挖掘 数据处理 索引
python常用pandas函数nlargest / nsmallest及其手动实现
python常用pandas函数nlargest / nsmallest及其手动实现
18 0
|
15天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
16 1
|
29天前
|
Python
Python函数使用(四)
Python函数使用(四)
63 0
|
8天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1
|
8天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
10 0
|
10天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
51 0
|
10天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
11天前
|
Python
python学习10-函数
python学习10-函数
|
11天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
14天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。