Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def init(self, config, servermode):
try:
chacha = ChaCha20Poly1305("0"*32)
except:
common.internal_print("OpenSSL library is outdated. Please update.", -1)
return False
except:
common.internal_print("Something went wrong with the cryptography engine. Most probably OpenSSL related.", -1)
return False
if servermode:
if not (os.path.exists(self.server_public_key_file) or os.path.exists(self.server_private_key_file)):
common.internal_print("Both public and private key is missing. This must be the first run. Generating keys...", 1)
private_key = ec.generate_private_key(self.curve, default_backend())
privkey_ser = private_key.private_bytes(serialization.Encoding.PEM,
serialization.PrivateFormat.TraditionalOpenSSL,
serialization.NoEncryption())
pubkey_ser = private_key.public_key().public_bytes(serialization.Encoding.PEM,
serialization.PublicFormat.SubjectPublicKeyInfo)
def WIN_get_subinterface_name(self):
IFACE_NAME_KEY = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"
NetCfgInstanceId = self.WIN_get_device_guid()
try:
regkey = registry.OpenKey(registry.HKEY_LOCAL_MACHINE, IFACE_NAME_KEY+NetCfgInstanceId+"\\Connection\\")
iface_name = registry.QueryValueEx(regkey, "Name")[0]
except WindowsError as e:
common.internal_print("Cannot get interface name. Registry key cannot be found: {0}".format(e), -1)
sys.exit(-1)
return iface_name
def communication_initialization(self):
try:
common.internal_print("Waiting for upgrade request", 0, self.verbosity, common.DEBUG)
response = self.comms_socket.recv(4096)
if len(response) == 0:
common.internal_print("Connection was dropped", 0, self.verbosity, common.DEBUG)
self.cleanup()
sys.exit(-1)
handshake_key = self.WebSocket_proto.get_handshake_init(response)
if handshake_key == None:
common.internal_print("No WebSocket-Key in request", -1, self.verbosity, common.DEBUG)
self.cleanup()
sys.exit(-1)
handshake = self.WebSocket_proto.calculate_handshake(handshake_key)
response = self.WebSocket_proto.switching_protocol(handshake)
self.comms_socket.send(response)
except:
common.internal_print("Malformed WebSocket packet", -1, self.verbosity, common.DEBUG)
return ""
masked = self.WebSocket_proto.is_masked(length2b)
header_length = self.WebSocket_proto.get_header_length(masked, length_type)
if message < header_length:
common.internal_print("Malformed WebSocket packet: wrong header length", -1, self.verbosity, common.DEBUG)
return ""
data_length = self.WebSocket_proto.get_data_length(message[:header_length], masked, length_type)
length = data_length + header_length
if len(message) >= length:
messages.append(self.transform(self.encryption, self.WebSocket_proto.get_data(message[0:length], header_length, data_length), 0))
common.internal_print("WebSocket read: {0} -> {1}".format(length, len(messages[len(messages)-1])), 0, self.verbosity, common.DEBUG)
self.partial_message = ""
message = message[length:]
else:
self.partial_message = message
break
if len(message) < 2:
self.partial_message = message
break
return messages
try:
readable, writable, exceptional = select.select(self.rlist, wlist, xlist, self.select_timeout)
except select.error, e:
print(e)
break
try:
if not readable:
if is_check:
raise socket.timeout
if not self.serverorclient:
# send dummy packets for keep alive, only if
# authenticated
if self.authenticated:
self.do_dummy_packet()
common.internal_print("Keep alive sent", 0, self.verbosity, common.DEBUG)
else:
# if no answer comes back from the server in
# 5 seconds, then just quit
unauth_counter += 1.0
if (unauth_counter*self.select_timeout)>5.0:
common.internal_print("Auth timed out. Is there any rate limit on your DNS?", -1, self.verbosity, common.DEBUG)
self.stop()
break
continue
for s in readable:
if (s in self.rlist) and not (s is self.comms_socket):
message = os.read(s, 4096)
while True:
if (len(message) < 4) or (message[0:1] != "\x45"): #Only care about IPv4
break
def recv(self):
messages = {}
try:
message, addr = self.comms_socket.recvfrom(4096)
except socket.error as se:
raise
if len(message) == 0:
if self.serverorclient:
common.internal_print("WTF? Client lost. Closing down thread.", -1)
else:
common.internal_print("WTF? Server lost. Closing down.", -1)
return messages
while True:
if addr not in messages:
messages[addr] = []
length = struct.unpack(">H", message[0:2])[0]+2
if len(message) == length:
messages[addr].append(self.transform(self.get_client_encryption((addr, 0)), message[2:length], 0))
common.internal_print("UDP read: {0}".format(len(messages[addr][len(messages[addr])-1])), 0, self.verbosity, common.DEBUG)
message = message[length:]
else:
#debug
print "size did not match"
print "len(message): {0}".format(len(message))
if client in self.clients:
if self.os_type == common.OS_WINDOWS:
import win32file
try:
win32file.CloseHandle(client.get_pipe_r())
win32file.CloseHandle(client.get_pipe_w())
except Exception as e:
common.internal_print("Remove authenticated client: CloseHandle exception: {0}".format(e), -1)
else:
try:
client.get_pipe_r_fd().close()
client.get_pipe_w_fd().close()
except Exception as e:
common.internal_print("Remove authenticated client: os.close exception: {0}".format(e), -1)
client.call_stopfp()
self.clients.remove(client)
return
def websocket_upgrade(self, server_socket):
request = self.WebSocket_proto.upgrade(base64.b64encode(os.urandom(9)).replace("/", "").replace("+", ""), self.config.get("Global", "remoteserverhost"), self.config.get(self.get_module_configname(), "serverport"), 13)
server_socket.send(request)
response = server_socket.recv(4096)
if response[9:12] != "101":
common.internal_print("Connection failed: {0}".format(response[0:response.find("\n")]), -1)
return False
return True
message = current_client.get_answer_queue().get()
if i == (top - 1):
if (aq_l - i - 1) < 256:
ql = chr(aq_l - i -1)
else:
ql = chr(255)
pre_message = current_client.get_download_encoding_class().encode(ql+message)
transformed_message = RRtype[2](self.DNS_common.get_character_from_userid(userid)+pre_message)
(temp, transaction_id, orig_question, addr, requery_count) = current_client.get_query_queue().get()
packet = self.DNS_proto.build_answer(transaction_id, [current_client.get_recordtype(), "", transformed_message], orig_question)
self.comms_socket.sendto(packet, addr)
pn = self.DNS_common.get_packet_number_from_header(message[0:2])
fn = self.DNS_common.get_fragment_number_from_header(message[0:2])
common.internal_print("DNS packet sent!: {0} - packet number: {1} / fragment: {2}".format(len(message), pn, fn), 0, self.verbosity, common.DEBUG)
else:
i = 0
# client side
while len(message) > self.qMTU:
fragment = ql+self.DNS_common.create_fragment_header(ord(channel_byte), self.qpacket_number, i, 0)+message[0:self.qMTU]
message = message[self.qMTU:]
efragment = self.DNS_common.get_character_from_userid(userid)+self.upload_encoding_class.encode(fragment)
data = ""
# TODO pack record hostname?
for j in range(0,int(math.ceil(float(len(efragment))/63.0))):
data += efragment[j*63:(j+1)*63]+"."
packet = self.DNS_proto.build_query(int(random.random() * 65535), data, self.hostname, self.RRtype_num)
common.internal_print("DNS packet sent_: {0} - packet number: {1} / fragment: {2}".format(len(fragment), self.qpacket_number, i), 0, self.verbosity, common.DEBUG)
self.comms_socket.sendto(packet, addr)
i += 1