This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
161 lines (141 loc) · 5.42 KB
/
run.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from cmath import log
from concurrent.futures import thread
from time import sleep
import socketserver
from socket import SHUT_RDWR
import cmd
from threading import Thread
from struct import unpack
from collections import namedtuple
from message.header import Header
from message.login import LoginRequest
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection, and overrides the
handle() method to implement communication to the client.
"""
def setup(self):
"""
called before the handle method, set the Stage!
"""
pass
def handle(self):
"""
Handle the Client Socket!
"""
self.data = self.request.recv(32)
self.session_run = False
header = Header(self.data[0:32])
if header.request_type == 37: #login
self._handle_login_package()
else:
print(f"Unknown request_type: {header.request_type}")
print(header.unpacked_data)
while self.session_run:
self.data = self.request.recv(32)
if len(self.data) == 0:
sleep(0.005)
continue
header = Header(self.data[0:32])
if header.request_type == 37: #login
print(f"recieved Login Type package")
else:
print(f"Unknown request_type: {header.request_type}")
print(header.unpacked_data)
def finish(self):
"""
called after the handle method, use this to clean up afterwards!
"""
pass
def _handle_login_package(self):
header = Header(self.data)
print(header.pkg_count, header.request_type, header.sender_id, header.echo)
self.data += self.request.recv(20)
nlen, plen = unpack("II", self.data[44:52])
self.data += self.request.recv(nlen + plen)
login_request = LoginRequest(self.data[32 : 52 + nlen + plen])
# Cut of Header + Package Data from self.data
self.data = self.data[52 + nlen + plen :]
reply = self.__net_line_reply_package(package_count=header.pkg_count,username=login_request.name, password=login_request.password)
self.request.sendall(reply)
def __net_line_reply_package(self, package_count,username,password):
echo_load = "40524b28eb000000"
users = {
"adam": 1,
"eve": 2,
"moep": 3,
}
server_ehlo = {
"host": "192.168.178.50",
"port": 1337,
"name": "Astro",
}
if not str(username) in users:
print("message: username or password incorrect")
return bytes.fromhex(
f"0a00000000000000000000000000000000000000{package_count.to_bytes(4, byteorder='little').hex()}000000000000000000000000000000000000000000000000000000000000000000000000"
)
user_hid = 1
print(f"{username} has joined!")
self.session_run = True
return bytes.fromhex(f"0a000000{echo_load}{ users[username].to_bytes(4, byteorder='little').hex()}{user_hid.to_bytes(2, byteorder='little').hex()}0a00{package_count.to_bytes(4, byteorder='little').hex()}0000000000000000"
+ f"00000000"
+ f"01000000"
+ f"00000000"
+ (len(server_ehlo["host"]) + len(server_ehlo["name"]) + 22)
.to_bytes(4, byteorder="little")
.hex()
+ f"01000000"
+ f"01000000"
+ f"01000000"
+ server_ehlo["port"].to_bytes(4, byteorder="little").hex()
+ f"01000000"
+ server_ehlo["host"].encode("latin-1").hex()
+ f"00"
+ server_ehlo["name"].encode("latin-1").hex()
+ f"00")
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
def main():
"""
The Main Function, which is executed when the server is run directly!
"""
HOST, PORT = "0.0.0.0", 1337
ThreadedTCPServer.allow_reuse_address = True
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request.
server_thread = Thread(target=server.serve_forever)
server_thread.name = "ServerThread"
server_thread.daemon = True
# Exit the server thread when the main thread terminates
# server_thread.daemon = True
server_thread.start()
# Comandline Interface for debugging and Fun!
class RebabelShell(cmd.Cmd):
intro = "Hello, Welcome to Rebabel Shell. Type help or ? to list commands.\n"
prompt = "(astro)"
file = None
def do_info(self, arg):
"""
Print Server Information! : INFO
"""
print(f"Server loop running in thread: {server_thread.name}")
print(f"IP: {ip} Port: {port}")
print(f"{server}")
def do_bye(self, arg):
"""
Stop ReBabel Shell, and exit! : BYE
"""
print("Thank you for flying with ReBabel!")
self.close()
return True
def close(self):
if self.file:
self.file.close()
self.file = None
RebabelShell().cmdloop()
if __name__ == "__main__":
main()