Python 实用第三方库集合-Windows篇

xiaoxiao2021-02-28  136

Python 实用第三方库集合-Windows篇

1. PyHook

0x01 简介: 优秀的第三方库PyHook能让我们很容易地捕获Windows所有的键盘和鼠标事件,它利用了原生的Windows函数SetWindowsHookEx,这个函数允许我们安装自定义的钩子函数,当特定的Windows事件发生时,这个钩子函数就会被调用。

0x02 安装: 1.安装pywin32(将会使用到pywin32中的pythoncom模块),下载地址:

https://sourceforge.net/projects/pywin32/files/pywin32/Build 221/

注意:请选择合适的版本下载,如果是在64位系统上装的32位版本Python,请选择32位pywin32下载。

2.安装PyHook,下载地址: https://sourceforge.net/projects/pyhook/files/

0x03 示例: 1.以下代码实现捕捉鼠标事件并打印(代码转自PyHook官网相关页面)

import pythoncom, pyHook def OnMouseEvent(event): # called when mouse events are received print 'MessageName:',event.MessageName print 'Message:',event.Message print 'Time:',event.Time print 'Window:',event.Window print 'WindowName:',event.WindowName print 'Position:',event.Position print 'Wheel:',event.Wheel print 'Injected:',event.Injected print '---' # return True to pass the event to other handlers return True # create a hook manager hm = pyHook.HookManager() # watch for all mouse events hm.MouseAll = OnMouseEvent # set the hook hm.HookMouse() # wait forever pythoncom.PumpMessages()

2.以下代码实现捕捉键盘事件并打印,(代码转自PyHook官网相关页面):

import pythoncom, pyHook def OnKeyboardEvent(event): print 'MessageName:',event.MessageName print 'Message:',event.Message print 'Time:',event.Time print 'Window:',event.Window print 'WindowName:',event.WindowName print 'Ascii:', event.Ascii, chr(event.Ascii) print 'Key:', event.Key print 'KeyID:', event.KeyID print 'ScanCode:', event.ScanCode print 'Extended:', event.Extended print 'Injected:', event.Injected print 'Alt', event.Alt print 'Transition', event.Transition print '---' # return True to pass the event to other handlers return True # create a hook manager hm = pyHook.HookManager() # watch for all Keyboard events hm.KeyDown = OnKeyboardEvent # set the hook hm.HookKeyboard() # wait forever pythoncom.PumpMessages()

PyHook其它详细用法请参考官方文档。

2. win32com

0x01 简介: Windows系统的COM组件自动话技术应用非常广泛,使用pywin32中的win32com模块可以利用IE浏览器原生的COM接口,控制任何一个IE浏览器会话,以此获取登录社交网站或者电子邮箱的凭证。

0x02 安装: 1.安装pywin32,下载地址:

https://sourceforge.net/projects/pywin32/files/pywin32/Build 221/

注意:请选择合适的版本下载,如果是在64位系统上装的32位版本Python,请选择32位pywin32下载。

0x03 示例: 1.以下代码实现使用对象访问IE浏览器正在运行的所以标签和实例,并打印相关信息:

import win32com.client #使用IE浏览器类的ID号,进行COM对象的实例化。 clsid = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}' windows = win32com.client.Dispatch(clsid) for browser in windows: print browser.LocationUrl print browser.Document.all

win32com其它详细用法请参考官方网站。

3. wmi

0x01 简介: 编程人员可以利用WMI库的API接口监控某些系统事件,当一个进程创建时,我们能捕获到进程的创建时间、进程创建的用户、实际启动的可执行文件、进程ID号、父进程ID号。

0x02 安装: 1.安装wmi,下载地址:

pip install wmi

0x03 示例: 1.以下代码实现Win32系统下,进程的监控,打印进程相关信息等:

import wmi #初始化接口 c = wmi.WMI() #创建进程监控器 process_watcher = c.Win32_Process.watch_for("creation") while True: try: new_process = process_watcher() proc_owner = new_process.GetOwner() proc_owner = "%s\\%s" % (proc_owner[0], proc_owner[2]) create_date = new_process.CreationDate executable = new_process.ExecutablePath cmdline = new_process.CommandLine pid = new_process.ProcessId parent_pid = new_process.ParentProcessId privileges = "N/A" process_log_message = "%s,%s,%s,%s,%s,%s,%s\r\n" % (create_date, proc_owner, executable, cmdline, pid, parent_pid, privileges) print process_log_message except: pass

wmi其它详细用法请参考官方网站。

转载请注明原文地址: https://www.6miu.com/read-48432.html

最新回复(0)