-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
179 lines (151 loc) · 5.75 KB
/
client.py
File metadata and controls
179 lines (151 loc) · 5.75 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import sys
import io
import json
import socket
import pyautogui
import threading
from pynput.mouse import Button, Controller
import keyboard
import tkinter as tk
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
from cryptography.hazmat.backends import default_backend
def chacha20_encrypt(key, plaintext, nonce):
algorithm = algorithms.ChaCha20(key, nonce)
cipher = Cipher(algorithm, mode=None, backend=default_backend())
encryptor = cipher.encryptor()
ciphertext = encryptor.update(plaintext)
return ciphertext
def chacha20_decrypt(key, ciphertext, nonce):
algorithm = algorithms.ChaCha20(key, nonce)
cipher = Cipher(algorithm, mode=None, backend=default_backend())
decryptor = cipher.decryptor()
decrypted_text = decryptor.update(ciphertext)
return decrypted_text
mouse_controller = Controller()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # Connect to a public DNS to get the right interface
HOST_IP = s.getsockname()[0]
s.close()
def message_listener():
msg_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
msg_server_socket.bind((HOST_IP, 12000))
msg_server_socket.listen()
conn, addr = msg_server_socket.accept()
buffer = ""
with conn:
while True:
try:
size_data = conn.recv(4)
if not size_data:
break
size = int.from_bytes(size_data, 'big')
data = b''
while len(data) < size:
packet = conn.recv(size - len(data))
if not packet:
break
data += packet
data = chacha20_decrypt(key, data, nonce)
if data.strip():
if data.strip() == b"CLOSEING FROM SERVER":
print("CLOSEING FROM SERVER")
on_closing()
execute_input(data.strip())
except Exception as e:
print(f"Error reciving input data from server: {e}")
break
conn.close()
def execute_input(json_data):
data = json.loads(json_data)
if data["event"] == "move":
mouse_controller.position = (
round(data["x"]*WIDTH_SCALE_FACTOR),
round(data["y"]*HEIGHT_SCALE_FACTOR)
)
elif data["event"] == "scroll":
mouse_controller.position = (
round(data["x"]*WIDTH_SCALE_FACTOR),
round(data["y"]*HEIGHT_SCALE_FACTOR)
)
if data["direction"] == "up":
mouse_controller.scroll(0, data["amount"])
elif data["direction"] == "down":
mouse_controller.scroll(0, -data["amount"])
else:
print("Unknown direction:", data["direction"])
elif data["event"] == "click":
mouse_controller.position = (
round(data["x"]*WIDTH_SCALE_FACTOR),
round(data["y"]*HEIGHT_SCALE_FACTOR)
)
button = None
if data["button"] == "Left":
button = Button.left
elif data["button"] == "Right":
button = Button.right
elif data["button"] == "Middle":
button = Button.middle
else:
print("Unknown button:", data["button"])
if button:
if data["action"] == "pressed":
mouse_controller.press(button)
elif data["action"] == "released":
mouse_controller.release(button)
else:
print("Unknown action:", data["action"])
elif data["event"] == "key":
try:
key = data["key"]
action = data["action"]
if action == "pressed":
keyboard.press(key)
elif action == "released":
keyboard.release(key)
except Exception as e:
print(f"Error processing key event: {e}")
def send_image():
while True:
screenshot = pyautogui.screenshot()
img_byte_arr = io.BytesIO()
screenshot.save(img_byte_arr, format='JPEG', quality=80)
data = img_byte_arr.getvalue()
data = chacha20_encrypt(key, data, nonce)
# Send length first
size = len(data).to_bytes(4, 'big')
try:
client_socket.sendall(size + data)
except Exception as e:
print(f"Error sending images to server: {e}")
def on_closing():
data = chacha20_encrypt(key, b'CLOSED FROM CLIENT', nonce)
size = len(data).to_bytes(4, 'big')
try:
client_socket.sendall(size + data)
except:
print("CLOSED BY SERVER")
client_socket.close()
root.destroy()
SERVER_IP = sys.argv[1]
key = bytes.fromhex(sys.argv[2])
nonce = bytes.fromhex(sys.argv[3])
PORT = 5000
SCREEN_WIDTH, SCREEN_HEIGHT = pyautogui.size()
WIDTH_SCALE_FACTOR = SCREEN_WIDTH/1280
HEIGHT_SCALE_FACTOR = SCREEN_HEIGHT/720
message_thread = threading.Thread(target=message_listener, daemon=True)
message_thread.start()
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((SERVER_IP, PORT))
send_image_thread = threading.Thread(target=send_image,daemon=True)
send_image_thread.start()
root = tk.Tk()
root.title("LANDesk - Remote Session Active")
root.config(bg="#9ECAD6")
root.minsize(height=120, width= 450)
root.maxsize(height=120, width= 450)
root.protocol("WM_DELETE_WINDOW", on_closing)
label = tk.Label(root,bg="#9ECAD6",font=("Comic Sans MS",17),text='Connected to IP '+SERVER_IP)
label.pack()
tk.Button(root, bg="#9ECAD6", font=("Comic Sans MS",13), text="Close", command=on_closing).pack()
root.mainloop()