-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
145 lines (136 loc) · 5.88 KB
/
server.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
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
import os
import platform
import socket
import threading
if platform.system().startswith("Windows"):
try:
from pystyle import *
except ImportError:
os.system("python -m pip install pystyle -q -q -q")
from pystyle import *
elif platform.system().startswith("Linux"):
try:
from pystyle import *
except ImportError:
os.system("python3 -m pip install pystyle -q -q -q")
from pystyle import *
banner = Center.XCenter(r"""
*****************************************************************************
* ____ __ _ _ _ _____ ___ _ _ _____ _____ ____ _ _______ *
* / / \/ | | | | | |_ _|_ _| \ | | ____|_ _/ ___| / \|_ _\ \ *
* | || |\/| | | | | | | | | || \| | _| | || | / _ \ | | | | *
* < < | | | | |_| | |___| | | || |\ | |___ | || |___ / ___ \| | > > *
* | ||_| |_|\___/|_____|_| |___|_| \_|_____| |_| \____/_/ \_\_| | | *
* \_\ /_/ *
* CROSS PLATFORM MULTI NETCAT SERVER *
* Coded By: Machine1337 *
*****************************************************************************
""")
os.system("cls||clear")
print(Colorate.Vertical(Colors.green_to_yellow, banner, 2))
class Client:
def __init__(self, connection, address, client_id):
self.connection = connection
self.address = address
self.client_id = client_id
self.data_received = False
def handle(self):
while True:
if self.data_received:
try:
data = self.connection.recv(4096)
if not data:
break
print(data.decode(), end='')
except ConnectionResetError:
break
except KeyboardInterrupt:
break
#self.connection.close()
class Server:
def __init__(self, host, port):
self.host = host
self.port = port
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind((host, port))
self.server_socket.listen(5)
self.clients = []
self.client_id = 0
def start(self):
print(f"[+] Listening on {self.host} {self.port}\n")
while True:
connection, address = self.server_socket.accept()
print(Colors.green+f"\n[*] Connection received on {address[0]} {address[1]}\n[*] SERVER COMMAND: ", end='')
client = Client(connection, address, self.client_id)
self.clients.append(client)
self.client_id += 1
client_thread = threading.Thread(target=client.handle)
client_thread.start()
def show_clients(self):
print(Colors.cyan+"\n[*] Connected clients:")
for client in self.clients:
print(f"ID: {client.client_id}, IP: {client.address[0]}, Port: {client.address[1]}")
print("", end='')
def shell(self, client_id):
try:
for client in self.clients:
if client.client_id == client_id:
print(Colors.yellow + f"[*] Connected to client {client_id}")
client.data_received = True
while True:
command = input("")
if command == "back":
os.system("cls||clear")
print(Colorate.Vertical(Colors.green_to_yellow, banner, 2))
self.server_commands()
elif command == "clear":
os.system("cls||clear")
print(Colorate.Vertical(Colors.green_to_yellow, banner, 2))
client.connection.send(command.encode() + b"\n")
print(f"[*] Sent command: {command}")
break
else:
print("[-] Client not found\n", end='')
except:
print(Colors.red + "\n[-] Ctrl+C Detected .......")
exit(1)
def server_commands(self):
try:
while True:
command = input(Colors.green + "[*] SERVER COMMAND: ")
if command.lower() == "exit":
break
elif command == "list":
os.system("cls||clear")
print(Colorate.Vertical(Colors.green_to_yellow, banner, 2))
self.show_clients()
elif command == 'help':
os.system("cls||clear")
print(Colorate.Vertical(Colors.green_to_yellow, banner, 2))
print(Colorate.Vertical(Colors.red_to_purple, """
**** SERVER COMMANDS MAIN MENU ****
1. id 0 | Entering current shell
2. list | Show Connected Clients
3. back | Back To The Server Main Menu
More Features Will Be Added
Follow:- github.com/machine1337
""", 2))
elif command == "clear":
os.system("cls||clear")
print(Colorate.Vertical(Colors.green_to_yellow, banner, 2))
elif command.startswith("id "):
client_id = int(command.split()[1])
self.shell(client_id)
except:
print(Colors.red+"\n[-] Ctrl+C Detected .......")
exit()
if __name__ == "__main__":
try:
server = Server("0.0.0.0", 9999)
server_thread = threading.Thread(target=server.start)
server_thread.start()
server.server_commands()
except KeyboardInterrupt:
print(Colors.red+"\n[-] Ctrl+C Detected......")
server.stop()
exit(1)