-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (109 loc) · 3.91 KB
/
main.py
File metadata and controls
133 lines (109 loc) · 3.91 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
"""
Command-line entry point for the educational keylogger.
Usage
-----
python main.py # default: logs/ directory, Ctrl+Alt+K to stop
python main.py -o mylogs # write logs to ./mylogs
python main.py -f today.txt # fixed filename instead of timestamped
python main.py --stop-char q # change stop combo to Ctrl+Alt+Q
python main.py --no-banner # skip the banner and consent prompt
The program prints its log path on startup (it does not hide itself),
writes keystrokes to a plain-text file you can inspect, and stops
cleanly when the stop combo is pressed.
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from keylogger import KeyLogger
BANNER = r"""
____ _ _
| _ \ _ _ | |/ /___ _ _ | | ___ __ _
| |_) | | | | | ' // _ \ | | | | | / _ \ / _` |
| __/| |_| |_ | . \ __/ |_| |_ | |__| (_) | (_| |
|_| \__, (_) |_|\_\___|\__, ( ) |_____\___/ \__, |
|___/ |___/|/ |___/
Educational Python Keystroke Logger
""".strip("\n")
ETHICAL_NOTICE = """
=============================================================
ETHICAL NOTICE
=============================================================
This tool is for EDUCATIONAL purposes only.
Only run it on devices you own, or where you have received
explicit WRITTEN permission from the owner.
Unauthorized keystroke logging is illegal in most
jurisdictions and can carry serious legal consequences.
=============================================================
""".strip("\n")
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="keylogger",
description="Educational Python keystroke logger.",
epilog="For educational and authorized-testing use only.",
)
parser.add_argument(
"-o",
"--output-dir",
type=Path,
default=Path("logs"),
help="Directory where log files are written (default: ./logs).",
)
parser.add_argument(
"-f",
"--filename",
type=str,
default=None,
help="Log filename. Default: timestamped keylog_YYYYMMDD_HHMMSS.txt.",
)
parser.add_argument(
"--stop-char",
type=str,
default="k",
metavar="CHAR",
help="Single letter used with Ctrl+Alt to stop the logger (default: k).",
)
parser.add_argument(
"--no-banner",
action="store_true",
help="Suppress the banner and consent prompt (for automated testing).",
)
return parser.parse_args(argv)
def confirm_consent() -> bool:
"""Return True only if the user explicitly acknowledges the ethical notice."""
try:
response = input(
"I will only use this on devices I own or am authorized to test. [y/N]: "
)
except EOFError:
return False
return response.strip().lower() in {"y", "yes"}
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
if len(args.stop_char) != 1 or not args.stop_char.isalpha():
print("[!] --stop-char must be a single ASCII letter.", file=sys.stderr)
return 2
if not args.no_banner:
print(BANNER)
print()
print(ETHICAL_NOTICE)
print()
if not confirm_consent():
print("[!] Consent not given. Exiting.")
return 1
logger = KeyLogger(
log_dir=args.output_dir,
filename=args.filename,
stop_char=args.stop_char,
)
try:
logger.start()
except KeyboardInterrupt:
print("\n[!] Interrupted by Ctrl+C.")
return 0
except Exception as exc: # noqa: BLE001 — surface any platform-level error
print(f"[!] Error while running listener: {exc}", file=sys.stderr)
return 3
return 0
if __name__ == "__main__":
sys.exit(main())