-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSleipnir.py
More file actions
206 lines (178 loc) · 6.27 KB
/
Sleipnir.py
File metadata and controls
206 lines (178 loc) · 6.27 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/python
# Sleipnir v 1.0.5
# Author: https://github.com/vaarg/
# Usage:
# As server:
# Sleipnir.py -t [IP] -s
# Sleipnir.py -t [IP] -p [PORT] -s
# As client:
# Sleipnir.py -t [IP]
# Sleipnir.py -t [IP] -p [PORT]
# Decription:
# An encrypted Python messenger with multi-threading. Sleipnir can be launched as either server or client.
# Dependencies:
# Python Cryptodome library - "pip install pycryptodomex"
from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad, unpad
import argparse
import base64
import socket
import threading
import time
# [=] Common:
def passwordSet(passwordSess):
global password
password = passwordSess
if len(password) < 16:
i = len(password)
while i < 16:
password += b'A'
i += 1
elif len(password) > 16:
password = password[0:16]
# [#] Encryption/Decryption:
def encryptMsg(data):
global ciphered_data
cipher = AES.new(password, AES.MODE_CBC, iv)
ciphered_data = cipher.encrypt(pad(data, AES.block_size))
ciphered_data = base64.encodebytes(ciphered_data)
def decryptMsg(ciphered_data):
global decrypted_data
ciphered_data = base64.decodebytes(ciphered_data)
cipher = AES.new(password, AES.MODE_CBC, iv)
decrypted_data = unpad(cipher.decrypt(ciphered_data), AES.block_size)
# [<] Server:
def broadcaster(message, prefix=""):
print(f"{prefix}{message.decode(encoding)}")
sendMessage = prefix.encode(encoding) + message
encryptMsg(sendMessage)
for sockets in clients:
sockets.send(ciphered_data)
def connector():
while True:
clientSocket, clientAddress = server.accept()
print(f"{clientAddress} has connected.")
addresses[clientSocket] = clientAddress
threading.Thread(target=handler, args=(clientSocket,clientAddress), daemon=True).start()
def handler(clientSocket, clientAddress):
username = clientSocket.recv(byteSize).decode(encoding)
clientSocket.send(bytes("Authenticating...", encoding))
time.sleep(1)
clients[clientSocket] = username
cliPass = clientSocket.recv(byteSize)
try:
decryptMsg(cliPass)
cliPass = decrypted_data
except:
cliPass = ""
sending = threading.Thread(target=serverSend, daemon=True)
sending.start()
stage = 0
while True:
if stage == 0:
if cliPass == password:
stage = 1
pass
else:
kill = f"{terminatingStr}"
clientSocket.send(bytes(kill, encoding))
clientSocket.close()
del clients[clientSocket]
broadcaster(bytes(f"{clientAddress} has been disconnected: Incorrect session password.", encoding))
break
if stage == 1:
stage = 2
success = f"You have successfully connected as {username}! Type '{terminatingStr}' to terminate connection at anypoint."
time.sleep(1)
clientSocket.send(bytes(success, encoding))
message = f"{username} has connected!"
broadcaster(message.encode(encoding))
pass
if stage == 2:
message = clientSocket.recv(byteSize)
decryptMsg(message)
if decrypted_data.decode(encoding) != f"{terminatingStr}":
broadcaster(decrypted_data, username+": ")
else:
clientSocket.close()
del clients[clientSocket]
disconnect = f"{username} has disconnected."
broadcaster(disconnect.encode(encoding))
break
def serverSend():
while True:
messageSend = input()
broadcaster(bytes(messageSend, encoding), "Host: ")
def serverInit():
global clients, addresses, server, newThread
clients = {}
addresses = {}
passwordSess = bytes(input("Enter server session password: "), encoding)
passwordSet(passwordSess)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((HOST, PORT))
server.listen(maxClients)
print("Awaiting connections...")
newThread = threading.Thread(target=connector, daemon=True)
newThread.start()
newThread.join()
server.close()
# [>] Client:
def receiver():
count = 0
while True:
messageRecv = clientSock.recv(byteSize)
count += 1
if count < 3:
messageRecv = messageRecv.decode(encoding)
if messageRecv == f"{terminatingStr}":
break
print(messageRecv)
else:
decryptMsg(messageRecv)
print(decrypted_data.decode(encoding))
print(f"Disconnected. Incorrect session password! Type '{terminatingStr}' to terminate program.")
def sender():
while True:
messageSend = input()
if messageSend == terminatingStr:
encryptMsg(messageSend.encode(encoding))
clientSock.send(ciphered_data)
break
encryptMsg(messageSend.encode(encoding))
clientSock.send(ciphered_data)
print(f"Termination sequence '{terminatingStr}' entered! Terminating client.")
clientSock.close()
def clientInit():
global clientSock
clientSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(f"Connecting to {HOST}:{PORT}.")
clientSock.connect((HOST,PORT))
passwordSess = bytes(input("Enter session password and then press [ENTER]: "), encoding)
passwordSet(passwordSess)
encryptMsg(password)
username = input("Enter username and the press [ENTER]: ")
clientSock.send(bytes(username, encoding))
clientSock.send(ciphered_data)
clientThread = threading.Thread(target=receiver, daemon=True)
clientThread.daemon = True
clientThread.start()
sender()
# [!] Main
parser = argparse.ArgumentParser()
parser.add_argument('-t','-T','--target', required=True, help="target IP")
parser.add_argument('-p','-P','--port', help="target PORT", type=int, default=4444)
parser.add_argument('-s','-S','--server', action='store_true', help="launch as Server")
args = parser.parse_args()
HOST = args.target
PORT = args.port
maxClients = 10
byteSize = 2048
encoding = "latin-1"
terminatingStr = "q!"
iv = 16 * b'\0'
if args.server:
serverInit()
else:
clientInit()